blob: 2bbfc4aeef0ecbc4fd3a12775f546dd83aca3cc5 [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"
Jingwen Chen341f7352022-01-11 05:42:49 +000028 "android/soong/cc/config"
Dan Albert914449f2016-06-17 16:45:24 -070029)
30
sophiez58cabb72020-05-29 13:37:12 -070031func init() {
Dan Albert06f58af2020-06-22 15:10:31 -070032 pctx.HostBinToolVariable("ndkStubGenerator", "ndkstubgen")
Dan Albertf1d14c72020-07-30 14:32:55 -070033 pctx.HostBinToolVariable("abidiff", "abidiff")
34 pctx.HostBinToolVariable("abitidy", "abitidy")
35 pctx.HostBinToolVariable("abidw", "abidw")
sophiez58cabb72020-05-29 13:37:12 -070036}
37
Dan Albert914449f2016-06-17 16:45:24 -070038var (
Colin Cross9d45bb72016-08-29 16:14:13 -070039 genStubSrc = pctx.AndroidStaticRule("genStubSrc",
Dan Albert914449f2016-06-17 16:45:24 -070040 blueprint.RuleParams{
Dan Albert06f58af2020-06-22 15:10:31 -070041 Command: "$ndkStubGenerator --arch $arch --api $apiLevel " +
42 "--api-map $apiMap $flags $in $out",
43 CommandDeps: []string{"$ndkStubGenerator"},
Jiyong Park3fd0baf2018-12-07 16:25:39 +090044 }, "arch", "apiLevel", "apiMap", "flags")
Dan Albert914449f2016-06-17 16:45:24 -070045
Dan Albertf1d14c72020-07-30 14:32:55 -070046 abidw = pctx.AndroidStaticRule("abidw",
47 blueprint.RuleParams{
48 Command: "$abidw --type-id-style hash --no-corpus-path " +
Matthias Maennichc2346f12021-09-02 20:45:33 +010049 "--no-show-locs --no-comp-dir-path -w $symbolList " +
50 "$in --out-file $out",
51 CommandDeps: []string{"$abidw"},
Dan Albertf1d14c72020-07-30 14:32:55 -070052 }, "symbolList")
53
Matthias Maennichc2346f12021-09-02 20:45:33 +010054 abitidy = pctx.AndroidStaticRule("abitidy",
55 blueprint.RuleParams{
Dan Albert604086f2021-06-15 13:23:44 -070056 Command: "$abitidy --all $flags -i $in -o $out",
Matthias Maennichc2346f12021-09-02 20:45:33 +010057 CommandDeps: []string{"$abitidy"},
Dan Albert604086f2021-06-15 13:23:44 -070058 }, "flags")
Matthias Maennichc2346f12021-09-02 20:45:33 +010059
Dan Albertf1d14c72020-07-30 14:32:55 -070060 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 Albert914449f2016-06-17 16:45:24 -070070 ndkLibrarySuffix = ".ndk"
Colin Cross4d9c2d12016-07-29 12:48:20 -070071
Colin Cross95f1ca02020-10-29 20:47:22 -070072 ndkKnownLibsKey = android.NewOnceKey("ndkKnownLibsKey")
Dan Albertde5aade2020-06-30 12:32:51 -070073 // protects ndkKnownLibs writes during parallel BeginMutator.
74 ndkKnownLibsLock sync.Mutex
Dan Albertf1d14c72020-07-30 14:32:55 -070075
76 stubImplementation = dependencyTag{name: "stubImplementation"}
Dan Albert914449f2016-06-17 16:45:24 -070077)
78
Dan Albert1a246272020-07-06 14:49:35 -070079// 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 Albert914449f2016-06-17 16:45:24 -070083// Creates a stub shared library based on the provided version file.
84//
Dan Albert914449f2016-06-17 16:45:24 -070085// Example:
86//
Colin Crossd079e0b2022-08-16 10:27:33 -070087// ndk_library {
88// name: "libfoo",
89// symbol_file: "libfoo.map.txt",
90// first_version: "9",
91// }
Dan Albert914449f2016-06-17 16:45:24 -070092type libraryProperties struct {
93 // Relative path to the symbol map.
94 // An example file can be seen here: TODO(danalbert): Make an example.
Inseob Kim5eb7ee92022-04-27 10:30:34 +090095 Symbol_file *string `android:"path"`
Dan Albert914449f2016-06-17 16:45:24 -070096
97 // The first API level a library was available. A library will be generated
98 // for every API level beginning with this one.
Nan Zhang0007d812017-11-07 10:57:05 -080099 First_version *string
Dan Albert914449f2016-06-17 16:45:24 -0700100
Dan Albert98dbb3b2017-01-03 15:16:29 -0800101 // The first API level that library should have the version script applied.
102 // This defaults to the value of first_version, and should almost never be
103 // used. This is only needed to work around platform bugs like
104 // https://github.com/android-ndk/ndk/issues/265.
Nan Zhang0007d812017-11-07 10:57:05 -0800105 Unversioned_until *string
Dan Albert604086f2021-06-15 13:23:44 -0700106
107 // If true, does not emit errors when APIs lacking type information are
108 // found. This is false by default and should not be enabled outside bionic,
109 // where it is enabled pending a fix for http://b/190554910 (no debug info
110 // for asm implemented symbols).
111 Allow_untyped_symbols *bool
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")
304 }
305 output := impl.UnstrippedOutputFile()
306 if output == nil {
307 ctx.ModuleErrorf("implementation module (%s) has no output", impl)
308 return nil
309 }
310
311 return output
312}
313
314func (this *stubDecorator) libraryName(ctx ModuleContext) string {
315 return strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix)
316}
317
318func (this *stubDecorator) findPrebuiltAbiDump(ctx ModuleContext,
319 apiLevel android.ApiLevel) android.OptionalPath {
320
321 subpath := filepath.Join("prebuilts/abi-dumps/ndk", apiLevel.String(),
322 ctx.Arch().ArchType.String(), this.libraryName(ctx), "abi.xml")
323 return android.ExistentPathForSource(ctx, subpath)
324}
325
326// Feature flag.
Dan Albertf71006a2022-04-14 23:08:51 +0000327func canDumpAbi(config android.Config) bool {
328 if runtime.GOOS == "darwin" {
329 return false
330 }
331 // abidw doesn't currently handle top-byte-ignore correctly. Disable ABI
332 // dumping for those configs while we wait for a fix. We'll still have ABI
333 // checking coverage from non-hwasan builds.
334 // http://b/190554910
335 if android.InList("hwaddress", config.SanitizeDevice()) {
336 return false
337 }
338 return true
Dan Albertf1d14c72020-07-30 14:32:55 -0700339}
340
341// Feature flag to disable diffing against prebuilts.
Dan Albertad665932021-06-07 13:19:49 -0700342func canDiffAbi() bool {
Dan Albertf1d14c72020-07-30 14:32:55 -0700343 return false
344}
345
346func (this *stubDecorator) dumpAbi(ctx ModuleContext, symbolList android.Path) {
347 implementationLibrary := this.findImplementationLibrary(ctx)
Matthias Maennichc2346f12021-09-02 20:45:33 +0100348 abiRawPath := getNdkAbiDumpInstallBase(ctx).Join(ctx,
Dan Albertf1d14c72020-07-30 14:32:55 -0700349 this.apiLevel.String(), ctx.Arch().ArchType.String(),
Matthias Maennichc2346f12021-09-02 20:45:33 +0100350 this.libraryName(ctx), "abi.raw.xml")
Dan Albertf1d14c72020-07-30 14:32:55 -0700351 ctx.Build(pctx, android.BuildParams{
352 Rule: abidw,
353 Description: fmt.Sprintf("abidw %s", implementationLibrary),
Dan Albertf1d14c72020-07-30 14:32:55 -0700354 Input: implementationLibrary,
Matthias Maennichc2346f12021-09-02 20:45:33 +0100355 Output: abiRawPath,
Dan Albertf1d14c72020-07-30 14:32:55 -0700356 Implicit: symbolList,
357 Args: map[string]string{
358 "symbolList": symbolList.String(),
359 },
360 })
Dan Albert604086f2021-06-15 13:23:44 -0700361
Matthias Maennichc2346f12021-09-02 20:45:33 +0100362 this.abiDumpPath = getNdkAbiDumpInstallBase(ctx).Join(ctx,
363 this.apiLevel.String(), ctx.Arch().ArchType.String(),
364 this.libraryName(ctx), "abi.xml")
Dan Albert604086f2021-06-15 13:23:44 -0700365 untypedFlag := "--abort-on-untyped-symbols"
366 if proptools.BoolDefault(this.properties.Allow_untyped_symbols, false) {
367 untypedFlag = ""
368 }
Matthias Maennichc2346f12021-09-02 20:45:33 +0100369 ctx.Build(pctx, android.BuildParams{
370 Rule: abitidy,
371 Description: fmt.Sprintf("abitidy %s", implementationLibrary),
372 Input: abiRawPath,
373 Output: this.abiDumpPath,
Dan Albert604086f2021-06-15 13:23:44 -0700374 Args: map[string]string{
375 "flags": untypedFlag,
376 },
Matthias Maennichc2346f12021-09-02 20:45:33 +0100377 })
Dan Albertf1d14c72020-07-30 14:32:55 -0700378}
379
380func findNextApiLevel(ctx ModuleContext, apiLevel android.ApiLevel) *android.ApiLevel {
381 apiLevels := append(ctx.Config().AllSupportedApiLevels(),
382 android.FutureApiLevel)
383 for _, api := range apiLevels {
384 if api.GreaterThan(apiLevel) {
385 return &api
386 }
387 }
388 return nil
389}
390
391func (this *stubDecorator) diffAbi(ctx ModuleContext) {
392 missingPrebuiltError := fmt.Sprintf(
393 "Did not find prebuilt ABI dump for %q. Generate with "+
394 "//development/tools/ndk/update_ndk_abi.sh.", this.libraryName(ctx))
395
396 // Catch any ABI changes compared to the checked-in definition of this API
397 // level.
398 abiDiffPath := android.PathForModuleOut(ctx, "abidiff.timestamp")
399 prebuiltAbiDump := this.findPrebuiltAbiDump(ctx, this.apiLevel)
400 if !prebuiltAbiDump.Valid() {
401 ctx.Build(pctx, android.BuildParams{
402 Rule: android.ErrorRule,
403 Output: abiDiffPath,
404 Args: map[string]string{
405 "error": missingPrebuiltError,
406 },
407 })
408 } else {
409 ctx.Build(pctx, android.BuildParams{
410 Rule: abidiff,
411 Description: fmt.Sprintf("abidiff %s %s", prebuiltAbiDump,
412 this.abiDumpPath),
413 Output: abiDiffPath,
414 Inputs: android.Paths{prebuiltAbiDump.Path(), this.abiDumpPath},
415 })
416 }
417 this.abiDiffPaths = append(this.abiDiffPaths, abiDiffPath)
418
419 // Also ensure that the ABI of the next API level (if there is one) matches
420 // this API level. *New* ABI is allowed, but any changes to APIs that exist
421 // in this API level are disallowed.
422 if !this.apiLevel.IsCurrent() {
423 nextApiLevel := findNextApiLevel(ctx, this.apiLevel)
424 if nextApiLevel == nil {
425 panic(fmt.Errorf("could not determine which API level follows "+
426 "non-current API level %s", this.apiLevel))
427 }
428 nextAbiDiffPath := android.PathForModuleOut(ctx,
429 "abidiff_next.timestamp")
430 nextAbiDump := this.findPrebuiltAbiDump(ctx, *nextApiLevel)
431 if !nextAbiDump.Valid() {
432 ctx.Build(pctx, android.BuildParams{
433 Rule: android.ErrorRule,
434 Output: nextAbiDiffPath,
435 Args: map[string]string{
436 "error": missingPrebuiltError,
437 },
438 })
439 } else {
440 ctx.Build(pctx, android.BuildParams{
441 Rule: abidiff,
442 Description: fmt.Sprintf("abidiff %s %s", this.abiDumpPath,
443 nextAbiDump),
444 Output: nextAbiDiffPath,
445 Inputs: android.Paths{this.abiDumpPath, nextAbiDump.Path()},
446 Args: map[string]string{
447 "args": "--no-added-syms",
448 },
449 })
450 }
451 this.abiDiffPaths = append(this.abiDiffPaths, nextAbiDiffPath)
452 }
453}
454
Dan Willemsenb916b802017-03-19 13:44:32 -0700455func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800456 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700457 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
458 }
459
Colin Cross5ec407b2020-09-30 11:41:33 -0700460 if !c.buildStubs() {
461 // NDK libraries have no implementation variant, nothing to do
462 return Objects{}
463 }
464
Dan Albert1a246272020-07-06 14:49:35 -0700465 if !c.initializeProperties(ctx) {
466 // Emits its own errors, so we don't need to.
467 return Objects{}
468 }
469
sophiez58cabb72020-05-29 13:37:12 -0700470 symbolFile := String(c.properties.Symbol_file)
Dan Albertf1d14c72020-07-30 14:32:55 -0700471 nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile, c.apiLevel, "")
472 objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
473 c.versionScriptPath = nativeAbiResult.versionScript
Dan Albertf71006a2022-04-14 23:08:51 +0000474 if canDumpAbi(ctx.Config()) {
Dan Albertf1d14c72020-07-30 14:32:55 -0700475 c.dumpAbi(ctx, nativeAbiResult.symbolList)
Dan Albertad665932021-06-07 13:19:49 -0700476 if canDiffAbi() {
Dan Albertf1d14c72020-07-30 14:32:55 -0700477 c.diffAbi(ctx)
478 }
479 }
Dan Albert1a246272020-07-06 14:49:35 -0700480 if c.apiLevel.IsCurrent() && ctx.PrimaryArch() {
sophiez4c4f8032021-08-16 22:54:00 -0700481 c.parsedCoverageXmlPath = parseSymbolFileForAPICoverage(ctx, symbolFile)
sophiez58cabb72020-05-29 13:37:12 -0700482 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700483 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700484}
485
Colin Cross37047f12016-12-13 17:06:13 -0800486func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700487 return Deps{}
488}
489
Dan Willemsen01a90592017-04-07 15:21:13 -0700490func (linker *stubDecorator) Name(name string) string {
491 return name + ndkLibrarySuffix
492}
493
Colin Crossb916a382016-07-29 17:28:03 -0700494func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700495 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700496 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700497}
498
Colin Crossb916a382016-07-29 17:28:03 -0700499func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700500 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700501
Colin Cross5ec407b2020-09-30 11:41:33 -0700502 if !stub.buildStubs() {
503 // NDK libraries have no implementation variant, nothing to do
504 return nil
505 }
506
Dan Albert1a246272020-07-06 14:49:35 -0700507 if shouldUseVersionScript(ctx, stub) {
Dan Albert98dbb3b2017-01-03 15:16:29 -0800508 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800509 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700510 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800511 }
512
Colin Cross5ec407b2020-09-30 11:41:33 -0700513 stub.libraryDecorator.skipAPIDefine = true
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700514 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700515}
516
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700517func (stub *stubDecorator) nativeCoverage() bool {
518 return false
519}
520
Colin Crossb916a382016-07-29 17:28:03 -0700521func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700522 arch := ctx.Target().Arch.ArchType.Name
Dan Albert914449f2016-06-17 16:45:24 -0700523 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
524 // architectures it's just installed to lib.
525 libDir := "lib"
526 if ctx.toolchain().Is64Bit() && arch != "arm64" {
527 libDir = "lib64"
528 }
529
530 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albert1a246272020-07-06 14:49:35 -0700531 "platforms/android-%s/arch-%s/usr/%s", stub.apiLevel, arch, libDir))
Colin Cross0875c522017-11-28 17:34:01 -0800532 stub.installPath = ctx.InstallFile(installDir, path.Base(), path)
Dan Albert914449f2016-06-17 16:45:24 -0700533}
534
Colin Cross36242852017-06-23 15:06:31 -0700535func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800536 module, library := NewLibrary(android.DeviceSupported)
537 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700538 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700539 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200540 library.disableStripping()
Dan Albert914449f2016-06-17 16:45:24 -0700541
Colin Crossb916a382016-07-29 17:28:03 -0700542 stub := &stubDecorator{
543 libraryDecorator: library,
544 }
545 module.compiler = stub
546 module.linker = stub
547 module.installer = stub
Colin Cross31076b32020-10-23 17:22:06 -0700548 module.library = stub
Dan Albert914449f2016-06-17 16:45:24 -0700549
Colin Crossc511bc52020-04-07 16:50:32 +0000550 module.Properties.AlwaysSdk = true
551 module.Properties.Sdk_version = StringPtr("current")
552
Colin Cross36242852017-06-23 15:06:31 -0700553 module.AddProperties(&stub.properties, &library.MutatedProperties)
554
555 return module
Dan Albert914449f2016-06-17 16:45:24 -0700556}
557
Dan Albertf740ed02020-07-24 14:19:06 -0700558// ndk_library creates a library that exposes a stub implementation of functions
559// and variables for use at build time only.
Jooyung Hanb90e4912019-12-09 18:21:48 +0900560func NdkLibraryFactory() android.Module {
Colin Cross36242852017-06-23 15:06:31 -0700561 module := newStubLibrary()
562 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
563 return module
Dan Albert914449f2016-06-17 16:45:24 -0700564}