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 |
| 102 | func intMin(a int, b int) int { |
| 103 | if a < b { |
| 104 | return a |
| 105 | } else { |
| 106 | return b |
| 107 | } |
| 108 | } |
| 109 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 110 | func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 111 | minVersion := 9 // Minimum version supported by the NDK. |
| 112 | // TODO(danalbert): Use PlatformSdkVersion when possible. |
| 113 | // This is an interesting case because for the moment we actually need 24 |
| 114 | // even though the latest released version in aosp is 23. prebuilts/ndk/r11 |
| 115 | // has android-24 versions of libraries, and as platform libraries get |
| 116 | // migrated the libraries in prebuilts will need to depend on them. |
| 117 | // |
| 118 | // Once everything is all moved over to the new stuff (when there isn't a |
| 119 | // prebuilts/ndk any more) then this should be fixable, but for now I think |
| 120 | // it needs to remain as-is. |
| 121 | maxVersion := 24 |
| 122 | firstArchVersions := map[string]int{ |
| 123 | "arm": 9, |
| 124 | "arm64": 21, |
| 125 | "mips": 9, |
| 126 | "mips64": 21, |
| 127 | "x86": 9, |
| 128 | "x86_64": 21, |
| 129 | } |
| 130 | |
| 131 | // If the NDK drops support for a platform version, we don't want to have to |
| 132 | // fix up every module that was using it as its minimum version. Clip to the |
| 133 | // supported version here instead. |
| 134 | firstVersion, err := strconv.Atoi(c.properties.First_version) |
| 135 | if err != nil { |
| 136 | mctx.ModuleErrorf("Invalid first_version value (must be int): %q", |
| 137 | c.properties.First_version) |
| 138 | } |
| 139 | if firstVersion < minVersion { |
| 140 | firstVersion = minVersion |
| 141 | } |
| 142 | |
| 143 | arch := mctx.Arch().ArchType.String() |
| 144 | firstArchVersion, ok := firstArchVersions[arch] |
| 145 | if !ok { |
| 146 | panic(fmt.Errorf("Arch %q not found in firstArchVersions", arch)) |
| 147 | } |
| 148 | firstGenVersion := intMin(firstVersion, firstArchVersion) |
| 149 | versionStrs := make([]string, maxVersion-firstGenVersion+1) |
| 150 | for version := firstGenVersion; version <= maxVersion; version++ { |
| 151 | versionStrs[version-firstGenVersion] = strconv.Itoa(version) |
| 152 | } |
| 153 | |
| 154 | modules := mctx.CreateVariations(versionStrs...) |
| 155 | for i, module := range modules { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 156 | module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = firstGenVersion + i |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | func ndkApiMutator(mctx android.BottomUpMutatorContext) { |
| 161 | if m, ok := mctx.Module().(*Module); ok { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 162 | if compiler, ok := m.compiler.(*stubDecorator); ok { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 163 | generateStubApiVariants(mctx, compiler) |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 168 | func (c *stubDecorator) compilerInit(ctx BaseModuleContext) { |
Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 169 | c.baseCompiler.compilerInit(ctx) |
| 170 | |
| 171 | name := strings.TrimSuffix(ctx.ModuleName(), ".ndk") |
| 172 | for _, lib := range ndkMigratedLibs { |
| 173 | if lib == name { |
| 174 | return |
| 175 | } |
| 176 | } |
| 177 | ndkMigratedLibs = append(ndkMigratedLibs, name) |
| 178 | } |
| 179 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 180 | func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 181 | arch := ctx.Arch().ArchType.String() |
| 182 | |
| 183 | if !strings.HasSuffix(ctx.ModuleName(), ndkLibrarySuffix) { |
| 184 | ctx.ModuleErrorf("ndk_library modules names must be suffixed with %q\n", |
| 185 | ndkLibrarySuffix) |
| 186 | } |
| 187 | libName := strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix) |
| 188 | fileBase := fmt.Sprintf("%s.%s.%d", libName, arch, c.properties.ApiLevel) |
| 189 | stubSrcName := fileBase + ".c" |
| 190 | stubSrcPath := android.PathForModuleGen(ctx, stubSrcName) |
| 191 | versionScriptName := fileBase + ".map" |
| 192 | versionScriptPath := android.PathForModuleGen(ctx, versionScriptName) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 193 | c.versionScriptPath = versionScriptPath |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 194 | symbolFilePath := android.PathForModuleSrc(ctx, c.properties.Symbol_file) |
| 195 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
| 196 | Rule: genStubSrc, |
| 197 | Outputs: []android.WritablePath{stubSrcPath, versionScriptPath}, |
| 198 | Input: symbolFilePath, |
| 199 | Args: map[string]string{ |
| 200 | "arch": arch, |
| 201 | "apiLevel": strconv.Itoa(c.properties.ApiLevel), |
| 202 | }, |
| 203 | }) |
| 204 | |
| 205 | flags.CFlags = append(flags.CFlags, |
| 206 | // We're knowingly doing some otherwise unsightly things with builtin |
| 207 | // functions here. We're just generating stub libraries, so ignore it. |
| 208 | "-Wno-incompatible-library-redeclaration", |
| 209 | "-Wno-builtin-requires-header", |
| 210 | "-Wno-invalid-noreturn", |
| 211 | |
| 212 | // These libraries aren't actually used. Don't worry about unwinding |
| 213 | // (avoids the need to link an unwinder into a fake library). |
| 214 | "-fno-unwind-tables", |
| 215 | ) |
| 216 | |
| 217 | subdir := "" |
| 218 | srcs := []string{} |
| 219 | excludeSrcs := []string{} |
| 220 | extraSrcs := []android.Path{stubSrcPath} |
| 221 | extraDeps := []android.Path{} |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 222 | return compileObjs(ctx, flags, subdir, srcs, excludeSrcs, |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 223 | extraSrcs, extraDeps) |
| 224 | } |
| 225 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 226 | func (linker *stubDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 227 | return Deps{} |
| 228 | } |
| 229 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 230 | func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 231 | stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 232 | ndkLibrarySuffix) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 233 | return stub.libraryDecorator.linkerFlags(ctx, flags) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 236 | func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 237 | objFiles android.Paths) android.Path { |
| 238 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 239 | linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String() |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 240 | flags.LdFlags = append(flags.LdFlags, linkerScriptFlag) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 241 | return stub.libraryDecorator.link(ctx, flags, deps, objFiles) |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 244 | func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 245 | arch := ctx.Target().Arch.ArchType.Name |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 246 | apiLevel := stub.properties.ApiLevel |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 247 | |
| 248 | // arm64 isn't actually a multilib toolchain, so unlike the other LP64 |
| 249 | // architectures it's just installed to lib. |
| 250 | libDir := "lib" |
| 251 | if ctx.toolchain().Is64Bit() && arch != "arm64" { |
| 252 | libDir = "lib64" |
| 253 | } |
| 254 | |
| 255 | installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf( |
| 256 | "platforms/android-%d/arch-%s/usr/%s", apiLevel, arch, libDir)) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 257 | stub.installPath = ctx.InstallFile(installDir, path).String() |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | func newStubLibrary() *Module { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 261 | module, library := NewLibrary(android.DeviceSupported, true, false) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 262 | module.stl = nil |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 263 | module.sanitize = nil |
| 264 | library.StripProperties.Strip.None = true |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 265 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 266 | stub := &stubDecorator{ |
| 267 | libraryDecorator: library, |
| 268 | } |
| 269 | module.compiler = stub |
| 270 | module.linker = stub |
| 271 | module.installer = stub |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 272 | |
| 273 | return module |
| 274 | } |
| 275 | |
| 276 | func ndkLibraryFactory() (blueprint.Module, []interface{}) { |
| 277 | module := newStubLibrary() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 278 | return module.Init() |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 279 | } |