Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | |
| 23 | "android/soong" |
| 24 | "android/soong/android" |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 25 | "android/soong/cc/config" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func init() { |
| 29 | soong.RegisterModuleType("ndk_prebuilt_library", ndkPrebuiltLibraryFactory) |
| 30 | soong.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory) |
| 31 | soong.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory) |
| 32 | soong.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory) |
| 33 | } |
| 34 | |
| 35 | // NDK prebuilt libraries. |
| 36 | // |
| 37 | // These differ from regular prebuilts in that they aren't stripped and usually aren't installed |
| 38 | // either (with the exception of the shared STLs, which are installed to the app's directory rather |
| 39 | // than to the system image). |
| 40 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 41 | func getNdkLibDir(ctx android.ModuleContext, toolchain config.Toolchain, version string) android.SourcePath { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 42 | suffix := "" |
| 43 | // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a |
| 44 | // multilib toolchain and stores the libraries in "lib". |
| 45 | if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 { |
| 46 | suffix = "64" |
| 47 | } |
| 48 | return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s", |
| 49 | version, toolchain.Name(), suffix)) |
| 50 | } |
| 51 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 52 | func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain config.Toolchain, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 53 | ext string, version string) android.Path { |
| 54 | |
| 55 | // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION. |
| 56 | // We want to translate to just NAME.EXT |
| 57 | name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0] |
| 58 | dir := getNdkLibDir(ctx, toolchain, version) |
| 59 | return dir.Join(ctx, name+ext) |
| 60 | } |
| 61 | |
| 62 | type ndkPrebuiltObjectLinker struct { |
| 63 | objectLinker |
| 64 | } |
| 65 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 66 | func (*ndkPrebuiltObjectLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 67 | // NDK objects can't have any dependencies |
| 68 | return deps |
| 69 | } |
| 70 | |
| 71 | func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) { |
| 72 | module := newBaseModule(android.DeviceSupported, android.MultilibBoth) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 73 | module.linker = &ndkPrebuiltObjectLinker{ |
| 74 | objectLinker: objectLinker{ |
| 75 | baseLinker: NewBaseLinker(), |
| 76 | }, |
| 77 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 78 | module.Properties.HideFromMake = true |
| 79 | return module.Init() |
| 80 | } |
| 81 | |
| 82 | func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags, |
| 83 | deps PathDeps, objFiles android.Paths) android.Path { |
| 84 | // A null build step, but it sets up the output path. |
| 85 | if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") { |
| 86 | ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name") |
| 87 | } |
| 88 | |
| 89 | return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion()) |
| 90 | } |
| 91 | |
| 92 | type ndkPrebuiltLibraryLinker struct { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 93 | *libraryDecorator |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 96 | func (ndk *ndkPrebuiltLibraryLinker) linkerProps() []interface{} { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 97 | return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 100 | func (*ndkPrebuiltLibraryLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 101 | // NDK libraries can't have any dependencies |
| 102 | return deps |
| 103 | } |
| 104 | |
| 105 | func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 106 | module, library := NewLibrary(android.DeviceSupported, true, false) |
| 107 | linker := &ndkPrebuiltLibraryLinker{ |
| 108 | libraryDecorator: library, |
| 109 | } |
| 110 | module.compiler = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 111 | module.linker = linker |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 112 | module.installer = nil |
| 113 | module.stl = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 114 | module.Properties.HideFromMake = true |
| 115 | return module.Init() |
| 116 | } |
| 117 | |
| 118 | func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags, |
| 119 | deps PathDeps, objFiles android.Paths) android.Path { |
| 120 | // A null build step, but it sets up the output path. |
| 121 | ndk.exportIncludes(ctx, "-isystem") |
| 122 | |
| 123 | return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(), |
| 124 | ctx.sdkVersion()) |
| 125 | } |
| 126 | |
| 127 | // The NDK STLs are slightly different from the prebuilt system libraries: |
| 128 | // * Are not specific to each platform version. |
| 129 | // * The libraries are not in a predictable location for each STL. |
| 130 | |
| 131 | type ndkPrebuiltStlLinker struct { |
| 132 | ndkPrebuiltLibraryLinker |
| 133 | } |
| 134 | |
| 135 | func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 136 | module, library := NewLibrary(android.DeviceSupported, true, false) |
| 137 | linker := &ndkPrebuiltStlLinker{ |
| 138 | ndkPrebuiltLibraryLinker: ndkPrebuiltLibraryLinker{ |
| 139 | libraryDecorator: library, |
| 140 | }, |
| 141 | } |
| 142 | module.compiler = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 143 | module.linker = linker |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 144 | module.installer = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 145 | module.Properties.HideFromMake = true |
| 146 | return module.Init() |
| 147 | } |
| 148 | |
| 149 | func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 150 | module, library := NewLibrary(android.DeviceSupported, false, true) |
| 151 | linker := &ndkPrebuiltStlLinker{ |
| 152 | ndkPrebuiltLibraryLinker: ndkPrebuiltLibraryLinker{ |
| 153 | libraryDecorator: library, |
| 154 | }, |
| 155 | } |
| 156 | module.compiler = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 157 | module.linker = linker |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 158 | module.installer = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 159 | module.Properties.HideFromMake = true |
| 160 | return module.Init() |
| 161 | } |
| 162 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 163 | func getNdkStlLibDir(ctx android.ModuleContext, toolchain config.Toolchain, stl string) android.SourcePath { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 164 | gccVersion := toolchain.GccVersion() |
| 165 | var libDir string |
| 166 | switch stl { |
| 167 | case "libstlport": |
| 168 | libDir = "cxx-stl/stlport/libs" |
| 169 | case "libc++": |
| 170 | libDir = "cxx-stl/llvm-libc++/libs" |
| 171 | case "libgnustl": |
| 172 | libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion) |
| 173 | } |
| 174 | |
| 175 | if libDir != "" { |
| 176 | ndkSrcRoot := "prebuilts/ndk/current/sources" |
| 177 | return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0]) |
| 178 | } |
| 179 | |
| 180 | ctx.ModuleErrorf("Unknown NDK STL: %s", stl) |
| 181 | return android.PathForSource(ctx, "") |
| 182 | } |
| 183 | |
| 184 | func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags, |
| 185 | deps PathDeps, objFiles android.Paths) android.Path { |
| 186 | // A null build step, but it sets up the output path. |
| 187 | if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") { |
| 188 | ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name") |
| 189 | } |
| 190 | |
| 191 | ndk.exportIncludes(ctx, "-I") |
| 192 | |
| 193 | libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_") |
| 194 | libExt := flags.Toolchain.ShlibSuffix() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 195 | if ndk.Properties.BuildStatic { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 196 | libExt = staticLibraryExtension |
| 197 | } |
| 198 | |
| 199 | stlName := strings.TrimSuffix(libName, "_shared") |
| 200 | stlName = strings.TrimSuffix(stlName, "_static") |
| 201 | libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName) |
| 202 | return libDir.Join(ctx, libName+libExt) |
| 203 | } |