blob: 58e742e5f16f96949380edc16c757b9751e74431 [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"
19 "strconv"
20 "strings"
Colin Crosse8a67a72016-08-07 21:17:54 -070021 "sync"
Dan Albert914449f2016-06-17 16:45:24 -070022
23 "github.com/google/blueprint"
24
25 "android/soong/android"
26)
27
sophiez58cabb72020-05-29 13:37:12 -070028func init() {
Dan Albert06f58af2020-06-22 15:10:31 -070029 pctx.HostBinToolVariable("ndkStubGenerator", "ndkstubgen")
sophiez58cabb72020-05-29 13:37:12 -070030 pctx.HostBinToolVariable("ndk_api_coverage_parser", "ndk_api_coverage_parser")
31}
32
Dan Albert914449f2016-06-17 16:45:24 -070033var (
Colin Cross9d45bb72016-08-29 16:14:13 -070034 genStubSrc = pctx.AndroidStaticRule("genStubSrc",
Dan Albert914449f2016-06-17 16:45:24 -070035 blueprint.RuleParams{
Dan Albert06f58af2020-06-22 15:10:31 -070036 Command: "$ndkStubGenerator --arch $arch --api $apiLevel " +
37 "--api-map $apiMap $flags $in $out",
38 CommandDeps: []string{"$ndkStubGenerator"},
Jiyong Park3fd0baf2018-12-07 16:25:39 +090039 }, "arch", "apiLevel", "apiMap", "flags")
Dan Albert914449f2016-06-17 16:45:24 -070040
sophiez58cabb72020-05-29 13:37:12 -070041 parseNdkApiRule = pctx.AndroidStaticRule("parseNdkApiRule",
42 blueprint.RuleParams{
43 Command: "$ndk_api_coverage_parser $in $out --api-map $apiMap",
44 CommandDeps: []string{"$ndk_api_coverage_parser"},
45 }, "apiMap")
46
Dan Albert914449f2016-06-17 16:45:24 -070047 ndkLibrarySuffix = ".ndk"
Colin Cross4d9c2d12016-07-29 12:48:20 -070048
Dan Albertde5aade2020-06-30 12:32:51 -070049 // Added as a variation dependency via depsMutator.
50 ndkKnownLibs = []string{}
51 // protects ndkKnownLibs writes during parallel BeginMutator.
52 ndkKnownLibsLock sync.Mutex
Dan Albert914449f2016-06-17 16:45:24 -070053)
54
55// Creates a stub shared library based on the provided version file.
56//
Dan Albert914449f2016-06-17 16:45:24 -070057// Example:
58//
59// ndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070060// name: "libfoo",
Dan Albert914449f2016-06-17 16:45:24 -070061// symbol_file: "libfoo.map.txt",
62// first_version: "9",
63// }
64//
65type libraryProperties struct {
66 // Relative path to the symbol map.
67 // An example file can be seen here: TODO(danalbert): Make an example.
Nan Zhang0007d812017-11-07 10:57:05 -080068 Symbol_file *string
Dan Albert914449f2016-06-17 16:45:24 -070069
70 // The first API level a library was available. A library will be generated
71 // for every API level beginning with this one.
Nan Zhang0007d812017-11-07 10:57:05 -080072 First_version *string
Dan Albert914449f2016-06-17 16:45:24 -070073
Dan Albert98dbb3b2017-01-03 15:16:29 -080074 // The first API level that library should have the version script applied.
75 // This defaults to the value of first_version, and should almost never be
76 // used. This is only needed to work around platform bugs like
77 // https://github.com/android-ndk/ndk/issues/265.
Nan Zhang0007d812017-11-07 10:57:05 -080078 Unversioned_until *string
Dan Albert98dbb3b2017-01-03 15:16:29 -080079
Dan Albert06f58af2020-06-22 15:10:31 -070080 // Private property for use by the mutator that splits per-API level. Can be
81 // one of <number:sdk_version> or <codename> or "current" passed to
82 // "ndkstubgen.py" as it is
Dan Albertfd86e9e2016-11-08 13:35:12 -080083 ApiLevel string `blueprint:"mutated"`
Dan Albert23d37e02018-11-28 08:30:10 -080084
85 // True if this API is not yet ready to be shipped in the NDK. It will be
86 // available in the platform for testing, but will be excluded from the
87 // sysroot provided to the NDK proper.
88 Draft bool
Dan Albert914449f2016-06-17 16:45:24 -070089}
90
Colin Crossb916a382016-07-29 17:28:03 -070091type stubDecorator struct {
92 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -070093
94 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -070095
sophiez58cabb72020-05-29 13:37:12 -070096 versionScriptPath android.ModuleGenPath
97 parsedCoverageXmlPath android.ModuleOutPath
98 installPath android.Path
Dan Albert914449f2016-06-17 16:45:24 -070099}
100
101// OMG GO
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700102func intMax(a int, b int) int {
103 if a > b {
Dan Albert914449f2016-06-17 16:45:24 -0700104 return a
105 } else {
106 return b
107 }
108}
109
Colin Cross0ea8ba82019-06-06 14:33:29 -0700110func normalizeNdkApiLevel(ctx android.BaseModuleContext, apiLevel string,
Dan Albertf5415d72017-08-17 16:19:59 -0700111 arch android.Arch) (string, error) {
112
Dan Albert92fe7402020-07-15 13:33:30 -0700113 if apiLevel == "" {
114 panic("empty apiLevel not allowed")
115 }
116
Dan Albert90f7a4d2016-11-08 14:34:24 -0800117 if apiLevel == "current" {
118 return apiLevel, nil
119 }
120
Colin Cross6510f912017-11-29 00:27:14 -0800121 minVersion := ctx.Config().MinSupportedSdkVersion()
Colin Crossce87b802017-04-13 13:00:26 -0700122 firstArchVersions := map[android.ArchType]int{
Dan Albertf5415d72017-08-17 16:19:59 -0700123 android.Arm: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700124 android.Arm64: 21,
Dan Albertf5415d72017-08-17 16:19:59 -0700125 android.X86: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700126 android.X86_64: 21,
Dan Albert914449f2016-06-17 16:45:24 -0700127 }
128
Colin Crossce87b802017-04-13 13:00:26 -0700129 firstArchVersion, ok := firstArchVersions[arch.ArchType]
Dan Albert2e5d7d42017-03-29 18:22:39 -0700130 if !ok {
Colin Crossce87b802017-04-13 13:00:26 -0700131 panic(fmt.Errorf("Arch %q not found in firstArchVersions", arch.ArchType))
Dan Albert2e5d7d42017-03-29 18:22:39 -0700132 }
133
134 if apiLevel == "minimum" {
135 return strconv.Itoa(firstArchVersion), nil
136 }
137
Dan Albert914449f2016-06-17 16:45:24 -0700138 // If the NDK drops support for a platform version, we don't want to have to
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700139 // fix up every module that was using it as its SDK version. Clip to the
Dan Albert914449f2016-06-17 16:45:24 -0700140 // supported version here instead.
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700141 version, err := strconv.Atoi(apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700142 if err != nil {
Dan Albert92fe7402020-07-15 13:33:30 -0700143 // Non-integer API levels are codenames.
144 return apiLevel, nil
Dan Albert914449f2016-06-17 16:45:24 -0700145 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700146 version = intMax(version, minVersion)
147
Dan Albert90f7a4d2016-11-08 14:34:24 -0800148 return strconv.Itoa(intMax(version, firstArchVersion)), nil
149}
150
151func getFirstGeneratedVersion(firstSupportedVersion string, platformVersion int) (int, error) {
152 if firstSupportedVersion == "current" {
153 return platformVersion + 1, nil
154 }
155
156 return strconv.Atoi(firstSupportedVersion)
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700157}
158
Colin Cross0ea8ba82019-06-06 14:33:29 -0700159func shouldUseVersionScript(ctx android.BaseModuleContext, stub *stubDecorator) (bool, error) {
Ryan Prichard37ebbde2018-07-24 12:37:24 -0700160 // unversioned_until is normally empty, in which case we should use the version script.
161 if String(stub.properties.Unversioned_until) == "" {
162 return true, nil
163 }
Dan Albert98dbb3b2017-01-03 15:16:29 -0800164
Nan Zhang0007d812017-11-07 10:57:05 -0800165 if String(stub.properties.Unversioned_until) == "current" {
Dan Albert022e7a32017-01-05 15:49:09 -0800166 if stub.properties.ApiLevel == "current" {
167 return true, nil
168 } else {
169 return false, nil
170 }
171 }
172
Dan Albert98dbb3b2017-01-03 15:16:29 -0800173 if stub.properties.ApiLevel == "current" {
174 return true, nil
175 }
176
Dan Alberte67144e2018-05-03 15:42:34 -0700177 unversionedUntil, err := android.ApiStrToNum(ctx, String(stub.properties.Unversioned_until))
Dan Albert98dbb3b2017-01-03 15:16:29 -0800178 if err != nil {
179 return true, err
180 }
181
Ryan Prichard37ebbde2018-07-24 12:37:24 -0700182 version, err := android.ApiStrToNum(ctx, stub.properties.ApiLevel)
183 if err != nil {
184 return true, err
Dan Alberte67144e2018-05-03 15:42:34 -0700185 }
186
Dan Albert98dbb3b2017-01-03 15:16:29 -0800187 return version >= unversionedUntil, nil
188}
189
Dan Albert92fe7402020-07-15 13:33:30 -0700190func generatePerApiVariants(ctx android.BottomUpMutatorContext, m *Module,
191 propName string, propValue string, perSplit func(*Module, string)) {
192 platformVersion := ctx.Config().PlatformSdkVersionInt()
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700193
Dan Albert92fe7402020-07-15 13:33:30 -0700194 firstSupportedVersion, err := normalizeNdkApiLevel(ctx, propValue,
195 ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700196 if err != nil {
Dan Albert92fe7402020-07-15 13:33:30 -0700197 ctx.PropertyErrorf(propName, err.Error())
Dan Albert914449f2016-06-17 16:45:24 -0700198 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700199
Dan Albert92fe7402020-07-15 13:33:30 -0700200 firstGenVersion, err := getFirstGeneratedVersion(firstSupportedVersion,
201 platformVersion)
Dan Albert90f7a4d2016-11-08 14:34:24 -0800202 if err != nil {
203 // In theory this is impossible because we've already run this through
204 // normalizeNdkApiLevel above.
Dan Albert92fe7402020-07-15 13:33:30 -0700205 ctx.PropertyErrorf(propName, err.Error())
Dan Albert90f7a4d2016-11-08 14:34:24 -0800206 }
207
Dan Albertfd86e9e2016-11-08 13:35:12 -0800208 var versionStrs []string
Dan Albert90f7a4d2016-11-08 14:34:24 -0800209 for version := firstGenVersion; version <= platformVersion; version++ {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800210 versionStrs = append(versionStrs, strconv.Itoa(version))
Dan Albert914449f2016-06-17 16:45:24 -0700211 }
Dan Albert92fe7402020-07-15 13:33:30 -0700212 versionStrs = append(versionStrs, ctx.Config().PlatformVersionActiveCodenames()...)
Dan Albertfd86e9e2016-11-08 13:35:12 -0800213 versionStrs = append(versionStrs, "current")
Dan Albert914449f2016-06-17 16:45:24 -0700214
Dan Albert92fe7402020-07-15 13:33:30 -0700215 modules := ctx.CreateVariations(versionStrs...)
Dan Albert914449f2016-06-17 16:45:24 -0700216 for i, module := range modules {
Dan Albert92fe7402020-07-15 13:33:30 -0700217 perSplit(module.(*Module), versionStrs[i])
Dan Albert914449f2016-06-17 16:45:24 -0700218 }
219}
220
Dan Albert92fe7402020-07-15 13:33:30 -0700221func NdkApiMutator(ctx android.BottomUpMutatorContext) {
222 if m, ok := ctx.Module().(*Module); ok {
Colin Crossd4025822017-04-13 12:53:07 -0700223 if m.Enabled() {
224 if compiler, ok := m.compiler.(*stubDecorator); ok {
Dan Albert92fe7402020-07-15 13:33:30 -0700225 if ctx.Os() != android.Android {
226 // These modules are always android.DeviceEnabled only, but
227 // those include Fuchsia devices, which we don't support.
228 ctx.Module().Disable()
229 return
230 }
231 generatePerApiVariants(ctx, m, "first_version",
232 String(compiler.properties.First_version),
233 func(m *Module, version string) {
234 m.compiler.(*stubDecorator).properties.ApiLevel =
235 version
236 })
237 } else if m.SplitPerApiLevel() && m.IsSdkVariant() {
238 if ctx.Os() != android.Android {
239 return
240 }
241 generatePerApiVariants(ctx, m, "min_sdk_version",
242 m.MinSdkVersion(), func(m *Module, version string) {
243 m.Properties.Sdk_version = &version
244 })
Colin Crossd4025822017-04-13 12:53:07 -0700245 }
Dan Albert914449f2016-06-17 16:45:24 -0700246 }
247 }
248}
249
Colin Crossb916a382016-07-29 17:28:03 -0700250func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700251 c.baseCompiler.compilerInit(ctx)
252
Dan Willemsen01a90592017-04-07 15:21:13 -0700253 name := ctx.baseModuleName()
254 if strings.HasSuffix(name, ndkLibrarySuffix) {
255 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
256 }
257
Dan Albertde5aade2020-06-30 12:32:51 -0700258 ndkKnownLibsLock.Lock()
259 defer ndkKnownLibsLock.Unlock()
260 for _, lib := range ndkKnownLibs {
Dan Albert7e9d2952016-08-04 13:02:36 -0700261 if lib == name {
262 return
263 }
264 }
Dan Albertde5aade2020-06-30 12:32:51 -0700265 ndkKnownLibs = append(ndkKnownLibs, name)
Dan Albert7e9d2952016-08-04 13:02:36 -0700266}
267
George Burgess IVf5310e32017-07-19 11:39:53 -0700268func addStubLibraryCompilerFlags(flags Flags) Flags {
Colin Cross4af21ed2019-11-04 09:37:55 -0800269 flags.Global.CFlags = append(flags.Global.CFlags,
George Burgess IVf5310e32017-07-19 11:39:53 -0700270 // We're knowingly doing some otherwise unsightly things with builtin
271 // functions here. We're just generating stub libraries, so ignore it.
272 "-Wno-incompatible-library-redeclaration",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800273 "-Wno-incomplete-setjmp-declaration",
George Burgess IVf5310e32017-07-19 11:39:53 -0700274 "-Wno-builtin-requires-header",
275 "-Wno-invalid-noreturn",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800276 "-Wall",
277 "-Werror",
George Burgess IVf5310e32017-07-19 11:39:53 -0700278 // These libraries aren't actually used. Don't worry about unwinding
279 // (avoids the need to link an unwinder into a fake library).
280 "-fno-unwind-tables",
281 )
Jiyong Park48d75ef2019-11-21 15:11:49 +0900282 // All symbols in the stubs library should be visible.
283 if inList("-fvisibility=hidden", flags.Local.CFlags) {
284 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
285 }
George Burgess IVf5310e32017-07-19 11:39:53 -0700286 return flags
287}
288
Colin Crossf18e1102017-11-16 14:33:08 -0800289func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
290 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700291 return addStubLibraryCompilerFlags(flags)
292}
293
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900294func compileStubLibrary(ctx ModuleContext, flags Flags, symbolFile, apiLevel, genstubFlags string) (Objects, android.ModuleGenPath) {
Dan Albert914449f2016-06-17 16:45:24 -0700295 arch := ctx.Arch().ArchType.String()
296
Dan Willemsenb916b802017-03-19 13:44:32 -0700297 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
298 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
299 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albert49927d22017-03-28 15:00:46 -0700300 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700301 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700302 Rule: genStubSrc,
303 Description: "generate stubs " + symbolFilePath.Rel(),
304 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
305 Input: symbolFilePath,
306 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700307 Args: map[string]string{
308 "arch": arch,
Dan Willemsenb916b802017-03-19 13:44:32 -0700309 "apiLevel": apiLevel,
Dan Albert49927d22017-03-28 15:00:46 -0700310 "apiMap": apiLevelsJson.String(),
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900311 "flags": genstubFlags,
Dan Albert914449f2016-06-17 16:45:24 -0700312 },
313 })
314
Dan Albert914449f2016-06-17 16:45:24 -0700315 subdir := ""
Colin Cross2f336352016-10-26 10:03:47 -0700316 srcs := []android.Path{stubSrcPath}
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800317 return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil, nil), versionScriptPath
Dan Willemsenb916b802017-03-19 13:44:32 -0700318}
319
sophiez58cabb72020-05-29 13:37:12 -0700320func parseSymbolFileForCoverage(ctx ModuleContext, symbolFile string) android.ModuleOutPath {
321 apiLevelsJson := android.GetApiLevelsJson(ctx)
322 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
323 outputFileName := strings.Split(symbolFilePath.Base(), ".")[0]
324 parsedApiCoveragePath := android.PathForModuleOut(ctx, outputFileName+".xml")
325 ctx.Build(pctx, android.BuildParams{
326 Rule: parseNdkApiRule,
327 Description: "parse ndk api symbol file for api coverage: " + symbolFilePath.Rel(),
328 Outputs: []android.WritablePath{parsedApiCoveragePath},
329 Input: symbolFilePath,
sophiez148b3172020-06-11 17:27:56 -0700330 Implicits: []android.Path{apiLevelsJson},
sophiez58cabb72020-05-29 13:37:12 -0700331 Args: map[string]string{
332 "apiMap": apiLevelsJson.String(),
333 },
334 })
335 return parsedApiCoveragePath
336}
337
Dan Willemsenb916b802017-03-19 13:44:32 -0700338func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800339 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700340 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
341 }
342
sophiez58cabb72020-05-29 13:37:12 -0700343 symbolFile := String(c.properties.Symbol_file)
344 objs, versionScript := compileStubLibrary(ctx, flags, symbolFile,
Nan Zhang0007d812017-11-07 10:57:05 -0800345 c.properties.ApiLevel, "")
Dan Willemsenb916b802017-03-19 13:44:32 -0700346 c.versionScriptPath = versionScript
sophiez58cabb72020-05-29 13:37:12 -0700347 if c.properties.ApiLevel == "current" && ctx.PrimaryArch() {
348 c.parsedCoverageXmlPath = parseSymbolFileForCoverage(ctx, symbolFile)
349 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700350 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700351}
352
Colin Cross37047f12016-12-13 17:06:13 -0800353func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700354 return Deps{}
355}
356
Dan Willemsen01a90592017-04-07 15:21:13 -0700357func (linker *stubDecorator) Name(name string) string {
358 return name + ndkLibrarySuffix
359}
360
Colin Crossb916a382016-07-29 17:28:03 -0700361func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700362 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700363 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700364}
365
Colin Crossb916a382016-07-29 17:28:03 -0700366func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700367 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700368
Dan Alberte67144e2018-05-03 15:42:34 -0700369 useVersionScript, err := shouldUseVersionScript(ctx, stub)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800370 if err != nil {
371 ctx.ModuleErrorf(err.Error())
372 }
373
374 if useVersionScript {
375 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800376 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700377 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800378 }
379
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700380 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700381}
382
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700383func (stub *stubDecorator) nativeCoverage() bool {
384 return false
385}
386
Colin Crossb916a382016-07-29 17:28:03 -0700387func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700388 arch := ctx.Target().Arch.ArchType.Name
Colin Crossb916a382016-07-29 17:28:03 -0700389 apiLevel := stub.properties.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700390
391 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
392 // architectures it's just installed to lib.
393 libDir := "lib"
394 if ctx.toolchain().Is64Bit() && arch != "arm64" {
395 libDir = "lib64"
396 }
397
398 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albertfd86e9e2016-11-08 13:35:12 -0800399 "platforms/android-%s/arch-%s/usr/%s", apiLevel, arch, libDir))
Colin Cross0875c522017-11-28 17:34:01 -0800400 stub.installPath = ctx.InstallFile(installDir, path.Base(), path)
Dan Albert914449f2016-06-17 16:45:24 -0700401}
402
Colin Cross36242852017-06-23 15:06:31 -0700403func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800404 module, library := NewLibrary(android.DeviceSupported)
405 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700406 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700407 module.sanitize = nil
Nan Zhang0007d812017-11-07 10:57:05 -0800408 library.StripProperties.Strip.None = BoolPtr(true)
Dan Albert914449f2016-06-17 16:45:24 -0700409
Colin Crossb916a382016-07-29 17:28:03 -0700410 stub := &stubDecorator{
411 libraryDecorator: library,
412 }
413 module.compiler = stub
414 module.linker = stub
415 module.installer = stub
Dan Albert914449f2016-06-17 16:45:24 -0700416
Colin Crossc511bc52020-04-07 16:50:32 +0000417 module.Properties.AlwaysSdk = true
418 module.Properties.Sdk_version = StringPtr("current")
419
Colin Cross36242852017-06-23 15:06:31 -0700420 module.AddProperties(&stub.properties, &library.MutatedProperties)
421
422 return module
Dan Albert914449f2016-06-17 16:45:24 -0700423}
424
Dan Albertf740ed02020-07-24 14:19:06 -0700425// ndk_library creates a library that exposes a stub implementation of functions
426// and variables for use at build time only.
Jooyung Hanb90e4912019-12-09 18:21:48 +0900427func NdkLibraryFactory() android.Module {
Colin Cross36242852017-06-23 15:06:31 -0700428 module := newStubLibrary()
429 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
dimitry03dc3f62019-05-09 14:07:34 +0200430 module.ModuleBase.EnableNativeBridgeSupportByDefault()
Colin Cross36242852017-06-23 15:06:31 -0700431 return module
Dan Albert914449f2016-06-17 16:45:24 -0700432}