blob: c91d85078dd3d2cc91e5b0fa769e393a4d22d57c [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("abidiff", "abidiff")
Dan Albertf1d14c72020-07-30 14:32:55 -070035 pctx.HostBinToolVariable("abidw", "abidw")
Matthias Maennichca8ae652023-06-30 21:52:17 +010036 pctx.HostBinToolVariable("stg", "stg")
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
Dan Albertf1d14c72020-07-30 14:32:55 -070047 abidw = pctx.AndroidStaticRule("abidw",
48 blueprint.RuleParams{
49 Command: "$abidw --type-id-style hash --no-corpus-path " +
Matthias Maennichc2346f12021-09-02 20:45:33 +010050 "--no-show-locs --no-comp-dir-path -w $symbolList " +
51 "$in --out-file $out",
52 CommandDeps: []string{"$abidw"},
Dan Albertf1d14c72020-07-30 14:32:55 -070053 }, "symbolList")
54
Matthias Maennichca8ae652023-06-30 21:52:17 +010055 xml2stg = pctx.AndroidStaticRule("xml2stg",
Matthias Maennichc2346f12021-09-02 20:45:33 +010056 blueprint.RuleParams{
Matthias Maennichca8ae652023-06-30 21:52:17 +010057 Command: "$stg --abi -i $in -o $out",
58 CommandDeps: []string{"$stg"},
59 })
Matthias Maennichc2346f12021-09-02 20:45:33 +010060
Dan Albertf1d14c72020-07-30 14:32:55 -070061 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 Albert914449f2016-06-17 16:45:24 -070071 ndkLibrarySuffix = ".ndk"
Colin Cross4d9c2d12016-07-29 12:48:20 -070072
Colin Cross95f1ca02020-10-29 20:47:22 -070073 ndkKnownLibsKey = android.NewOnceKey("ndkKnownLibsKey")
Dan Albertde5aade2020-06-30 12:32:51 -070074 // protects ndkKnownLibs writes during parallel BeginMutator.
75 ndkKnownLibsLock sync.Mutex
Dan Albertf1d14c72020-07-30 14:32:55 -070076
77 stubImplementation = dependencyTag{name: "stubImplementation"}
Dan Albert914449f2016-06-17 16:45:24 -070078)
79
Dan Albert1a246272020-07-06 14:49:35 -070080// 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 Albert914449f2016-06-17 16:45:24 -070084// Creates a stub shared library based on the provided version file.
85//
Dan Albert914449f2016-06-17 16:45:24 -070086// Example:
87//
Spandan Das73bcafc2022-08-18 23:26:00 +000088// ndk_library {
89//
90// name: "libfoo",
91// symbol_file: "libfoo.map.txt",
92// first_version: "9",
93//
94// }
Dan Albert914449f2016-06-17 16:45:24 -070095type libraryProperties struct {
96 // Relative path to the symbol map.
97 // An example file can be seen here: TODO(danalbert): Make an example.
Inseob Kim5eb7ee92022-04-27 10:30:34 +090098 Symbol_file *string `android:"path"`
Dan Albert914449f2016-06-17 16:45:24 -070099
100 // The first API level a library was available. A library will be generated
101 // for every API level beginning with this one.
Nan Zhang0007d812017-11-07 10:57:05 -0800102 First_version *string
Dan Albert914449f2016-06-17 16:45:24 -0700103
Dan Albert98dbb3b2017-01-03 15:16:29 -0800104 // 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 Zhang0007d812017-11-07 10:57:05 -0800108 Unversioned_until *string
Dan Albert604086f2021-06-15 13:23:44 -0700109
Spandan Das73bcafc2022-08-18 23:26:00 +0000110 // Headers presented by this library to the Public API Surface
111 Export_header_libs []string
Dan Albert914449f2016-06-17 16:45:24 -0700112}
113
Colin Crossb916a382016-07-29 17:28:03 -0700114type stubDecorator struct {
115 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -0700116
117 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -0700118
sophiez58cabb72020-05-29 13:37:12 -0700119 versionScriptPath android.ModuleGenPath
120 parsedCoverageXmlPath android.ModuleOutPath
121 installPath android.Path
Dan Albertf1d14c72020-07-30 14:32:55 -0700122 abiDumpPath android.OutputPath
123 abiDiffPaths android.Paths
Dan Albert1a246272020-07-06 14:49:35 -0700124
125 apiLevel android.ApiLevel
126 firstVersion android.ApiLevel
127 unversionedUntil android.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700128}
129
Colin Cross0477b422020-10-13 18:43:54 -0700130var _ versionedInterface = (*stubDecorator)(nil)
131
Dan Albert1a246272020-07-06 14:49:35 -0700132func shouldUseVersionScript(ctx BaseModuleContext, stub *stubDecorator) bool {
133 return stub.apiLevel.GreaterThanOrEqualTo(stub.unversionedUntil)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800134}
135
Colin Cross0477b422020-10-13 18:43:54 -0700136func (stub *stubDecorator) implementationModuleName(name string) string {
137 return strings.TrimSuffix(name, ndkLibrarySuffix)
138}
139
Colin Cross3572cf72020-10-01 15:58:11 -0700140func ndkLibraryVersions(ctx android.BaseMutatorContext, from android.ApiLevel) []string {
Dan Albert1a246272020-07-06 14:49:35 -0700141 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 Albert914449f2016-06-17 16:45:24 -0700148 }
Dan Albert0b176c82020-07-23 16:43:25 -0700149 versionStrs = append(versionStrs, android.FutureApiLevel.String())
Dan Albert914449f2016-06-17 16:45:24 -0700150
Colin Cross5ec407b2020-09-30 11:41:33 -0700151 return versionStrs
152}
153
Colin Cross3572cf72020-10-01 15:58:11 -0700154func (this *stubDecorator) stubsVersions(ctx android.BaseMutatorContext) []string {
155 if !ctx.Module().Enabled() {
156 return nil
157 }
Dan Albertf1d14c72020-07-30 14:32:55 -0700158 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
159 ctx.Module().Disable()
160 return nil
161 }
Colin Cross3572cf72020-10-01 15:58:11 -0700162 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 Albert1a246272020-07-06 14:49:35 -0700171func (this *stubDecorator) initializeProperties(ctx BaseModuleContext) bool {
Colin Cross5ec407b2020-09-30 11:41:33 -0700172 this.apiLevel = nativeApiLevelOrPanic(ctx, this.stubsVersion())
Dan Albert1a246272020-07-06 14:49:35 -0700173
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 Parkee9b1172021-04-06 17:40:32 +0900182 str := proptools.StringDefault(this.properties.Unversioned_until, "minimum")
183 this.unversionedUntil, err = nativeApiLevelFromUser(ctx, str)
Dan Albert1a246272020-07-06 14:49:35 -0700184 if err != nil {
185 ctx.PropertyErrorf("unversioned_until", err.Error())
186 return false
187 }
188
189 return true
190}
191
Colin Cross95f1ca02020-10-29 20:47:22 -0700192func getNDKKnownLibs(config android.Config) *[]string {
193 return config.Once(ndkKnownLibsKey, func() interface{} {
194 return &[]string{}
195 }).(*[]string)
196}
197
Colin Crossb916a382016-07-29 17:28:03 -0700198func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700199 c.baseCompiler.compilerInit(ctx)
200
Dan Willemsen01a90592017-04-07 15:21:13 -0700201 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 Albertde5aade2020-06-30 12:32:51 -0700206 ndkKnownLibsLock.Lock()
207 defer ndkKnownLibsLock.Unlock()
Colin Cross95f1ca02020-10-29 20:47:22 -0700208 ndkKnownLibs := getNDKKnownLibs(ctx.Config())
209 for _, lib := range *ndkKnownLibs {
Dan Albert7e9d2952016-08-04 13:02:36 -0700210 if lib == name {
211 return
212 }
213 }
Colin Cross95f1ca02020-10-29 20:47:22 -0700214 *ndkKnownLibs = append(*ndkKnownLibs, name)
Dan Albert7e9d2952016-08-04 13:02:36 -0700215}
216
Jingwen Chen341f7352022-01-11 05:42:49 +0000217var 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
231func init() {
232 config.ExportStringList("StubLibraryCompilerFlags", stubLibraryCompilerFlags)
233}
234
George Burgess IVf5310e32017-07-19 11:39:53 -0700235func addStubLibraryCompilerFlags(flags Flags) Flags {
Jingwen Chen341f7352022-01-11 05:42:49 +0000236 flags.Global.CFlags = append(flags.Global.CFlags, stubLibraryCompilerFlags...)
Jiyong Park48d75ef2019-11-21 15:11:49 +0900237 // 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 IVf5310e32017-07-19 11:39:53 -0700241 return flags
242}
243
Colin Crossf18e1102017-11-16 14:33:08 -0800244func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
245 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700246 return addStubLibraryCompilerFlags(flags)
247}
248
Dan Albertf1d14c72020-07-30 14:32:55 -0700249type ndkApiOutputs struct {
250 stubSrc android.ModuleGenPath
251 versionScript android.ModuleGenPath
252 symbolList android.ModuleGenPath
253}
254
255func parseNativeAbiDefinition(ctx ModuleContext, symbolFile string,
256 apiLevel android.ApiLevel, genstubFlags string) ndkApiOutputs {
Dan Albert914449f2016-06-17 16:45:24 -0700257
Dan Willemsenb916b802017-03-19 13:44:32 -0700258 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
259 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
260 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albertf1d14c72020-07-30 14:32:55 -0700261 symbolListPath := android.PathForModuleGen(ctx, "abi_symbol_list.txt")
Dan Albert49927d22017-03-28 15:00:46 -0700262 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700263 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700264 Rule: genStubSrc,
265 Description: "generate stubs " + symbolFilePath.Rel(),
Dan Albertf1d14c72020-07-30 14:32:55 -0700266 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath,
267 symbolListPath},
268 Input: symbolFilePath,
269 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700270 Args: map[string]string{
Dan Albertf1d14c72020-07-30 14:32:55 -0700271 "arch": ctx.Arch().ArchType.String(),
272 "apiLevel": apiLevel.String(),
Dan Albert49927d22017-03-28 15:00:46 -0700273 "apiMap": apiLevelsJson.String(),
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900274 "flags": genstubFlags,
Dan Albert914449f2016-06-17 16:45:24 -0700275 },
276 })
277
Dan Albertf1d14c72020-07-30 14:32:55 -0700278 return ndkApiOutputs{
279 stubSrc: stubSrcPath,
280 versionScript: versionScriptPath,
281 symbolList: symbolListPath,
282 }
283}
284
285func compileStubLibrary(ctx ModuleContext, flags Flags, src android.Path) Objects {
Mitch Phillips4e5f9a12022-04-29 13:12:28 -0700286 // 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 Albertf1d14c72020-07-30 14:32:55 -0700290 return compileObjs(ctx, flagsToBuilderFlags(flags), "",
Chih-Hung Hsieh9db8a0c2022-02-17 12:54:45 -0800291 android.Paths{src}, nil, nil, nil, nil)
Dan Willemsenb916b802017-03-19 13:44:32 -0700292}
293
Dan Albertf1d14c72020-07-30 14:32:55 -0700294func (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 Stokes73d32452022-11-01 14:05:08 +0000304 return nil
Dan Albertf1d14c72020-07-30 14:32:55 -0700305 }
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
315func (this *stubDecorator) libraryName(ctx ModuleContext) string {
316 return strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix)
317}
318
319func (this *stubDecorator) findPrebuiltAbiDump(ctx ModuleContext,
320 apiLevel android.ApiLevel) android.OptionalPath {
321
322 subpath := filepath.Join("prebuilts/abi-dumps/ndk", apiLevel.String(),
Matthias Maennichca8ae652023-06-30 21:52:17 +0100323 ctx.Arch().ArchType.String(), this.libraryName(ctx), "abi.stg")
Dan Albertf1d14c72020-07-30 14:32:55 -0700324 return android.ExistentPathForSource(ctx, subpath)
325}
326
327// Feature flag.
Dan Albertf71006a2022-04-14 23:08:51 +0000328func 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 Albert326ab242023-04-20 17:38:29 +0000339 // 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 Albertf1d14c72020-07-30 14:32:55 -0700347}
348
349// Feature flag to disable diffing against prebuilts.
Dan Albertad665932021-06-07 13:19:49 -0700350func canDiffAbi() bool {
Dan Albertf1d14c72020-07-30 14:32:55 -0700351 return false
352}
353
354func (this *stubDecorator) dumpAbi(ctx ModuleContext, symbolList android.Path) {
355 implementationLibrary := this.findImplementationLibrary(ctx)
Matthias Maennichc2346f12021-09-02 20:45:33 +0100356 abiRawPath := getNdkAbiDumpInstallBase(ctx).Join(ctx,
Dan Albertf1d14c72020-07-30 14:32:55 -0700357 this.apiLevel.String(), ctx.Arch().ArchType.String(),
Matthias Maennichc2346f12021-09-02 20:45:33 +0100358 this.libraryName(ctx), "abi.raw.xml")
Dan Albertf1d14c72020-07-30 14:32:55 -0700359 ctx.Build(pctx, android.BuildParams{
360 Rule: abidw,
361 Description: fmt.Sprintf("abidw %s", implementationLibrary),
Dan Albertf1d14c72020-07-30 14:32:55 -0700362 Input: implementationLibrary,
Matthias Maennichc2346f12021-09-02 20:45:33 +0100363 Output: abiRawPath,
Dan Albertf1d14c72020-07-30 14:32:55 -0700364 Implicit: symbolList,
365 Args: map[string]string{
366 "symbolList": symbolList.String(),
367 },
368 })
Dan Albert604086f2021-06-15 13:23:44 -0700369
Matthias Maennichc2346f12021-09-02 20:45:33 +0100370 this.abiDumpPath = getNdkAbiDumpInstallBase(ctx).Join(ctx,
371 this.apiLevel.String(), ctx.Arch().ArchType.String(),
Matthias Maennichca8ae652023-06-30 21:52:17 +0100372 this.libraryName(ctx), "abi.stg")
Matthias Maennichc2346f12021-09-02 20:45:33 +0100373 ctx.Build(pctx, android.BuildParams{
Matthias Maennichca8ae652023-06-30 21:52:17 +0100374 Rule: xml2stg,
375 Description: fmt.Sprintf("xml2stg %s", implementationLibrary),
Matthias Maennichc2346f12021-09-02 20:45:33 +0100376 Input: abiRawPath,
377 Output: this.abiDumpPath,
378 })
Dan Albertf1d14c72020-07-30 14:32:55 -0700379}
380
381func 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
392func (this *stubDecorator) diffAbi(ctx ModuleContext) {
Dan Albertf1d14c72020-07-30 14:32:55 -0700393 // 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 Albertf7cb5632022-11-29 17:20:16 +0000397 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 Albertf1d14c72020-07-30 14:32:55 -0700401 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 Willemsenb916b802017-03-19 13:44:32 -0700456func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800457 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700458 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
459 }
460
Colin Cross5ec407b2020-09-30 11:41:33 -0700461 if !c.buildStubs() {
462 // NDK libraries have no implementation variant, nothing to do
463 return Objects{}
464 }
465
Dan Albert1a246272020-07-06 14:49:35 -0700466 if !c.initializeProperties(ctx) {
467 // Emits its own errors, so we don't need to.
468 return Objects{}
469 }
470
sophiez58cabb72020-05-29 13:37:12 -0700471 symbolFile := String(c.properties.Symbol_file)
Dan Albertf1d14c72020-07-30 14:32:55 -0700472 nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile, c.apiLevel, "")
473 objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
474 c.versionScriptPath = nativeAbiResult.versionScript
Dan Albertf71006a2022-04-14 23:08:51 +0000475 if canDumpAbi(ctx.Config()) {
Dan Albertf1d14c72020-07-30 14:32:55 -0700476 c.dumpAbi(ctx, nativeAbiResult.symbolList)
Dan Albertad665932021-06-07 13:19:49 -0700477 if canDiffAbi() {
Dan Albertf1d14c72020-07-30 14:32:55 -0700478 c.diffAbi(ctx)
479 }
480 }
Dan Albert1a246272020-07-06 14:49:35 -0700481 if c.apiLevel.IsCurrent() && ctx.PrimaryArch() {
sophiez4c4f8032021-08-16 22:54:00 -0700482 c.parsedCoverageXmlPath = parseSymbolFileForAPICoverage(ctx, symbolFile)
sophiez58cabb72020-05-29 13:37:12 -0700483 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700484 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700485}
486
Spandan Das73bcafc2022-08-18 23:26:00 +0000487// Add a dependency on the header modules of this ndk_library
Colin Cross37047f12016-12-13 17:06:13 -0800488func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Spandan Das73bcafc2022-08-18 23:26:00 +0000489 return Deps{
490 HeaderLibs: linker.properties.Export_header_libs,
491 }
Dan Albert914449f2016-06-17 16:45:24 -0700492}
493
Dan Willemsen01a90592017-04-07 15:21:13 -0700494func (linker *stubDecorator) Name(name string) string {
495 return name + ndkLibrarySuffix
496}
497
Colin Crossb916a382016-07-29 17:28:03 -0700498func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700499 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700500 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700501}
502
Colin Crossb916a382016-07-29 17:28:03 -0700503func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700504 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700505
Colin Cross5ec407b2020-09-30 11:41:33 -0700506 if !stub.buildStubs() {
507 // NDK libraries have no implementation variant, nothing to do
508 return nil
509 }
510
Dan Albert1a246272020-07-06 14:49:35 -0700511 if shouldUseVersionScript(ctx, stub) {
Dan Albert98dbb3b2017-01-03 15:16:29 -0800512 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800513 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700514 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800515 }
516
Colin Cross5ec407b2020-09-30 11:41:33 -0700517 stub.libraryDecorator.skipAPIDefine = true
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700518 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700519}
520
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700521func (stub *stubDecorator) nativeCoverage() bool {
522 return false
523}
524
Dan Albert4048bb02023-04-03 20:19:07 +0000525// Returns the install path for unversioned NDK libraries (currently only static
526// libraries).
527func getUnversionedLibraryInstallPath(ctx ModuleContext) android.InstallPath {
528 return getNdkSysrootBase(ctx).Join(ctx, "usr/lib", config.NDKTriple(ctx.toolchain()))
529}
Rebecca Chyung961cf1c2023-04-03 05:17:17 +0000530
Dan Albert4048bb02023-04-03 20:19:07 +0000531// Returns the install path for versioned NDK libraries. These are most often
532// stubs, but the same paths are used for CRT objects.
533func getVersionedLibraryInstallPath(ctx ModuleContext, apiLevel android.ApiLevel) android.InstallPath {
534 return getUnversionedLibraryInstallPath(ctx).Join(ctx, apiLevel.String())
535}
536
537func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
538 installDir := getVersionedLibraryInstallPath(ctx, stub.apiLevel)
Colin Cross0875c522017-11-28 17:34:01 -0800539 stub.installPath = ctx.InstallFile(installDir, path.Base(), path)
Dan Albert914449f2016-06-17 16:45:24 -0700540}
541
Colin Cross36242852017-06-23 15:06:31 -0700542func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800543 module, library := NewLibrary(android.DeviceSupported)
544 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700545 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700546 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200547 library.disableStripping()
Dan Albert914449f2016-06-17 16:45:24 -0700548
Colin Crossb916a382016-07-29 17:28:03 -0700549 stub := &stubDecorator{
550 libraryDecorator: library,
551 }
552 module.compiler = stub
553 module.linker = stub
554 module.installer = stub
Colin Cross31076b32020-10-23 17:22:06 -0700555 module.library = stub
Dan Albert914449f2016-06-17 16:45:24 -0700556
Colin Crossc511bc52020-04-07 16:50:32 +0000557 module.Properties.AlwaysSdk = true
558 module.Properties.Sdk_version = StringPtr("current")
559
Colin Cross36242852017-06-23 15:06:31 -0700560 module.AddProperties(&stub.properties, &library.MutatedProperties)
561
562 return module
Dan Albert914449f2016-06-17 16:45:24 -0700563}
564
Dan Albertf740ed02020-07-24 14:19:06 -0700565// ndk_library creates a library that exposes a stub implementation of functions
566// and variables for use at build time only.
Jooyung Hanb90e4912019-12-09 18:21:48 +0900567func NdkLibraryFactory() android.Module {
Colin Cross36242852017-06-23 15:06:31 -0700568 module := newStubLibrary()
569 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
Spandan Das1278c2c2022-08-19 18:17:28 +0000570 android.InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700571 return module
Dan Albert914449f2016-06-17 16:45:24 -0700572}
Spandan Das1278c2c2022-08-19 18:17:28 +0000573
574type 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 Das4238c652022-09-09 01:38:47 +0000582func apiHeaderLabels(ctx android.TopDownMutatorContext, hdrLibs []string) bazel.LabelList {
Spandan Das1278c2c2022-08-19 18:17:28 +0000583 addSuffix := func(ctx android.BazelConversionPathContext, module blueprint.Module) string {
584 label := android.BazelModuleLabel(ctx, module)
585 return android.ApiContributionTargetName(label)
586 }
Spandan Das4238c652022-09-09 01:38:47 +0000587 return android.BazelLabelForModuleDepsWithFn(ctx, hdrLibs, addSuffix)
Spandan Das1278c2c2022-08-19 18:17:28 +0000588}