Dan Albert | 914449f | 2016-06-17 16:45:24 -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 | "strconv" |
| 20 | "strings" |
Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 21 | "sync" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | |
| 25 | "android/soong/android" |
| 26 | ) |
| 27 | |
| 28 | var ( |
| 29 | toolPath = pctx.SourcePathVariable("toolPath", "build/soong/cc/gen_stub_libs.py") |
| 30 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 31 | genStubSrc = pctx.AndroidStaticRule("genStubSrc", |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 32 | blueprint.RuleParams{ |
| 33 | Command: "$toolPath --arch $arch --api $apiLevel $in $out", |
| 34 | Description: "genStubSrc $out", |
| 35 | CommandDeps: []string{"$toolPath"}, |
| 36 | }, "arch", "apiLevel") |
| 37 | |
| 38 | ndkLibrarySuffix = ".ndk" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 39 | |
| 40 | ndkPrebuiltSharedLibs = []string{ |
| 41 | "android", |
| 42 | "c", |
| 43 | "dl", |
| 44 | "EGL", |
| 45 | "GLESv1_CM", |
| 46 | "GLESv2", |
| 47 | "GLESv3", |
| 48 | "jnigraphics", |
| 49 | "log", |
| 50 | "mediandk", |
| 51 | "m", |
| 52 | "OpenMAXAL", |
| 53 | "OpenSLES", |
| 54 | "stdc++", |
| 55 | "vulkan", |
| 56 | "z", |
| 57 | } |
| 58 | ndkPrebuiltSharedLibraries = addPrefix(append([]string(nil), ndkPrebuiltSharedLibs...), "lib") |
| 59 | |
| 60 | // These libraries have migrated over to the new ndk_library, which is added |
| 61 | // as a variation dependency via depsMutator. |
Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 62 | ndkMigratedLibs = []string{} |
| 63 | ndkMigratedLibsLock sync.Mutex // protects ndkMigratedLibs writes during parallel beginMutator |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 64 | ) |
| 65 | |
| 66 | // Creates a stub shared library based on the provided version file. |
| 67 | // |
| 68 | // The name of the generated file will be based on the module name by stripping |
| 69 | // the ".ndk" suffix from the module name. Module names must end with ".ndk" |
| 70 | // (as a convention to allow soong to guess the NDK name of a dependency when |
| 71 | // needed). "libfoo.ndk" will generate "libfoo.so. |
| 72 | // |
| 73 | // Example: |
| 74 | // |
| 75 | // ndk_library { |
| 76 | // name: "libfoo.ndk", |
| 77 | // symbol_file: "libfoo.map.txt", |
| 78 | // first_version: "9", |
| 79 | // } |
| 80 | // |
| 81 | type libraryProperties struct { |
| 82 | // Relative path to the symbol map. |
| 83 | // An example file can be seen here: TODO(danalbert): Make an example. |
| 84 | Symbol_file string |
| 85 | |
| 86 | // The first API level a library was available. A library will be generated |
| 87 | // for every API level beginning with this one. |
| 88 | First_version string |
| 89 | |
| 90 | // Private property for use by the mutator that splits per-API level. |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 91 | ApiLevel string `blueprint:"mutated"` |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 94 | type stubDecorator struct { |
| 95 | *libraryDecorator |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 96 | |
| 97 | properties libraryProperties |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 98 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 99 | versionScriptPath android.ModuleGenPath |
| 100 | installPath string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // OMG GO |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 104 | func intMax(a int, b int) int { |
| 105 | if a > b { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 106 | return a |
| 107 | } else { |
| 108 | return b |
| 109 | } |
| 110 | } |
| 111 | |
Dan Albert | 90f7a4d | 2016-11-08 14:34:24 -0800 | [diff] [blame^] | 112 | func normalizeNdkApiLevel(apiLevel string, arch android.Arch) (string, error) { |
| 113 | if apiLevel == "current" { |
| 114 | return apiLevel, nil |
| 115 | } |
| 116 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 117 | minVersion := 9 // Minimum version supported by the NDK. |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 118 | firstArchVersions := map[string]int{ |
| 119 | "arm": 9, |
| 120 | "arm64": 21, |
| 121 | "mips": 9, |
| 122 | "mips64": 21, |
| 123 | "x86": 9, |
| 124 | "x86_64": 21, |
| 125 | } |
| 126 | |
| 127 | // If the NDK drops support for a platform version, we don't want to have to |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 128 | // fix up every module that was using it as its SDK version. Clip to the |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 129 | // supported version here instead. |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 130 | version, err := strconv.Atoi(apiLevel) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 131 | if err != nil { |
Dan Albert | 90f7a4d | 2016-11-08 14:34:24 -0800 | [diff] [blame^] | 132 | return "", fmt.Errorf("API level must be an integer (is %q)", apiLevel) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 133 | } |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 134 | version = intMax(version, minVersion) |
| 135 | |
| 136 | archStr := arch.ArchType.String() |
| 137 | firstArchVersion, ok := firstArchVersions[archStr] |
| 138 | if !ok { |
| 139 | panic(fmt.Errorf("Arch %q not found in firstArchVersions", archStr)) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Dan Albert | 90f7a4d | 2016-11-08 14:34:24 -0800 | [diff] [blame^] | 142 | return strconv.Itoa(intMax(version, firstArchVersion)), nil |
| 143 | } |
| 144 | |
| 145 | func getFirstGeneratedVersion(firstSupportedVersion string, platformVersion int) (int, error) { |
| 146 | if firstSupportedVersion == "current" { |
| 147 | return platformVersion + 1, nil |
| 148 | } |
| 149 | |
| 150 | return strconv.Atoi(firstSupportedVersion) |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) { |
Dan Albert | 90f7a4d | 2016-11-08 14:34:24 -0800 | [diff] [blame^] | 154 | platformVersion := mctx.AConfig().PlatformSdkVersionInt() |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 155 | |
Dan Albert | 90f7a4d | 2016-11-08 14:34:24 -0800 | [diff] [blame^] | 156 | firstSupportedVersion, err := normalizeNdkApiLevel(c.properties.First_version, |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 157 | mctx.Arch()) |
| 158 | if err != nil { |
| 159 | mctx.PropertyErrorf("first_version", err.Error()) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 160 | } |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 161 | |
Dan Albert | 90f7a4d | 2016-11-08 14:34:24 -0800 | [diff] [blame^] | 162 | firstGenVersion, err := getFirstGeneratedVersion(firstSupportedVersion, platformVersion) |
| 163 | if err != nil { |
| 164 | // In theory this is impossible because we've already run this through |
| 165 | // normalizeNdkApiLevel above. |
| 166 | mctx.PropertyErrorf("first_version", err.Error()) |
| 167 | } |
| 168 | |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 169 | var versionStrs []string |
Dan Albert | 90f7a4d | 2016-11-08 14:34:24 -0800 | [diff] [blame^] | 170 | for version := firstGenVersion; version <= platformVersion; version++ { |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 171 | versionStrs = append(versionStrs, strconv.Itoa(version)) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 172 | } |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 173 | versionStrs = append(versionStrs, "current") |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 174 | |
| 175 | modules := mctx.CreateVariations(versionStrs...) |
| 176 | for i, module := range modules { |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 177 | module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = versionStrs[i] |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | func ndkApiMutator(mctx android.BottomUpMutatorContext) { |
| 182 | if m, ok := mctx.Module().(*Module); ok { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 183 | if compiler, ok := m.compiler.(*stubDecorator); ok { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 184 | generateStubApiVariants(mctx, compiler) |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 189 | func (c *stubDecorator) compilerInit(ctx BaseModuleContext) { |
Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 190 | c.baseCompiler.compilerInit(ctx) |
| 191 | |
| 192 | name := strings.TrimSuffix(ctx.ModuleName(), ".ndk") |
Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 193 | ndkMigratedLibsLock.Lock() |
| 194 | defer ndkMigratedLibsLock.Unlock() |
Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 195 | for _, lib := range ndkMigratedLibs { |
| 196 | if lib == name { |
| 197 | return |
| 198 | } |
| 199 | } |
| 200 | ndkMigratedLibs = append(ndkMigratedLibs, name) |
| 201 | } |
| 202 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 203 | func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 204 | arch := ctx.Arch().ArchType.String() |
| 205 | |
| 206 | if !strings.HasSuffix(ctx.ModuleName(), ndkLibrarySuffix) { |
| 207 | ctx.ModuleErrorf("ndk_library modules names must be suffixed with %q\n", |
| 208 | ndkLibrarySuffix) |
| 209 | } |
| 210 | libName := strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix) |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 211 | fileBase := fmt.Sprintf("%s.%s.%s", libName, arch, c.properties.ApiLevel) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 212 | stubSrcName := fileBase + ".c" |
| 213 | stubSrcPath := android.PathForModuleGen(ctx, stubSrcName) |
| 214 | versionScriptName := fileBase + ".map" |
| 215 | versionScriptPath := android.PathForModuleGen(ctx, versionScriptName) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 216 | c.versionScriptPath = versionScriptPath |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 217 | symbolFilePath := android.PathForModuleSrc(ctx, c.properties.Symbol_file) |
| 218 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
| 219 | Rule: genStubSrc, |
| 220 | Outputs: []android.WritablePath{stubSrcPath, versionScriptPath}, |
| 221 | Input: symbolFilePath, |
| 222 | Args: map[string]string{ |
| 223 | "arch": arch, |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 224 | "apiLevel": c.properties.ApiLevel, |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 225 | }, |
| 226 | }) |
| 227 | |
| 228 | flags.CFlags = append(flags.CFlags, |
| 229 | // We're knowingly doing some otherwise unsightly things with builtin |
| 230 | // functions here. We're just generating stub libraries, so ignore it. |
| 231 | "-Wno-incompatible-library-redeclaration", |
| 232 | "-Wno-builtin-requires-header", |
| 233 | "-Wno-invalid-noreturn", |
| 234 | |
| 235 | // These libraries aren't actually used. Don't worry about unwinding |
| 236 | // (avoids the need to link an unwinder into a fake library). |
| 237 | "-fno-unwind-tables", |
| 238 | ) |
| 239 | |
| 240 | subdir := "" |
Colin Cross | 2f33635 | 2016-10-26 10:03:47 -0700 | [diff] [blame] | 241 | srcs := []android.Path{stubSrcPath} |
| 242 | return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 245 | func (linker *stubDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 246 | return Deps{} |
| 247 | } |
| 248 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 249 | func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 250 | stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 251 | ndkLibrarySuffix) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 252 | return stub.libraryDecorator.linkerFlags(ctx, flags) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 255 | func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 256 | objs Objects) android.Path { |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 257 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 258 | linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String() |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 259 | flags.LdFlags = append(flags.LdFlags, linkerScriptFlag) |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 260 | return stub.libraryDecorator.link(ctx, flags, deps, objs) |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 263 | func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 264 | arch := ctx.Target().Arch.ArchType.Name |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 265 | apiLevel := stub.properties.ApiLevel |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 266 | |
| 267 | // arm64 isn't actually a multilib toolchain, so unlike the other LP64 |
| 268 | // architectures it's just installed to lib. |
| 269 | libDir := "lib" |
| 270 | if ctx.toolchain().Is64Bit() && arch != "arm64" { |
| 271 | libDir = "lib64" |
| 272 | } |
| 273 | |
| 274 | installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf( |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 275 | "platforms/android-%s/arch-%s/usr/%s", apiLevel, arch, libDir)) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 276 | stub.installPath = ctx.InstallFile(installDir, path).String() |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Dan Albert | 705c84b | 2016-08-08 10:45:03 -0700 | [diff] [blame] | 279 | func newStubLibrary() (*Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 280 | module, library := NewLibrary(android.DeviceSupported, true, false) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 281 | module.stl = nil |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 282 | module.sanitize = nil |
| 283 | library.StripProperties.Strip.None = true |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 284 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 285 | stub := &stubDecorator{ |
| 286 | libraryDecorator: library, |
| 287 | } |
| 288 | module.compiler = stub |
| 289 | module.linker = stub |
| 290 | module.installer = stub |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 291 | |
Dan Albert | 705c84b | 2016-08-08 10:45:03 -0700 | [diff] [blame] | 292 | return module, []interface{}{&stub.properties} |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | func ndkLibraryFactory() (blueprint.Module, []interface{}) { |
Dan Albert | 705c84b | 2016-08-08 10:45:03 -0700 | [diff] [blame] | 296 | module, properties := newStubLibrary() |
| 297 | return android.InitAndroidArchModule(module, android.DeviceSupported, |
| 298 | android.MultilibBoth, properties...) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 299 | } |