blob: b822e5cbb7151bbd42008af3d4e608498d35d925 [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
Spandan Das73bcafc2022-08-18 23:26:00 +0000106 // Headers presented by this library to the Public API Surface
107 Export_header_libs []string
Dan Albert914449f2016-06-17 16:45:24 -0700108}
109
Colin Crossb916a382016-07-29 17:28:03 -0700110type stubDecorator struct {
111 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -0700112
113 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -0700114
sophiez58cabb72020-05-29 13:37:12 -0700115 versionScriptPath android.ModuleGenPath
116 parsedCoverageXmlPath android.ModuleOutPath
117 installPath android.Path
Dan Albertf1d14c72020-07-30 14:32:55 -0700118 abiDumpPath android.OutputPath
119 abiDiffPaths android.Paths
Dan Albert1a246272020-07-06 14:49:35 -0700120
121 apiLevel android.ApiLevel
122 firstVersion android.ApiLevel
123 unversionedUntil android.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700124}
125
Colin Cross0477b422020-10-13 18:43:54 -0700126var _ versionedInterface = (*stubDecorator)(nil)
127
Dan Albert1a246272020-07-06 14:49:35 -0700128func shouldUseVersionScript(ctx BaseModuleContext, stub *stubDecorator) bool {
129 return stub.apiLevel.GreaterThanOrEqualTo(stub.unversionedUntil)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800130}
131
Colin Cross0477b422020-10-13 18:43:54 -0700132func (stub *stubDecorator) implementationModuleName(name string) string {
133 return strings.TrimSuffix(name, ndkLibrarySuffix)
134}
135
Colin Crossadd04a82024-05-22 09:57:59 -0700136func ndkLibraryVersions(ctx android.BaseModuleContext, from android.ApiLevel) []string {
Dan Albert1a246272020-07-06 14:49:35 -0700137 var versions []android.ApiLevel
138 versionStrs := []string{}
139 for _, version := range ctx.Config().AllSupportedApiLevels() {
140 if version.GreaterThanOrEqualTo(from) {
141 versions = append(versions, version)
142 versionStrs = append(versionStrs, version.String())
143 }
Dan Albert914449f2016-06-17 16:45:24 -0700144 }
Dan Albert0b176c82020-07-23 16:43:25 -0700145 versionStrs = append(versionStrs, android.FutureApiLevel.String())
Dan Albert914449f2016-06-17 16:45:24 -0700146
Colin Cross5ec407b2020-09-30 11:41:33 -0700147 return versionStrs
148}
149
Colin Crossadd04a82024-05-22 09:57:59 -0700150func (this *stubDecorator) stubsVersions(ctx android.BaseModuleContext) []string {
Cole Fausta963b942024-04-11 17:43:00 -0700151 if !ctx.Module().Enabled(ctx) {
Colin Cross3572cf72020-10-01 15:58:11 -0700152 return nil
153 }
Dan Albertf1d14c72020-07-30 14:32:55 -0700154 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
155 ctx.Module().Disable()
156 return nil
157 }
Colin Cross3572cf72020-10-01 15:58:11 -0700158 firstVersion, err := nativeApiLevelFromUser(ctx,
159 String(this.properties.First_version))
160 if err != nil {
161 ctx.PropertyErrorf("first_version", err.Error())
162 return nil
163 }
164 return ndkLibraryVersions(ctx, firstVersion)
165}
166
Dan Albert1a246272020-07-06 14:49:35 -0700167func (this *stubDecorator) initializeProperties(ctx BaseModuleContext) bool {
Colin Cross5ec407b2020-09-30 11:41:33 -0700168 this.apiLevel = nativeApiLevelOrPanic(ctx, this.stubsVersion())
Dan Albert1a246272020-07-06 14:49:35 -0700169
170 var err error
171 this.firstVersion, err = nativeApiLevelFromUser(ctx,
172 String(this.properties.First_version))
173 if err != nil {
174 ctx.PropertyErrorf("first_version", err.Error())
175 return false
176 }
177
Jiyong Parkee9b1172021-04-06 17:40:32 +0900178 str := proptools.StringDefault(this.properties.Unversioned_until, "minimum")
179 this.unversionedUntil, err = nativeApiLevelFromUser(ctx, str)
Dan Albert1a246272020-07-06 14:49:35 -0700180 if err != nil {
181 ctx.PropertyErrorf("unversioned_until", err.Error())
182 return false
183 }
184
185 return true
186}
187
Colin Cross95f1ca02020-10-29 20:47:22 -0700188func getNDKKnownLibs(config android.Config) *[]string {
189 return config.Once(ndkKnownLibsKey, func() interface{} {
190 return &[]string{}
191 }).(*[]string)
192}
193
Colin Crossb916a382016-07-29 17:28:03 -0700194func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700195 c.baseCompiler.compilerInit(ctx)
196
Dan Willemsen01a90592017-04-07 15:21:13 -0700197 name := ctx.baseModuleName()
198 if strings.HasSuffix(name, ndkLibrarySuffix) {
199 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
200 }
201
Dan Albertde5aade2020-06-30 12:32:51 -0700202 ndkKnownLibsLock.Lock()
203 defer ndkKnownLibsLock.Unlock()
Colin Cross95f1ca02020-10-29 20:47:22 -0700204 ndkKnownLibs := getNDKKnownLibs(ctx.Config())
205 for _, lib := range *ndkKnownLibs {
Dan Albert7e9d2952016-08-04 13:02:36 -0700206 if lib == name {
207 return
208 }
209 }
Colin Cross95f1ca02020-10-29 20:47:22 -0700210 *ndkKnownLibs = append(*ndkKnownLibs, name)
Dan Albert7e9d2952016-08-04 13:02:36 -0700211}
212
Jingwen Chen341f7352022-01-11 05:42:49 +0000213var stubLibraryCompilerFlags = []string{
214 // We're knowingly doing some otherwise unsightly things with builtin
215 // functions here. We're just generating stub libraries, so ignore it.
216 "-Wno-incompatible-library-redeclaration",
217 "-Wno-incomplete-setjmp-declaration",
218 "-Wno-builtin-requires-header",
219 "-Wno-invalid-noreturn",
220 "-Wall",
221 "-Werror",
222 // These libraries aren't actually used. Don't worry about unwinding
223 // (avoids the need to link an unwinder into a fake library).
224 "-fno-unwind-tables",
225}
226
227func init() {
Cole Faust8982b1c2024-04-08 16:54:45 -0700228 pctx.StaticVariable("StubLibraryCompilerFlags", strings.Join(stubLibraryCompilerFlags, " "))
Jingwen Chen341f7352022-01-11 05:42:49 +0000229}
230
George Burgess IVf5310e32017-07-19 11:39:53 -0700231func addStubLibraryCompilerFlags(flags Flags) Flags {
Jingwen Chen341f7352022-01-11 05:42:49 +0000232 flags.Global.CFlags = append(flags.Global.CFlags, stubLibraryCompilerFlags...)
Jiyong Park48d75ef2019-11-21 15:11:49 +0900233 // All symbols in the stubs library should be visible.
234 if inList("-fvisibility=hidden", flags.Local.CFlags) {
235 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
236 }
George Burgess IVf5310e32017-07-19 11:39:53 -0700237 return flags
238}
239
Colin Crossf18e1102017-11-16 14:33:08 -0800240func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
241 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700242 return addStubLibraryCompilerFlags(flags)
243}
244
Dan Albertf1d14c72020-07-30 14:32:55 -0700245type ndkApiOutputs struct {
246 stubSrc android.ModuleGenPath
247 versionScript android.ModuleGenPath
248 symbolList android.ModuleGenPath
249}
250
251func parseNativeAbiDefinition(ctx ModuleContext, symbolFile string,
252 apiLevel android.ApiLevel, genstubFlags string) ndkApiOutputs {
Dan Albert914449f2016-06-17 16:45:24 -0700253
Dan Willemsenb916b802017-03-19 13:44:32 -0700254 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
255 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
256 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albertf1d14c72020-07-30 14:32:55 -0700257 symbolListPath := android.PathForModuleGen(ctx, "abi_symbol_list.txt")
Dan Albert49927d22017-03-28 15:00:46 -0700258 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700259 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700260 Rule: genStubSrc,
261 Description: "generate stubs " + symbolFilePath.Rel(),
Dan Albertf1d14c72020-07-30 14:32:55 -0700262 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath,
263 symbolListPath},
264 Input: symbolFilePath,
265 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700266 Args: map[string]string{
Dan Albertf1d14c72020-07-30 14:32:55 -0700267 "arch": ctx.Arch().ArchType.String(),
268 "apiLevel": apiLevel.String(),
Dan Albert49927d22017-03-28 15:00:46 -0700269 "apiMap": apiLevelsJson.String(),
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900270 "flags": genstubFlags,
Dan Albert914449f2016-06-17 16:45:24 -0700271 },
272 })
273
Dan Albertf1d14c72020-07-30 14:32:55 -0700274 return ndkApiOutputs{
275 stubSrc: stubSrcPath,
276 versionScript: versionScriptPath,
277 symbolList: symbolListPath,
278 }
279}
280
281func compileStubLibrary(ctx ModuleContext, flags Flags, src android.Path) Objects {
Mitch Phillips4e5f9a12022-04-29 13:12:28 -0700282 // libc/libm stubs libraries end up mismatching with clang's internal definition of these
283 // functions (which have noreturn attributes and other things). Because we just want to create a
284 // stub with symbol definitions, and types aren't important in C, ignore the mismatch.
285 flags.Local.ConlyFlags = append(flags.Local.ConlyFlags, "-fno-builtin")
Dan Albertf1d14c72020-07-30 14:32:55 -0700286 return compileObjs(ctx, flagsToBuilderFlags(flags), "",
Chih-Hung Hsieh9db8a0c2022-02-17 12:54:45 -0800287 android.Paths{src}, nil, nil, nil, nil)
Dan Willemsenb916b802017-03-19 13:44:32 -0700288}
289
Dan Albertf1d14c72020-07-30 14:32:55 -0700290func (this *stubDecorator) findImplementationLibrary(ctx ModuleContext) android.Path {
291 dep := ctx.GetDirectDepWithTag(strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix),
292 stubImplementation)
293 if dep == nil {
294 ctx.ModuleErrorf("Could not find implementation for stub")
295 return nil
296 }
297 impl, ok := dep.(*Module)
298 if !ok {
299 ctx.ModuleErrorf("Implementation for stub is not correct module type")
Alan Stokes73d32452022-11-01 14:05:08 +0000300 return nil
Dan Albertf1d14c72020-07-30 14:32:55 -0700301 }
302 output := impl.UnstrippedOutputFile()
303 if output == nil {
304 ctx.ModuleErrorf("implementation module (%s) has no output", impl)
305 return nil
306 }
307
308 return output
309}
310
311func (this *stubDecorator) libraryName(ctx ModuleContext) string {
312 return strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix)
313}
314
315func (this *stubDecorator) findPrebuiltAbiDump(ctx ModuleContext,
316 apiLevel android.ApiLevel) android.OptionalPath {
317
318 subpath := filepath.Join("prebuilts/abi-dumps/ndk", apiLevel.String(),
Matthias Maennichca8ae652023-06-30 21:52:17 +0100319 ctx.Arch().ArchType.String(), this.libraryName(ctx), "abi.stg")
Dan Albertf1d14c72020-07-30 14:32:55 -0700320 return android.ExistentPathForSource(ctx, subpath)
321}
322
323// Feature flag.
Dan Albertf71006a2022-04-14 23:08:51 +0000324func canDumpAbi(config android.Config) bool {
325 if runtime.GOOS == "darwin" {
326 return false
327 }
Dan Albert326ab242023-04-20 17:38:29 +0000328 // http://b/156513478
Aleksei Vetrov146e9822023-11-24 19:54:26 +0000329 return config.ReleaseNdkAbiMonitored()
Dan Albertf1d14c72020-07-30 14:32:55 -0700330}
331
332// Feature flag to disable diffing against prebuilts.
Aleksei Vetrov146e9822023-11-24 19:54:26 +0000333func canDiffAbi(config android.Config) bool {
334 return config.ReleaseNdkAbiMonitored()
Dan Albertf1d14c72020-07-30 14:32:55 -0700335}
336
Matthias Maennich55486f82023-07-03 12:25:27 +0100337func (this *stubDecorator) dumpAbi(ctx ModuleContext, symbolList android.Path) {
338 implementationLibrary := this.findImplementationLibrary(ctx)
339 this.abiDumpPath = getNdkAbiDumpInstallBase(ctx).Join(ctx,
340 this.apiLevel.String(), ctx.Arch().ArchType.String(),
341 this.libraryName(ctx), "abi.stg")
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000342 headersList := getNdkABIHeadersFile(ctx)
Matthias Maennich55486f82023-07-03 12:25:27 +0100343 ctx.Build(pctx, android.BuildParams{
344 Rule: stg,
345 Description: fmt.Sprintf("stg %s", implementationLibrary),
346 Input: implementationLibrary,
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000347 Implicits: []android.Path{
348 symbolList,
349 headersList,
350 },
351 Output: this.abiDumpPath,
Matthias Maennich55486f82023-07-03 12:25:27 +0100352 Args: map[string]string{
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000353 "symbolList": symbolList.String(),
354 "headersList": headersList.String(),
Matthias Maennich55486f82023-07-03 12:25:27 +0100355 },
356 })
357}
358
Dan Albertf1d14c72020-07-30 14:32:55 -0700359func findNextApiLevel(ctx ModuleContext, apiLevel android.ApiLevel) *android.ApiLevel {
360 apiLevels := append(ctx.Config().AllSupportedApiLevels(),
361 android.FutureApiLevel)
362 for _, api := range apiLevels {
363 if api.GreaterThan(apiLevel) {
364 return &api
365 }
366 }
367 return nil
368}
369
370func (this *stubDecorator) diffAbi(ctx ModuleContext) {
Dan Albertf1d14c72020-07-30 14:32:55 -0700371 // Catch any ABI changes compared to the checked-in definition of this API
372 // level.
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100373 abiDiffPath := android.PathForModuleOut(ctx, "stgdiff.timestamp")
Dan Albertf1d14c72020-07-30 14:32:55 -0700374 prebuiltAbiDump := this.findPrebuiltAbiDump(ctx, this.apiLevel)
Aleksei Vetrov9c8c5ab2023-09-20 13:09:25 +0000375 missingPrebuiltErrorTemplate :=
376 "Did not find prebuilt ABI dump for %q (%q). Generate with " +
377 "//development/tools/ndk/update_ndk_abi.sh."
Dan Albertf7cb5632022-11-29 17:20:16 +0000378 missingPrebuiltError := fmt.Sprintf(
Aleksei Vetrov9c8c5ab2023-09-20 13:09:25 +0000379 missingPrebuiltErrorTemplate, this.libraryName(ctx),
Dan Albertf7cb5632022-11-29 17:20:16 +0000380 prebuiltAbiDump.InvalidReason())
Dan Albertf1d14c72020-07-30 14:32:55 -0700381 if !prebuiltAbiDump.Valid() {
382 ctx.Build(pctx, android.BuildParams{
383 Rule: android.ErrorRule,
384 Output: abiDiffPath,
385 Args: map[string]string{
386 "error": missingPrebuiltError,
387 },
388 })
389 } else {
390 ctx.Build(pctx, android.BuildParams{
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100391 Rule: stgdiff,
392 Description: fmt.Sprintf("Comparing ABI %s %s", prebuiltAbiDump,
Dan Albertf1d14c72020-07-30 14:32:55 -0700393 this.abiDumpPath),
394 Output: abiDiffPath,
395 Inputs: android.Paths{prebuiltAbiDump.Path(), this.abiDumpPath},
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100396 Args: map[string]string{
397 "args": "--format=small",
398 },
Dan Albertf1d14c72020-07-30 14:32:55 -0700399 })
400 }
401 this.abiDiffPaths = append(this.abiDiffPaths, abiDiffPath)
402
403 // Also ensure that the ABI of the next API level (if there is one) matches
404 // this API level. *New* ABI is allowed, but any changes to APIs that exist
405 // in this API level are disallowed.
Aleksei Vetrov8c02bbc2023-09-19 15:35:08 +0000406 if !this.apiLevel.IsCurrent() && prebuiltAbiDump.Valid() {
Dan Albertf1d14c72020-07-30 14:32:55 -0700407 nextApiLevel := findNextApiLevel(ctx, this.apiLevel)
408 if nextApiLevel == nil {
409 panic(fmt.Errorf("could not determine which API level follows "+
410 "non-current API level %s", this.apiLevel))
411 }
412 nextAbiDiffPath := android.PathForModuleOut(ctx,
413 "abidiff_next.timestamp")
414 nextAbiDump := this.findPrebuiltAbiDump(ctx, *nextApiLevel)
Aleksei Vetrov9c8c5ab2023-09-20 13:09:25 +0000415 missingNextPrebuiltError := fmt.Sprintf(
416 missingPrebuiltErrorTemplate, this.libraryName(ctx),
417 nextAbiDump.InvalidReason())
Dan Albertf1d14c72020-07-30 14:32:55 -0700418 if !nextAbiDump.Valid() {
419 ctx.Build(pctx, android.BuildParams{
420 Rule: android.ErrorRule,
421 Output: nextAbiDiffPath,
422 Args: map[string]string{
Aleksei Vetrov9c8c5ab2023-09-20 13:09:25 +0000423 "error": missingNextPrebuiltError,
Dan Albertf1d14c72020-07-30 14:32:55 -0700424 },
425 })
426 } else {
427 ctx.Build(pctx, android.BuildParams{
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100428 Rule: stgdiff,
Aleksei Vetrov8c02bbc2023-09-19 15:35:08 +0000429 Description: fmt.Sprintf(
430 "Comparing ABI to the next API level %s %s",
431 prebuiltAbiDump, nextAbiDump),
Dan Albertf1d14c72020-07-30 14:32:55 -0700432 Output: nextAbiDiffPath,
Aleksei Vetrov8c02bbc2023-09-19 15:35:08 +0000433 Inputs: android.Paths{
434 prebuiltAbiDump.Path(), nextAbiDump.Path()},
Dan Albertf1d14c72020-07-30 14:32:55 -0700435 Args: map[string]string{
Matthias Maennich658bb4d2023-06-30 23:28:15 +0100436 "args": "--format=small --ignore=interface_addition",
Dan Albertf1d14c72020-07-30 14:32:55 -0700437 },
438 })
439 }
440 this.abiDiffPaths = append(this.abiDiffPaths, nextAbiDiffPath)
441 }
442}
443
Dan Willemsenb916b802017-03-19 13:44:32 -0700444func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800445 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700446 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
447 }
448
Colin Cross5ec407b2020-09-30 11:41:33 -0700449 if !c.buildStubs() {
450 // NDK libraries have no implementation variant, nothing to do
451 return Objects{}
452 }
453
Dan Albert1a246272020-07-06 14:49:35 -0700454 if !c.initializeProperties(ctx) {
455 // Emits its own errors, so we don't need to.
456 return Objects{}
457 }
458
sophiez58cabb72020-05-29 13:37:12 -0700459 symbolFile := String(c.properties.Symbol_file)
Dan Albertf1d14c72020-07-30 14:32:55 -0700460 nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile, c.apiLevel, "")
461 objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
462 c.versionScriptPath = nativeAbiResult.versionScript
Dan Albertf71006a2022-04-14 23:08:51 +0000463 if canDumpAbi(ctx.Config()) {
Matthias Maenniche914f2d2023-07-03 12:29:27 +0100464 c.dumpAbi(ctx, nativeAbiResult.symbolList)
Aleksei Vetrov146e9822023-11-24 19:54:26 +0000465 if canDiffAbi(ctx.Config()) {
Dan Albertf1d14c72020-07-30 14:32:55 -0700466 c.diffAbi(ctx)
467 }
468 }
Dan Albert1a246272020-07-06 14:49:35 -0700469 if c.apiLevel.IsCurrent() && ctx.PrimaryArch() {
sophiez4c4f8032021-08-16 22:54:00 -0700470 c.parsedCoverageXmlPath = parseSymbolFileForAPICoverage(ctx, symbolFile)
sophiez58cabb72020-05-29 13:37:12 -0700471 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700472 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700473}
474
Spandan Das73bcafc2022-08-18 23:26:00 +0000475// Add a dependency on the header modules of this ndk_library
Colin Cross37047f12016-12-13 17:06:13 -0800476func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Spandan Das73bcafc2022-08-18 23:26:00 +0000477 return Deps{
478 HeaderLibs: linker.properties.Export_header_libs,
479 }
Dan Albert914449f2016-06-17 16:45:24 -0700480}
481
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800482func (linker *stubDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
483 linker.libraryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
484 // Overwrites the SubName computed by libraryDecorator
485 moduleInfoJSON.SubName = ndkLibrarySuffix + "." + linker.apiLevel.String()
486}
487
Dan Willemsen01a90592017-04-07 15:21:13 -0700488func (linker *stubDecorator) Name(name string) string {
489 return name + ndkLibrarySuffix
490}
491
Colin Crossb916a382016-07-29 17:28:03 -0700492func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700493 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700494 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700495}
496
Colin Crossb916a382016-07-29 17:28:03 -0700497func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700498 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700499
Colin Cross5ec407b2020-09-30 11:41:33 -0700500 if !stub.buildStubs() {
501 // NDK libraries have no implementation variant, nothing to do
502 return nil
503 }
504
Dan Albert1a246272020-07-06 14:49:35 -0700505 if shouldUseVersionScript(ctx, stub) {
Dan Albert98dbb3b2017-01-03 15:16:29 -0800506 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800507 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700508 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800509 }
510
Colin Cross5ec407b2020-09-30 11:41:33 -0700511 stub.libraryDecorator.skipAPIDefine = true
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700512 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700513}
514
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700515func (stub *stubDecorator) nativeCoverage() bool {
516 return false
517}
518
Dan Albert4048bb02023-04-03 20:19:07 +0000519// Returns the install path for unversioned NDK libraries (currently only static
520// libraries).
Spandan Dasf280b232024-04-04 21:25:51 +0000521func getUnversionedLibraryInstallPath(ctx ModuleContext) android.OutputPath {
Dan Albert4048bb02023-04-03 20:19:07 +0000522 return getNdkSysrootBase(ctx).Join(ctx, "usr/lib", config.NDKTriple(ctx.toolchain()))
523}
Rebecca Chyung961cf1c2023-04-03 05:17:17 +0000524
Dan Albert4048bb02023-04-03 20:19:07 +0000525// Returns the install path for versioned NDK libraries. These are most often
526// stubs, but the same paths are used for CRT objects.
Spandan Dasf280b232024-04-04 21:25:51 +0000527func getVersionedLibraryInstallPath(ctx ModuleContext, apiLevel android.ApiLevel) android.OutputPath {
Dan Albert4048bb02023-04-03 20:19:07 +0000528 return getUnversionedLibraryInstallPath(ctx).Join(ctx, apiLevel.String())
529}
530
531func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
532 installDir := getVersionedLibraryInstallPath(ctx, stub.apiLevel)
Spandan Dasf280b232024-04-04 21:25:51 +0000533 out := installDir.Join(ctx, path.Base())
534 ctx.Build(pctx, android.BuildParams{
535 Rule: android.Cp,
536 Input: path,
537 Output: out,
538 })
539 stub.installPath = out
Dan Albert914449f2016-06-17 16:45:24 -0700540}
541
Colin Cross36242852017-06-23 15:06:31 -0700542func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800543 module, library := NewLibrary(android.DeviceSupported)
544 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700545 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700546 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200547 library.disableStripping()
Dan Albert914449f2016-06-17 16:45:24 -0700548
Colin Crossb916a382016-07-29 17:28:03 -0700549 stub := &stubDecorator{
550 libraryDecorator: library,
551 }
552 module.compiler = stub
553 module.linker = stub
554 module.installer = stub
Colin Cross31076b32020-10-23 17:22:06 -0700555 module.library = stub
Dan Albert914449f2016-06-17 16:45:24 -0700556
Colin Crossc511bc52020-04-07 16:50:32 +0000557 module.Properties.AlwaysSdk = true
558 module.Properties.Sdk_version = StringPtr("current")
559
Colin Cross36242852017-06-23 15:06:31 -0700560 module.AddProperties(&stub.properties, &library.MutatedProperties)
561
562 return module
Dan Albert914449f2016-06-17 16:45:24 -0700563}
564
Dan Albertf740ed02020-07-24 14:19:06 -0700565// ndk_library creates a library that exposes a stub implementation of functions
566// and variables for use at build time only.
Jooyung Hanb90e4912019-12-09 18:21:48 +0900567func NdkLibraryFactory() android.Module {
Colin Cross36242852017-06-23 15:06:31 -0700568 module := newStubLibrary()
569 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
570 return module
Dan Albert914449f2016-06-17 16:45:24 -0700571}