blob: a5c43fe219fed201eeb9a3452bdc426228b72f91 [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 Albert914449f2016-06-17 16:45:24 -070019 "strings"
Colin Crosse8a67a72016-08-07 21:17:54 -070020 "sync"
Dan Albert914449f2016-06-17 16:45:24 -070021
22 "github.com/google/blueprint"
23
24 "android/soong/android"
25)
26
sophiez58cabb72020-05-29 13:37:12 -070027func init() {
Dan Albert06f58af2020-06-22 15:10:31 -070028 pctx.HostBinToolVariable("ndkStubGenerator", "ndkstubgen")
sophiez58cabb72020-05-29 13:37:12 -070029 pctx.HostBinToolVariable("ndk_api_coverage_parser", "ndk_api_coverage_parser")
30}
31
Dan Albert914449f2016-06-17 16:45:24 -070032var (
Colin Cross9d45bb72016-08-29 16:14:13 -070033 genStubSrc = pctx.AndroidStaticRule("genStubSrc",
Dan Albert914449f2016-06-17 16:45:24 -070034 blueprint.RuleParams{
Dan Albert06f58af2020-06-22 15:10:31 -070035 Command: "$ndkStubGenerator --arch $arch --api $apiLevel " +
36 "--api-map $apiMap $flags $in $out",
37 CommandDeps: []string{"$ndkStubGenerator"},
Jiyong Park3fd0baf2018-12-07 16:25:39 +090038 }, "arch", "apiLevel", "apiMap", "flags")
Dan Albert914449f2016-06-17 16:45:24 -070039
sophiez58cabb72020-05-29 13:37:12 -070040 parseNdkApiRule = pctx.AndroidStaticRule("parseNdkApiRule",
41 blueprint.RuleParams{
42 Command: "$ndk_api_coverage_parser $in $out --api-map $apiMap",
43 CommandDeps: []string{"$ndk_api_coverage_parser"},
44 }, "apiMap")
45
Dan Albert914449f2016-06-17 16:45:24 -070046 ndkLibrarySuffix = ".ndk"
Colin Cross4d9c2d12016-07-29 12:48:20 -070047
Colin Cross95f1ca02020-10-29 20:47:22 -070048 ndkKnownLibsKey = android.NewOnceKey("ndkKnownLibsKey")
Dan Albertde5aade2020-06-30 12:32:51 -070049 // protects ndkKnownLibs writes during parallel BeginMutator.
50 ndkKnownLibsLock sync.Mutex
Dan Albert914449f2016-06-17 16:45:24 -070051)
52
Dan Albert1a246272020-07-06 14:49:35 -070053// The First_version and Unversioned_until properties of this struct should not
54// be used directly, but rather through the ApiLevel returning methods
55// firstVersion() and unversionedUntil().
56
Dan Albert914449f2016-06-17 16:45:24 -070057// Creates a stub shared library based on the provided version file.
58//
Dan Albert914449f2016-06-17 16:45:24 -070059// Example:
60//
61// ndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070062// name: "libfoo",
Dan Albert914449f2016-06-17 16:45:24 -070063// symbol_file: "libfoo.map.txt",
64// first_version: "9",
65// }
66//
67type libraryProperties struct {
68 // Relative path to the symbol map.
69 // An example file can be seen here: TODO(danalbert): Make an example.
Nan Zhang0007d812017-11-07 10:57:05 -080070 Symbol_file *string
Dan Albert914449f2016-06-17 16:45:24 -070071
72 // The first API level a library was available. A library will be generated
73 // for every API level beginning with this one.
Nan Zhang0007d812017-11-07 10:57:05 -080074 First_version *string
Dan Albert914449f2016-06-17 16:45:24 -070075
Dan Albert98dbb3b2017-01-03 15:16:29 -080076 // The first API level that library should have the version script applied.
77 // This defaults to the value of first_version, and should almost never be
78 // used. This is only needed to work around platform bugs like
79 // https://github.com/android-ndk/ndk/issues/265.
Nan Zhang0007d812017-11-07 10:57:05 -080080 Unversioned_until *string
Dan Albert98dbb3b2017-01-03 15:16:29 -080081
Dan Albert23d37e02018-11-28 08:30:10 -080082 // True if this API is not yet ready to be shipped in the NDK. It will be
83 // available in the platform for testing, but will be excluded from the
84 // sysroot provided to the NDK proper.
85 Draft bool
Dan Albert914449f2016-06-17 16:45:24 -070086}
87
Colin Crossb916a382016-07-29 17:28:03 -070088type stubDecorator struct {
89 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -070090
91 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -070092
sophiez58cabb72020-05-29 13:37:12 -070093 versionScriptPath android.ModuleGenPath
94 parsedCoverageXmlPath android.ModuleOutPath
95 installPath android.Path
Dan Albert1a246272020-07-06 14:49:35 -070096
97 apiLevel android.ApiLevel
98 firstVersion android.ApiLevel
99 unversionedUntil android.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700100}
101
Colin Cross0477b422020-10-13 18:43:54 -0700102var _ versionedInterface = (*stubDecorator)(nil)
103
Dan Albert1a246272020-07-06 14:49:35 -0700104func shouldUseVersionScript(ctx BaseModuleContext, stub *stubDecorator) bool {
105 return stub.apiLevel.GreaterThanOrEqualTo(stub.unversionedUntil)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800106}
107
Colin Cross0477b422020-10-13 18:43:54 -0700108func (stub *stubDecorator) implementationModuleName(name string) string {
109 return strings.TrimSuffix(name, ndkLibrarySuffix)
110}
111
Colin Cross3572cf72020-10-01 15:58:11 -0700112func ndkLibraryVersions(ctx android.BaseMutatorContext, from android.ApiLevel) []string {
Dan Albert1a246272020-07-06 14:49:35 -0700113 var versions []android.ApiLevel
114 versionStrs := []string{}
115 for _, version := range ctx.Config().AllSupportedApiLevels() {
116 if version.GreaterThanOrEqualTo(from) {
117 versions = append(versions, version)
118 versionStrs = append(versionStrs, version.String())
119 }
Dan Albert914449f2016-06-17 16:45:24 -0700120 }
Dan Albert0b176c82020-07-23 16:43:25 -0700121 versionStrs = append(versionStrs, android.FutureApiLevel.String())
Dan Albert914449f2016-06-17 16:45:24 -0700122
Colin Cross5ec407b2020-09-30 11:41:33 -0700123 return versionStrs
124}
125
Colin Cross3572cf72020-10-01 15:58:11 -0700126func (this *stubDecorator) stubsVersions(ctx android.BaseMutatorContext) []string {
127 if !ctx.Module().Enabled() {
128 return nil
129 }
130 firstVersion, err := nativeApiLevelFromUser(ctx,
131 String(this.properties.First_version))
132 if err != nil {
133 ctx.PropertyErrorf("first_version", err.Error())
134 return nil
135 }
136 return ndkLibraryVersions(ctx, firstVersion)
137}
138
Dan Albert1a246272020-07-06 14:49:35 -0700139func (this *stubDecorator) initializeProperties(ctx BaseModuleContext) bool {
Colin Cross5ec407b2020-09-30 11:41:33 -0700140 this.apiLevel = nativeApiLevelOrPanic(ctx, this.stubsVersion())
Dan Albert1a246272020-07-06 14:49:35 -0700141
142 var err error
143 this.firstVersion, err = nativeApiLevelFromUser(ctx,
144 String(this.properties.First_version))
145 if err != nil {
146 ctx.PropertyErrorf("first_version", err.Error())
147 return false
148 }
149
150 this.unversionedUntil, err = nativeApiLevelFromUserWithDefault(ctx,
151 String(this.properties.Unversioned_until), "minimum")
152 if err != nil {
153 ctx.PropertyErrorf("unversioned_until", err.Error())
154 return false
155 }
156
157 return true
158}
159
Colin Cross95f1ca02020-10-29 20:47:22 -0700160func getNDKKnownLibs(config android.Config) *[]string {
161 return config.Once(ndkKnownLibsKey, func() interface{} {
162 return &[]string{}
163 }).(*[]string)
164}
165
Colin Crossb916a382016-07-29 17:28:03 -0700166func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700167 c.baseCompiler.compilerInit(ctx)
168
Dan Willemsen01a90592017-04-07 15:21:13 -0700169 name := ctx.baseModuleName()
170 if strings.HasSuffix(name, ndkLibrarySuffix) {
171 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
172 }
173
Dan Albertde5aade2020-06-30 12:32:51 -0700174 ndkKnownLibsLock.Lock()
175 defer ndkKnownLibsLock.Unlock()
Colin Cross95f1ca02020-10-29 20:47:22 -0700176 ndkKnownLibs := getNDKKnownLibs(ctx.Config())
177 for _, lib := range *ndkKnownLibs {
Dan Albert7e9d2952016-08-04 13:02:36 -0700178 if lib == name {
179 return
180 }
181 }
Colin Cross95f1ca02020-10-29 20:47:22 -0700182 *ndkKnownLibs = append(*ndkKnownLibs, name)
Dan Albert7e9d2952016-08-04 13:02:36 -0700183}
184
George Burgess IVf5310e32017-07-19 11:39:53 -0700185func addStubLibraryCompilerFlags(flags Flags) Flags {
Colin Cross4af21ed2019-11-04 09:37:55 -0800186 flags.Global.CFlags = append(flags.Global.CFlags,
George Burgess IVf5310e32017-07-19 11:39:53 -0700187 // We're knowingly doing some otherwise unsightly things with builtin
188 // functions here. We're just generating stub libraries, so ignore it.
189 "-Wno-incompatible-library-redeclaration",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800190 "-Wno-incomplete-setjmp-declaration",
George Burgess IVf5310e32017-07-19 11:39:53 -0700191 "-Wno-builtin-requires-header",
192 "-Wno-invalid-noreturn",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800193 "-Wall",
194 "-Werror",
George Burgess IVf5310e32017-07-19 11:39:53 -0700195 // These libraries aren't actually used. Don't worry about unwinding
196 // (avoids the need to link an unwinder into a fake library).
197 "-fno-unwind-tables",
198 )
Jiyong Park48d75ef2019-11-21 15:11:49 +0900199 // All symbols in the stubs library should be visible.
200 if inList("-fvisibility=hidden", flags.Local.CFlags) {
201 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
202 }
George Burgess IVf5310e32017-07-19 11:39:53 -0700203 return flags
204}
205
Colin Crossf18e1102017-11-16 14:33:08 -0800206func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
207 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700208 return addStubLibraryCompilerFlags(flags)
209}
210
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900211func compileStubLibrary(ctx ModuleContext, flags Flags, symbolFile, apiLevel, genstubFlags string) (Objects, android.ModuleGenPath) {
Dan Albert914449f2016-06-17 16:45:24 -0700212 arch := ctx.Arch().ArchType.String()
213
Dan Willemsenb916b802017-03-19 13:44:32 -0700214 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
215 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
216 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albert49927d22017-03-28 15:00:46 -0700217 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700218 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700219 Rule: genStubSrc,
220 Description: "generate stubs " + symbolFilePath.Rel(),
221 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
222 Input: symbolFilePath,
223 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700224 Args: map[string]string{
225 "arch": arch,
Dan Willemsenb916b802017-03-19 13:44:32 -0700226 "apiLevel": apiLevel,
Dan Albert49927d22017-03-28 15:00:46 -0700227 "apiMap": apiLevelsJson.String(),
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900228 "flags": genstubFlags,
Dan Albert914449f2016-06-17 16:45:24 -0700229 },
230 })
231
Dan Albert914449f2016-06-17 16:45:24 -0700232 subdir := ""
Colin Cross2f336352016-10-26 10:03:47 -0700233 srcs := []android.Path{stubSrcPath}
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800234 return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil, nil), versionScriptPath
Dan Willemsenb916b802017-03-19 13:44:32 -0700235}
236
sophiez58cabb72020-05-29 13:37:12 -0700237func parseSymbolFileForCoverage(ctx ModuleContext, symbolFile string) android.ModuleOutPath {
238 apiLevelsJson := android.GetApiLevelsJson(ctx)
239 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
240 outputFileName := strings.Split(symbolFilePath.Base(), ".")[0]
241 parsedApiCoveragePath := android.PathForModuleOut(ctx, outputFileName+".xml")
242 ctx.Build(pctx, android.BuildParams{
243 Rule: parseNdkApiRule,
244 Description: "parse ndk api symbol file for api coverage: " + symbolFilePath.Rel(),
245 Outputs: []android.WritablePath{parsedApiCoveragePath},
246 Input: symbolFilePath,
sophiez148b3172020-06-11 17:27:56 -0700247 Implicits: []android.Path{apiLevelsJson},
sophiez58cabb72020-05-29 13:37:12 -0700248 Args: map[string]string{
249 "apiMap": apiLevelsJson.String(),
250 },
251 })
252 return parsedApiCoveragePath
253}
254
Dan Willemsenb916b802017-03-19 13:44:32 -0700255func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800256 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700257 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
258 }
259
Colin Cross5ec407b2020-09-30 11:41:33 -0700260 if !c.buildStubs() {
261 // NDK libraries have no implementation variant, nothing to do
262 return Objects{}
263 }
264
Dan Albert1a246272020-07-06 14:49:35 -0700265 if !c.initializeProperties(ctx) {
266 // Emits its own errors, so we don't need to.
267 return Objects{}
268 }
269
sophiez58cabb72020-05-29 13:37:12 -0700270 symbolFile := String(c.properties.Symbol_file)
271 objs, versionScript := compileStubLibrary(ctx, flags, symbolFile,
Dan Albert1a246272020-07-06 14:49:35 -0700272 c.apiLevel.String(), "")
Dan Willemsenb916b802017-03-19 13:44:32 -0700273 c.versionScriptPath = versionScript
Dan Albert1a246272020-07-06 14:49:35 -0700274 if c.apiLevel.IsCurrent() && ctx.PrimaryArch() {
sophiez58cabb72020-05-29 13:37:12 -0700275 c.parsedCoverageXmlPath = parseSymbolFileForCoverage(ctx, symbolFile)
276 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700277 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700278}
279
Colin Cross37047f12016-12-13 17:06:13 -0800280func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700281 return Deps{}
282}
283
Dan Willemsen01a90592017-04-07 15:21:13 -0700284func (linker *stubDecorator) Name(name string) string {
285 return name + ndkLibrarySuffix
286}
287
Colin Crossb916a382016-07-29 17:28:03 -0700288func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700289 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700290 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700291}
292
Colin Crossb916a382016-07-29 17:28:03 -0700293func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700294 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700295
Colin Cross5ec407b2020-09-30 11:41:33 -0700296 if !stub.buildStubs() {
297 // NDK libraries have no implementation variant, nothing to do
298 return nil
299 }
300
Dan Albert1a246272020-07-06 14:49:35 -0700301 if shouldUseVersionScript(ctx, stub) {
Dan Albert98dbb3b2017-01-03 15:16:29 -0800302 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800303 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700304 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800305 }
306
Colin Cross5ec407b2020-09-30 11:41:33 -0700307 stub.libraryDecorator.skipAPIDefine = true
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700308 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700309}
310
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700311func (stub *stubDecorator) nativeCoverage() bool {
312 return false
313}
314
Colin Crossb916a382016-07-29 17:28:03 -0700315func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700316 arch := ctx.Target().Arch.ArchType.Name
Dan Albert914449f2016-06-17 16:45:24 -0700317 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
318 // architectures it's just installed to lib.
319 libDir := "lib"
320 if ctx.toolchain().Is64Bit() && arch != "arm64" {
321 libDir = "lib64"
322 }
323
324 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albert1a246272020-07-06 14:49:35 -0700325 "platforms/android-%s/arch-%s/usr/%s", stub.apiLevel, arch, libDir))
Colin Cross0875c522017-11-28 17:34:01 -0800326 stub.installPath = ctx.InstallFile(installDir, path.Base(), path)
Dan Albert914449f2016-06-17 16:45:24 -0700327}
328
Colin Cross36242852017-06-23 15:06:31 -0700329func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800330 module, library := NewLibrary(android.DeviceSupported)
331 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700332 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700333 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200334 library.disableStripping()
Dan Albert914449f2016-06-17 16:45:24 -0700335
Colin Crossb916a382016-07-29 17:28:03 -0700336 stub := &stubDecorator{
337 libraryDecorator: library,
338 }
339 module.compiler = stub
340 module.linker = stub
341 module.installer = stub
Colin Cross31076b32020-10-23 17:22:06 -0700342 module.library = stub
Dan Albert914449f2016-06-17 16:45:24 -0700343
Colin Crossc511bc52020-04-07 16:50:32 +0000344 module.Properties.AlwaysSdk = true
345 module.Properties.Sdk_version = StringPtr("current")
346
Colin Cross36242852017-06-23 15:06:31 -0700347 module.AddProperties(&stub.properties, &library.MutatedProperties)
348
349 return module
Dan Albert914449f2016-06-17 16:45:24 -0700350}
351
Dan Albertf740ed02020-07-24 14:19:06 -0700352// ndk_library creates a library that exposes a stub implementation of functions
353// and variables for use at build time only.
Jooyung Hanb90e4912019-12-09 18:21:48 +0900354func NdkLibraryFactory() android.Module {
Colin Cross36242852017-06-23 15:06:31 -0700355 module := newStubLibrary()
356 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
dimitry03dc3f62019-05-09 14:07:34 +0200357 module.ModuleBase.EnableNativeBridgeSupportByDefault()
Colin Cross36242852017-06-23 15:06:31 -0700358 return module
Dan Albert914449f2016-06-17 16:45:24 -0700359}