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