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