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