blob: 1f0fc07646b27a3a4f6292c2468582d0a6f2744f [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")
Matthias Maennichca8ae652023-06-30 21:52:17 +010033 pctx.HostBinToolVariable("stg", "stg")
Matthias Maennich658bb4d2023-06-30 23:28:15 +010034 pctx.HostBinToolVariable("stgdiff", "stgdiff")
sophiez58cabb72020-05-29 13:37:12 -070035}
36
Dan Albert914449f2016-06-17 16:45:24 -070037var (
Colin Cross9d45bb72016-08-29 16:14:13 -070038 genStubSrc = pctx.AndroidStaticRule("genStubSrc",
Dan Albert914449f2016-06-17 16:45:24 -070039 blueprint.RuleParams{
Dan Albert06f58af2020-06-22 15:10:31 -070040 Command: "$ndkStubGenerator --arch $arch --api $apiLevel " +
41 "--api-map $apiMap $flags $in $out",
42 CommandDeps: []string{"$ndkStubGenerator"},
Jiyong Park3fd0baf2018-12-07 16:25:39 +090043 }, "arch", "apiLevel", "apiMap", "flags")
Dan Albert914449f2016-06-17 16:45:24 -070044
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +000045 // $headersList should include paths to public headers. All types
46 // that are defined outside of public headers will be excluded from
47 // ABI monitoring.
48 //
49 // STG tool doesn't access content of files listed in $headersList,
50 // so there is no need to add them to dependencies.
Matthias Maennich55486f82023-07-03 12:25:27 +010051 stg = pctx.AndroidStaticRule("stg",
52 blueprint.RuleParams{
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +000053 Command: "$stg -S :$symbolList --file-filter :$headersList --elf $in -o $out",
Matthias Maennich55486f82023-07-03 12:25:27 +010054 CommandDeps: []string{"$stg"},
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +000055 }, "symbolList", "headersList")
Matthias Maennich55486f82023-07-03 12:25:27 +010056
Matthias Maennich658bb4d2023-06-30 23:28:15 +010057 stgdiff = pctx.AndroidStaticRule("stgdiff",
Dan Albertf1d14c72020-07-30 14:32:55 -070058 blueprint.RuleParams{
59 // Need to create *some* output for ninja. We don't want to use tee
60 // because we don't want to spam the build output with "nothing
61 // changed" messages, so redirect output message to $out, and if
62 // changes were detected print the output and fail.
Dan Albert5b2fd582023-10-05 21:43:41 +000063 Command: "$stgdiff $args --stg $in -o $out || (cat $out && echo 'Run $$ANDROID_BUILD_TOP/development/tools/ndk/update_ndk_abi.sh to update the ABI dumps.' && false)",
Matthias Maennich658bb4d2023-06-30 23:28:15 +010064 CommandDeps: []string{"$stgdiff"},
Dan Albertf1d14c72020-07-30 14:32:55 -070065 }, "args")
66
Dan Albert914449f2016-06-17 16:45:24 -070067 ndkLibrarySuffix = ".ndk"
Colin Cross4d9c2d12016-07-29 12:48:20 -070068
Colin Cross95f1ca02020-10-29 20:47:22 -070069 ndkKnownLibsKey = android.NewOnceKey("ndkKnownLibsKey")
Dan Albertde5aade2020-06-30 12:32:51 -070070 // protects ndkKnownLibs writes during parallel BeginMutator.
71 ndkKnownLibsLock sync.Mutex
Dan Albertf1d14c72020-07-30 14:32:55 -070072
73 stubImplementation = dependencyTag{name: "stubImplementation"}
Dan Albert914449f2016-06-17 16:45:24 -070074)
75
Dan Albert1a246272020-07-06 14:49:35 -070076// The First_version and Unversioned_until properties of this struct should not
77// be used directly, but rather through the ApiLevel returning methods
78// firstVersion() and unversionedUntil().
79
Dan Albert914449f2016-06-17 16:45:24 -070080// Creates a stub shared library based on the provided version file.
81//
Dan Albert914449f2016-06-17 16:45:24 -070082// Example:
83//
Spandan Das73bcafc2022-08-18 23:26:00 +000084// ndk_library {
85//
86// name: "libfoo",
87// symbol_file: "libfoo.map.txt",
88// first_version: "9",
89//
90// }
Dan Albert914449f2016-06-17 16:45:24 -070091type libraryProperties struct {
92 // Relative path to the symbol map.
93 // An example file can be seen here: TODO(danalbert): Make an example.
Inseob Kim5eb7ee92022-04-27 10:30:34 +090094 Symbol_file *string `android:"path"`
Dan Albert914449f2016-06-17 16:45:24 -070095
96 // The first API level a library was available. A library will be generated
97 // for every API level beginning with this one.
Nan Zhang0007d812017-11-07 10:57:05 -080098 First_version *string
Dan Albert914449f2016-06-17 16:45:24 -070099
Dan Albert98dbb3b2017-01-03 15:16:29 -0800100 // The first API level that library should have the version script applied.
101 // This defaults to the value of first_version, and should almost never be
102 // used. This is only needed to work around platform bugs like
103 // https://github.com/android-ndk/ndk/issues/265.
Nan Zhang0007d812017-11-07 10:57:05 -0800104 Unversioned_until *string
Dan Albert604086f2021-06-15 13:23:44 -0700105
Colin Crossa158a712024-09-04 15:33:04 +0000106 // DO NOT USE THIS
107 // NDK libraries should not export their headers. Headers belonging to NDK
108 // libraries should be added to the NDK with an ndk_headers module.
Spandan Das73bcafc2022-08-18 23:26:00 +0000109 Export_header_libs []string
Colin Crossa158a712024-09-04 15:33:04 +0000110
111 // Do not add other export_* properties without consulting with danalbert@.
112 // Consumers of ndk_library modules should emulate the typical NDK build
113 // behavior as closely as possible (that is, all NDK APIs are exposed to
114 // builds via --sysroot). Export behaviors used in Soong will not be present
115 // for app developers as they don't use Soong, and reliance on these export
116 // behaviors can mask issues with the NDK sysroot.
Dan Albert914449f2016-06-17 16:45:24 -0700117}
118
Colin Crossb916a382016-07-29 17:28:03 -0700119type stubDecorator struct {
120 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -0700121
122 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -0700123
sophiez58cabb72020-05-29 13:37:12 -0700124 versionScriptPath android.ModuleGenPath
125 parsedCoverageXmlPath android.ModuleOutPath
126 installPath android.Path
Dan Albertf1d14c72020-07-30 14:32:55 -0700127 abiDumpPath android.OutputPath
Dan Albert2d0dce12024-09-18 19:34:32 +0000128 hasAbiDump bool
Dan Albertf1d14c72020-07-30 14:32:55 -0700129 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
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000136var _ VersionedInterface = (*stubDecorator)(nil)
Colin Cross0477b422020-10-13 18:43:54 -0700137
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
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000142func (stub *stubDecorator) ImplementationModuleName(name string) string {
Colin Cross0477b422020-10-13 18:43:54 -0700143 return strings.TrimSuffix(name, ndkLibrarySuffix)
144}
145
Colin Crossadd04a82024-05-22 09:57:59 -0700146func ndkLibraryVersions(ctx android.BaseModuleContext, from android.ApiLevel) []string {
Dan Albert1a246272020-07-06 14:49:35 -0700147 versionStrs := []string{}
Dan Albert499c92e2024-10-31 22:15:40 +0000148 for _, version := range ctx.Config().FinalApiLevels() {
Dan Albert1a246272020-07-06 14:49:35 -0700149 if version.GreaterThanOrEqualTo(from) {
Dan Albert1a246272020-07-06 14:49:35 -0700150 versionStrs = append(versionStrs, version.String())
151 }
Dan Albert914449f2016-06-17 16:45:24 -0700152 }
Dan Albert0b176c82020-07-23 16:43:25 -0700153 versionStrs = append(versionStrs, android.FutureApiLevel.String())
Dan Albert914449f2016-06-17 16:45:24 -0700154
Colin Cross5ec407b2020-09-30 11:41:33 -0700155 return versionStrs
156}
157
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000158func (this *stubDecorator) StubsVersions(ctx android.BaseModuleContext) []string {
Cole Fausta963b942024-04-11 17:43:00 -0700159 if !ctx.Module().Enabled(ctx) {
Colin Cross3572cf72020-10-01 15:58:11 -0700160 return nil
161 }
Dan Albertf1d14c72020-07-30 14:32:55 -0700162 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
163 ctx.Module().Disable()
164 return nil
165 }
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000166 firstVersion, err := NativeApiLevelFromUser(ctx,
Colin Cross3572cf72020-10-01 15:58:11 -0700167 String(this.properties.First_version))
168 if err != nil {
169 ctx.PropertyErrorf("first_version", err.Error())
170 return nil
171 }
172 return ndkLibraryVersions(ctx, firstVersion)
173}
174
Dan Albert1a246272020-07-06 14:49:35 -0700175func (this *stubDecorator) initializeProperties(ctx BaseModuleContext) bool {
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000176 this.apiLevel = nativeApiLevelOrPanic(ctx, this.StubsVersion())
Dan Albert1a246272020-07-06 14:49:35 -0700177
178 var err error
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000179 this.firstVersion, err = NativeApiLevelFromUser(ctx,
Dan Albert1a246272020-07-06 14:49:35 -0700180 String(this.properties.First_version))
181 if err != nil {
182 ctx.PropertyErrorf("first_version", err.Error())
183 return false
184 }
185
Jiyong Parkee9b1172021-04-06 17:40:32 +0900186 str := proptools.StringDefault(this.properties.Unversioned_until, "minimum")
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000187 this.unversionedUntil, err = NativeApiLevelFromUser(ctx, str)
Dan Albert1a246272020-07-06 14:49:35 -0700188 if err != nil {
189 ctx.PropertyErrorf("unversioned_until", err.Error())
190 return false
191 }
192
193 return true
194}
195
Colin Cross95f1ca02020-10-29 20:47:22 -0700196func getNDKKnownLibs(config android.Config) *[]string {
197 return config.Once(ndkKnownLibsKey, func() interface{} {
198 return &[]string{}
199 }).(*[]string)
200}
201
Colin Crossb916a382016-07-29 17:28:03 -0700202func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700203 c.baseCompiler.compilerInit(ctx)
204
Dan Willemsen01a90592017-04-07 15:21:13 -0700205 name := ctx.baseModuleName()
206 if strings.HasSuffix(name, ndkLibrarySuffix) {
207 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
208 }
209
Dan Albertde5aade2020-06-30 12:32:51 -0700210 ndkKnownLibsLock.Lock()
211 defer ndkKnownLibsLock.Unlock()
Colin Cross95f1ca02020-10-29 20:47:22 -0700212 ndkKnownLibs := getNDKKnownLibs(ctx.Config())
213 for _, lib := range *ndkKnownLibs {
Dan Albert7e9d2952016-08-04 13:02:36 -0700214 if lib == name {
215 return
216 }
217 }
Colin Cross95f1ca02020-10-29 20:47:22 -0700218 *ndkKnownLibs = append(*ndkKnownLibs, name)
Dan Albert7e9d2952016-08-04 13:02:36 -0700219}
220
Jingwen Chen341f7352022-01-11 05:42:49 +0000221var stubLibraryCompilerFlags = []string{
222 // We're knowingly doing some otherwise unsightly things with builtin
223 // functions here. We're just generating stub libraries, so ignore it.
224 "-Wno-incompatible-library-redeclaration",
225 "-Wno-incomplete-setjmp-declaration",
226 "-Wno-builtin-requires-header",
227 "-Wno-invalid-noreturn",
228 "-Wall",
229 "-Werror",
230 // These libraries aren't actually used. Don't worry about unwinding
231 // (avoids the need to link an unwinder into a fake library).
232 "-fno-unwind-tables",
233}
234
235func init() {
Cole Faust8982b1c2024-04-08 16:54:45 -0700236 pctx.StaticVariable("StubLibraryCompilerFlags", strings.Join(stubLibraryCompilerFlags, " "))
Jingwen Chen341f7352022-01-11 05:42:49 +0000237}
238
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000239func AddStubLibraryCompilerFlags(flags Flags) Flags {
Jingwen Chen341f7352022-01-11 05:42:49 +0000240 flags.Global.CFlags = append(flags.Global.CFlags, stubLibraryCompilerFlags...)
Jiyong Park48d75ef2019-11-21 15:11:49 +0900241 // All symbols in the stubs library should be visible.
242 if inList("-fvisibility=hidden", flags.Local.CFlags) {
243 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
244 }
George Burgess IVf5310e32017-07-19 11:39:53 -0700245 return flags
246}
247
Colin Crossf18e1102017-11-16 14:33:08 -0800248func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
249 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000250 return AddStubLibraryCompilerFlags(flags)
George Burgess IVf5310e32017-07-19 11:39:53 -0700251}
252
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000253type NdkApiOutputs struct {
254 StubSrc android.ModuleGenPath
255 VersionScript android.ModuleGenPath
Dan Albertf1d14c72020-07-30 14:32:55 -0700256 symbolList android.ModuleGenPath
257}
258
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000259func ParseNativeAbiDefinition(ctx android.ModuleContext, symbolFile string,
260 apiLevel android.ApiLevel, genstubFlags string) NdkApiOutputs {
Dan Albert914449f2016-06-17 16:45:24 -0700261
Dan Willemsenb916b802017-03-19 13:44:32 -0700262 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
263 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
264 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albertf1d14c72020-07-30 14:32:55 -0700265 symbolListPath := android.PathForModuleGen(ctx, "abi_symbol_list.txt")
Dan Albert49927d22017-03-28 15:00:46 -0700266 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700267 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700268 Rule: genStubSrc,
269 Description: "generate stubs " + symbolFilePath.Rel(),
Dan Albertf1d14c72020-07-30 14:32:55 -0700270 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath,
271 symbolListPath},
272 Input: symbolFilePath,
273 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700274 Args: map[string]string{
Dan Albertf1d14c72020-07-30 14:32:55 -0700275 "arch": ctx.Arch().ArchType.String(),
276 "apiLevel": apiLevel.String(),
Dan Albert49927d22017-03-28 15:00:46 -0700277 "apiMap": apiLevelsJson.String(),
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900278 "flags": genstubFlags,
Dan Albert914449f2016-06-17 16:45:24 -0700279 },
280 })
281
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000282 return NdkApiOutputs{
283 StubSrc: stubSrcPath,
284 VersionScript: versionScriptPath,
Dan Albertf1d14c72020-07-30 14:32:55 -0700285 symbolList: symbolListPath,
286 }
287}
288
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000289func CompileStubLibrary(ctx android.ModuleContext, flags Flags, src android.Path, sharedFlags *SharedFlags) Objects {
Mitch Phillips4e5f9a12022-04-29 13:12:28 -0700290 // libc/libm stubs libraries end up mismatching with clang's internal definition of these
291 // functions (which have noreturn attributes and other things). Because we just want to create a
292 // stub with symbol definitions, and types aren't important in C, ignore the mismatch.
293 flags.Local.ConlyFlags = append(flags.Local.ConlyFlags, "-fno-builtin")
Dan Albertf1d14c72020-07-30 14:32:55 -0700294 return compileObjs(ctx, flagsToBuilderFlags(flags), "",
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000295 android.Paths{src}, nil, nil, nil, nil, sharedFlags)
Dan Willemsenb916b802017-03-19 13:44:32 -0700296}
297
Dan Albertf1d14c72020-07-30 14:32:55 -0700298func (this *stubDecorator) findImplementationLibrary(ctx ModuleContext) android.Path {
Yu Liu4f825132024-12-18 00:35:39 +0000299 dep := ctx.GetDirectDepProxyWithTag(strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix),
Dan Albertf1d14c72020-07-30 14:32:55 -0700300 stubImplementation)
301 if dep == nil {
Yu Liu4f825132024-12-18 00:35:39 +0000302 ctx.ModuleErrorf("Could not find implementation for stub: ")
Dan Albertf1d14c72020-07-30 14:32:55 -0700303 return nil
304 }
Yu Liu8024b922024-12-20 23:31:32 +0000305 if _, ok := android.OtherModuleProvider(ctx, *dep, CcInfoProvider); !ok {
Dan Albertf1d14c72020-07-30 14:32:55 -0700306 ctx.ModuleErrorf("Implementation for stub is not correct module type")
Alan Stokes73d32452022-11-01 14:05:08 +0000307 return nil
Dan Albertf1d14c72020-07-30 14:32:55 -0700308 }
Yu Liu8024b922024-12-20 23:31:32 +0000309 output := android.OtherModuleProviderOrDefault(ctx, *dep, LinkableInfoProvider).UnstrippedOutputFile
Dan Albertf1d14c72020-07-30 14:32:55 -0700310 if output == nil {
Yu Liu4f825132024-12-18 00:35:39 +0000311 ctx.ModuleErrorf("implementation module (%s) has no output", *dep)
Dan Albertf1d14c72020-07-30 14:32:55 -0700312 return nil
313 }
314
315 return output
316}
317
318func (this *stubDecorator) libraryName(ctx ModuleContext) string {
319 return strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix)
320}
321
322func (this *stubDecorator) findPrebuiltAbiDump(ctx ModuleContext,
323 apiLevel android.ApiLevel) android.OptionalPath {
324
325 subpath := filepath.Join("prebuilts/abi-dumps/ndk", apiLevel.String(),
Matthias Maennichca8ae652023-06-30 21:52:17 +0100326 ctx.Arch().ArchType.String(), this.libraryName(ctx), "abi.stg")
Dan Albertf1d14c72020-07-30 14:32:55 -0700327 return android.ExistentPathForSource(ctx, subpath)
328}
329
Dan Albert499c92e2024-10-31 22:15:40 +0000330func (this *stubDecorator) builtAbiDumpLocation(ctx ModuleContext, apiLevel android.ApiLevel) android.OutputPath {
331 return getNdkAbiDumpInstallBase(ctx).Join(ctx,
332 apiLevel.String(), ctx.Arch().ArchType.String(),
333 this.libraryName(ctx), "abi.stg")
334}
335
Dan Albertf1d14c72020-07-30 14:32:55 -0700336// Feature flag.
Dan Albert2d0dce12024-09-18 19:34:32 +0000337func (this *stubDecorator) canDumpAbi(ctx ModuleContext) bool {
Dan Albertf71006a2022-04-14 23:08:51 +0000338 if runtime.GOOS == "darwin" {
339 return false
340 }
Dan Albert2d0dce12024-09-18 19:34:32 +0000341 if strings.HasPrefix(ctx.ModuleDir(), "bionic/") {
Dan Albertfbe57802024-08-09 20:39:39 +0000342 // Bionic has enough uncommon implementation details like ifuncs and asm
343 // code that the ABI tracking here has a ton of false positives. That's
344 // causing pretty extreme friction for development there, so disabling
345 // it until the workflow can be improved.
346 //
347 // http://b/358653811
348 return false
349 }
Dan Albert2d0dce12024-09-18 19:34:32 +0000350
Dan Albert326ab242023-04-20 17:38:29 +0000351 // http://b/156513478
Dan Albert2d0dce12024-09-18 19:34:32 +0000352 return ctx.Config().ReleaseNdkAbiMonitored()
Dan Albertf1d14c72020-07-30 14:32:55 -0700353}
354
355// Feature flag to disable diffing against prebuilts.
Dan Albert499c92e2024-10-31 22:15:40 +0000356func (this *stubDecorator) canDiffAbi(config android.Config) bool {
357 if this.apiLevel.IsCurrent() {
358 // Diffs are performed from this to next, and there's nothing after
359 // current.
360 return false
361 }
362
Aleksei Vetrov146e9822023-11-24 19:54:26 +0000363 return config.ReleaseNdkAbiMonitored()
Dan Albertf1d14c72020-07-30 14:32:55 -0700364}
365
Matthias Maennich55486f82023-07-03 12:25:27 +0100366func (this *stubDecorator) dumpAbi(ctx ModuleContext, symbolList android.Path) {
367 implementationLibrary := this.findImplementationLibrary(ctx)
Dan Albert499c92e2024-10-31 22:15:40 +0000368 this.abiDumpPath = this.builtAbiDumpLocation(ctx, this.apiLevel)
Dan Albert2d0dce12024-09-18 19:34:32 +0000369 this.hasAbiDump = true
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000370 headersList := getNdkABIHeadersFile(ctx)
Matthias Maennich55486f82023-07-03 12:25:27 +0100371 ctx.Build(pctx, android.BuildParams{
372 Rule: stg,
373 Description: fmt.Sprintf("stg %s", implementationLibrary),
374 Input: implementationLibrary,
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000375 Implicits: []android.Path{
376 symbolList,
377 headersList,
378 },
379 Output: this.abiDumpPath,
Matthias Maennich55486f82023-07-03 12:25:27 +0100380 Args: map[string]string{
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000381 "symbolList": symbolList.String(),
382 "headersList": headersList.String(),
Matthias Maennich55486f82023-07-03 12:25:27 +0100383 },
384 })
385}
386
Dan Albertf1d14c72020-07-30 14:32:55 -0700387func findNextApiLevel(ctx ModuleContext, apiLevel android.ApiLevel) *android.ApiLevel {
Dan Albert499c92e2024-10-31 22:15:40 +0000388 apiLevels := append(ctx.Config().FinalApiLevels(),
Dan Albertf1d14c72020-07-30 14:32:55 -0700389 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.
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100401 abiDiffPath := android.PathForModuleOut(ctx, "stgdiff.timestamp")
Dan Albertf1d14c72020-07-30 14:32:55 -0700402 prebuiltAbiDump := this.findPrebuiltAbiDump(ctx, this.apiLevel)
Aleksei Vetrov9c8c5ab2023-09-20 13:09:25 +0000403 missingPrebuiltErrorTemplate :=
404 "Did not find prebuilt ABI dump for %q (%q). Generate with " +
405 "//development/tools/ndk/update_ndk_abi.sh."
Dan Albertf7cb5632022-11-29 17:20:16 +0000406 missingPrebuiltError := fmt.Sprintf(
Aleksei Vetrov9c8c5ab2023-09-20 13:09:25 +0000407 missingPrebuiltErrorTemplate, this.libraryName(ctx),
Dan Albertf7cb5632022-11-29 17:20:16 +0000408 prebuiltAbiDump.InvalidReason())
Dan Albertf1d14c72020-07-30 14:32:55 -0700409 if !prebuiltAbiDump.Valid() {
410 ctx.Build(pctx, android.BuildParams{
411 Rule: android.ErrorRule,
412 Output: abiDiffPath,
413 Args: map[string]string{
414 "error": missingPrebuiltError,
415 },
416 })
417 } else {
418 ctx.Build(pctx, android.BuildParams{
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100419 Rule: stgdiff,
420 Description: fmt.Sprintf("Comparing ABI %s %s", prebuiltAbiDump,
Dan Albertf1d14c72020-07-30 14:32:55 -0700421 this.abiDumpPath),
422 Output: abiDiffPath,
423 Inputs: android.Paths{prebuiltAbiDump.Path(), this.abiDumpPath},
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100424 Args: map[string]string{
425 "args": "--format=small",
426 },
Dan Albertf1d14c72020-07-30 14:32:55 -0700427 })
428 }
429 this.abiDiffPaths = append(this.abiDiffPaths, abiDiffPath)
430
431 // Also ensure that the ABI of the next API level (if there is one) matches
432 // this API level. *New* ABI is allowed, but any changes to APIs that exist
433 // in this API level are disallowed.
Dan Albert9c8e0c42024-09-18 20:45:44 +0000434 if prebuiltAbiDump.Valid() {
Dan Albertf1d14c72020-07-30 14:32:55 -0700435 nextApiLevel := findNextApiLevel(ctx, this.apiLevel)
436 if nextApiLevel == nil {
437 panic(fmt.Errorf("could not determine which API level follows "+
438 "non-current API level %s", this.apiLevel))
439 }
Dan Albert9c8e0c42024-09-18 20:45:44 +0000440
Dan Albert53527762024-11-01 19:15:27 +0000441 // Preview ABI levels are not recorded in prebuilts. ABI compatibility
Dan Albert499c92e2024-10-31 22:15:40 +0000442 // for preview APIs is still monitored via "current" so we have early
443 // warning rather than learning about an ABI break during finalization,
444 // but is only checked against the "current" API dumps in the out
445 // directory.
446 nextAbiDiffPath := android.PathForModuleOut(ctx,
447 "abidiff_next.timestamp")
448
449 var nextAbiDump android.OptionalPath
450 if nextApiLevel.IsCurrent() {
451 nextAbiDump = android.OptionalPathForPath(
452 this.builtAbiDumpLocation(ctx, *nextApiLevel),
453 )
454 } else {
455 nextAbiDump = this.findPrebuiltAbiDump(ctx, *nextApiLevel)
456 }
457
458 if !nextAbiDump.Valid() {
Dan Albert9c8e0c42024-09-18 20:45:44 +0000459 missingNextPrebuiltError := fmt.Sprintf(
460 missingPrebuiltErrorTemplate, this.libraryName(ctx),
461 nextAbiDump.InvalidReason())
Dan Albert499c92e2024-10-31 22:15:40 +0000462 ctx.Build(pctx, android.BuildParams{
463 Rule: android.ErrorRule,
464 Output: nextAbiDiffPath,
465 Args: map[string]string{
466 "error": missingNextPrebuiltError,
467 },
468 })
469 } else {
470 ctx.Build(pctx, android.BuildParams{
471 Rule: stgdiff,
472 Description: fmt.Sprintf(
473 "Comparing ABI to the next API level %s %s",
474 prebuiltAbiDump, nextAbiDump),
475 Output: nextAbiDiffPath,
476 Inputs: android.Paths{
477 prebuiltAbiDump.Path(), nextAbiDump.Path()},
478 Args: map[string]string{
479 "args": "--format=small --ignore=interface_addition",
480 },
481 })
Dan Albertf1d14c72020-07-30 14:32:55 -0700482 }
Dan Albert499c92e2024-10-31 22:15:40 +0000483 this.abiDiffPaths = append(this.abiDiffPaths, nextAbiDiffPath)
Dan Albertf1d14c72020-07-30 14:32:55 -0700484 }
485}
486
Dan Willemsenb916b802017-03-19 13:44:32 -0700487func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800488 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700489 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
490 }
491
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000492 if !c.BuildStubs() {
Colin Cross5ec407b2020-09-30 11:41:33 -0700493 // NDK libraries have no implementation variant, nothing to do
494 return Objects{}
495 }
496
Dan Albert1a246272020-07-06 14:49:35 -0700497 if !c.initializeProperties(ctx) {
498 // Emits its own errors, so we don't need to.
499 return Objects{}
500 }
501
sophiez58cabb72020-05-29 13:37:12 -0700502 symbolFile := String(c.properties.Symbol_file)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000503 nativeAbiResult := ParseNativeAbiDefinition(ctx, symbolFile, c.apiLevel, "")
504 objs := CompileStubLibrary(ctx, flags, nativeAbiResult.StubSrc, ctx.getSharedFlags())
505 c.versionScriptPath = nativeAbiResult.VersionScript
Dan Albert2d0dce12024-09-18 19:34:32 +0000506 if c.canDumpAbi(ctx) {
Matthias Maenniche914f2d2023-07-03 12:29:27 +0100507 c.dumpAbi(ctx, nativeAbiResult.symbolList)
Dan Albert499c92e2024-10-31 22:15:40 +0000508 if c.canDiffAbi(ctx.Config()) {
Dan Albertf1d14c72020-07-30 14:32:55 -0700509 c.diffAbi(ctx)
510 }
511 }
Dan Albert1a246272020-07-06 14:49:35 -0700512 if c.apiLevel.IsCurrent() && ctx.PrimaryArch() {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000513 c.parsedCoverageXmlPath = ParseSymbolFileForAPICoverage(ctx, symbolFile)
sophiez58cabb72020-05-29 13:37:12 -0700514 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700515 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700516}
517
Spandan Das73bcafc2022-08-18 23:26:00 +0000518// Add a dependency on the header modules of this ndk_library
Colin Cross37047f12016-12-13 17:06:13 -0800519func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Spandan Das73bcafc2022-08-18 23:26:00 +0000520 return Deps{
Steven Morelandbedc9972024-09-03 21:46:14 +0000521 ReexportHeaderLibHeaders: linker.properties.Export_header_libs,
522 HeaderLibs: linker.properties.Export_header_libs,
Spandan Das73bcafc2022-08-18 23:26:00 +0000523 }
Dan Albert914449f2016-06-17 16:45:24 -0700524}
525
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800526func (linker *stubDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
527 linker.libraryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
528 // Overwrites the SubName computed by libraryDecorator
529 moduleInfoJSON.SubName = ndkLibrarySuffix + "." + linker.apiLevel.String()
530}
531
Dan Willemsen01a90592017-04-07 15:21:13 -0700532func (linker *stubDecorator) Name(name string) string {
533 return name + ndkLibrarySuffix
534}
535
Colin Crossb916a382016-07-29 17:28:03 -0700536func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700537 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700538 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700539}
540
Colin Crossb916a382016-07-29 17:28:03 -0700541func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700542 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700543
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000544 if !stub.BuildStubs() {
Colin Cross5ec407b2020-09-30 11:41:33 -0700545 // NDK libraries have no implementation variant, nothing to do
546 return nil
547 }
548
Dan Albert1a246272020-07-06 14:49:35 -0700549 if shouldUseVersionScript(ctx, stub) {
Dan Albert98dbb3b2017-01-03 15:16:29 -0800550 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800551 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700552 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800553 }
554
Colin Cross5ec407b2020-09-30 11:41:33 -0700555 stub.libraryDecorator.skipAPIDefine = true
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700556 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700557}
558
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700559func (stub *stubDecorator) nativeCoverage() bool {
560 return false
561}
562
Dan Albert4048bb02023-04-03 20:19:07 +0000563// Returns the install path for unversioned NDK libraries (currently only static
564// libraries).
Spandan Dasf280b232024-04-04 21:25:51 +0000565func getUnversionedLibraryInstallPath(ctx ModuleContext) android.OutputPath {
Dan Albert4048bb02023-04-03 20:19:07 +0000566 return getNdkSysrootBase(ctx).Join(ctx, "usr/lib", config.NDKTriple(ctx.toolchain()))
567}
Rebecca Chyung961cf1c2023-04-03 05:17:17 +0000568
Dan Albert4048bb02023-04-03 20:19:07 +0000569// Returns the install path for versioned NDK libraries. These are most often
570// stubs, but the same paths are used for CRT objects.
Spandan Dasf280b232024-04-04 21:25:51 +0000571func getVersionedLibraryInstallPath(ctx ModuleContext, apiLevel android.ApiLevel) android.OutputPath {
Dan Albert4048bb02023-04-03 20:19:07 +0000572 return getUnversionedLibraryInstallPath(ctx).Join(ctx, apiLevel.String())
573}
574
575func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
576 installDir := getVersionedLibraryInstallPath(ctx, stub.apiLevel)
Spandan Dasf280b232024-04-04 21:25:51 +0000577 out := installDir.Join(ctx, path.Base())
578 ctx.Build(pctx, android.BuildParams{
579 Rule: android.Cp,
580 Input: path,
581 Output: out,
582 })
583 stub.installPath = out
Dan Albert914449f2016-06-17 16:45:24 -0700584}
585
Colin Cross36242852017-06-23 15:06:31 -0700586func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800587 module, library := NewLibrary(android.DeviceSupported)
588 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700589 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700590 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200591 library.disableStripping()
Dan Albert914449f2016-06-17 16:45:24 -0700592
Colin Crossb916a382016-07-29 17:28:03 -0700593 stub := &stubDecorator{
594 libraryDecorator: library,
595 }
596 module.compiler = stub
597 module.linker = stub
598 module.installer = stub
Colin Cross31076b32020-10-23 17:22:06 -0700599 module.library = stub
Dan Albert914449f2016-06-17 16:45:24 -0700600
Colin Crossc511bc52020-04-07 16:50:32 +0000601 module.Properties.AlwaysSdk = true
602 module.Properties.Sdk_version = StringPtr("current")
603
Colin Cross36242852017-06-23 15:06:31 -0700604 module.AddProperties(&stub.properties, &library.MutatedProperties)
605
606 return module
Dan Albert914449f2016-06-17 16:45:24 -0700607}
608
Dan Albertf740ed02020-07-24 14:19:06 -0700609// ndk_library creates a library that exposes a stub implementation of functions
610// and variables for use at build time only.
Jooyung Hanb90e4912019-12-09 18:21:48 +0900611func NdkLibraryFactory() android.Module {
Colin Cross36242852017-06-23 15:06:31 -0700612 module := newStubLibrary()
613 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
614 return module
Dan Albert914449f2016-06-17 16:45:24 -0700615}