blob: 1b09f88c2bf518552bab9cdee1289bb4a7636621 [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
28var (
29 toolPath = pctx.SourcePathVariable("toolPath", "build/soong/cc/gen_stub_libs.py")
30
Colin Cross9d45bb72016-08-29 16:14:13 -070031 genStubSrc = pctx.AndroidStaticRule("genStubSrc",
Dan Albert914449f2016-06-17 16:45:24 -070032 blueprint.RuleParams{
Dan Albert49927d22017-03-28 15:00:46 -070033 Command: "$toolPath --arch $arch --api $apiLevel --api-map " +
Jiyong Park3fd0baf2018-12-07 16:25:39 +090034 "$apiMap $flags $in $out",
Dan Albert914449f2016-06-17 16:45:24 -070035 CommandDeps: []string{"$toolPath"},
Jiyong Park3fd0baf2018-12-07 16:25:39 +090036 }, "arch", "apiLevel", "apiMap", "flags")
Dan Albert914449f2016-06-17 16:45:24 -070037
38 ndkLibrarySuffix = ".ndk"
Colin Cross4d9c2d12016-07-29 12:48:20 -070039
40 ndkPrebuiltSharedLibs = []string{
41 "android",
Steven Morelandfa287842018-08-29 20:14:18 -070042 "binder_ndk",
Colin Cross4d9c2d12016-07-29 12:48:20 -070043 "c",
44 "dl",
45 "EGL",
46 "GLESv1_CM",
47 "GLESv2",
48 "GLESv3",
49 "jnigraphics",
50 "log",
51 "mediandk",
52 "m",
53 "OpenMAXAL",
54 "OpenSLES",
55 "stdc++",
56 "vulkan",
57 "z",
58 }
59 ndkPrebuiltSharedLibraries = addPrefix(append([]string(nil), ndkPrebuiltSharedLibs...), "lib")
60
61 // These libraries have migrated over to the new ndk_library, which is added
62 // as a variation dependency via depsMutator.
Colin Crosse8a67a72016-08-07 21:17:54 -070063 ndkMigratedLibs = []string{}
Colin Crosse40b4ea2018-10-02 22:25:58 -070064 ndkMigratedLibsLock sync.Mutex // protects ndkMigratedLibs writes during parallel BeginMutator
Dan Albert914449f2016-06-17 16:45:24 -070065)
66
67// Creates a stub shared library based on the provided version file.
68//
Dan Albert914449f2016-06-17 16:45:24 -070069// Example:
70//
71// ndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070072// name: "libfoo",
Dan Albert914449f2016-06-17 16:45:24 -070073// symbol_file: "libfoo.map.txt",
74// first_version: "9",
75// }
76//
77type libraryProperties struct {
78 // Relative path to the symbol map.
79 // An example file can be seen here: TODO(danalbert): Make an example.
Nan Zhang0007d812017-11-07 10:57:05 -080080 Symbol_file *string
Dan Albert914449f2016-06-17 16:45:24 -070081
82 // The first API level a library was available. A library will be generated
83 // for every API level beginning with this one.
Nan Zhang0007d812017-11-07 10:57:05 -080084 First_version *string
Dan Albert914449f2016-06-17 16:45:24 -070085
Dan Albert98dbb3b2017-01-03 15:16:29 -080086 // The first API level that library should have the version script applied.
87 // This defaults to the value of first_version, and should almost never be
88 // used. This is only needed to work around platform bugs like
89 // https://github.com/android-ndk/ndk/issues/265.
Nan Zhang0007d812017-11-07 10:57:05 -080090 Unversioned_until *string
Dan Albert98dbb3b2017-01-03 15:16:29 -080091
Dan Albert914449f2016-06-17 16:45:24 -070092 // Private property for use by the mutator that splits per-API level.
Dan Albertfd86e9e2016-11-08 13:35:12 -080093 ApiLevel string `blueprint:"mutated"`
Dan Albert23d37e02018-11-28 08:30:10 -080094
95 // True if this API is not yet ready to be shipped in the NDK. It will be
96 // available in the platform for testing, but will be excluded from the
97 // sysroot provided to the NDK proper.
98 Draft bool
Dan Albert914449f2016-06-17 16:45:24 -070099}
100
Colin Crossb916a382016-07-29 17:28:03 -0700101type stubDecorator struct {
102 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -0700103
104 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -0700105
Colin Crossb916a382016-07-29 17:28:03 -0700106 versionScriptPath android.ModuleGenPath
Colin Cross0875c522017-11-28 17:34:01 -0800107 installPath android.Path
Dan Albert914449f2016-06-17 16:45:24 -0700108}
109
110// OMG GO
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700111func intMax(a int, b int) int {
112 if a > b {
Dan Albert914449f2016-06-17 16:45:24 -0700113 return a
114 } else {
115 return b
116 }
117}
118
Dan Albertf5415d72017-08-17 16:19:59 -0700119func normalizeNdkApiLevel(ctx android.BaseContext, apiLevel string,
120 arch android.Arch) (string, error) {
121
Dan Albert90f7a4d2016-11-08 14:34:24 -0800122 if apiLevel == "current" {
123 return apiLevel, nil
124 }
125
Colin Cross6510f912017-11-29 00:27:14 -0800126 minVersion := ctx.Config().MinSupportedSdkVersion()
Colin Crossce87b802017-04-13 13:00:26 -0700127 firstArchVersions := map[android.ArchType]int{
Dan Albertf5415d72017-08-17 16:19:59 -0700128 android.Arm: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700129 android.Arm64: 21,
Dan Albertf5415d72017-08-17 16:19:59 -0700130 android.Mips: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700131 android.Mips64: 21,
Dan Albertf5415d72017-08-17 16:19:59 -0700132 android.X86: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700133 android.X86_64: 21,
Dan Albert914449f2016-06-17 16:45:24 -0700134 }
135
Colin Crossce87b802017-04-13 13:00:26 -0700136 firstArchVersion, ok := firstArchVersions[arch.ArchType]
Dan Albert2e5d7d42017-03-29 18:22:39 -0700137 if !ok {
Colin Crossce87b802017-04-13 13:00:26 -0700138 panic(fmt.Errorf("Arch %q not found in firstArchVersions", arch.ArchType))
Dan Albert2e5d7d42017-03-29 18:22:39 -0700139 }
140
141 if apiLevel == "minimum" {
142 return strconv.Itoa(firstArchVersion), nil
143 }
144
Dan Albert914449f2016-06-17 16:45:24 -0700145 // If the NDK drops support for a platform version, we don't want to have to
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700146 // fix up every module that was using it as its SDK version. Clip to the
Dan Albert914449f2016-06-17 16:45:24 -0700147 // supported version here instead.
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700148 version, err := strconv.Atoi(apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700149 if err != nil {
Dan Albert90f7a4d2016-11-08 14:34:24 -0800150 return "", fmt.Errorf("API level must be an integer (is %q)", apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700151 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700152 version = intMax(version, minVersion)
153
Dan Albert90f7a4d2016-11-08 14:34:24 -0800154 return strconv.Itoa(intMax(version, firstArchVersion)), nil
155}
156
157func getFirstGeneratedVersion(firstSupportedVersion string, platformVersion int) (int, error) {
158 if firstSupportedVersion == "current" {
159 return platformVersion + 1, nil
160 }
161
162 return strconv.Atoi(firstSupportedVersion)
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700163}
164
Dan Alberte67144e2018-05-03 15:42:34 -0700165func shouldUseVersionScript(ctx android.BaseContext, stub *stubDecorator) (bool, error) {
Ryan Prichard37ebbde2018-07-24 12:37:24 -0700166 // unversioned_until is normally empty, in which case we should use the version script.
167 if String(stub.properties.Unversioned_until) == "" {
168 return true, nil
169 }
Dan Albert98dbb3b2017-01-03 15:16:29 -0800170
Nan Zhang0007d812017-11-07 10:57:05 -0800171 if String(stub.properties.Unversioned_until) == "current" {
Dan Albert022e7a32017-01-05 15:49:09 -0800172 if stub.properties.ApiLevel == "current" {
173 return true, nil
174 } else {
175 return false, nil
176 }
177 }
178
Dan Albert98dbb3b2017-01-03 15:16:29 -0800179 if stub.properties.ApiLevel == "current" {
180 return true, nil
181 }
182
Dan Alberte67144e2018-05-03 15:42:34 -0700183 unversionedUntil, err := android.ApiStrToNum(ctx, String(stub.properties.Unversioned_until))
Dan Albert98dbb3b2017-01-03 15:16:29 -0800184 if err != nil {
185 return true, err
186 }
187
Ryan Prichard37ebbde2018-07-24 12:37:24 -0700188 version, err := android.ApiStrToNum(ctx, stub.properties.ApiLevel)
189 if err != nil {
190 return true, err
Dan Alberte67144e2018-05-03 15:42:34 -0700191 }
192
Dan Albert98dbb3b2017-01-03 15:16:29 -0800193 return version >= unversionedUntil, nil
194}
195
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700196func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) {
Colin Cross6510f912017-11-29 00:27:14 -0800197 platformVersion := mctx.Config().PlatformSdkVersionInt()
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700198
Nan Zhang0007d812017-11-07 10:57:05 -0800199 firstSupportedVersion, err := normalizeNdkApiLevel(mctx, String(c.properties.First_version),
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700200 mctx.Arch())
201 if err != nil {
202 mctx.PropertyErrorf("first_version", err.Error())
Dan Albert914449f2016-06-17 16:45:24 -0700203 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700204
Dan Albert90f7a4d2016-11-08 14:34:24 -0800205 firstGenVersion, err := getFirstGeneratedVersion(firstSupportedVersion, platformVersion)
206 if err != nil {
207 // In theory this is impossible because we've already run this through
208 // normalizeNdkApiLevel above.
209 mctx.PropertyErrorf("first_version", err.Error())
210 }
211
Dan Albertfd86e9e2016-11-08 13:35:12 -0800212 var versionStrs []string
Dan Albert90f7a4d2016-11-08 14:34:24 -0800213 for version := firstGenVersion; version <= platformVersion; version++ {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800214 versionStrs = append(versionStrs, strconv.Itoa(version))
Dan Albert914449f2016-06-17 16:45:24 -0700215 }
Colin Cross6510f912017-11-29 00:27:14 -0800216 versionStrs = append(versionStrs, mctx.Config().PlatformVersionActiveCodenames()...)
Dan Albertfd86e9e2016-11-08 13:35:12 -0800217 versionStrs = append(versionStrs, "current")
Dan Albert914449f2016-06-17 16:45:24 -0700218
219 modules := mctx.CreateVariations(versionStrs...)
220 for i, module := range modules {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800221 module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = versionStrs[i]
Dan Albert914449f2016-06-17 16:45:24 -0700222 }
223}
224
225func ndkApiMutator(mctx android.BottomUpMutatorContext) {
226 if m, ok := mctx.Module().(*Module); ok {
Colin Crossd4025822017-04-13 12:53:07 -0700227 if m.Enabled() {
228 if compiler, ok := m.compiler.(*stubDecorator); ok {
229 generateStubApiVariants(mctx, compiler)
230 }
Dan Albert914449f2016-06-17 16:45:24 -0700231 }
232 }
233}
234
Colin Crossb916a382016-07-29 17:28:03 -0700235func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700236 c.baseCompiler.compilerInit(ctx)
237
Dan Willemsen01a90592017-04-07 15:21:13 -0700238 name := ctx.baseModuleName()
239 if strings.HasSuffix(name, ndkLibrarySuffix) {
240 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
241 }
242
Colin Crosse8a67a72016-08-07 21:17:54 -0700243 ndkMigratedLibsLock.Lock()
244 defer ndkMigratedLibsLock.Unlock()
Dan Albert7e9d2952016-08-04 13:02:36 -0700245 for _, lib := range ndkMigratedLibs {
246 if lib == name {
247 return
248 }
249 }
250 ndkMigratedLibs = append(ndkMigratedLibs, name)
251}
252
George Burgess IVf5310e32017-07-19 11:39:53 -0700253func addStubLibraryCompilerFlags(flags Flags) Flags {
254 flags.CFlags = append(flags.CFlags,
255 // We're knowingly doing some otherwise unsightly things with builtin
256 // functions here. We're just generating stub libraries, so ignore it.
257 "-Wno-incompatible-library-redeclaration",
258 "-Wno-builtin-requires-header",
259 "-Wno-invalid-noreturn",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800260 "-Wall",
261 "-Werror",
George Burgess IVf5310e32017-07-19 11:39:53 -0700262 // These libraries aren't actually used. Don't worry about unwinding
263 // (avoids the need to link an unwinder into a fake library).
264 "-fno-unwind-tables",
265 )
266 return flags
267}
268
Colin Crossf18e1102017-11-16 14:33:08 -0800269func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
270 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700271 return addStubLibraryCompilerFlags(flags)
272}
273
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900274func compileStubLibrary(ctx ModuleContext, flags Flags, symbolFile, apiLevel, genstubFlags string) (Objects, android.ModuleGenPath) {
Dan Albert914449f2016-06-17 16:45:24 -0700275 arch := ctx.Arch().ArchType.String()
276
Dan Willemsenb916b802017-03-19 13:44:32 -0700277 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
278 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
279 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albert49927d22017-03-28 15:00:46 -0700280 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700281 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700282 Rule: genStubSrc,
283 Description: "generate stubs " + symbolFilePath.Rel(),
284 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
285 Input: symbolFilePath,
286 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700287 Args: map[string]string{
288 "arch": arch,
Dan Willemsenb916b802017-03-19 13:44:32 -0700289 "apiLevel": apiLevel,
Dan Albert49927d22017-03-28 15:00:46 -0700290 "apiMap": apiLevelsJson.String(),
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900291 "flags": genstubFlags,
Dan Albert914449f2016-06-17 16:45:24 -0700292 },
293 })
294
Dan Albert914449f2016-06-17 16:45:24 -0700295 subdir := ""
Colin Cross2f336352016-10-26 10:03:47 -0700296 srcs := []android.Path{stubSrcPath}
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800297 return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil, nil), versionScriptPath
Dan Willemsenb916b802017-03-19 13:44:32 -0700298}
299
300func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800301 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700302 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
303 }
304
Nan Zhang0007d812017-11-07 10:57:05 -0800305 objs, versionScript := compileStubLibrary(ctx, flags, String(c.properties.Symbol_file),
306 c.properties.ApiLevel, "")
Dan Willemsenb916b802017-03-19 13:44:32 -0700307 c.versionScriptPath = versionScript
308 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700309}
310
Colin Cross37047f12016-12-13 17:06:13 -0800311func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700312 return Deps{}
313}
314
Dan Willemsen01a90592017-04-07 15:21:13 -0700315func (linker *stubDecorator) Name(name string) string {
316 return name + ndkLibrarySuffix
317}
318
Colin Crossb916a382016-07-29 17:28:03 -0700319func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700320 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700321 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700322}
323
Colin Crossb916a382016-07-29 17:28:03 -0700324func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700325 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700326
Dan Alberte67144e2018-05-03 15:42:34 -0700327 useVersionScript, err := shouldUseVersionScript(ctx, stub)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800328 if err != nil {
329 ctx.ModuleErrorf(err.Error())
330 }
331
332 if useVersionScript {
333 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
334 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
335 }
336
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700337 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700338}
339
Colin Crossb916a382016-07-29 17:28:03 -0700340func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700341 arch := ctx.Target().Arch.ArchType.Name
Colin Crossb916a382016-07-29 17:28:03 -0700342 apiLevel := stub.properties.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700343
344 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
345 // architectures it's just installed to lib.
346 libDir := "lib"
347 if ctx.toolchain().Is64Bit() && arch != "arm64" {
348 libDir = "lib64"
349 }
350
351 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albertfd86e9e2016-11-08 13:35:12 -0800352 "platforms/android-%s/arch-%s/usr/%s", apiLevel, arch, libDir))
Colin Cross0875c522017-11-28 17:34:01 -0800353 stub.installPath = ctx.InstallFile(installDir, path.Base(), path)
Dan Albert914449f2016-06-17 16:45:24 -0700354}
355
Colin Cross36242852017-06-23 15:06:31 -0700356func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800357 module, library := NewLibrary(android.DeviceSupported)
358 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700359 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700360 module.sanitize = nil
Nan Zhang0007d812017-11-07 10:57:05 -0800361 library.StripProperties.Strip.None = BoolPtr(true)
Dan Albert914449f2016-06-17 16:45:24 -0700362
Colin Crossb916a382016-07-29 17:28:03 -0700363 stub := &stubDecorator{
364 libraryDecorator: library,
365 }
366 module.compiler = stub
367 module.linker = stub
368 module.installer = stub
Dan Albert914449f2016-06-17 16:45:24 -0700369
Colin Cross36242852017-06-23 15:06:31 -0700370 module.AddProperties(&stub.properties, &library.MutatedProperties)
371
372 return module
Dan Albert914449f2016-06-17 16:45:24 -0700373}
374
Colin Cross36242852017-06-23 15:06:31 -0700375func ndkLibraryFactory() android.Module {
376 module := newStubLibrary()
377 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
378 return module
Dan Albert914449f2016-06-17 16:45:24 -0700379}