blob: a45838063be841293f49f2384973b2a0b758463c [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"
28)
29
sophiez58cabb72020-05-29 13:37:12 -070030func init() {
Dan Albert06f58af2020-06-22 15:10:31 -070031 pctx.HostBinToolVariable("ndkStubGenerator", "ndkstubgen")
sophiez58cabb72020-05-29 13:37:12 -070032 pctx.HostBinToolVariable("ndk_api_coverage_parser", "ndk_api_coverage_parser")
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
sophiez58cabb72020-05-29 13:37:12 -070046 parseNdkApiRule = pctx.AndroidStaticRule("parseNdkApiRule",
47 blueprint.RuleParams{
48 Command: "$ndk_api_coverage_parser $in $out --api-map $apiMap",
49 CommandDeps: []string{"$ndk_api_coverage_parser"},
50 }, "apiMap")
51
Dan Albertf1d14c72020-07-30 14:32:55 -070052 abidw = pctx.AndroidStaticRule("abidw",
53 blueprint.RuleParams{
54 Command: "$abidw --type-id-style hash --no-corpus-path " +
55 "--no-show-locs --no-comp-dir-path -w $symbolList $in | " +
56 "$abitidy --all -o $out",
57 CommandDeps: []string{"$abitidy", "$abidw"},
58 }, "symbolList")
59
60 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 Albert914449f2016-06-17 16:45:24 -0700107}
108
Colin Crossb916a382016-07-29 17:28:03 -0700109type stubDecorator struct {
110 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -0700111
112 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -0700113
sophiez58cabb72020-05-29 13:37:12 -0700114 versionScriptPath android.ModuleGenPath
115 parsedCoverageXmlPath android.ModuleOutPath
116 installPath android.Path
Dan Albertf1d14c72020-07-30 14:32:55 -0700117 abiDumpPath android.OutputPath
118 abiDiffPaths android.Paths
Dan Albert1a246272020-07-06 14:49:35 -0700119
120 apiLevel android.ApiLevel
121 firstVersion android.ApiLevel
122 unversionedUntil android.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700123}
124
Colin Cross0477b422020-10-13 18:43:54 -0700125var _ versionedInterface = (*stubDecorator)(nil)
126
Dan Albert1a246272020-07-06 14:49:35 -0700127func shouldUseVersionScript(ctx BaseModuleContext, stub *stubDecorator) bool {
128 return stub.apiLevel.GreaterThanOrEqualTo(stub.unversionedUntil)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800129}
130
Colin Cross0477b422020-10-13 18:43:54 -0700131func (stub *stubDecorator) implementationModuleName(name string) string {
132 return strings.TrimSuffix(name, ndkLibrarySuffix)
133}
134
Colin Cross3572cf72020-10-01 15:58:11 -0700135func ndkLibraryVersions(ctx android.BaseMutatorContext, from android.ApiLevel) []string {
Dan Albert1a246272020-07-06 14:49:35 -0700136 var versions []android.ApiLevel
137 versionStrs := []string{}
138 for _, version := range ctx.Config().AllSupportedApiLevels() {
139 if version.GreaterThanOrEqualTo(from) {
140 versions = append(versions, version)
141 versionStrs = append(versionStrs, version.String())
142 }
Dan Albert914449f2016-06-17 16:45:24 -0700143 }
Dan Albert0b176c82020-07-23 16:43:25 -0700144 versionStrs = append(versionStrs, android.FutureApiLevel.String())
Dan Albert914449f2016-06-17 16:45:24 -0700145
Colin Cross5ec407b2020-09-30 11:41:33 -0700146 return versionStrs
147}
148
Colin Cross3572cf72020-10-01 15:58:11 -0700149func (this *stubDecorator) stubsVersions(ctx android.BaseMutatorContext) []string {
150 if !ctx.Module().Enabled() {
151 return nil
152 }
Dan Albertf1d14c72020-07-30 14:32:55 -0700153 if ctx.Os() != android.Android {
154 // These modules are always android.DeviceEnabled only, but
155 // those include Fuchsia devices, which we don't support.
156 ctx.Module().Disable()
157 return nil
158 }
159 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
George Burgess IVf5310e32017-07-19 11:39:53 -0700218func addStubLibraryCompilerFlags(flags Flags) Flags {
Colin Cross4af21ed2019-11-04 09:37:55 -0800219 flags.Global.CFlags = append(flags.Global.CFlags,
George Burgess IVf5310e32017-07-19 11:39:53 -0700220 // We're knowingly doing some otherwise unsightly things with builtin
221 // functions here. We're just generating stub libraries, so ignore it.
222 "-Wno-incompatible-library-redeclaration",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800223 "-Wno-incomplete-setjmp-declaration",
George Burgess IVf5310e32017-07-19 11:39:53 -0700224 "-Wno-builtin-requires-header",
225 "-Wno-invalid-noreturn",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800226 "-Wall",
227 "-Werror",
George Burgess IVf5310e32017-07-19 11:39:53 -0700228 // These libraries aren't actually used. Don't worry about unwinding
229 // (avoids the need to link an unwinder into a fake library).
230 "-fno-unwind-tables",
231 )
Jiyong Park48d75ef2019-11-21 15:11:49 +0900232 // All symbols in the stubs library should be visible.
233 if inList("-fvisibility=hidden", flags.Local.CFlags) {
234 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
235 }
George Burgess IVf5310e32017-07-19 11:39:53 -0700236 return flags
237}
238
Colin Crossf18e1102017-11-16 14:33:08 -0800239func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
240 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700241 return addStubLibraryCompilerFlags(flags)
242}
243
Dan Albertf1d14c72020-07-30 14:32:55 -0700244type ndkApiOutputs struct {
245 stubSrc android.ModuleGenPath
246 versionScript android.ModuleGenPath
247 symbolList android.ModuleGenPath
248}
249
250func parseNativeAbiDefinition(ctx ModuleContext, symbolFile string,
251 apiLevel android.ApiLevel, genstubFlags string) ndkApiOutputs {
Dan Albert914449f2016-06-17 16:45:24 -0700252
Dan Willemsenb916b802017-03-19 13:44:32 -0700253 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
254 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
255 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albertf1d14c72020-07-30 14:32:55 -0700256 symbolListPath := android.PathForModuleGen(ctx, "abi_symbol_list.txt")
Dan Albert49927d22017-03-28 15:00:46 -0700257 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700258 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700259 Rule: genStubSrc,
260 Description: "generate stubs " + symbolFilePath.Rel(),
Dan Albertf1d14c72020-07-30 14:32:55 -0700261 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath,
262 symbolListPath},
263 Input: symbolFilePath,
264 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700265 Args: map[string]string{
Dan Albertf1d14c72020-07-30 14:32:55 -0700266 "arch": ctx.Arch().ArchType.String(),
267 "apiLevel": apiLevel.String(),
Dan Albert49927d22017-03-28 15:00:46 -0700268 "apiMap": apiLevelsJson.String(),
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900269 "flags": genstubFlags,
Dan Albert914449f2016-06-17 16:45:24 -0700270 },
271 })
272
Dan Albertf1d14c72020-07-30 14:32:55 -0700273 return ndkApiOutputs{
274 stubSrc: stubSrcPath,
275 versionScript: versionScriptPath,
276 symbolList: symbolListPath,
277 }
278}
279
280func compileStubLibrary(ctx ModuleContext, flags Flags, src android.Path) Objects {
281 return compileObjs(ctx, flagsToBuilderFlags(flags), "",
282 android.Paths{src}, nil, nil)
Dan Willemsenb916b802017-03-19 13:44:32 -0700283}
284
sophiez58cabb72020-05-29 13:37:12 -0700285func parseSymbolFileForCoverage(ctx ModuleContext, symbolFile string) android.ModuleOutPath {
286 apiLevelsJson := android.GetApiLevelsJson(ctx)
287 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
288 outputFileName := strings.Split(symbolFilePath.Base(), ".")[0]
289 parsedApiCoveragePath := android.PathForModuleOut(ctx, outputFileName+".xml")
290 ctx.Build(pctx, android.BuildParams{
291 Rule: parseNdkApiRule,
292 Description: "parse ndk api symbol file for api coverage: " + symbolFilePath.Rel(),
293 Outputs: []android.WritablePath{parsedApiCoveragePath},
294 Input: symbolFilePath,
sophiez148b3172020-06-11 17:27:56 -0700295 Implicits: []android.Path{apiLevelsJson},
sophiez58cabb72020-05-29 13:37:12 -0700296 Args: map[string]string{
297 "apiMap": apiLevelsJson.String(),
298 },
299 })
300 return parsedApiCoveragePath
301}
302
Dan Albertf1d14c72020-07-30 14:32:55 -0700303func (this *stubDecorator) findImplementationLibrary(ctx ModuleContext) android.Path {
304 dep := ctx.GetDirectDepWithTag(strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix),
305 stubImplementation)
306 if dep == nil {
307 ctx.ModuleErrorf("Could not find implementation for stub")
308 return nil
309 }
310 impl, ok := dep.(*Module)
311 if !ok {
312 ctx.ModuleErrorf("Implementation for stub is not correct module type")
313 }
314 output := impl.UnstrippedOutputFile()
315 if output == nil {
316 ctx.ModuleErrorf("implementation module (%s) has no output", impl)
317 return nil
318 }
319
320 return output
321}
322
323func (this *stubDecorator) libraryName(ctx ModuleContext) string {
324 return strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix)
325}
326
327func (this *stubDecorator) findPrebuiltAbiDump(ctx ModuleContext,
328 apiLevel android.ApiLevel) android.OptionalPath {
329
330 subpath := filepath.Join("prebuilts/abi-dumps/ndk", apiLevel.String(),
331 ctx.Arch().ArchType.String(), this.libraryName(ctx), "abi.xml")
332 return android.ExistentPathForSource(ctx, subpath)
333}
334
335// Feature flag.
Dan Albertad665932021-06-07 13:19:49 -0700336func canDumpAbi() bool {
337 return runtime.GOOS != "darwin"
Dan Albertf1d14c72020-07-30 14:32:55 -0700338}
339
340// Feature flag to disable diffing against prebuilts.
Dan Albertad665932021-06-07 13:19:49 -0700341func canDiffAbi() bool {
Dan Albertf1d14c72020-07-30 14:32:55 -0700342 return false
343}
344
345func (this *stubDecorator) dumpAbi(ctx ModuleContext, symbolList android.Path) {
346 implementationLibrary := this.findImplementationLibrary(ctx)
347 this.abiDumpPath = getNdkAbiDumpInstallBase(ctx).Join(ctx,
348 this.apiLevel.String(), ctx.Arch().ArchType.String(),
349 this.libraryName(ctx), "abi.xml")
350 ctx.Build(pctx, android.BuildParams{
351 Rule: abidw,
352 Description: fmt.Sprintf("abidw %s", implementationLibrary),
353 Output: this.abiDumpPath,
354 Input: implementationLibrary,
355 Implicit: symbolList,
356 Args: map[string]string{
357 "symbolList": symbolList.String(),
358 },
359 })
360}
361
362func findNextApiLevel(ctx ModuleContext, apiLevel android.ApiLevel) *android.ApiLevel {
363 apiLevels := append(ctx.Config().AllSupportedApiLevels(),
364 android.FutureApiLevel)
365 for _, api := range apiLevels {
366 if api.GreaterThan(apiLevel) {
367 return &api
368 }
369 }
370 return nil
371}
372
373func (this *stubDecorator) diffAbi(ctx ModuleContext) {
374 missingPrebuiltError := fmt.Sprintf(
375 "Did not find prebuilt ABI dump for %q. Generate with "+
376 "//development/tools/ndk/update_ndk_abi.sh.", this.libraryName(ctx))
377
378 // Catch any ABI changes compared to the checked-in definition of this API
379 // level.
380 abiDiffPath := android.PathForModuleOut(ctx, "abidiff.timestamp")
381 prebuiltAbiDump := this.findPrebuiltAbiDump(ctx, this.apiLevel)
382 if !prebuiltAbiDump.Valid() {
383 ctx.Build(pctx, android.BuildParams{
384 Rule: android.ErrorRule,
385 Output: abiDiffPath,
386 Args: map[string]string{
387 "error": missingPrebuiltError,
388 },
389 })
390 } else {
391 ctx.Build(pctx, android.BuildParams{
392 Rule: abidiff,
393 Description: fmt.Sprintf("abidiff %s %s", prebuiltAbiDump,
394 this.abiDumpPath),
395 Output: abiDiffPath,
396 Inputs: android.Paths{prebuiltAbiDump.Path(), this.abiDumpPath},
397 })
398 }
399 this.abiDiffPaths = append(this.abiDiffPaths, abiDiffPath)
400
401 // Also ensure that the ABI of the next API level (if there is one) matches
402 // this API level. *New* ABI is allowed, but any changes to APIs that exist
403 // in this API level are disallowed.
404 if !this.apiLevel.IsCurrent() {
405 nextApiLevel := findNextApiLevel(ctx, this.apiLevel)
406 if nextApiLevel == nil {
407 panic(fmt.Errorf("could not determine which API level follows "+
408 "non-current API level %s", this.apiLevel))
409 }
410 nextAbiDiffPath := android.PathForModuleOut(ctx,
411 "abidiff_next.timestamp")
412 nextAbiDump := this.findPrebuiltAbiDump(ctx, *nextApiLevel)
413 if !nextAbiDump.Valid() {
414 ctx.Build(pctx, android.BuildParams{
415 Rule: android.ErrorRule,
416 Output: nextAbiDiffPath,
417 Args: map[string]string{
418 "error": missingPrebuiltError,
419 },
420 })
421 } else {
422 ctx.Build(pctx, android.BuildParams{
423 Rule: abidiff,
424 Description: fmt.Sprintf("abidiff %s %s", this.abiDumpPath,
425 nextAbiDump),
426 Output: nextAbiDiffPath,
427 Inputs: android.Paths{this.abiDumpPath, nextAbiDump.Path()},
428 Args: map[string]string{
429 "args": "--no-added-syms",
430 },
431 })
432 }
433 this.abiDiffPaths = append(this.abiDiffPaths, nextAbiDiffPath)
434 }
435}
436
Dan Willemsenb916b802017-03-19 13:44:32 -0700437func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800438 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700439 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
440 }
441
Colin Cross5ec407b2020-09-30 11:41:33 -0700442 if !c.buildStubs() {
443 // NDK libraries have no implementation variant, nothing to do
444 return Objects{}
445 }
446
Dan Albert1a246272020-07-06 14:49:35 -0700447 if !c.initializeProperties(ctx) {
448 // Emits its own errors, so we don't need to.
449 return Objects{}
450 }
451
sophiez58cabb72020-05-29 13:37:12 -0700452 symbolFile := String(c.properties.Symbol_file)
Dan Albertf1d14c72020-07-30 14:32:55 -0700453 nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile, c.apiLevel, "")
454 objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
455 c.versionScriptPath = nativeAbiResult.versionScript
Dan Albertad665932021-06-07 13:19:49 -0700456 if canDumpAbi() {
Dan Albertf1d14c72020-07-30 14:32:55 -0700457 c.dumpAbi(ctx, nativeAbiResult.symbolList)
Dan Albertad665932021-06-07 13:19:49 -0700458 if canDiffAbi() {
Dan Albertf1d14c72020-07-30 14:32:55 -0700459 c.diffAbi(ctx)
460 }
461 }
Dan Albert1a246272020-07-06 14:49:35 -0700462 if c.apiLevel.IsCurrent() && ctx.PrimaryArch() {
sophiez58cabb72020-05-29 13:37:12 -0700463 c.parsedCoverageXmlPath = parseSymbolFileForCoverage(ctx, symbolFile)
464 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700465 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700466}
467
Colin Cross37047f12016-12-13 17:06:13 -0800468func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700469 return Deps{}
470}
471
Dan Willemsen01a90592017-04-07 15:21:13 -0700472func (linker *stubDecorator) Name(name string) string {
473 return name + ndkLibrarySuffix
474}
475
Colin Crossb916a382016-07-29 17:28:03 -0700476func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700477 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700478 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700479}
480
Colin Crossb916a382016-07-29 17:28:03 -0700481func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700482 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700483
Colin Cross5ec407b2020-09-30 11:41:33 -0700484 if !stub.buildStubs() {
485 // NDK libraries have no implementation variant, nothing to do
486 return nil
487 }
488
Dan Albert1a246272020-07-06 14:49:35 -0700489 if shouldUseVersionScript(ctx, stub) {
Dan Albert98dbb3b2017-01-03 15:16:29 -0800490 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800491 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700492 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800493 }
494
Colin Cross5ec407b2020-09-30 11:41:33 -0700495 stub.libraryDecorator.skipAPIDefine = true
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700496 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700497}
498
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700499func (stub *stubDecorator) nativeCoverage() bool {
500 return false
501}
502
Colin Crossb916a382016-07-29 17:28:03 -0700503func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700504 arch := ctx.Target().Arch.ArchType.Name
Dan Albert914449f2016-06-17 16:45:24 -0700505 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
506 // architectures it's just installed to lib.
507 libDir := "lib"
508 if ctx.toolchain().Is64Bit() && arch != "arm64" {
509 libDir = "lib64"
510 }
511
512 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albert1a246272020-07-06 14:49:35 -0700513 "platforms/android-%s/arch-%s/usr/%s", stub.apiLevel, arch, libDir))
Colin Cross0875c522017-11-28 17:34:01 -0800514 stub.installPath = ctx.InstallFile(installDir, path.Base(), path)
Dan Albert914449f2016-06-17 16:45:24 -0700515}
516
Colin Cross36242852017-06-23 15:06:31 -0700517func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800518 module, library := NewLibrary(android.DeviceSupported)
519 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700520 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700521 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200522 library.disableStripping()
Dan Albert914449f2016-06-17 16:45:24 -0700523
Colin Crossb916a382016-07-29 17:28:03 -0700524 stub := &stubDecorator{
525 libraryDecorator: library,
526 }
527 module.compiler = stub
528 module.linker = stub
529 module.installer = stub
Colin Cross31076b32020-10-23 17:22:06 -0700530 module.library = stub
Dan Albert914449f2016-06-17 16:45:24 -0700531
Colin Crossc511bc52020-04-07 16:50:32 +0000532 module.Properties.AlwaysSdk = true
533 module.Properties.Sdk_version = StringPtr("current")
534
Colin Cross36242852017-06-23 15:06:31 -0700535 module.AddProperties(&stub.properties, &library.MutatedProperties)
536
537 return module
Dan Albert914449f2016-06-17 16:45:24 -0700538}
539
Dan Albertf740ed02020-07-24 14:19:06 -0700540// ndk_library creates a library that exposes a stub implementation of functions
541// and variables for use at build time only.
Jooyung Hanb90e4912019-12-09 18:21:48 +0900542func NdkLibraryFactory() android.Module {
Colin Cross36242852017-06-23 15:06:31 -0700543 module := newStubLibrary()
544 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
545 return module
Dan Albert914449f2016-06-17 16:45:24 -0700546}