blob: 7303b71aa244af9a1fce48a3effb88a657c3f579 [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
Dan Albertde5aade2020-06-30 12:32:51 -070048 // Added as a variation dependency via depsMutator.
49 ndkKnownLibs = []string{}
50 // protects ndkKnownLibs writes during parallel BeginMutator.
51 ndkKnownLibsLock sync.Mutex
Dan Albert914449f2016-06-17 16:45:24 -070052)
53
Dan Albert1a246272020-07-06 14:49:35 -070054// The First_version and Unversioned_until properties of this struct should not
55// be used directly, but rather through the ApiLevel returning methods
56// firstVersion() and unversionedUntil().
57
Dan Albert914449f2016-06-17 16:45:24 -070058// Creates a stub shared library based on the provided version file.
59//
Dan Albert914449f2016-06-17 16:45:24 -070060// Example:
61//
62// ndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070063// name: "libfoo",
Dan Albert914449f2016-06-17 16:45:24 -070064// symbol_file: "libfoo.map.txt",
65// first_version: "9",
66// }
67//
68type libraryProperties struct {
69 // Relative path to the symbol map.
70 // An example file can be seen here: TODO(danalbert): Make an example.
Nan Zhang0007d812017-11-07 10:57:05 -080071 Symbol_file *string
Dan Albert914449f2016-06-17 16:45:24 -070072
73 // The first API level a library was available. A library will be generated
74 // for every API level beginning with this one.
Nan Zhang0007d812017-11-07 10:57:05 -080075 First_version *string
Dan Albert914449f2016-06-17 16:45:24 -070076
Dan Albert98dbb3b2017-01-03 15:16:29 -080077 // The first API level that library should have the version script applied.
78 // This defaults to the value of first_version, and should almost never be
79 // used. This is only needed to work around platform bugs like
80 // https://github.com/android-ndk/ndk/issues/265.
Nan Zhang0007d812017-11-07 10:57:05 -080081 Unversioned_until *string
Dan Albert98dbb3b2017-01-03 15:16:29 -080082
Dan Albert23d37e02018-11-28 08:30:10 -080083 // True if this API is not yet ready to be shipped in the NDK. It will be
84 // available in the platform for testing, but will be excluded from the
85 // sysroot provided to the NDK proper.
86 Draft bool
Dan Albert914449f2016-06-17 16:45:24 -070087}
88
Colin Crossb916a382016-07-29 17:28:03 -070089type stubDecorator struct {
90 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -070091
92 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -070093
sophiez58cabb72020-05-29 13:37:12 -070094 versionScriptPath android.ModuleGenPath
95 parsedCoverageXmlPath android.ModuleOutPath
96 installPath android.Path
Dan Albert1a246272020-07-06 14:49:35 -070097
98 apiLevel android.ApiLevel
99 firstVersion android.ApiLevel
100 unversionedUntil android.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700101}
102
Dan Albert1a246272020-07-06 14:49:35 -0700103func shouldUseVersionScript(ctx BaseModuleContext, stub *stubDecorator) bool {
104 return stub.apiLevel.GreaterThanOrEqualTo(stub.unversionedUntil)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800105}
106
Colin Cross5ec407b2020-09-30 11:41:33 -0700107func ndkLibraryVersions(ctx android.BottomUpMutatorContext, from android.ApiLevel) []string {
Dan Albert1a246272020-07-06 14:49:35 -0700108 var versions []android.ApiLevel
109 versionStrs := []string{}
110 for _, version := range ctx.Config().AllSupportedApiLevels() {
111 if version.GreaterThanOrEqualTo(from) {
112 versions = append(versions, version)
113 versionStrs = append(versionStrs, version.String())
114 }
Dan Albert914449f2016-06-17 16:45:24 -0700115 }
Dan Albert0b176c82020-07-23 16:43:25 -0700116 versionStrs = append(versionStrs, android.FutureApiLevel.String())
Dan Albert914449f2016-06-17 16:45:24 -0700117
Colin Cross5ec407b2020-09-30 11:41:33 -0700118 return versionStrs
119}
120
121func generatePerApiVariants(ctx android.BottomUpMutatorContext,
122 from android.ApiLevel, perSplit func(*Module, android.ApiLevel)) {
123
124 versionStrs := ndkLibraryVersions(ctx, from)
Dan Albert92fe7402020-07-15 13:33:30 -0700125 modules := ctx.CreateVariations(versionStrs...)
Colin Cross5ec407b2020-09-30 11:41:33 -0700126
Dan Albert914449f2016-06-17 16:45:24 -0700127 for i, module := range modules {
Colin Cross5ec407b2020-09-30 11:41:33 -0700128 perSplit(module.(*Module), android.ApiLevelOrPanic(ctx, versionStrs[i]))
129
Dan Albert914449f2016-06-17 16:45:24 -0700130 }
131}
132
Dan Albert92fe7402020-07-15 13:33:30 -0700133func NdkApiMutator(ctx android.BottomUpMutatorContext) {
134 if m, ok := ctx.Module().(*Module); ok {
Colin Crossd4025822017-04-13 12:53:07 -0700135 if m.Enabled() {
136 if compiler, ok := m.compiler.(*stubDecorator); ok {
Dan Albert92fe7402020-07-15 13:33:30 -0700137 if ctx.Os() != android.Android {
138 // These modules are always android.DeviceEnabled only, but
139 // those include Fuchsia devices, which we don't support.
140 ctx.Module().Disable()
141 return
142 }
Dan Albert1a246272020-07-06 14:49:35 -0700143 firstVersion, err := nativeApiLevelFromUser(ctx,
144 String(compiler.properties.First_version))
145 if err != nil {
146 ctx.PropertyErrorf("first_version", err.Error())
147 return
148 }
Colin Cross5ec407b2020-09-30 11:41:33 -0700149 m.SetAllStubsVersions(ndkLibraryVersions(ctx, firstVersion))
Dan Albert92fe7402020-07-15 13:33:30 -0700150 } else if m.SplitPerApiLevel() && m.IsSdkVariant() {
151 if ctx.Os() != android.Android {
152 return
153 }
Dan Albert1a246272020-07-06 14:49:35 -0700154 from, err := nativeApiLevelFromUser(ctx, m.MinSdkVersion())
155 if err != nil {
156 ctx.PropertyErrorf("min_sdk_version", err.Error())
157 return
158 }
Colin Cross5ec407b2020-09-30 11:41:33 -0700159 generatePerApiVariants(ctx, from,
Dan Albert1a246272020-07-06 14:49:35 -0700160 func(m *Module, version android.ApiLevel) {
161 m.Properties.Sdk_version = StringPtr(version.String())
Dan Albert92fe7402020-07-15 13:33:30 -0700162 })
Colin Crossd4025822017-04-13 12:53:07 -0700163 }
Dan Albert914449f2016-06-17 16:45:24 -0700164 }
165 }
166}
167
Dan Albert1a246272020-07-06 14:49:35 -0700168func (this *stubDecorator) initializeProperties(ctx BaseModuleContext) bool {
Colin Cross5ec407b2020-09-30 11:41:33 -0700169 this.apiLevel = nativeApiLevelOrPanic(ctx, this.stubsVersion())
Dan Albert1a246272020-07-06 14:49:35 -0700170
171 var err error
172 this.firstVersion, err = nativeApiLevelFromUser(ctx,
173 String(this.properties.First_version))
174 if err != nil {
175 ctx.PropertyErrorf("first_version", err.Error())
176 return false
177 }
178
179 this.unversionedUntil, err = nativeApiLevelFromUserWithDefault(ctx,
180 String(this.properties.Unversioned_until), "minimum")
181 if err != nil {
182 ctx.PropertyErrorf("unversioned_until", err.Error())
183 return false
184 }
185
186 return true
187}
188
Colin Crossb916a382016-07-29 17:28:03 -0700189func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700190 c.baseCompiler.compilerInit(ctx)
191
Dan Willemsen01a90592017-04-07 15:21:13 -0700192 name := ctx.baseModuleName()
193 if strings.HasSuffix(name, ndkLibrarySuffix) {
194 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
195 }
196
Dan Albertde5aade2020-06-30 12:32:51 -0700197 ndkKnownLibsLock.Lock()
198 defer ndkKnownLibsLock.Unlock()
199 for _, lib := range ndkKnownLibs {
Dan Albert7e9d2952016-08-04 13:02:36 -0700200 if lib == name {
201 return
202 }
203 }
Dan Albertde5aade2020-06-30 12:32:51 -0700204 ndkKnownLibs = append(ndkKnownLibs, name)
Dan Albert7e9d2952016-08-04 13:02:36 -0700205}
206
George Burgess IVf5310e32017-07-19 11:39:53 -0700207func addStubLibraryCompilerFlags(flags Flags) Flags {
Colin Cross4af21ed2019-11-04 09:37:55 -0800208 flags.Global.CFlags = append(flags.Global.CFlags,
George Burgess IVf5310e32017-07-19 11:39:53 -0700209 // We're knowingly doing some otherwise unsightly things with builtin
210 // functions here. We're just generating stub libraries, so ignore it.
211 "-Wno-incompatible-library-redeclaration",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800212 "-Wno-incomplete-setjmp-declaration",
George Burgess IVf5310e32017-07-19 11:39:53 -0700213 "-Wno-builtin-requires-header",
214 "-Wno-invalid-noreturn",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800215 "-Wall",
216 "-Werror",
George Burgess IVf5310e32017-07-19 11:39:53 -0700217 // These libraries aren't actually used. Don't worry about unwinding
218 // (avoids the need to link an unwinder into a fake library).
219 "-fno-unwind-tables",
220 )
Jiyong Park48d75ef2019-11-21 15:11:49 +0900221 // All symbols in the stubs library should be visible.
222 if inList("-fvisibility=hidden", flags.Local.CFlags) {
223 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
224 }
George Burgess IVf5310e32017-07-19 11:39:53 -0700225 return flags
226}
227
Colin Crossf18e1102017-11-16 14:33:08 -0800228func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
229 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700230 return addStubLibraryCompilerFlags(flags)
231}
232
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900233func compileStubLibrary(ctx ModuleContext, flags Flags, symbolFile, apiLevel, genstubFlags string) (Objects, android.ModuleGenPath) {
Dan Albert914449f2016-06-17 16:45:24 -0700234 arch := ctx.Arch().ArchType.String()
235
Dan Willemsenb916b802017-03-19 13:44:32 -0700236 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
237 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
238 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albert49927d22017-03-28 15:00:46 -0700239 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700240 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700241 Rule: genStubSrc,
242 Description: "generate stubs " + symbolFilePath.Rel(),
243 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
244 Input: symbolFilePath,
245 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700246 Args: map[string]string{
247 "arch": arch,
Dan Willemsenb916b802017-03-19 13:44:32 -0700248 "apiLevel": apiLevel,
Dan Albert49927d22017-03-28 15:00:46 -0700249 "apiMap": apiLevelsJson.String(),
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900250 "flags": genstubFlags,
Dan Albert914449f2016-06-17 16:45:24 -0700251 },
252 })
253
Dan Albert914449f2016-06-17 16:45:24 -0700254 subdir := ""
Colin Cross2f336352016-10-26 10:03:47 -0700255 srcs := []android.Path{stubSrcPath}
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800256 return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil, nil), versionScriptPath
Dan Willemsenb916b802017-03-19 13:44:32 -0700257}
258
sophiez58cabb72020-05-29 13:37:12 -0700259func parseSymbolFileForCoverage(ctx ModuleContext, symbolFile string) android.ModuleOutPath {
260 apiLevelsJson := android.GetApiLevelsJson(ctx)
261 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
262 outputFileName := strings.Split(symbolFilePath.Base(), ".")[0]
263 parsedApiCoveragePath := android.PathForModuleOut(ctx, outputFileName+".xml")
264 ctx.Build(pctx, android.BuildParams{
265 Rule: parseNdkApiRule,
266 Description: "parse ndk api symbol file for api coverage: " + symbolFilePath.Rel(),
267 Outputs: []android.WritablePath{parsedApiCoveragePath},
268 Input: symbolFilePath,
sophiez148b3172020-06-11 17:27:56 -0700269 Implicits: []android.Path{apiLevelsJson},
sophiez58cabb72020-05-29 13:37:12 -0700270 Args: map[string]string{
271 "apiMap": apiLevelsJson.String(),
272 },
273 })
274 return parsedApiCoveragePath
275}
276
Dan Willemsenb916b802017-03-19 13:44:32 -0700277func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800278 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700279 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
280 }
281
Colin Cross5ec407b2020-09-30 11:41:33 -0700282 if !c.buildStubs() {
283 // NDK libraries have no implementation variant, nothing to do
284 return Objects{}
285 }
286
Dan Albert1a246272020-07-06 14:49:35 -0700287 if !c.initializeProperties(ctx) {
288 // Emits its own errors, so we don't need to.
289 return Objects{}
290 }
291
sophiez58cabb72020-05-29 13:37:12 -0700292 symbolFile := String(c.properties.Symbol_file)
293 objs, versionScript := compileStubLibrary(ctx, flags, symbolFile,
Dan Albert1a246272020-07-06 14:49:35 -0700294 c.apiLevel.String(), "")
Dan Willemsenb916b802017-03-19 13:44:32 -0700295 c.versionScriptPath = versionScript
Dan Albert1a246272020-07-06 14:49:35 -0700296 if c.apiLevel.IsCurrent() && ctx.PrimaryArch() {
sophiez58cabb72020-05-29 13:37:12 -0700297 c.parsedCoverageXmlPath = parseSymbolFileForCoverage(ctx, symbolFile)
298 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700299 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700300}
301
Colin Cross37047f12016-12-13 17:06:13 -0800302func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700303 return Deps{}
304}
305
Dan Willemsen01a90592017-04-07 15:21:13 -0700306func (linker *stubDecorator) Name(name string) string {
307 return name + ndkLibrarySuffix
308}
309
Colin Crossb916a382016-07-29 17:28:03 -0700310func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700311 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700312 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700313}
314
Colin Crossb916a382016-07-29 17:28:03 -0700315func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700316 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700317
Colin Cross5ec407b2020-09-30 11:41:33 -0700318 if !stub.buildStubs() {
319 // NDK libraries have no implementation variant, nothing to do
320 return nil
321 }
322
Dan Albert1a246272020-07-06 14:49:35 -0700323 if shouldUseVersionScript(ctx, stub) {
Dan Albert98dbb3b2017-01-03 15:16:29 -0800324 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800325 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700326 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800327 }
328
Colin Cross5ec407b2020-09-30 11:41:33 -0700329 stub.libraryDecorator.skipAPIDefine = true
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700330 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700331}
332
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700333func (stub *stubDecorator) nativeCoverage() bool {
334 return false
335}
336
Colin Crossb916a382016-07-29 17:28:03 -0700337func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700338 arch := ctx.Target().Arch.ArchType.Name
Dan Albert914449f2016-06-17 16:45:24 -0700339 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
340 // architectures it's just installed to lib.
341 libDir := "lib"
342 if ctx.toolchain().Is64Bit() && arch != "arm64" {
343 libDir = "lib64"
344 }
345
346 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albert1a246272020-07-06 14:49:35 -0700347 "platforms/android-%s/arch-%s/usr/%s", stub.apiLevel, arch, libDir))
Colin Cross0875c522017-11-28 17:34:01 -0800348 stub.installPath = ctx.InstallFile(installDir, path.Base(), path)
Dan Albert914449f2016-06-17 16:45:24 -0700349}
350
Colin Cross36242852017-06-23 15:06:31 -0700351func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800352 module, library := NewLibrary(android.DeviceSupported)
353 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700354 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700355 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200356 library.disableStripping()
Dan Albert914449f2016-06-17 16:45:24 -0700357
Colin Crossb916a382016-07-29 17:28:03 -0700358 stub := &stubDecorator{
359 libraryDecorator: library,
360 }
361 module.compiler = stub
362 module.linker = stub
363 module.installer = stub
Dan Albert914449f2016-06-17 16:45:24 -0700364
Colin Crossc511bc52020-04-07 16:50:32 +0000365 module.Properties.AlwaysSdk = true
366 module.Properties.Sdk_version = StringPtr("current")
367
Colin Cross36242852017-06-23 15:06:31 -0700368 module.AddProperties(&stub.properties, &library.MutatedProperties)
369
370 return module
Dan Albert914449f2016-06-17 16:45:24 -0700371}
372
Dan Albertf740ed02020-07-24 14:19:06 -0700373// ndk_library creates a library that exposes a stub implementation of functions
374// and variables for use at build time only.
Jooyung Hanb90e4912019-12-09 18:21:48 +0900375func NdkLibraryFactory() android.Module {
Colin Cross36242852017-06-23 15:06:31 -0700376 module := newStubLibrary()
377 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
dimitry03dc3f62019-05-09 14:07:34 +0200378 module.ModuleBase.EnableNativeBridgeSupportByDefault()
Colin Cross36242852017-06-23 15:06:31 -0700379 return module
Dan Albert914449f2016-06-17 16:45:24 -0700380}