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 | |
| 66 | func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 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) |
| 73 | module.linker = &ndkPrebuiltObjectLinker{} |
| 74 | module.Properties.HideFromMake = true |
| 75 | return module.Init() |
| 76 | } |
| 77 | |
| 78 | func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags, |
| 79 | deps PathDeps, objFiles android.Paths) android.Path { |
| 80 | // A null build step, but it sets up the output path. |
| 81 | if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") { |
| 82 | ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name") |
| 83 | } |
| 84 | |
| 85 | return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion()) |
| 86 | } |
| 87 | |
| 88 | type ndkPrebuiltLibraryLinker struct { |
| 89 | libraryLinker |
| 90 | } |
| 91 | |
| 92 | var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil) |
| 93 | var _ exportedFlagsProducer = (*libraryLinker)(nil) |
| 94 | |
| 95 | func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} { |
| 96 | return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties) |
| 97 | } |
| 98 | |
| 99 | func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 100 | // NDK libraries can't have any dependencies |
| 101 | return deps |
| 102 | } |
| 103 | |
| 104 | func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) { |
| 105 | module := newBaseModule(android.DeviceSupported, android.MultilibBoth) |
| 106 | linker := &ndkPrebuiltLibraryLinker{} |
| 107 | linker.dynamicProperties.BuildShared = true |
| 108 | module.linker = linker |
| 109 | module.Properties.HideFromMake = true |
| 110 | return module.Init() |
| 111 | } |
| 112 | |
| 113 | func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags, |
| 114 | deps PathDeps, objFiles android.Paths) android.Path { |
| 115 | // A null build step, but it sets up the output path. |
| 116 | ndk.exportIncludes(ctx, "-isystem") |
| 117 | |
| 118 | return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(), |
| 119 | ctx.sdkVersion()) |
| 120 | } |
| 121 | |
| 122 | // The NDK STLs are slightly different from the prebuilt system libraries: |
| 123 | // * Are not specific to each platform version. |
| 124 | // * The libraries are not in a predictable location for each STL. |
| 125 | |
| 126 | type ndkPrebuiltStlLinker struct { |
| 127 | ndkPrebuiltLibraryLinker |
| 128 | } |
| 129 | |
| 130 | func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) { |
| 131 | module := newBaseModule(android.DeviceSupported, android.MultilibBoth) |
| 132 | linker := &ndkPrebuiltStlLinker{} |
| 133 | linker.dynamicProperties.BuildShared = true |
| 134 | module.linker = linker |
| 135 | module.Properties.HideFromMake = true |
| 136 | return module.Init() |
| 137 | } |
| 138 | |
| 139 | func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) { |
| 140 | module := newBaseModule(android.DeviceSupported, android.MultilibBoth) |
| 141 | linker := &ndkPrebuiltStlLinker{} |
| 142 | linker.dynamicProperties.BuildStatic = true |
| 143 | module.linker = linker |
| 144 | module.Properties.HideFromMake = true |
| 145 | return module.Init() |
| 146 | } |
| 147 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame^] | 148 | func getNdkStlLibDir(ctx android.ModuleContext, toolchain config.Toolchain, stl string) android.SourcePath { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 149 | gccVersion := toolchain.GccVersion() |
| 150 | var libDir string |
| 151 | switch stl { |
| 152 | case "libstlport": |
| 153 | libDir = "cxx-stl/stlport/libs" |
| 154 | case "libc++": |
| 155 | libDir = "cxx-stl/llvm-libc++/libs" |
| 156 | case "libgnustl": |
| 157 | libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion) |
| 158 | } |
| 159 | |
| 160 | if libDir != "" { |
| 161 | ndkSrcRoot := "prebuilts/ndk/current/sources" |
| 162 | return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0]) |
| 163 | } |
| 164 | |
| 165 | ctx.ModuleErrorf("Unknown NDK STL: %s", stl) |
| 166 | return android.PathForSource(ctx, "") |
| 167 | } |
| 168 | |
| 169 | func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags, |
| 170 | deps PathDeps, objFiles android.Paths) android.Path { |
| 171 | // A null build step, but it sets up the output path. |
| 172 | if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") { |
| 173 | ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name") |
| 174 | } |
| 175 | |
| 176 | ndk.exportIncludes(ctx, "-I") |
| 177 | |
| 178 | libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_") |
| 179 | libExt := flags.Toolchain.ShlibSuffix() |
| 180 | if ndk.dynamicProperties.BuildStatic { |
| 181 | libExt = staticLibraryExtension |
| 182 | } |
| 183 | |
| 184 | stlName := strings.TrimSuffix(libName, "_shared") |
| 185 | stlName = strings.TrimSuffix(stlName, "_static") |
| 186 | libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName) |
| 187 | return libDir.Join(ctx, libName+libExt) |
| 188 | } |
| 189 | |
| 190 | func linkageMutator(mctx android.BottomUpMutatorContext) { |
| 191 | if m, ok := mctx.Module().(*Module); ok { |
| 192 | if m.linker != nil { |
| 193 | if linker, ok := m.linker.(baseLinkerInterface); ok { |
| 194 | var modules []blueprint.Module |
| 195 | if linker.buildStatic() && linker.buildShared() { |
| 196 | modules = mctx.CreateLocalVariations("static", "shared") |
| 197 | static := modules[0].(*Module) |
| 198 | shared := modules[1].(*Module) |
| 199 | |
| 200 | static.linker.(baseLinkerInterface).setStatic(true) |
| 201 | shared.linker.(baseLinkerInterface).setStatic(false) |
| 202 | |
| 203 | if staticCompiler, ok := static.compiler.(*libraryCompiler); ok { |
| 204 | sharedCompiler := shared.compiler.(*libraryCompiler) |
| 205 | if len(staticCompiler.Properties.Static.Cflags) == 0 && |
| 206 | len(sharedCompiler.Properties.Shared.Cflags) == 0 { |
| 207 | // Optimize out compiling common .o files twice for static+shared libraries |
| 208 | mctx.AddInterVariantDependency(reuseObjTag, shared, static) |
| 209 | sharedCompiler.baseCompiler.Properties.Srcs = nil |
| 210 | sharedCompiler.baseCompiler.Properties.Generated_sources = nil |
| 211 | } |
| 212 | } |
| 213 | } else if linker.buildStatic() { |
| 214 | modules = mctx.CreateLocalVariations("static") |
| 215 | modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true) |
| 216 | } else if linker.buildShared() { |
| 217 | modules = mctx.CreateLocalVariations("shared") |
| 218 | modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false) |
| 219 | } else { |
| 220 | panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName())) |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |