blob: c0c6eef3f9340a054d373fadd61caff6a790d901 [file] [log] [blame]
Dan Albert914449f2016-06-17 16:45:24 -07001// 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
15package cc
16
17import (
18 "fmt"
Dan Albertf1d14c72020-07-30 14:32:55 -070019 "path/filepath"
Dan Albertad665932021-06-07 13:19:49 -070020 "runtime"
Dan Albert914449f2016-06-17 16:45:24 -070021 "strings"
Colin Crosse8a67a72016-08-07 21:17:54 -070022 "sync"
Dan Albert914449f2016-06-17 16:45:24 -070023
24 "github.com/google/blueprint"
Jiyong Parkee9b1172021-04-06 17:40:32 +090025 "github.com/google/blueprint/proptools"
Dan Albert914449f2016-06-17 16:45:24 -070026
27 "android/soong/android"
Spandan Das1278c2c2022-08-19 18:17:28 +000028 "android/soong/bazel"
Jingwen Chen341f7352022-01-11 05:42:49 +000029 "android/soong/cc/config"
Dan Albert914449f2016-06-17 16:45:24 -070030)
31
sophiez58cabb72020-05-29 13:37:12 -070032func init() {
Dan Albert06f58af2020-06-22 15:10:31 -070033 pctx.HostBinToolVariable("ndkStubGenerator", "ndkstubgen")
Dan Albertf1d14c72020-07-30 14:32:55 -070034 pctx.HostBinToolVariable("abidw", "abidw")
Matthias Maennichca8ae652023-06-30 21:52:17 +010035 pctx.HostBinToolVariable("stg", "stg")
Matthias Maennich658bb4d2023-06-30 23:28:15 +010036 pctx.HostBinToolVariable("stgdiff", "stgdiff")
sophiez58cabb72020-05-29 13:37:12 -070037}
38
Dan Albert914449f2016-06-17 16:45:24 -070039var (
Colin Cross9d45bb72016-08-29 16:14:13 -070040 genStubSrc = pctx.AndroidStaticRule("genStubSrc",
Dan Albert914449f2016-06-17 16:45:24 -070041 blueprint.RuleParams{
Dan Albert06f58af2020-06-22 15:10:31 -070042 Command: "$ndkStubGenerator --arch $arch --api $apiLevel " +
43 "--api-map $apiMap $flags $in $out",
44 CommandDeps: []string{"$ndkStubGenerator"},
Jiyong Park3fd0baf2018-12-07 16:25:39 +090045 }, "arch", "apiLevel", "apiMap", "flags")
Dan Albert914449f2016-06-17 16:45:24 -070046
Matthias Maennich55486f82023-07-03 12:25:27 +010047 // TODO(b/156513478): remove once migration to STG is complete
Dan Albertf1d14c72020-07-30 14:32:55 -070048 abidw = pctx.AndroidStaticRule("abidw",
49 blueprint.RuleParams{
50 Command: "$abidw --type-id-style hash --no-corpus-path " +
Matthias Maennichc2346f12021-09-02 20:45:33 +010051 "--no-show-locs --no-comp-dir-path -w $symbolList " +
52 "$in --out-file $out",
53 CommandDeps: []string{"$abidw"},
Dan Albertf1d14c72020-07-30 14:32:55 -070054 }, "symbolList")
55
Matthias Maennich55486f82023-07-03 12:25:27 +010056 stg = pctx.AndroidStaticRule("stg",
57 blueprint.RuleParams{
58 Command: "$stg -S :$symbolList --elf $in -o $out",
59 CommandDeps: []string{"$stg"},
60 }, "symbolList")
61
62 // TODO(b/156513478): remove once migration to STG is complete
Matthias Maennichca8ae652023-06-30 21:52:17 +010063 xml2stg = pctx.AndroidStaticRule("xml2stg",
Matthias Maennichc2346f12021-09-02 20:45:33 +010064 blueprint.RuleParams{
Matthias Maennichca8ae652023-06-30 21:52:17 +010065 Command: "$stg --abi -i $in -o $out",
66 CommandDeps: []string{"$stg"},
67 })
Matthias Maennichc2346f12021-09-02 20:45:33 +010068
Matthias Maennich658bb4d2023-06-30 23:28:15 +010069 stgdiff = pctx.AndroidStaticRule("stgdiff",
Dan Albertf1d14c72020-07-30 14:32:55 -070070 blueprint.RuleParams{
71 // Need to create *some* output for ninja. We don't want to use tee
72 // because we don't want to spam the build output with "nothing
73 // changed" messages, so redirect output message to $out, and if
74 // changes were detected print the output and fail.
Matthias Maennich658bb4d2023-06-30 23:28:15 +010075 Command: "$stgdiff $args --stg $in -o $out || (cat $out && false)",
76 CommandDeps: []string{"$stgdiff"},
Dan Albertf1d14c72020-07-30 14:32:55 -070077 }, "args")
78
Dan Albert914449f2016-06-17 16:45:24 -070079 ndkLibrarySuffix = ".ndk"
Colin Cross4d9c2d12016-07-29 12:48:20 -070080
Colin Cross95f1ca02020-10-29 20:47:22 -070081 ndkKnownLibsKey = android.NewOnceKey("ndkKnownLibsKey")
Dan Albertde5aade2020-06-30 12:32:51 -070082 // protects ndkKnownLibs writes during parallel BeginMutator.
83 ndkKnownLibsLock sync.Mutex
Dan Albertf1d14c72020-07-30 14:32:55 -070084
85 stubImplementation = dependencyTag{name: "stubImplementation"}
Dan Albert914449f2016-06-17 16:45:24 -070086)
87
Dan Albert1a246272020-07-06 14:49:35 -070088// The First_version and Unversioned_until properties of this struct should not
89// be used directly, but rather through the ApiLevel returning methods
90// firstVersion() and unversionedUntil().
91
Dan Albert914449f2016-06-17 16:45:24 -070092// Creates a stub shared library based on the provided version file.
93//
Dan Albert914449f2016-06-17 16:45:24 -070094// Example:
95//
Spandan Das73bcafc2022-08-18 23:26:00 +000096// ndk_library {
97//
98// name: "libfoo",
99// symbol_file: "libfoo.map.txt",
100// first_version: "9",
101//
102// }
Dan Albert914449f2016-06-17 16:45:24 -0700103type libraryProperties struct {
104 // Relative path to the symbol map.
105 // An example file can be seen here: TODO(danalbert): Make an example.
Inseob Kim5eb7ee92022-04-27 10:30:34 +0900106 Symbol_file *string `android:"path"`
Dan Albert914449f2016-06-17 16:45:24 -0700107
108 // The first API level a library was available. A library will be generated
109 // for every API level beginning with this one.
Nan Zhang0007d812017-11-07 10:57:05 -0800110 First_version *string
Dan Albert914449f2016-06-17 16:45:24 -0700111
Dan Albert98dbb3b2017-01-03 15:16:29 -0800112 // The first API level that library should have the version script applied.
113 // This defaults to the value of first_version, and should almost never be
114 // used. This is only needed to work around platform bugs like
115 // https://github.com/android-ndk/ndk/issues/265.
Nan Zhang0007d812017-11-07 10:57:05 -0800116 Unversioned_until *string
Dan Albert604086f2021-06-15 13:23:44 -0700117
Spandan Das73bcafc2022-08-18 23:26:00 +0000118 // Headers presented by this library to the Public API Surface
119 Export_header_libs []string
Matthias Maennich55486f82023-07-03 12:25:27 +0100120
121 // TODO(b/156513478): remove once migration to STG is complete
122 // Fall back to the legacy abidw ABI extraction pipeline
123 Legacy_use_abidw *bool
Dan Albert914449f2016-06-17 16:45:24 -0700124}
125
Colin Crossb916a382016-07-29 17:28:03 -0700126type stubDecorator struct {
127 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -0700128
129 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -0700130
sophiez58cabb72020-05-29 13:37:12 -0700131 versionScriptPath android.ModuleGenPath
132 parsedCoverageXmlPath android.ModuleOutPath
133 installPath android.Path
Dan Albertf1d14c72020-07-30 14:32:55 -0700134 abiDumpPath android.OutputPath
135 abiDiffPaths android.Paths
Dan Albert1a246272020-07-06 14:49:35 -0700136
137 apiLevel android.ApiLevel
138 firstVersion android.ApiLevel
139 unversionedUntil android.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700140}
141
Colin Cross0477b422020-10-13 18:43:54 -0700142var _ versionedInterface = (*stubDecorator)(nil)
143
Dan Albert1a246272020-07-06 14:49:35 -0700144func shouldUseVersionScript(ctx BaseModuleContext, stub *stubDecorator) bool {
145 return stub.apiLevel.GreaterThanOrEqualTo(stub.unversionedUntil)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800146}
147
Colin Cross0477b422020-10-13 18:43:54 -0700148func (stub *stubDecorator) implementationModuleName(name string) string {
149 return strings.TrimSuffix(name, ndkLibrarySuffix)
150}
151
Colin Cross3572cf72020-10-01 15:58:11 -0700152func ndkLibraryVersions(ctx android.BaseMutatorContext, from android.ApiLevel) []string {
Dan Albert1a246272020-07-06 14:49:35 -0700153 var versions []android.ApiLevel
154 versionStrs := []string{}
155 for _, version := range ctx.Config().AllSupportedApiLevels() {
156 if version.GreaterThanOrEqualTo(from) {
157 versions = append(versions, version)
158 versionStrs = append(versionStrs, version.String())
159 }
Dan Albert914449f2016-06-17 16:45:24 -0700160 }
Dan Albert0b176c82020-07-23 16:43:25 -0700161 versionStrs = append(versionStrs, android.FutureApiLevel.String())
Dan Albert914449f2016-06-17 16:45:24 -0700162
Colin Cross5ec407b2020-09-30 11:41:33 -0700163 return versionStrs
164}
165
Colin Cross3572cf72020-10-01 15:58:11 -0700166func (this *stubDecorator) stubsVersions(ctx android.BaseMutatorContext) []string {
167 if !ctx.Module().Enabled() {
168 return nil
169 }
Dan Albertf1d14c72020-07-30 14:32:55 -0700170 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
171 ctx.Module().Disable()
172 return nil
173 }
Colin Cross3572cf72020-10-01 15:58:11 -0700174 firstVersion, err := nativeApiLevelFromUser(ctx,
175 String(this.properties.First_version))
176 if err != nil {
177 ctx.PropertyErrorf("first_version", err.Error())
178 return nil
179 }
180 return ndkLibraryVersions(ctx, firstVersion)
181}
182
Dan Albert1a246272020-07-06 14:49:35 -0700183func (this *stubDecorator) initializeProperties(ctx BaseModuleContext) bool {
Colin Cross5ec407b2020-09-30 11:41:33 -0700184 this.apiLevel = nativeApiLevelOrPanic(ctx, this.stubsVersion())
Dan Albert1a246272020-07-06 14:49:35 -0700185
186 var err error
187 this.firstVersion, err = nativeApiLevelFromUser(ctx,
188 String(this.properties.First_version))
189 if err != nil {
190 ctx.PropertyErrorf("first_version", err.Error())
191 return false
192 }
193
Jiyong Parkee9b1172021-04-06 17:40:32 +0900194 str := proptools.StringDefault(this.properties.Unversioned_until, "minimum")
195 this.unversionedUntil, err = nativeApiLevelFromUser(ctx, str)
Dan Albert1a246272020-07-06 14:49:35 -0700196 if err != nil {
197 ctx.PropertyErrorf("unversioned_until", err.Error())
198 return false
199 }
200
201 return true
202}
203
Colin Cross95f1ca02020-10-29 20:47:22 -0700204func getNDKKnownLibs(config android.Config) *[]string {
205 return config.Once(ndkKnownLibsKey, func() interface{} {
206 return &[]string{}
207 }).(*[]string)
208}
209
Colin Crossb916a382016-07-29 17:28:03 -0700210func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700211 c.baseCompiler.compilerInit(ctx)
212
Dan Willemsen01a90592017-04-07 15:21:13 -0700213 name := ctx.baseModuleName()
214 if strings.HasSuffix(name, ndkLibrarySuffix) {
215 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
216 }
217
Dan Albertde5aade2020-06-30 12:32:51 -0700218 ndkKnownLibsLock.Lock()
219 defer ndkKnownLibsLock.Unlock()
Colin Cross95f1ca02020-10-29 20:47:22 -0700220 ndkKnownLibs := getNDKKnownLibs(ctx.Config())
221 for _, lib := range *ndkKnownLibs {
Dan Albert7e9d2952016-08-04 13:02:36 -0700222 if lib == name {
223 return
224 }
225 }
Colin Cross95f1ca02020-10-29 20:47:22 -0700226 *ndkKnownLibs = append(*ndkKnownLibs, name)
Dan Albert7e9d2952016-08-04 13:02:36 -0700227}
228
Jingwen Chen341f7352022-01-11 05:42:49 +0000229var stubLibraryCompilerFlags = []string{
230 // We're knowingly doing some otherwise unsightly things with builtin
231 // functions here. We're just generating stub libraries, so ignore it.
232 "-Wno-incompatible-library-redeclaration",
233 "-Wno-incomplete-setjmp-declaration",
234 "-Wno-builtin-requires-header",
235 "-Wno-invalid-noreturn",
236 "-Wall",
237 "-Werror",
238 // These libraries aren't actually used. Don't worry about unwinding
239 // (avoids the need to link an unwinder into a fake library).
240 "-fno-unwind-tables",
241}
242
243func init() {
244 config.ExportStringList("StubLibraryCompilerFlags", stubLibraryCompilerFlags)
245}
246
George Burgess IVf5310e32017-07-19 11:39:53 -0700247func addStubLibraryCompilerFlags(flags Flags) Flags {
Jingwen Chen341f7352022-01-11 05:42:49 +0000248 flags.Global.CFlags = append(flags.Global.CFlags, stubLibraryCompilerFlags...)
Jiyong Park48d75ef2019-11-21 15:11:49 +0900249 // All symbols in the stubs library should be visible.
250 if inList("-fvisibility=hidden", flags.Local.CFlags) {
251 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
252 }
George Burgess IVf5310e32017-07-19 11:39:53 -0700253 return flags
254}
255
Colin Crossf18e1102017-11-16 14:33:08 -0800256func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
257 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700258 return addStubLibraryCompilerFlags(flags)
259}
260
Dan Albertf1d14c72020-07-30 14:32:55 -0700261type ndkApiOutputs struct {
262 stubSrc android.ModuleGenPath
263 versionScript android.ModuleGenPath
264 symbolList android.ModuleGenPath
265}
266
267func parseNativeAbiDefinition(ctx ModuleContext, symbolFile string,
268 apiLevel android.ApiLevel, genstubFlags string) ndkApiOutputs {
Dan Albert914449f2016-06-17 16:45:24 -0700269
Dan Willemsenb916b802017-03-19 13:44:32 -0700270 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
271 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
272 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albertf1d14c72020-07-30 14:32:55 -0700273 symbolListPath := android.PathForModuleGen(ctx, "abi_symbol_list.txt")
Dan Albert49927d22017-03-28 15:00:46 -0700274 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700275 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700276 Rule: genStubSrc,
277 Description: "generate stubs " + symbolFilePath.Rel(),
Dan Albertf1d14c72020-07-30 14:32:55 -0700278 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath,
279 symbolListPath},
280 Input: symbolFilePath,
281 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700282 Args: map[string]string{
Dan Albertf1d14c72020-07-30 14:32:55 -0700283 "arch": ctx.Arch().ArchType.String(),
284 "apiLevel": apiLevel.String(),
Dan Albert49927d22017-03-28 15:00:46 -0700285 "apiMap": apiLevelsJson.String(),
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900286 "flags": genstubFlags,
Dan Albert914449f2016-06-17 16:45:24 -0700287 },
288 })
289
Dan Albertf1d14c72020-07-30 14:32:55 -0700290 return ndkApiOutputs{
291 stubSrc: stubSrcPath,
292 versionScript: versionScriptPath,
293 symbolList: symbolListPath,
294 }
295}
296
297func compileStubLibrary(ctx ModuleContext, flags Flags, src android.Path) Objects {
Mitch Phillips4e5f9a12022-04-29 13:12:28 -0700298 // libc/libm stubs libraries end up mismatching with clang's internal definition of these
299 // functions (which have noreturn attributes and other things). Because we just want to create a
300 // stub with symbol definitions, and types aren't important in C, ignore the mismatch.
301 flags.Local.ConlyFlags = append(flags.Local.ConlyFlags, "-fno-builtin")
Dan Albertf1d14c72020-07-30 14:32:55 -0700302 return compileObjs(ctx, flagsToBuilderFlags(flags), "",
Chih-Hung Hsieh9db8a0c2022-02-17 12:54:45 -0800303 android.Paths{src}, nil, nil, nil, nil)
Dan Willemsenb916b802017-03-19 13:44:32 -0700304}
305
Dan Albertf1d14c72020-07-30 14:32:55 -0700306func (this *stubDecorator) findImplementationLibrary(ctx ModuleContext) android.Path {
307 dep := ctx.GetDirectDepWithTag(strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix),
308 stubImplementation)
309 if dep == nil {
310 ctx.ModuleErrorf("Could not find implementation for stub")
311 return nil
312 }
313 impl, ok := dep.(*Module)
314 if !ok {
315 ctx.ModuleErrorf("Implementation for stub is not correct module type")
Alan Stokes73d32452022-11-01 14:05:08 +0000316 return nil
Dan Albertf1d14c72020-07-30 14:32:55 -0700317 }
318 output := impl.UnstrippedOutputFile()
319 if output == nil {
320 ctx.ModuleErrorf("implementation module (%s) has no output", impl)
321 return nil
322 }
323
324 return output
325}
326
327func (this *stubDecorator) libraryName(ctx ModuleContext) string {
328 return strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix)
329}
330
331func (this *stubDecorator) findPrebuiltAbiDump(ctx ModuleContext,
332 apiLevel android.ApiLevel) android.OptionalPath {
333
334 subpath := filepath.Join("prebuilts/abi-dumps/ndk", apiLevel.String(),
Matthias Maennichca8ae652023-06-30 21:52:17 +0100335 ctx.Arch().ArchType.String(), this.libraryName(ctx), "abi.stg")
Dan Albertf1d14c72020-07-30 14:32:55 -0700336 return android.ExistentPathForSource(ctx, subpath)
337}
338
339// Feature flag.
Dan Albertf71006a2022-04-14 23:08:51 +0000340func canDumpAbi(config android.Config) bool {
341 if runtime.GOOS == "darwin" {
342 return false
343 }
344 // abidw doesn't currently handle top-byte-ignore correctly. Disable ABI
345 // dumping for those configs while we wait for a fix. We'll still have ABI
346 // checking coverage from non-hwasan builds.
347 // http://b/190554910
348 if android.InList("hwaddress", config.SanitizeDevice()) {
349 return false
350 }
Dan Albert326ab242023-04-20 17:38:29 +0000351 // http://b/156513478
352 // http://b/277624006
353 // This step is expensive. We're not able to do anything with the outputs of
354 // this step yet (canDiffAbi is flagged off because libabigail isn't able to
355 // handle all our libraries), disable it. There's no sense in protecting
356 // against checking in code that breaks abidw since by the time any of this
357 // can be turned on we'll need to migrate to STG anyway.
358 return false
Dan Albertf1d14c72020-07-30 14:32:55 -0700359}
360
361// Feature flag to disable diffing against prebuilts.
Dan Albertad665932021-06-07 13:19:49 -0700362func canDiffAbi() bool {
Dan Albertf1d14c72020-07-30 14:32:55 -0700363 return false
364}
365
Matthias Maennich55486f82023-07-03 12:25:27 +0100366// TODO(b/156513478): remove once migration to STG is complete
367func (this *stubDecorator) dumpAbiLegacy(ctx ModuleContext, symbolList android.Path) {
Dan Albertf1d14c72020-07-30 14:32:55 -0700368 implementationLibrary := this.findImplementationLibrary(ctx)
Matthias Maennichc2346f12021-09-02 20:45:33 +0100369 abiRawPath := getNdkAbiDumpInstallBase(ctx).Join(ctx,
Dan Albertf1d14c72020-07-30 14:32:55 -0700370 this.apiLevel.String(), ctx.Arch().ArchType.String(),
Matthias Maennichc2346f12021-09-02 20:45:33 +0100371 this.libraryName(ctx), "abi.raw.xml")
Dan Albertf1d14c72020-07-30 14:32:55 -0700372 ctx.Build(pctx, android.BuildParams{
373 Rule: abidw,
374 Description: fmt.Sprintf("abidw %s", implementationLibrary),
Dan Albertf1d14c72020-07-30 14:32:55 -0700375 Input: implementationLibrary,
Matthias Maennichc2346f12021-09-02 20:45:33 +0100376 Output: abiRawPath,
Dan Albertf1d14c72020-07-30 14:32:55 -0700377 Implicit: symbolList,
378 Args: map[string]string{
379 "symbolList": symbolList.String(),
380 },
381 })
Dan Albert604086f2021-06-15 13:23:44 -0700382
Matthias Maennichc2346f12021-09-02 20:45:33 +0100383 this.abiDumpPath = getNdkAbiDumpInstallBase(ctx).Join(ctx,
384 this.apiLevel.String(), ctx.Arch().ArchType.String(),
Matthias Maennichca8ae652023-06-30 21:52:17 +0100385 this.libraryName(ctx), "abi.stg")
Matthias Maennichc2346f12021-09-02 20:45:33 +0100386 ctx.Build(pctx, android.BuildParams{
Matthias Maennichca8ae652023-06-30 21:52:17 +0100387 Rule: xml2stg,
388 Description: fmt.Sprintf("xml2stg %s", implementationLibrary),
Matthias Maennichc2346f12021-09-02 20:45:33 +0100389 Input: abiRawPath,
390 Output: this.abiDumpPath,
391 })
Dan Albertf1d14c72020-07-30 14:32:55 -0700392}
393
Matthias Maennich55486f82023-07-03 12:25:27 +0100394func (this *stubDecorator) dumpAbi(ctx ModuleContext, symbolList android.Path) {
395 implementationLibrary := this.findImplementationLibrary(ctx)
396 this.abiDumpPath = getNdkAbiDumpInstallBase(ctx).Join(ctx,
397 this.apiLevel.String(), ctx.Arch().ArchType.String(),
398 this.libraryName(ctx), "abi.stg")
399 ctx.Build(pctx, android.BuildParams{
400 Rule: stg,
401 Description: fmt.Sprintf("stg %s", implementationLibrary),
402 Input: implementationLibrary,
403 Implicit: symbolList,
404 Output: this.abiDumpPath,
405 Args: map[string]string{
406 "symbolList": symbolList.String(),
407 },
408 })
409}
410
Dan Albertf1d14c72020-07-30 14:32:55 -0700411func findNextApiLevel(ctx ModuleContext, apiLevel android.ApiLevel) *android.ApiLevel {
412 apiLevels := append(ctx.Config().AllSupportedApiLevels(),
413 android.FutureApiLevel)
414 for _, api := range apiLevels {
415 if api.GreaterThan(apiLevel) {
416 return &api
417 }
418 }
419 return nil
420}
421
422func (this *stubDecorator) diffAbi(ctx ModuleContext) {
Dan Albertf1d14c72020-07-30 14:32:55 -0700423 // Catch any ABI changes compared to the checked-in definition of this API
424 // level.
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100425 abiDiffPath := android.PathForModuleOut(ctx, "stgdiff.timestamp")
Dan Albertf1d14c72020-07-30 14:32:55 -0700426 prebuiltAbiDump := this.findPrebuiltAbiDump(ctx, this.apiLevel)
Dan Albertf7cb5632022-11-29 17:20:16 +0000427 missingPrebuiltError := fmt.Sprintf(
428 "Did not find prebuilt ABI dump for %q (%q). Generate with "+
429 "//development/tools/ndk/update_ndk_abi.sh.", this.libraryName(ctx),
430 prebuiltAbiDump.InvalidReason())
Dan Albertf1d14c72020-07-30 14:32:55 -0700431 if !prebuiltAbiDump.Valid() {
432 ctx.Build(pctx, android.BuildParams{
433 Rule: android.ErrorRule,
434 Output: abiDiffPath,
435 Args: map[string]string{
436 "error": missingPrebuiltError,
437 },
438 })
439 } else {
440 ctx.Build(pctx, android.BuildParams{
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100441 Rule: stgdiff,
442 Description: fmt.Sprintf("Comparing ABI %s %s", prebuiltAbiDump,
Dan Albertf1d14c72020-07-30 14:32:55 -0700443 this.abiDumpPath),
444 Output: abiDiffPath,
445 Inputs: android.Paths{prebuiltAbiDump.Path(), this.abiDumpPath},
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100446 Args: map[string]string{
447 "args": "--format=small",
448 },
Dan Albertf1d14c72020-07-30 14:32:55 -0700449 })
450 }
451 this.abiDiffPaths = append(this.abiDiffPaths, abiDiffPath)
452
453 // Also ensure that the ABI of the next API level (if there is one) matches
454 // this API level. *New* ABI is allowed, but any changes to APIs that exist
455 // in this API level are disallowed.
456 if !this.apiLevel.IsCurrent() {
457 nextApiLevel := findNextApiLevel(ctx, this.apiLevel)
458 if nextApiLevel == nil {
459 panic(fmt.Errorf("could not determine which API level follows "+
460 "non-current API level %s", this.apiLevel))
461 }
462 nextAbiDiffPath := android.PathForModuleOut(ctx,
463 "abidiff_next.timestamp")
464 nextAbiDump := this.findPrebuiltAbiDump(ctx, *nextApiLevel)
465 if !nextAbiDump.Valid() {
466 ctx.Build(pctx, android.BuildParams{
467 Rule: android.ErrorRule,
468 Output: nextAbiDiffPath,
469 Args: map[string]string{
470 "error": missingPrebuiltError,
471 },
472 })
473 } else {
474 ctx.Build(pctx, android.BuildParams{
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100475 Rule: stgdiff,
Dan Albertf1d14c72020-07-30 14:32:55 -0700476 Description: fmt.Sprintf("abidiff %s %s", this.abiDumpPath,
477 nextAbiDump),
478 Output: nextAbiDiffPath,
479 Inputs: android.Paths{this.abiDumpPath, nextAbiDump.Path()},
480 Args: map[string]string{
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100481 "args": "--format=small --ignore=interface_addition",
Dan Albertf1d14c72020-07-30 14:32:55 -0700482 },
483 })
484 }
485 this.abiDiffPaths = append(this.abiDiffPaths, nextAbiDiffPath)
486 }
487}
488
Dan Willemsenb916b802017-03-19 13:44:32 -0700489func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800490 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700491 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
492 }
493
Colin Cross5ec407b2020-09-30 11:41:33 -0700494 if !c.buildStubs() {
495 // NDK libraries have no implementation variant, nothing to do
496 return Objects{}
497 }
498
Dan Albert1a246272020-07-06 14:49:35 -0700499 if !c.initializeProperties(ctx) {
500 // Emits its own errors, so we don't need to.
501 return Objects{}
502 }
503
sophiez58cabb72020-05-29 13:37:12 -0700504 symbolFile := String(c.properties.Symbol_file)
Dan Albertf1d14c72020-07-30 14:32:55 -0700505 nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile, c.apiLevel, "")
506 objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
507 c.versionScriptPath = nativeAbiResult.versionScript
Dan Albertf71006a2022-04-14 23:08:51 +0000508 if canDumpAbi(ctx.Config()) {
Matthias Maennich55486f82023-07-03 12:25:27 +0100509 if proptools.BoolDefault(c.properties.Legacy_use_abidw, false) {
510 c.dumpAbiLegacy(ctx, nativeAbiResult.symbolList)
511 } else {
512 c.dumpAbi(ctx, nativeAbiResult.symbolList)
513 }
Dan Albertad665932021-06-07 13:19:49 -0700514 if canDiffAbi() {
Dan Albertf1d14c72020-07-30 14:32:55 -0700515 c.diffAbi(ctx)
516 }
517 }
Dan Albert1a246272020-07-06 14:49:35 -0700518 if c.apiLevel.IsCurrent() && ctx.PrimaryArch() {
sophiez4c4f8032021-08-16 22:54:00 -0700519 c.parsedCoverageXmlPath = parseSymbolFileForAPICoverage(ctx, symbolFile)
sophiez58cabb72020-05-29 13:37:12 -0700520 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700521 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700522}
523
Spandan Das73bcafc2022-08-18 23:26:00 +0000524// Add a dependency on the header modules of this ndk_library
Colin Cross37047f12016-12-13 17:06:13 -0800525func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Spandan Das73bcafc2022-08-18 23:26:00 +0000526 return Deps{
527 HeaderLibs: linker.properties.Export_header_libs,
528 }
Dan Albert914449f2016-06-17 16:45:24 -0700529}
530
Dan Willemsen01a90592017-04-07 15:21:13 -0700531func (linker *stubDecorator) Name(name string) string {
532 return name + ndkLibrarySuffix
533}
534
Colin Crossb916a382016-07-29 17:28:03 -0700535func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700536 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700537 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700538}
539
Colin Crossb916a382016-07-29 17:28:03 -0700540func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700541 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700542
Colin Cross5ec407b2020-09-30 11:41:33 -0700543 if !stub.buildStubs() {
544 // NDK libraries have no implementation variant, nothing to do
545 return nil
546 }
547
Dan Albert1a246272020-07-06 14:49:35 -0700548 if shouldUseVersionScript(ctx, stub) {
Dan Albert98dbb3b2017-01-03 15:16:29 -0800549 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800550 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700551 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800552 }
553
Colin Cross5ec407b2020-09-30 11:41:33 -0700554 stub.libraryDecorator.skipAPIDefine = true
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700555 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700556}
557
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700558func (stub *stubDecorator) nativeCoverage() bool {
559 return false
560}
561
Dan Albert4048bb02023-04-03 20:19:07 +0000562// Returns the install path for unversioned NDK libraries (currently only static
563// libraries).
564func getUnversionedLibraryInstallPath(ctx ModuleContext) android.InstallPath {
565 return getNdkSysrootBase(ctx).Join(ctx, "usr/lib", config.NDKTriple(ctx.toolchain()))
566}
Rebecca Chyung961cf1c2023-04-03 05:17:17 +0000567
Dan Albert4048bb02023-04-03 20:19:07 +0000568// Returns the install path for versioned NDK libraries. These are most often
569// stubs, but the same paths are used for CRT objects.
570func getVersionedLibraryInstallPath(ctx ModuleContext, apiLevel android.ApiLevel) android.InstallPath {
571 return getUnversionedLibraryInstallPath(ctx).Join(ctx, apiLevel.String())
572}
573
574func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
575 installDir := getVersionedLibraryInstallPath(ctx, stub.apiLevel)
Colin Cross0875c522017-11-28 17:34:01 -0800576 stub.installPath = ctx.InstallFile(installDir, path.Base(), path)
Dan Albert914449f2016-06-17 16:45:24 -0700577}
578
Colin Cross36242852017-06-23 15:06:31 -0700579func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800580 module, library := NewLibrary(android.DeviceSupported)
581 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700582 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700583 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200584 library.disableStripping()
Dan Albert914449f2016-06-17 16:45:24 -0700585
Colin Crossb916a382016-07-29 17:28:03 -0700586 stub := &stubDecorator{
587 libraryDecorator: library,
588 }
589 module.compiler = stub
590 module.linker = stub
591 module.installer = stub
Colin Cross31076b32020-10-23 17:22:06 -0700592 module.library = stub
Dan Albert914449f2016-06-17 16:45:24 -0700593
Colin Crossc511bc52020-04-07 16:50:32 +0000594 module.Properties.AlwaysSdk = true
595 module.Properties.Sdk_version = StringPtr("current")
596
Colin Cross36242852017-06-23 15:06:31 -0700597 module.AddProperties(&stub.properties, &library.MutatedProperties)
598
599 return module
Dan Albert914449f2016-06-17 16:45:24 -0700600}
601
Dan Albertf740ed02020-07-24 14:19:06 -0700602// ndk_library creates a library that exposes a stub implementation of functions
603// and variables for use at build time only.
Jooyung Hanb90e4912019-12-09 18:21:48 +0900604func NdkLibraryFactory() android.Module {
Colin Cross36242852017-06-23 15:06:31 -0700605 module := newStubLibrary()
606 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
Spandan Das1278c2c2022-08-19 18:17:28 +0000607 android.InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700608 return module
Dan Albert914449f2016-06-17 16:45:24 -0700609}
Spandan Das1278c2c2022-08-19 18:17:28 +0000610
611type bazelCcApiContributionAttributes struct {
612 Api bazel.LabelAttribute
613 Api_surfaces bazel.StringListAttribute
614 Hdrs bazel.LabelListAttribute
615 Library_name string
616}
617
618// Names of the cc_api_header targets in the bp2build workspace
Spandan Das4238c652022-09-09 01:38:47 +0000619func apiHeaderLabels(ctx android.TopDownMutatorContext, hdrLibs []string) bazel.LabelList {
Spandan Das1278c2c2022-08-19 18:17:28 +0000620 addSuffix := func(ctx android.BazelConversionPathContext, module blueprint.Module) string {
621 label := android.BazelModuleLabel(ctx, module)
622 return android.ApiContributionTargetName(label)
623 }
Spandan Das4238c652022-09-09 01:38:47 +0000624 return android.BazelLabelForModuleDepsWithFn(ctx, hdrLibs, addSuffix)
Spandan Das1278c2c2022-08-19 18:17:28 +0000625}