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