blob: 59cea769605a99c3f3dc5cfaf96ba1d190781aa3 [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//
87// ndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070088// name: "libfoo",
Dan Albert914449f2016-06-17 16:45:24 -070089// symbol_file: "libfoo.map.txt",
90// first_version: "9",
91// }
92//
93type libraryProperties struct {
94 // Relative path to the symbol map.
95 // An example file can be seen here: TODO(danalbert): Make an example.
Nan Zhang0007d812017-11-07 10:57:05 -080096 Symbol_file *string
Dan Albert914449f2016-06-17 16:45:24 -070097
98 // The first API level a library was available. A library will be generated
99 // for every API level beginning with this one.
Nan Zhang0007d812017-11-07 10:57:05 -0800100 First_version *string
Dan Albert914449f2016-06-17 16:45:24 -0700101
Dan Albert98dbb3b2017-01-03 15:16:29 -0800102 // The first API level that library should have the version script applied.
103 // This defaults to the value of first_version, and should almost never be
104 // used. This is only needed to work around platform bugs like
105 // https://github.com/android-ndk/ndk/issues/265.
Nan Zhang0007d812017-11-07 10:57:05 -0800106 Unversioned_until *string
Dan Albert604086f2021-06-15 13:23:44 -0700107
108 // If true, does not emit errors when APIs lacking type information are
109 // found. This is false by default and should not be enabled outside bionic,
110 // where it is enabled pending a fix for http://b/190554910 (no debug info
111 // for asm implemented symbols).
112 Allow_untyped_symbols *bool
Dan Albert914449f2016-06-17 16:45:24 -0700113}
114
Colin Crossb916a382016-07-29 17:28:03 -0700115type stubDecorator struct {
116 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -0700117
118 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -0700119
sophiez58cabb72020-05-29 13:37:12 -0700120 versionScriptPath android.ModuleGenPath
121 parsedCoverageXmlPath android.ModuleOutPath
122 installPath android.Path
Dan Albertf1d14c72020-07-30 14:32:55 -0700123 abiDumpPath android.OutputPath
124 abiDiffPaths android.Paths
Dan Albert1a246272020-07-06 14:49:35 -0700125
126 apiLevel android.ApiLevel
127 firstVersion android.ApiLevel
128 unversionedUntil android.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700129}
130
Colin Cross0477b422020-10-13 18:43:54 -0700131var _ versionedInterface = (*stubDecorator)(nil)
132
Dan Albert1a246272020-07-06 14:49:35 -0700133func shouldUseVersionScript(ctx BaseModuleContext, stub *stubDecorator) bool {
134 return stub.apiLevel.GreaterThanOrEqualTo(stub.unversionedUntil)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800135}
136
Colin Cross0477b422020-10-13 18:43:54 -0700137func (stub *stubDecorator) implementationModuleName(name string) string {
138 return strings.TrimSuffix(name, ndkLibrarySuffix)
139}
140
Colin Cross3572cf72020-10-01 15:58:11 -0700141func ndkLibraryVersions(ctx android.BaseMutatorContext, from android.ApiLevel) []string {
Dan Albert1a246272020-07-06 14:49:35 -0700142 var versions []android.ApiLevel
143 versionStrs := []string{}
144 for _, version := range ctx.Config().AllSupportedApiLevels() {
145 if version.GreaterThanOrEqualTo(from) {
146 versions = append(versions, version)
147 versionStrs = append(versionStrs, version.String())
148 }
Dan Albert914449f2016-06-17 16:45:24 -0700149 }
Dan Albert0b176c82020-07-23 16:43:25 -0700150 versionStrs = append(versionStrs, android.FutureApiLevel.String())
Dan Albert914449f2016-06-17 16:45:24 -0700151
Colin Cross5ec407b2020-09-30 11:41:33 -0700152 return versionStrs
153}
154
Colin Cross3572cf72020-10-01 15:58:11 -0700155func (this *stubDecorator) stubsVersions(ctx android.BaseMutatorContext) []string {
156 if !ctx.Module().Enabled() {
157 return nil
158 }
Dan Albertf1d14c72020-07-30 14:32:55 -0700159 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
160 ctx.Module().Disable()
161 return nil
162 }
Colin Cross3572cf72020-10-01 15:58:11 -0700163 firstVersion, err := nativeApiLevelFromUser(ctx,
164 String(this.properties.First_version))
165 if err != nil {
166 ctx.PropertyErrorf("first_version", err.Error())
167 return nil
168 }
169 return ndkLibraryVersions(ctx, firstVersion)
170}
171
Dan Albert1a246272020-07-06 14:49:35 -0700172func (this *stubDecorator) initializeProperties(ctx BaseModuleContext) bool {
Colin Cross5ec407b2020-09-30 11:41:33 -0700173 this.apiLevel = nativeApiLevelOrPanic(ctx, this.stubsVersion())
Dan Albert1a246272020-07-06 14:49:35 -0700174
175 var err error
176 this.firstVersion, err = nativeApiLevelFromUser(ctx,
177 String(this.properties.First_version))
178 if err != nil {
179 ctx.PropertyErrorf("first_version", err.Error())
180 return false
181 }
182
Jiyong Parkee9b1172021-04-06 17:40:32 +0900183 str := proptools.StringDefault(this.properties.Unversioned_until, "minimum")
184 this.unversionedUntil, err = nativeApiLevelFromUser(ctx, str)
Dan Albert1a246272020-07-06 14:49:35 -0700185 if err != nil {
186 ctx.PropertyErrorf("unversioned_until", err.Error())
187 return false
188 }
189
190 return true
191}
192
Colin Cross95f1ca02020-10-29 20:47:22 -0700193func getNDKKnownLibs(config android.Config) *[]string {
194 return config.Once(ndkKnownLibsKey, func() interface{} {
195 return &[]string{}
196 }).(*[]string)
197}
198
Colin Crossb916a382016-07-29 17:28:03 -0700199func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700200 c.baseCompiler.compilerInit(ctx)
201
Dan Willemsen01a90592017-04-07 15:21:13 -0700202 name := ctx.baseModuleName()
203 if strings.HasSuffix(name, ndkLibrarySuffix) {
204 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
205 }
206
Dan Albertde5aade2020-06-30 12:32:51 -0700207 ndkKnownLibsLock.Lock()
208 defer ndkKnownLibsLock.Unlock()
Colin Cross95f1ca02020-10-29 20:47:22 -0700209 ndkKnownLibs := getNDKKnownLibs(ctx.Config())
210 for _, lib := range *ndkKnownLibs {
Dan Albert7e9d2952016-08-04 13:02:36 -0700211 if lib == name {
212 return
213 }
214 }
Colin Cross95f1ca02020-10-29 20:47:22 -0700215 *ndkKnownLibs = append(*ndkKnownLibs, name)
Dan Albert7e9d2952016-08-04 13:02:36 -0700216}
217
Jingwen Chen341f7352022-01-11 05:42:49 +0000218var stubLibraryCompilerFlags = []string{
219 // We're knowingly doing some otherwise unsightly things with builtin
220 // functions here. We're just generating stub libraries, so ignore it.
221 "-Wno-incompatible-library-redeclaration",
222 "-Wno-incomplete-setjmp-declaration",
223 "-Wno-builtin-requires-header",
224 "-Wno-invalid-noreturn",
225 "-Wall",
226 "-Werror",
227 // These libraries aren't actually used. Don't worry about unwinding
228 // (avoids the need to link an unwinder into a fake library).
229 "-fno-unwind-tables",
230}
231
232func init() {
233 config.ExportStringList("StubLibraryCompilerFlags", stubLibraryCompilerFlags)
234}
235
George Burgess IVf5310e32017-07-19 11:39:53 -0700236func addStubLibraryCompilerFlags(flags Flags) Flags {
Jingwen Chen341f7352022-01-11 05:42:49 +0000237 flags.Global.CFlags = append(flags.Global.CFlags, stubLibraryCompilerFlags...)
Jiyong Park48d75ef2019-11-21 15:11:49 +0900238 // All symbols in the stubs library should be visible.
239 if inList("-fvisibility=hidden", flags.Local.CFlags) {
240 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
241 }
George Burgess IVf5310e32017-07-19 11:39:53 -0700242 return flags
243}
244
Colin Crossf18e1102017-11-16 14:33:08 -0800245func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
246 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700247 return addStubLibraryCompilerFlags(flags)
248}
249
Dan Albertf1d14c72020-07-30 14:32:55 -0700250type ndkApiOutputs struct {
251 stubSrc android.ModuleGenPath
252 versionScript android.ModuleGenPath
253 symbolList android.ModuleGenPath
254}
255
256func parseNativeAbiDefinition(ctx ModuleContext, symbolFile string,
257 apiLevel android.ApiLevel, genstubFlags string) ndkApiOutputs {
Dan Albert914449f2016-06-17 16:45:24 -0700258
Dan Willemsenb916b802017-03-19 13:44:32 -0700259 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
260 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
261 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albertf1d14c72020-07-30 14:32:55 -0700262 symbolListPath := android.PathForModuleGen(ctx, "abi_symbol_list.txt")
Dan Albert49927d22017-03-28 15:00:46 -0700263 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700264 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700265 Rule: genStubSrc,
266 Description: "generate stubs " + symbolFilePath.Rel(),
Dan Albertf1d14c72020-07-30 14:32:55 -0700267 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath,
268 symbolListPath},
269 Input: symbolFilePath,
270 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700271 Args: map[string]string{
Dan Albertf1d14c72020-07-30 14:32:55 -0700272 "arch": ctx.Arch().ArchType.String(),
273 "apiLevel": apiLevel.String(),
Dan Albert49927d22017-03-28 15:00:46 -0700274 "apiMap": apiLevelsJson.String(),
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900275 "flags": genstubFlags,
Dan Albert914449f2016-06-17 16:45:24 -0700276 },
277 })
278
Dan Albertf1d14c72020-07-30 14:32:55 -0700279 return ndkApiOutputs{
280 stubSrc: stubSrcPath,
281 versionScript: versionScriptPath,
282 symbolList: symbolListPath,
283 }
284}
285
286func compileStubLibrary(ctx ModuleContext, flags Flags, src android.Path) Objects {
Mitch Phillips4e5f9a12022-04-29 13:12:28 -0700287 // libc/libm stubs libraries end up mismatching with clang's internal definition of these
288 // functions (which have noreturn attributes and other things). Because we just want to create a
289 // stub with symbol definitions, and types aren't important in C, ignore the mismatch.
290 flags.Local.ConlyFlags = append(flags.Local.ConlyFlags, "-fno-builtin")
Dan Albertf1d14c72020-07-30 14:32:55 -0700291 return compileObjs(ctx, flagsToBuilderFlags(flags), "",
Chih-Hung Hsieh9db8a0c2022-02-17 12:54:45 -0800292 android.Paths{src}, nil, nil, nil, nil)
Dan Willemsenb916b802017-03-19 13:44:32 -0700293}
294
Dan Albertf1d14c72020-07-30 14:32:55 -0700295func (this *stubDecorator) findImplementationLibrary(ctx ModuleContext) android.Path {
296 dep := ctx.GetDirectDepWithTag(strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix),
297 stubImplementation)
298 if dep == nil {
299 ctx.ModuleErrorf("Could not find implementation for stub")
300 return nil
301 }
302 impl, ok := dep.(*Module)
303 if !ok {
304 ctx.ModuleErrorf("Implementation for stub is not correct module type")
305 }
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(),
323 ctx.Arch().ArchType.String(), this.libraryName(ctx), "abi.xml")
324 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 }
339 return true
Dan Albertf1d14c72020-07-30 14:32:55 -0700340}
341
342// Feature flag to disable diffing against prebuilts.
Dan Albertad665932021-06-07 13:19:49 -0700343func canDiffAbi() bool {
Dan Albertf1d14c72020-07-30 14:32:55 -0700344 return false
345}
346
347func (this *stubDecorator) dumpAbi(ctx ModuleContext, symbolList android.Path) {
348 implementationLibrary := this.findImplementationLibrary(ctx)
Matthias Maennichc2346f12021-09-02 20:45:33 +0100349 abiRawPath := getNdkAbiDumpInstallBase(ctx).Join(ctx,
Dan Albertf1d14c72020-07-30 14:32:55 -0700350 this.apiLevel.String(), ctx.Arch().ArchType.String(),
Matthias Maennichc2346f12021-09-02 20:45:33 +0100351 this.libraryName(ctx), "abi.raw.xml")
Dan Albertf1d14c72020-07-30 14:32:55 -0700352 ctx.Build(pctx, android.BuildParams{
353 Rule: abidw,
354 Description: fmt.Sprintf("abidw %s", implementationLibrary),
Dan Albertf1d14c72020-07-30 14:32:55 -0700355 Input: implementationLibrary,
Matthias Maennichc2346f12021-09-02 20:45:33 +0100356 Output: abiRawPath,
Dan Albertf1d14c72020-07-30 14:32:55 -0700357 Implicit: symbolList,
358 Args: map[string]string{
359 "symbolList": symbolList.String(),
360 },
361 })
Dan Albert604086f2021-06-15 13:23:44 -0700362
Matthias Maennichc2346f12021-09-02 20:45:33 +0100363 this.abiDumpPath = getNdkAbiDumpInstallBase(ctx).Join(ctx,
364 this.apiLevel.String(), ctx.Arch().ArchType.String(),
365 this.libraryName(ctx), "abi.xml")
Dan Albert604086f2021-06-15 13:23:44 -0700366 untypedFlag := "--abort-on-untyped-symbols"
367 if proptools.BoolDefault(this.properties.Allow_untyped_symbols, false) {
368 untypedFlag = ""
369 }
Matthias Maennichc2346f12021-09-02 20:45:33 +0100370 ctx.Build(pctx, android.BuildParams{
371 Rule: abitidy,
372 Description: fmt.Sprintf("abitidy %s", implementationLibrary),
373 Input: abiRawPath,
374 Output: this.abiDumpPath,
Dan Albert604086f2021-06-15 13:23:44 -0700375 Args: map[string]string{
376 "flags": untypedFlag,
377 },
Matthias Maennichc2346f12021-09-02 20:45:33 +0100378 })
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) {
393 missingPrebuiltError := fmt.Sprintf(
394 "Did not find prebuilt ABI dump for %q. Generate with "+
395 "//development/tools/ndk/update_ndk_abi.sh.", this.libraryName(ctx))
396
397 // Catch any ABI changes compared to the checked-in definition of this API
398 // level.
399 abiDiffPath := android.PathForModuleOut(ctx, "abidiff.timestamp")
400 prebuiltAbiDump := this.findPrebuiltAbiDump(ctx, this.apiLevel)
401 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
Colin Cross37047f12016-12-13 17:06:13 -0800487func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700488 return Deps{}
489}
490
Dan Willemsen01a90592017-04-07 15:21:13 -0700491func (linker *stubDecorator) Name(name string) string {
492 return name + ndkLibrarySuffix
493}
494
Colin Crossb916a382016-07-29 17:28:03 -0700495func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700496 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700497 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700498}
499
Colin Crossb916a382016-07-29 17:28:03 -0700500func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700501 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700502
Colin Cross5ec407b2020-09-30 11:41:33 -0700503 if !stub.buildStubs() {
504 // NDK libraries have no implementation variant, nothing to do
505 return nil
506 }
507
Dan Albert1a246272020-07-06 14:49:35 -0700508 if shouldUseVersionScript(ctx, stub) {
Dan Albert98dbb3b2017-01-03 15:16:29 -0800509 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800510 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700511 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800512 }
513
Colin Cross5ec407b2020-09-30 11:41:33 -0700514 stub.libraryDecorator.skipAPIDefine = true
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700515 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700516}
517
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700518func (stub *stubDecorator) nativeCoverage() bool {
519 return false
520}
521
Colin Crossb916a382016-07-29 17:28:03 -0700522func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700523 arch := ctx.Target().Arch.ArchType.Name
Dan Albert914449f2016-06-17 16:45:24 -0700524 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
525 // architectures it's just installed to lib.
526 libDir := "lib"
527 if ctx.toolchain().Is64Bit() && arch != "arm64" {
528 libDir = "lib64"
529 }
530
531 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albert1a246272020-07-06 14:49:35 -0700532 "platforms/android-%s/arch-%s/usr/%s", stub.apiLevel, arch, libDir))
Colin Cross0875c522017-11-28 17:34:01 -0800533 stub.installPath = ctx.InstallFile(installDir, path.Base(), path)
Dan Albert914449f2016-06-17 16:45:24 -0700534}
535
Colin Cross36242852017-06-23 15:06:31 -0700536func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800537 module, library := NewLibrary(android.DeviceSupported)
538 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700539 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700540 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200541 library.disableStripping()
Dan Albert914449f2016-06-17 16:45:24 -0700542
Colin Crossb916a382016-07-29 17:28:03 -0700543 stub := &stubDecorator{
544 libraryDecorator: library,
545 }
546 module.compiler = stub
547 module.linker = stub
548 module.installer = stub
Colin Cross31076b32020-10-23 17:22:06 -0700549 module.library = stub
Dan Albert914449f2016-06-17 16:45:24 -0700550
Colin Crossc511bc52020-04-07 16:50:32 +0000551 module.Properties.AlwaysSdk = true
552 module.Properties.Sdk_version = StringPtr("current")
553
Colin Cross36242852017-06-23 15:06:31 -0700554 module.AddProperties(&stub.properties, &library.MutatedProperties)
555
556 return module
Dan Albert914449f2016-06-17 16:45:24 -0700557}
558
Dan Albertf740ed02020-07-24 14:19:06 -0700559// ndk_library creates a library that exposes a stub implementation of functions
560// and variables for use at build time only.
Jooyung Hanb90e4912019-12-09 18:21:48 +0900561func NdkLibraryFactory() android.Module {
Colin Cross36242852017-06-23 15:06:31 -0700562 module := newStubLibrary()
563 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
564 return module
Dan Albert914449f2016-06-17 16:45:24 -0700565}