blob: 2473ba2a3841c0f07b55160f2d3aed9fab1a1fc1 [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")
35 pctx.HostBinToolVariable("abitidy", "abitidy")
36 pctx.HostBinToolVariable("abidw", "abidw")
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 Maennichc2346f12021-09-02 20:45:33 +010055 abitidy = pctx.AndroidStaticRule("abitidy",
56 blueprint.RuleParams{
Dan Albert604086f2021-06-15 13:23:44 -070057 Command: "$abitidy --all $flags -i $in -o $out",
Matthias Maennichc2346f12021-09-02 20:45:33 +010058 CommandDeps: []string{"$abitidy"},
Dan Albert604086f2021-06-15 13:23:44 -070059 }, "flags")
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
110 // If true, does not emit errors when APIs lacking type information are
111 // found. This is false by default and should not be enabled outside bionic,
112 // where it is enabled pending a fix for http://b/190554910 (no debug info
113 // for asm implemented symbols).
114 Allow_untyped_symbols *bool
Spandan Das73bcafc2022-08-18 23:26:00 +0000115
116 // Headers presented by this library to the Public API Surface
117 Export_header_libs []string
Dan Albert914449f2016-06-17 16:45:24 -0700118}
119
Colin Crossb916a382016-07-29 17:28:03 -0700120type stubDecorator struct {
121 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -0700122
123 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -0700124
sophiez58cabb72020-05-29 13:37:12 -0700125 versionScriptPath android.ModuleGenPath
126 parsedCoverageXmlPath android.ModuleOutPath
127 installPath android.Path
Dan Albertf1d14c72020-07-30 14:32:55 -0700128 abiDumpPath android.OutputPath
129 abiDiffPaths android.Paths
Dan Albert1a246272020-07-06 14:49:35 -0700130
131 apiLevel android.ApiLevel
132 firstVersion android.ApiLevel
133 unversionedUntil android.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700134}
135
Colin Cross0477b422020-10-13 18:43:54 -0700136var _ versionedInterface = (*stubDecorator)(nil)
137
Dan Albert1a246272020-07-06 14:49:35 -0700138func shouldUseVersionScript(ctx BaseModuleContext, stub *stubDecorator) bool {
139 return stub.apiLevel.GreaterThanOrEqualTo(stub.unversionedUntil)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800140}
141
Colin Cross0477b422020-10-13 18:43:54 -0700142func (stub *stubDecorator) implementationModuleName(name string) string {
143 return strings.TrimSuffix(name, ndkLibrarySuffix)
144}
145
Colin Cross3572cf72020-10-01 15:58:11 -0700146func ndkLibraryVersions(ctx android.BaseMutatorContext, from android.ApiLevel) []string {
Dan Albert1a246272020-07-06 14:49:35 -0700147 var versions []android.ApiLevel
148 versionStrs := []string{}
149 for _, version := range ctx.Config().AllSupportedApiLevels() {
150 if version.GreaterThanOrEqualTo(from) {
151 versions = append(versions, version)
152 versionStrs = append(versionStrs, version.String())
153 }
Dan Albert914449f2016-06-17 16:45:24 -0700154 }
Dan Albert0b176c82020-07-23 16:43:25 -0700155 versionStrs = append(versionStrs, android.FutureApiLevel.String())
Dan Albert914449f2016-06-17 16:45:24 -0700156
Colin Cross5ec407b2020-09-30 11:41:33 -0700157 return versionStrs
158}
159
Colin Cross3572cf72020-10-01 15:58:11 -0700160func (this *stubDecorator) stubsVersions(ctx android.BaseMutatorContext) []string {
161 if !ctx.Module().Enabled() {
162 return nil
163 }
Dan Albertf1d14c72020-07-30 14:32:55 -0700164 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
165 ctx.Module().Disable()
166 return nil
167 }
Colin Cross3572cf72020-10-01 15:58:11 -0700168 firstVersion, err := nativeApiLevelFromUser(ctx,
169 String(this.properties.First_version))
170 if err != nil {
171 ctx.PropertyErrorf("first_version", err.Error())
172 return nil
173 }
174 return ndkLibraryVersions(ctx, firstVersion)
175}
176
Dan Albert1a246272020-07-06 14:49:35 -0700177func (this *stubDecorator) initializeProperties(ctx BaseModuleContext) bool {
Colin Cross5ec407b2020-09-30 11:41:33 -0700178 this.apiLevel = nativeApiLevelOrPanic(ctx, this.stubsVersion())
Dan Albert1a246272020-07-06 14:49:35 -0700179
180 var err error
181 this.firstVersion, err = nativeApiLevelFromUser(ctx,
182 String(this.properties.First_version))
183 if err != nil {
184 ctx.PropertyErrorf("first_version", err.Error())
185 return false
186 }
187
Jiyong Parkee9b1172021-04-06 17:40:32 +0900188 str := proptools.StringDefault(this.properties.Unversioned_until, "minimum")
189 this.unversionedUntil, err = nativeApiLevelFromUser(ctx, str)
Dan Albert1a246272020-07-06 14:49:35 -0700190 if err != nil {
191 ctx.PropertyErrorf("unversioned_until", err.Error())
192 return false
193 }
194
195 return true
196}
197
Colin Cross95f1ca02020-10-29 20:47:22 -0700198func getNDKKnownLibs(config android.Config) *[]string {
199 return config.Once(ndkKnownLibsKey, func() interface{} {
200 return &[]string{}
201 }).(*[]string)
202}
203
Colin Crossb916a382016-07-29 17:28:03 -0700204func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700205 c.baseCompiler.compilerInit(ctx)
206
Dan Willemsen01a90592017-04-07 15:21:13 -0700207 name := ctx.baseModuleName()
208 if strings.HasSuffix(name, ndkLibrarySuffix) {
209 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
210 }
211
Dan Albertde5aade2020-06-30 12:32:51 -0700212 ndkKnownLibsLock.Lock()
213 defer ndkKnownLibsLock.Unlock()
Colin Cross95f1ca02020-10-29 20:47:22 -0700214 ndkKnownLibs := getNDKKnownLibs(ctx.Config())
215 for _, lib := range *ndkKnownLibs {
Dan Albert7e9d2952016-08-04 13:02:36 -0700216 if lib == name {
217 return
218 }
219 }
Colin Cross95f1ca02020-10-29 20:47:22 -0700220 *ndkKnownLibs = append(*ndkKnownLibs, name)
Dan Albert7e9d2952016-08-04 13:02:36 -0700221}
222
Jingwen Chen341f7352022-01-11 05:42:49 +0000223var stubLibraryCompilerFlags = []string{
224 // We're knowingly doing some otherwise unsightly things with builtin
225 // functions here. We're just generating stub libraries, so ignore it.
226 "-Wno-incompatible-library-redeclaration",
227 "-Wno-incomplete-setjmp-declaration",
228 "-Wno-builtin-requires-header",
229 "-Wno-invalid-noreturn",
230 "-Wall",
231 "-Werror",
232 // These libraries aren't actually used. Don't worry about unwinding
233 // (avoids the need to link an unwinder into a fake library).
234 "-fno-unwind-tables",
235}
236
237func init() {
238 config.ExportStringList("StubLibraryCompilerFlags", stubLibraryCompilerFlags)
239}
240
George Burgess IVf5310e32017-07-19 11:39:53 -0700241func addStubLibraryCompilerFlags(flags Flags) Flags {
Jingwen Chen341f7352022-01-11 05:42:49 +0000242 flags.Global.CFlags = append(flags.Global.CFlags, stubLibraryCompilerFlags...)
Jiyong Park48d75ef2019-11-21 15:11:49 +0900243 // All symbols in the stubs library should be visible.
244 if inList("-fvisibility=hidden", flags.Local.CFlags) {
245 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
246 }
George Burgess IVf5310e32017-07-19 11:39:53 -0700247 return flags
248}
249
Colin Crossf18e1102017-11-16 14:33:08 -0800250func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
251 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700252 return addStubLibraryCompilerFlags(flags)
253}
254
Dan Albertf1d14c72020-07-30 14:32:55 -0700255type ndkApiOutputs struct {
256 stubSrc android.ModuleGenPath
257 versionScript android.ModuleGenPath
258 symbolList android.ModuleGenPath
259}
260
261func parseNativeAbiDefinition(ctx ModuleContext, symbolFile string,
262 apiLevel android.ApiLevel, genstubFlags string) ndkApiOutputs {
Dan Albert914449f2016-06-17 16:45:24 -0700263
Dan Willemsenb916b802017-03-19 13:44:32 -0700264 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
265 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
266 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albertf1d14c72020-07-30 14:32:55 -0700267 symbolListPath := android.PathForModuleGen(ctx, "abi_symbol_list.txt")
Dan Albert49927d22017-03-28 15:00:46 -0700268 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700269 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700270 Rule: genStubSrc,
271 Description: "generate stubs " + symbolFilePath.Rel(),
Dan Albertf1d14c72020-07-30 14:32:55 -0700272 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath,
273 symbolListPath},
274 Input: symbolFilePath,
275 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700276 Args: map[string]string{
Dan Albertf1d14c72020-07-30 14:32:55 -0700277 "arch": ctx.Arch().ArchType.String(),
278 "apiLevel": apiLevel.String(),
Dan Albert49927d22017-03-28 15:00:46 -0700279 "apiMap": apiLevelsJson.String(),
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900280 "flags": genstubFlags,
Dan Albert914449f2016-06-17 16:45:24 -0700281 },
282 })
283
Dan Albertf1d14c72020-07-30 14:32:55 -0700284 return ndkApiOutputs{
285 stubSrc: stubSrcPath,
286 versionScript: versionScriptPath,
287 symbolList: symbolListPath,
288 }
289}
290
291func compileStubLibrary(ctx ModuleContext, flags Flags, src android.Path) Objects {
Mitch Phillips4e5f9a12022-04-29 13:12:28 -0700292 // libc/libm stubs libraries end up mismatching with clang's internal definition of these
293 // functions (which have noreturn attributes and other things). Because we just want to create a
294 // stub with symbol definitions, and types aren't important in C, ignore the mismatch.
295 flags.Local.ConlyFlags = append(flags.Local.ConlyFlags, "-fno-builtin")
Dan Albertf1d14c72020-07-30 14:32:55 -0700296 return compileObjs(ctx, flagsToBuilderFlags(flags), "",
Chih-Hung Hsieh9db8a0c2022-02-17 12:54:45 -0800297 android.Paths{src}, nil, nil, nil, nil)
Dan Willemsenb916b802017-03-19 13:44:32 -0700298}
299
Dan Albertf1d14c72020-07-30 14:32:55 -0700300func (this *stubDecorator) findImplementationLibrary(ctx ModuleContext) android.Path {
301 dep := ctx.GetDirectDepWithTag(strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix),
302 stubImplementation)
303 if dep == nil {
304 ctx.ModuleErrorf("Could not find implementation for stub")
305 return nil
306 }
307 impl, ok := dep.(*Module)
308 if !ok {
309 ctx.ModuleErrorf("Implementation for stub is not correct module type")
Alan Stokes73d32452022-11-01 14:05:08 +0000310 return nil
Dan Albertf1d14c72020-07-30 14:32:55 -0700311 }
312 output := impl.UnstrippedOutputFile()
313 if output == nil {
314 ctx.ModuleErrorf("implementation module (%s) has no output", impl)
315 return nil
316 }
317
318 return output
319}
320
321func (this *stubDecorator) libraryName(ctx ModuleContext) string {
322 return strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix)
323}
324
325func (this *stubDecorator) findPrebuiltAbiDump(ctx ModuleContext,
326 apiLevel android.ApiLevel) android.OptionalPath {
327
328 subpath := filepath.Join("prebuilts/abi-dumps/ndk", apiLevel.String(),
329 ctx.Arch().ArchType.String(), this.libraryName(ctx), "abi.xml")
330 return android.ExistentPathForSource(ctx, subpath)
331}
332
333// Feature flag.
Dan Albertf71006a2022-04-14 23:08:51 +0000334func canDumpAbi(config android.Config) bool {
335 if runtime.GOOS == "darwin" {
336 return false
337 }
338 // abidw doesn't currently handle top-byte-ignore correctly. Disable ABI
339 // dumping for those configs while we wait for a fix. We'll still have ABI
340 // checking coverage from non-hwasan builds.
341 // http://b/190554910
342 if android.InList("hwaddress", config.SanitizeDevice()) {
343 return false
344 }
345 return true
Dan Albertf1d14c72020-07-30 14:32:55 -0700346}
347
348// Feature flag to disable diffing against prebuilts.
Dan Albertad665932021-06-07 13:19:49 -0700349func canDiffAbi() bool {
Dan Albertf1d14c72020-07-30 14:32:55 -0700350 return false
351}
352
353func (this *stubDecorator) dumpAbi(ctx ModuleContext, symbolList android.Path) {
354 implementationLibrary := this.findImplementationLibrary(ctx)
Matthias Maennichc2346f12021-09-02 20:45:33 +0100355 abiRawPath := getNdkAbiDumpInstallBase(ctx).Join(ctx,
Dan Albertf1d14c72020-07-30 14:32:55 -0700356 this.apiLevel.String(), ctx.Arch().ArchType.String(),
Matthias Maennichc2346f12021-09-02 20:45:33 +0100357 this.libraryName(ctx), "abi.raw.xml")
Dan Albertf1d14c72020-07-30 14:32:55 -0700358 ctx.Build(pctx, android.BuildParams{
359 Rule: abidw,
360 Description: fmt.Sprintf("abidw %s", implementationLibrary),
Dan Albertf1d14c72020-07-30 14:32:55 -0700361 Input: implementationLibrary,
Matthias Maennichc2346f12021-09-02 20:45:33 +0100362 Output: abiRawPath,
Dan Albertf1d14c72020-07-30 14:32:55 -0700363 Implicit: symbolList,
364 Args: map[string]string{
365 "symbolList": symbolList.String(),
366 },
367 })
Dan Albert604086f2021-06-15 13:23:44 -0700368
Matthias Maennichc2346f12021-09-02 20:45:33 +0100369 this.abiDumpPath = getNdkAbiDumpInstallBase(ctx).Join(ctx,
370 this.apiLevel.String(), ctx.Arch().ArchType.String(),
371 this.libraryName(ctx), "abi.xml")
Dan Albert604086f2021-06-15 13:23:44 -0700372 untypedFlag := "--abort-on-untyped-symbols"
373 if proptools.BoolDefault(this.properties.Allow_untyped_symbols, false) {
374 untypedFlag = ""
375 }
Matthias Maennichc2346f12021-09-02 20:45:33 +0100376 ctx.Build(pctx, android.BuildParams{
377 Rule: abitidy,
378 Description: fmt.Sprintf("abitidy %s", implementationLibrary),
379 Input: abiRawPath,
380 Output: this.abiDumpPath,
Dan Albert604086f2021-06-15 13:23:44 -0700381 Args: map[string]string{
382 "flags": untypedFlag,
383 },
Matthias Maennichc2346f12021-09-02 20:45:33 +0100384 })
Dan Albertf1d14c72020-07-30 14:32:55 -0700385}
386
387func findNextApiLevel(ctx ModuleContext, apiLevel android.ApiLevel) *android.ApiLevel {
388 apiLevels := append(ctx.Config().AllSupportedApiLevels(),
389 android.FutureApiLevel)
390 for _, api := range apiLevels {
391 if api.GreaterThan(apiLevel) {
392 return &api
393 }
394 }
395 return nil
396}
397
398func (this *stubDecorator) diffAbi(ctx ModuleContext) {
Dan Albertf1d14c72020-07-30 14:32:55 -0700399 // Catch any ABI changes compared to the checked-in definition of this API
400 // level.
401 abiDiffPath := android.PathForModuleOut(ctx, "abidiff.timestamp")
402 prebuiltAbiDump := this.findPrebuiltAbiDump(ctx, this.apiLevel)
Dan Albertf7cb5632022-11-29 17:20:16 +0000403 missingPrebuiltError := fmt.Sprintf(
404 "Did not find prebuilt ABI dump for %q (%q). Generate with "+
405 "//development/tools/ndk/update_ndk_abi.sh.", this.libraryName(ctx),
406 prebuiltAbiDump.InvalidReason())
Dan Albertf1d14c72020-07-30 14:32:55 -0700407 if !prebuiltAbiDump.Valid() {
408 ctx.Build(pctx, android.BuildParams{
409 Rule: android.ErrorRule,
410 Output: abiDiffPath,
411 Args: map[string]string{
412 "error": missingPrebuiltError,
413 },
414 })
415 } else {
416 ctx.Build(pctx, android.BuildParams{
417 Rule: abidiff,
418 Description: fmt.Sprintf("abidiff %s %s", prebuiltAbiDump,
419 this.abiDumpPath),
420 Output: abiDiffPath,
421 Inputs: android.Paths{prebuiltAbiDump.Path(), this.abiDumpPath},
422 })
423 }
424 this.abiDiffPaths = append(this.abiDiffPaths, abiDiffPath)
425
426 // Also ensure that the ABI of the next API level (if there is one) matches
427 // this API level. *New* ABI is allowed, but any changes to APIs that exist
428 // in this API level are disallowed.
429 if !this.apiLevel.IsCurrent() {
430 nextApiLevel := findNextApiLevel(ctx, this.apiLevel)
431 if nextApiLevel == nil {
432 panic(fmt.Errorf("could not determine which API level follows "+
433 "non-current API level %s", this.apiLevel))
434 }
435 nextAbiDiffPath := android.PathForModuleOut(ctx,
436 "abidiff_next.timestamp")
437 nextAbiDump := this.findPrebuiltAbiDump(ctx, *nextApiLevel)
438 if !nextAbiDump.Valid() {
439 ctx.Build(pctx, android.BuildParams{
440 Rule: android.ErrorRule,
441 Output: nextAbiDiffPath,
442 Args: map[string]string{
443 "error": missingPrebuiltError,
444 },
445 })
446 } else {
447 ctx.Build(pctx, android.BuildParams{
448 Rule: abidiff,
449 Description: fmt.Sprintf("abidiff %s %s", this.abiDumpPath,
450 nextAbiDump),
451 Output: nextAbiDiffPath,
452 Inputs: android.Paths{this.abiDumpPath, nextAbiDump.Path()},
453 Args: map[string]string{
454 "args": "--no-added-syms",
455 },
456 })
457 }
458 this.abiDiffPaths = append(this.abiDiffPaths, nextAbiDiffPath)
459 }
460}
461
Dan Willemsenb916b802017-03-19 13:44:32 -0700462func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800463 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700464 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
465 }
466
Colin Cross5ec407b2020-09-30 11:41:33 -0700467 if !c.buildStubs() {
468 // NDK libraries have no implementation variant, nothing to do
469 return Objects{}
470 }
471
Dan Albert1a246272020-07-06 14:49:35 -0700472 if !c.initializeProperties(ctx) {
473 // Emits its own errors, so we don't need to.
474 return Objects{}
475 }
476
sophiez58cabb72020-05-29 13:37:12 -0700477 symbolFile := String(c.properties.Symbol_file)
Dan Albertf1d14c72020-07-30 14:32:55 -0700478 nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile, c.apiLevel, "")
479 objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
480 c.versionScriptPath = nativeAbiResult.versionScript
Dan Albertf71006a2022-04-14 23:08:51 +0000481 if canDumpAbi(ctx.Config()) {
Dan Albertf1d14c72020-07-30 14:32:55 -0700482 c.dumpAbi(ctx, nativeAbiResult.symbolList)
Dan Albertad665932021-06-07 13:19:49 -0700483 if canDiffAbi() {
Dan Albertf1d14c72020-07-30 14:32:55 -0700484 c.diffAbi(ctx)
485 }
486 }
Dan Albert1a246272020-07-06 14:49:35 -0700487 if c.apiLevel.IsCurrent() && ctx.PrimaryArch() {
sophiez4c4f8032021-08-16 22:54:00 -0700488 c.parsedCoverageXmlPath = parseSymbolFileForAPICoverage(ctx, symbolFile)
sophiez58cabb72020-05-29 13:37:12 -0700489 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700490 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700491}
492
Spandan Das73bcafc2022-08-18 23:26:00 +0000493// Add a dependency on the header modules of this ndk_library
Colin Cross37047f12016-12-13 17:06:13 -0800494func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Spandan Das73bcafc2022-08-18 23:26:00 +0000495 return Deps{
496 HeaderLibs: linker.properties.Export_header_libs,
497 }
Dan Albert914449f2016-06-17 16:45:24 -0700498}
499
Dan Willemsen01a90592017-04-07 15:21:13 -0700500func (linker *stubDecorator) Name(name string) string {
501 return name + ndkLibrarySuffix
502}
503
Colin Crossb916a382016-07-29 17:28:03 -0700504func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700505 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700506 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700507}
508
Colin Crossb916a382016-07-29 17:28:03 -0700509func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700510 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700511
Colin Cross5ec407b2020-09-30 11:41:33 -0700512 if !stub.buildStubs() {
513 // NDK libraries have no implementation variant, nothing to do
514 return nil
515 }
516
Dan Albert1a246272020-07-06 14:49:35 -0700517 if shouldUseVersionScript(ctx, stub) {
Dan Albert98dbb3b2017-01-03 15:16:29 -0800518 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800519 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700520 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800521 }
522
Colin Cross5ec407b2020-09-30 11:41:33 -0700523 stub.libraryDecorator.skipAPIDefine = true
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700524 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700525}
526
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700527func (stub *stubDecorator) nativeCoverage() bool {
528 return false
529}
530
Colin Crossb916a382016-07-29 17:28:03 -0700531func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700532 arch := ctx.Target().Arch.ArchType.Name
Dan Albert914449f2016-06-17 16:45:24 -0700533 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
534 // architectures it's just installed to lib.
535 libDir := "lib"
536 if ctx.toolchain().Is64Bit() && arch != "arm64" {
537 libDir = "lib64"
538 }
539
540 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albert1a246272020-07-06 14:49:35 -0700541 "platforms/android-%s/arch-%s/usr/%s", stub.apiLevel, arch, libDir))
Colin Cross0875c522017-11-28 17:34:01 -0800542 stub.installPath = ctx.InstallFile(installDir, path.Base(), path)
Dan Albert914449f2016-06-17 16:45:24 -0700543}
544
Colin Cross36242852017-06-23 15:06:31 -0700545func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800546 module, library := NewLibrary(android.DeviceSupported)
547 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700548 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700549 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200550 library.disableStripping()
Dan Albert914449f2016-06-17 16:45:24 -0700551
Colin Crossb916a382016-07-29 17:28:03 -0700552 stub := &stubDecorator{
553 libraryDecorator: library,
554 }
555 module.compiler = stub
556 module.linker = stub
557 module.installer = stub
Colin Cross31076b32020-10-23 17:22:06 -0700558 module.library = stub
Dan Albert914449f2016-06-17 16:45:24 -0700559
Colin Crossc511bc52020-04-07 16:50:32 +0000560 module.Properties.AlwaysSdk = true
561 module.Properties.Sdk_version = StringPtr("current")
562
Colin Cross36242852017-06-23 15:06:31 -0700563 module.AddProperties(&stub.properties, &library.MutatedProperties)
564
565 return module
Dan Albert914449f2016-06-17 16:45:24 -0700566}
567
Dan Albertf740ed02020-07-24 14:19:06 -0700568// ndk_library creates a library that exposes a stub implementation of functions
569// and variables for use at build time only.
Jooyung Hanb90e4912019-12-09 18:21:48 +0900570func NdkLibraryFactory() android.Module {
Colin Cross36242852017-06-23 15:06:31 -0700571 module := newStubLibrary()
572 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
Spandan Das1278c2c2022-08-19 18:17:28 +0000573 android.InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700574 return module
Dan Albert914449f2016-06-17 16:45:24 -0700575}
Spandan Das1278c2c2022-08-19 18:17:28 +0000576
577type bazelCcApiContributionAttributes struct {
578 Api bazel.LabelAttribute
579 Api_surfaces bazel.StringListAttribute
580 Hdrs bazel.LabelListAttribute
581 Library_name string
582}
583
584// Names of the cc_api_header targets in the bp2build workspace
Spandan Das4238c652022-09-09 01:38:47 +0000585func apiHeaderLabels(ctx android.TopDownMutatorContext, hdrLibs []string) bazel.LabelList {
Spandan Das1278c2c2022-08-19 18:17:28 +0000586 addSuffix := func(ctx android.BazelConversionPathContext, module blueprint.Module) string {
587 label := android.BazelModuleLabel(ctx, module)
588 return android.ApiContributionTargetName(label)
589 }
Spandan Das4238c652022-09-09 01:38:47 +0000590 return android.BazelLabelForModuleDepsWithFn(ctx, hdrLibs, addSuffix)
Spandan Das1278c2c2022-08-19 18:17:28 +0000591}
592
593func ndkLibraryBp2build(ctx android.TopDownMutatorContext, m *Module) {
594 props := bazel.BazelTargetModuleProperties{
595 Rule_class: "cc_api_contribution",
596 Bzl_load_location: "//build/bazel/rules/apis:cc_api_contribution.bzl",
597 }
598 stubLibrary := m.compiler.(*stubDecorator)
599 attrs := &bazelCcApiContributionAttributes{
600 Library_name: stubLibrary.implementationModuleName(m.Name()),
601 Api_surfaces: bazel.MakeStringListAttribute(
602 []string{android.PublicApi.String()}),
603 }
604 if symbolFile := stubLibrary.properties.Symbol_file; symbolFile != nil {
605 apiLabel := android.BazelLabelForModuleSrcSingle(ctx, proptools.String(symbolFile)).Label
606 attrs.Api = *bazel.MakeLabelAttribute(apiLabel)
607 }
Spandan Das4238c652022-09-09 01:38:47 +0000608 apiHeaders := apiHeaderLabels(ctx, stubLibrary.properties.Export_header_libs)
Spandan Das1278c2c2022-08-19 18:17:28 +0000609 attrs.Hdrs = bazel.MakeLabelListAttribute(apiHeaders)
610 apiContributionTargetName := android.ApiContributionTargetName(ctx.ModuleName())
611 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: apiContributionTargetName}, attrs)
612}