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" |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 19 | "path/filepath" |
Dan Albert | ad66593 | 2021-06-07 13:19:49 -0700 | [diff] [blame] | 20 | "runtime" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 21 | "strings" |
Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 22 | "sync" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint" |
Jiyong Park | ee9b117 | 2021-04-06 17:40:32 +0900 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 26 | |
| 27 | "android/soong/android" |
| 28 | ) |
| 29 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 30 | func init() { |
Dan Albert | 06f58af | 2020-06-22 15:10:31 -0700 | [diff] [blame] | 31 | pctx.HostBinToolVariable("ndkStubGenerator", "ndkstubgen") |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 32 | pctx.HostBinToolVariable("abidiff", "abidiff") |
| 33 | pctx.HostBinToolVariable("abitidy", "abitidy") |
| 34 | pctx.HostBinToolVariable("abidw", "abidw") |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 37 | var ( |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 38 | genStubSrc = pctx.AndroidStaticRule("genStubSrc", |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 39 | blueprint.RuleParams{ |
Dan Albert | 06f58af | 2020-06-22 15:10:31 -0700 | [diff] [blame] | 40 | Command: "$ndkStubGenerator --arch $arch --api $apiLevel " + |
| 41 | "--api-map $apiMap $flags $in $out", |
| 42 | CommandDeps: []string{"$ndkStubGenerator"}, |
Jiyong Park | 3fd0baf | 2018-12-07 16:25:39 +0900 | [diff] [blame] | 43 | }, "arch", "apiLevel", "apiMap", "flags") |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 44 | |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 45 | abidw = pctx.AndroidStaticRule("abidw", |
| 46 | blueprint.RuleParams{ |
| 47 | Command: "$abidw --type-id-style hash --no-corpus-path " + |
| 48 | "--no-show-locs --no-comp-dir-path -w $symbolList $in | " + |
| 49 | "$abitidy --all -o $out", |
| 50 | CommandDeps: []string{"$abitidy", "$abidw"}, |
| 51 | }, "symbolList") |
| 52 | |
| 53 | abidiff = pctx.AndroidStaticRule("abidiff", |
| 54 | blueprint.RuleParams{ |
| 55 | // Need to create *some* output for ninja. We don't want to use tee |
| 56 | // because we don't want to spam the build output with "nothing |
| 57 | // changed" messages, so redirect output message to $out, and if |
| 58 | // changes were detected print the output and fail. |
| 59 | Command: "$abidiff $args $in > $out || (cat $out && false)", |
| 60 | CommandDeps: []string{"$abidiff"}, |
| 61 | }, "args") |
| 62 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 63 | ndkLibrarySuffix = ".ndk" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 64 | |
Colin Cross | 95f1ca0 | 2020-10-29 20:47:22 -0700 | [diff] [blame] | 65 | ndkKnownLibsKey = android.NewOnceKey("ndkKnownLibsKey") |
Dan Albert | de5aade | 2020-06-30 12:32:51 -0700 | [diff] [blame] | 66 | // protects ndkKnownLibs writes during parallel BeginMutator. |
| 67 | ndkKnownLibsLock sync.Mutex |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 68 | |
| 69 | stubImplementation = dependencyTag{name: "stubImplementation"} |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 70 | ) |
| 71 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 72 | // The First_version and Unversioned_until properties of this struct should not |
| 73 | // be used directly, but rather through the ApiLevel returning methods |
| 74 | // firstVersion() and unversionedUntil(). |
| 75 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 76 | // Creates a stub shared library based on the provided version file. |
| 77 | // |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 78 | // Example: |
| 79 | // |
| 80 | // ndk_library { |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 81 | // name: "libfoo", |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 82 | // symbol_file: "libfoo.map.txt", |
| 83 | // first_version: "9", |
| 84 | // } |
| 85 | // |
| 86 | type libraryProperties struct { |
| 87 | // Relative path to the symbol map. |
| 88 | // An example file can be seen here: TODO(danalbert): Make an example. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 89 | Symbol_file *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 90 | |
| 91 | // The first API level a library was available. A library will be generated |
| 92 | // for every API level beginning with this one. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 93 | First_version *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 94 | |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 95 | // The first API level that library should have the version script applied. |
| 96 | // This defaults to the value of first_version, and should almost never be |
| 97 | // used. This is only needed to work around platform bugs like |
| 98 | // https://github.com/android-ndk/ndk/issues/265. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 99 | Unversioned_until *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 102 | type stubDecorator struct { |
| 103 | *libraryDecorator |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 104 | |
| 105 | properties libraryProperties |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 106 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 107 | versionScriptPath android.ModuleGenPath |
| 108 | parsedCoverageXmlPath android.ModuleOutPath |
| 109 | installPath android.Path |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 110 | abiDumpPath android.OutputPath |
| 111 | abiDiffPaths android.Paths |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 112 | |
| 113 | apiLevel android.ApiLevel |
| 114 | firstVersion android.ApiLevel |
| 115 | unversionedUntil android.ApiLevel |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Colin Cross | 0477b42 | 2020-10-13 18:43:54 -0700 | [diff] [blame] | 118 | var _ versionedInterface = (*stubDecorator)(nil) |
| 119 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 120 | func shouldUseVersionScript(ctx BaseModuleContext, stub *stubDecorator) bool { |
| 121 | return stub.apiLevel.GreaterThanOrEqualTo(stub.unversionedUntil) |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Colin Cross | 0477b42 | 2020-10-13 18:43:54 -0700 | [diff] [blame] | 124 | func (stub *stubDecorator) implementationModuleName(name string) string { |
| 125 | return strings.TrimSuffix(name, ndkLibrarySuffix) |
| 126 | } |
| 127 | |
Colin Cross | 3572cf7 | 2020-10-01 15:58:11 -0700 | [diff] [blame] | 128 | func ndkLibraryVersions(ctx android.BaseMutatorContext, from android.ApiLevel) []string { |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 129 | var versions []android.ApiLevel |
| 130 | versionStrs := []string{} |
| 131 | for _, version := range ctx.Config().AllSupportedApiLevels() { |
| 132 | if version.GreaterThanOrEqualTo(from) { |
| 133 | versions = append(versions, version) |
| 134 | versionStrs = append(versionStrs, version.String()) |
| 135 | } |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 136 | } |
Dan Albert | 0b176c8 | 2020-07-23 16:43:25 -0700 | [diff] [blame] | 137 | versionStrs = append(versionStrs, android.FutureApiLevel.String()) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 138 | |
Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 139 | return versionStrs |
| 140 | } |
| 141 | |
Colin Cross | 3572cf7 | 2020-10-01 15:58:11 -0700 | [diff] [blame] | 142 | func (this *stubDecorator) stubsVersions(ctx android.BaseMutatorContext) []string { |
| 143 | if !ctx.Module().Enabled() { |
| 144 | return nil |
| 145 | } |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 146 | if ctx.Target().NativeBridge == android.NativeBridgeEnabled { |
| 147 | ctx.Module().Disable() |
| 148 | return nil |
| 149 | } |
Colin Cross | 3572cf7 | 2020-10-01 15:58:11 -0700 | [diff] [blame] | 150 | firstVersion, err := nativeApiLevelFromUser(ctx, |
| 151 | String(this.properties.First_version)) |
| 152 | if err != nil { |
| 153 | ctx.PropertyErrorf("first_version", err.Error()) |
| 154 | return nil |
| 155 | } |
| 156 | return ndkLibraryVersions(ctx, firstVersion) |
| 157 | } |
| 158 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 159 | func (this *stubDecorator) initializeProperties(ctx BaseModuleContext) bool { |
Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 160 | this.apiLevel = nativeApiLevelOrPanic(ctx, this.stubsVersion()) |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 161 | |
| 162 | var err error |
| 163 | this.firstVersion, err = nativeApiLevelFromUser(ctx, |
| 164 | String(this.properties.First_version)) |
| 165 | if err != nil { |
| 166 | ctx.PropertyErrorf("first_version", err.Error()) |
| 167 | return false |
| 168 | } |
| 169 | |
Jiyong Park | ee9b117 | 2021-04-06 17:40:32 +0900 | [diff] [blame] | 170 | str := proptools.StringDefault(this.properties.Unversioned_until, "minimum") |
| 171 | this.unversionedUntil, err = nativeApiLevelFromUser(ctx, str) |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 172 | if err != nil { |
| 173 | ctx.PropertyErrorf("unversioned_until", err.Error()) |
| 174 | return false |
| 175 | } |
| 176 | |
| 177 | return true |
| 178 | } |
| 179 | |
Colin Cross | 95f1ca0 | 2020-10-29 20:47:22 -0700 | [diff] [blame] | 180 | func getNDKKnownLibs(config android.Config) *[]string { |
| 181 | return config.Once(ndkKnownLibsKey, func() interface{} { |
| 182 | return &[]string{} |
| 183 | }).(*[]string) |
| 184 | } |
| 185 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 186 | func (c *stubDecorator) compilerInit(ctx BaseModuleContext) { |
Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 187 | c.baseCompiler.compilerInit(ctx) |
| 188 | |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 189 | name := ctx.baseModuleName() |
| 190 | if strings.HasSuffix(name, ndkLibrarySuffix) { |
| 191 | ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix) |
| 192 | } |
| 193 | |
Dan Albert | de5aade | 2020-06-30 12:32:51 -0700 | [diff] [blame] | 194 | ndkKnownLibsLock.Lock() |
| 195 | defer ndkKnownLibsLock.Unlock() |
Colin Cross | 95f1ca0 | 2020-10-29 20:47:22 -0700 | [diff] [blame] | 196 | ndkKnownLibs := getNDKKnownLibs(ctx.Config()) |
| 197 | for _, lib := range *ndkKnownLibs { |
Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 198 | if lib == name { |
| 199 | return |
| 200 | } |
| 201 | } |
Colin Cross | 95f1ca0 | 2020-10-29 20:47:22 -0700 | [diff] [blame] | 202 | *ndkKnownLibs = append(*ndkKnownLibs, name) |
Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 205 | func addStubLibraryCompilerFlags(flags Flags) Flags { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 206 | flags.Global.CFlags = append(flags.Global.CFlags, |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 207 | // We're knowingly doing some otherwise unsightly things with builtin |
| 208 | // functions here. We're just generating stub libraries, so ignore it. |
| 209 | "-Wno-incompatible-library-redeclaration", |
Nick Desaulniers | eb20744 | 2019-12-12 10:15:42 -0800 | [diff] [blame] | 210 | "-Wno-incomplete-setjmp-declaration", |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 211 | "-Wno-builtin-requires-header", |
| 212 | "-Wno-invalid-noreturn", |
Chih-Hung Hsieh | 64a38dc | 2017-11-14 14:09:14 -0800 | [diff] [blame] | 213 | "-Wall", |
| 214 | "-Werror", |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 215 | // These libraries aren't actually used. Don't worry about unwinding |
| 216 | // (avoids the need to link an unwinder into a fake library). |
| 217 | "-fno-unwind-tables", |
| 218 | ) |
Jiyong Park | 48d75ef | 2019-11-21 15:11:49 +0900 | [diff] [blame] | 219 | // All symbols in the stubs library should be visible. |
| 220 | if inList("-fvisibility=hidden", flags.Local.CFlags) { |
| 221 | flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default") |
| 222 | } |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 223 | return flags |
| 224 | } |
| 225 | |
Colin Cross | f18e110 | 2017-11-16 14:33:08 -0800 | [diff] [blame] | 226 | func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags { |
| 227 | flags = stub.baseCompiler.compilerFlags(ctx, flags, deps) |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 228 | return addStubLibraryCompilerFlags(flags) |
| 229 | } |
| 230 | |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 231 | type ndkApiOutputs struct { |
| 232 | stubSrc android.ModuleGenPath |
| 233 | versionScript android.ModuleGenPath |
| 234 | symbolList android.ModuleGenPath |
| 235 | } |
| 236 | |
| 237 | func parseNativeAbiDefinition(ctx ModuleContext, symbolFile string, |
| 238 | apiLevel android.ApiLevel, genstubFlags string) ndkApiOutputs { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 239 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 240 | stubSrcPath := android.PathForModuleGen(ctx, "stub.c") |
| 241 | versionScriptPath := android.PathForModuleGen(ctx, "stub.map") |
| 242 | symbolFilePath := android.PathForModuleSrc(ctx, symbolFile) |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 243 | symbolListPath := android.PathForModuleGen(ctx, "abi_symbol_list.txt") |
Dan Albert | 49927d2 | 2017-03-28 15:00:46 -0700 | [diff] [blame] | 244 | apiLevelsJson := android.GetApiLevelsJson(ctx) |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 245 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 246 | Rule: genStubSrc, |
| 247 | Description: "generate stubs " + symbolFilePath.Rel(), |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 248 | Outputs: []android.WritablePath{stubSrcPath, versionScriptPath, |
| 249 | symbolListPath}, |
| 250 | Input: symbolFilePath, |
| 251 | Implicits: []android.Path{apiLevelsJson}, |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 252 | Args: map[string]string{ |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 253 | "arch": ctx.Arch().ArchType.String(), |
| 254 | "apiLevel": apiLevel.String(), |
Dan Albert | 49927d2 | 2017-03-28 15:00:46 -0700 | [diff] [blame] | 255 | "apiMap": apiLevelsJson.String(), |
Jiyong Park | 3fd0baf | 2018-12-07 16:25:39 +0900 | [diff] [blame] | 256 | "flags": genstubFlags, |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 257 | }, |
| 258 | }) |
| 259 | |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 260 | return ndkApiOutputs{ |
| 261 | stubSrc: stubSrcPath, |
| 262 | versionScript: versionScriptPath, |
| 263 | symbolList: symbolListPath, |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | func compileStubLibrary(ctx ModuleContext, flags Flags, src android.Path) Objects { |
| 268 | return compileObjs(ctx, flagsToBuilderFlags(flags), "", |
| 269 | android.Paths{src}, nil, nil) |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 272 | func (this *stubDecorator) findImplementationLibrary(ctx ModuleContext) android.Path { |
| 273 | dep := ctx.GetDirectDepWithTag(strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix), |
| 274 | stubImplementation) |
| 275 | if dep == nil { |
| 276 | ctx.ModuleErrorf("Could not find implementation for stub") |
| 277 | return nil |
| 278 | } |
| 279 | impl, ok := dep.(*Module) |
| 280 | if !ok { |
| 281 | ctx.ModuleErrorf("Implementation for stub is not correct module type") |
| 282 | } |
| 283 | output := impl.UnstrippedOutputFile() |
| 284 | if output == nil { |
| 285 | ctx.ModuleErrorf("implementation module (%s) has no output", impl) |
| 286 | return nil |
| 287 | } |
| 288 | |
| 289 | return output |
| 290 | } |
| 291 | |
| 292 | func (this *stubDecorator) libraryName(ctx ModuleContext) string { |
| 293 | return strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix) |
| 294 | } |
| 295 | |
| 296 | func (this *stubDecorator) findPrebuiltAbiDump(ctx ModuleContext, |
| 297 | apiLevel android.ApiLevel) android.OptionalPath { |
| 298 | |
| 299 | subpath := filepath.Join("prebuilts/abi-dumps/ndk", apiLevel.String(), |
| 300 | ctx.Arch().ArchType.String(), this.libraryName(ctx), "abi.xml") |
| 301 | return android.ExistentPathForSource(ctx, subpath) |
| 302 | } |
| 303 | |
| 304 | // Feature flag. |
Dan Albert | ad66593 | 2021-06-07 13:19:49 -0700 | [diff] [blame] | 305 | func canDumpAbi() bool { |
| 306 | return runtime.GOOS != "darwin" |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // Feature flag to disable diffing against prebuilts. |
Dan Albert | ad66593 | 2021-06-07 13:19:49 -0700 | [diff] [blame] | 310 | func canDiffAbi() bool { |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 311 | return false |
| 312 | } |
| 313 | |
| 314 | func (this *stubDecorator) dumpAbi(ctx ModuleContext, symbolList android.Path) { |
| 315 | implementationLibrary := this.findImplementationLibrary(ctx) |
| 316 | this.abiDumpPath = getNdkAbiDumpInstallBase(ctx).Join(ctx, |
| 317 | this.apiLevel.String(), ctx.Arch().ArchType.String(), |
| 318 | this.libraryName(ctx), "abi.xml") |
| 319 | ctx.Build(pctx, android.BuildParams{ |
| 320 | Rule: abidw, |
| 321 | Description: fmt.Sprintf("abidw %s", implementationLibrary), |
| 322 | Output: this.abiDumpPath, |
| 323 | Input: implementationLibrary, |
| 324 | Implicit: symbolList, |
| 325 | Args: map[string]string{ |
| 326 | "symbolList": symbolList.String(), |
| 327 | }, |
| 328 | }) |
| 329 | } |
| 330 | |
| 331 | func findNextApiLevel(ctx ModuleContext, apiLevel android.ApiLevel) *android.ApiLevel { |
| 332 | apiLevels := append(ctx.Config().AllSupportedApiLevels(), |
| 333 | android.FutureApiLevel) |
| 334 | for _, api := range apiLevels { |
| 335 | if api.GreaterThan(apiLevel) { |
| 336 | return &api |
| 337 | } |
| 338 | } |
| 339 | return nil |
| 340 | } |
| 341 | |
| 342 | func (this *stubDecorator) diffAbi(ctx ModuleContext) { |
| 343 | missingPrebuiltError := fmt.Sprintf( |
| 344 | "Did not find prebuilt ABI dump for %q. Generate with "+ |
| 345 | "//development/tools/ndk/update_ndk_abi.sh.", this.libraryName(ctx)) |
| 346 | |
| 347 | // Catch any ABI changes compared to the checked-in definition of this API |
| 348 | // level. |
| 349 | abiDiffPath := android.PathForModuleOut(ctx, "abidiff.timestamp") |
| 350 | prebuiltAbiDump := this.findPrebuiltAbiDump(ctx, this.apiLevel) |
| 351 | if !prebuiltAbiDump.Valid() { |
| 352 | ctx.Build(pctx, android.BuildParams{ |
| 353 | Rule: android.ErrorRule, |
| 354 | Output: abiDiffPath, |
| 355 | Args: map[string]string{ |
| 356 | "error": missingPrebuiltError, |
| 357 | }, |
| 358 | }) |
| 359 | } else { |
| 360 | ctx.Build(pctx, android.BuildParams{ |
| 361 | Rule: abidiff, |
| 362 | Description: fmt.Sprintf("abidiff %s %s", prebuiltAbiDump, |
| 363 | this.abiDumpPath), |
| 364 | Output: abiDiffPath, |
| 365 | Inputs: android.Paths{prebuiltAbiDump.Path(), this.abiDumpPath}, |
| 366 | }) |
| 367 | } |
| 368 | this.abiDiffPaths = append(this.abiDiffPaths, abiDiffPath) |
| 369 | |
| 370 | // Also ensure that the ABI of the next API level (if there is one) matches |
| 371 | // this API level. *New* ABI is allowed, but any changes to APIs that exist |
| 372 | // in this API level are disallowed. |
| 373 | if !this.apiLevel.IsCurrent() { |
| 374 | nextApiLevel := findNextApiLevel(ctx, this.apiLevel) |
| 375 | if nextApiLevel == nil { |
| 376 | panic(fmt.Errorf("could not determine which API level follows "+ |
| 377 | "non-current API level %s", this.apiLevel)) |
| 378 | } |
| 379 | nextAbiDiffPath := android.PathForModuleOut(ctx, |
| 380 | "abidiff_next.timestamp") |
| 381 | nextAbiDump := this.findPrebuiltAbiDump(ctx, *nextApiLevel) |
| 382 | if !nextAbiDump.Valid() { |
| 383 | ctx.Build(pctx, android.BuildParams{ |
| 384 | Rule: android.ErrorRule, |
| 385 | Output: nextAbiDiffPath, |
| 386 | Args: map[string]string{ |
| 387 | "error": missingPrebuiltError, |
| 388 | }, |
| 389 | }) |
| 390 | } else { |
| 391 | ctx.Build(pctx, android.BuildParams{ |
| 392 | Rule: abidiff, |
| 393 | Description: fmt.Sprintf("abidiff %s %s", this.abiDumpPath, |
| 394 | nextAbiDump), |
| 395 | Output: nextAbiDiffPath, |
| 396 | Inputs: android.Paths{this.abiDumpPath, nextAbiDump.Path()}, |
| 397 | Args: map[string]string{ |
| 398 | "args": "--no-added-syms", |
| 399 | }, |
| 400 | }) |
| 401 | } |
| 402 | this.abiDiffPaths = append(this.abiDiffPaths, nextAbiDiffPath) |
| 403 | } |
| 404 | } |
| 405 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 406 | func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 407 | if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") { |
Dan Albert | 15be0c6 | 2017-06-13 15:14:56 -0700 | [diff] [blame] | 408 | ctx.PropertyErrorf("symbol_file", "must end with .map.txt") |
| 409 | } |
| 410 | |
Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 411 | if !c.buildStubs() { |
| 412 | // NDK libraries have no implementation variant, nothing to do |
| 413 | return Objects{} |
| 414 | } |
| 415 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 416 | if !c.initializeProperties(ctx) { |
| 417 | // Emits its own errors, so we don't need to. |
| 418 | return Objects{} |
| 419 | } |
| 420 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 421 | symbolFile := String(c.properties.Symbol_file) |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 422 | nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile, c.apiLevel, "") |
| 423 | objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc) |
| 424 | c.versionScriptPath = nativeAbiResult.versionScript |
Dan Albert | ad66593 | 2021-06-07 13:19:49 -0700 | [diff] [blame] | 425 | if canDumpAbi() { |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 426 | c.dumpAbi(ctx, nativeAbiResult.symbolList) |
Dan Albert | ad66593 | 2021-06-07 13:19:49 -0700 | [diff] [blame] | 427 | if canDiffAbi() { |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 428 | c.diffAbi(ctx) |
| 429 | } |
| 430 | } |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 431 | if c.apiLevel.IsCurrent() && ctx.PrimaryArch() { |
sophiez | 4c4f803 | 2021-08-16 22:54:00 -0700 | [diff] [blame^] | 432 | c.parsedCoverageXmlPath = parseSymbolFileForAPICoverage(ctx, symbolFile) |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 433 | } |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 434 | return objs |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 437 | func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 438 | return Deps{} |
| 439 | } |
| 440 | |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 441 | func (linker *stubDecorator) Name(name string) string { |
| 442 | return name + ndkLibrarySuffix |
| 443 | } |
| 444 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 445 | func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 446 | stub.libraryDecorator.libName = ctx.baseModuleName() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 447 | return stub.libraryDecorator.linkerFlags(ctx, flags) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 450 | func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 451 | objs Objects) android.Path { |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 452 | |
Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 453 | if !stub.buildStubs() { |
| 454 | // NDK libraries have no implementation variant, nothing to do |
| 455 | return nil |
| 456 | } |
| 457 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 458 | if shouldUseVersionScript(ctx, stub) { |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 459 | linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String() |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 460 | flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag) |
Dan Willemsen | 939408a | 2019-06-10 18:02:25 -0700 | [diff] [blame] | 461 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath) |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 464 | stub.libraryDecorator.skipAPIDefine = true |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 465 | return stub.libraryDecorator.link(ctx, flags, deps, objs) |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 468 | func (stub *stubDecorator) nativeCoverage() bool { |
| 469 | return false |
| 470 | } |
| 471 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 472 | func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 473 | arch := ctx.Target().Arch.ArchType.Name |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 474 | // arm64 isn't actually a multilib toolchain, so unlike the other LP64 |
| 475 | // architectures it's just installed to lib. |
| 476 | libDir := "lib" |
| 477 | if ctx.toolchain().Is64Bit() && arch != "arm64" { |
| 478 | libDir = "lib64" |
| 479 | } |
| 480 | |
| 481 | installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf( |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 482 | "platforms/android-%s/arch-%s/usr/%s", stub.apiLevel, arch, libDir)) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 483 | stub.installPath = ctx.InstallFile(installDir, path.Base(), path) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 486 | func newStubLibrary() *Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 487 | module, library := NewLibrary(android.DeviceSupported) |
| 488 | library.BuildOnlyShared() |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 489 | module.stl = nil |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 490 | module.sanitize = nil |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 491 | library.disableStripping() |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 492 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 493 | stub := &stubDecorator{ |
| 494 | libraryDecorator: library, |
| 495 | } |
| 496 | module.compiler = stub |
| 497 | module.linker = stub |
| 498 | module.installer = stub |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 499 | module.library = stub |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 500 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 501 | module.Properties.AlwaysSdk = true |
| 502 | module.Properties.Sdk_version = StringPtr("current") |
| 503 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 504 | module.AddProperties(&stub.properties, &library.MutatedProperties) |
| 505 | |
| 506 | return module |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Dan Albert | f740ed0 | 2020-07-24 14:19:06 -0700 | [diff] [blame] | 509 | // ndk_library creates a library that exposes a stub implementation of functions |
| 510 | // and variables for use at build time only. |
Jooyung Han | b90e491 | 2019-12-09 18:21:48 +0900 | [diff] [blame] | 511 | func NdkLibraryFactory() android.Module { |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 512 | module := newStubLibrary() |
| 513 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth) |
| 514 | return module |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 515 | } |