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