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