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