blob: 3ae4452555e4c4ef06f5a1d4180ef6fe02c25a8f [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++",
Dan Willemsen62b9cf92019-02-06 18:40:16 -080056 "sync",
Colin Cross4d9c2d12016-07-29 12:48:20 -070057 "vulkan",
58 "z",
59 }
60 ndkPrebuiltSharedLibraries = addPrefix(append([]string(nil), ndkPrebuiltSharedLibs...), "lib")
61
62 // These libraries have migrated over to the new ndk_library, which is added
63 // as a variation dependency via depsMutator.
Colin Crosse8a67a72016-08-07 21:17:54 -070064 ndkMigratedLibs = []string{}
Colin Crosse40b4ea2018-10-02 22:25:58 -070065 ndkMigratedLibsLock sync.Mutex // protects ndkMigratedLibs writes during parallel BeginMutator
Dan Albert914449f2016-06-17 16:45:24 -070066)
67
68// Creates a stub shared library based on the provided version file.
69//
Dan Albert914449f2016-06-17 16:45:24 -070070// Example:
71//
72// ndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070073// name: "libfoo",
Dan Albert914449f2016-06-17 16:45:24 -070074// symbol_file: "libfoo.map.txt",
75// first_version: "9",
76// }
77//
78type libraryProperties struct {
79 // Relative path to the symbol map.
80 // An example file can be seen here: TODO(danalbert): Make an example.
Nan Zhang0007d812017-11-07 10:57:05 -080081 Symbol_file *string
Dan Albert914449f2016-06-17 16:45:24 -070082
83 // The first API level a library was available. A library will be generated
84 // for every API level beginning with this one.
Nan Zhang0007d812017-11-07 10:57:05 -080085 First_version *string
Dan Albert914449f2016-06-17 16:45:24 -070086
Dan Albert98dbb3b2017-01-03 15:16:29 -080087 // The first API level that library should have the version script applied.
88 // This defaults to the value of first_version, and should almost never be
89 // used. This is only needed to work around platform bugs like
90 // https://github.com/android-ndk/ndk/issues/265.
Nan Zhang0007d812017-11-07 10:57:05 -080091 Unversioned_until *string
Dan Albert98dbb3b2017-01-03 15:16:29 -080092
Dan Albert914449f2016-06-17 16:45:24 -070093 // Private property for use by the mutator that splits per-API level.
Dan Albertfd86e9e2016-11-08 13:35:12 -080094 ApiLevel string `blueprint:"mutated"`
Dan Albert23d37e02018-11-28 08:30:10 -080095
96 // True if this API is not yet ready to be shipped in the NDK. It will be
97 // available in the platform for testing, but will be excluded from the
98 // sysroot provided to the NDK proper.
99 Draft bool
Dan Albert914449f2016-06-17 16:45:24 -0700100}
101
Colin Crossb916a382016-07-29 17:28:03 -0700102type stubDecorator struct {
103 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -0700104
105 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -0700106
Colin Crossb916a382016-07-29 17:28:03 -0700107 versionScriptPath android.ModuleGenPath
Colin Cross0875c522017-11-28 17:34:01 -0800108 installPath android.Path
Dan Albert914449f2016-06-17 16:45:24 -0700109}
110
111// OMG GO
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700112func intMax(a int, b int) int {
113 if a > b {
Dan Albert914449f2016-06-17 16:45:24 -0700114 return a
115 } else {
116 return b
117 }
118}
119
Dan Albertf5415d72017-08-17 16:19:59 -0700120func normalizeNdkApiLevel(ctx android.BaseContext, apiLevel string,
121 arch android.Arch) (string, error) {
122
Dan Albert90f7a4d2016-11-08 14:34:24 -0800123 if apiLevel == "current" {
124 return apiLevel, nil
125 }
126
Colin Cross6510f912017-11-29 00:27:14 -0800127 minVersion := ctx.Config().MinSupportedSdkVersion()
Colin Crossce87b802017-04-13 13:00:26 -0700128 firstArchVersions := map[android.ArchType]int{
Dan Albertf5415d72017-08-17 16:19:59 -0700129 android.Arm: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700130 android.Arm64: 21,
Dan Albertf5415d72017-08-17 16:19:59 -0700131 android.Mips: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700132 android.Mips64: 21,
Dan Albertf5415d72017-08-17 16:19:59 -0700133 android.X86: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700134 android.X86_64: 21,
Dan Albert914449f2016-06-17 16:45:24 -0700135 }
136
Colin Crossce87b802017-04-13 13:00:26 -0700137 firstArchVersion, ok := firstArchVersions[arch.ArchType]
Dan Albert2e5d7d42017-03-29 18:22:39 -0700138 if !ok {
Colin Crossce87b802017-04-13 13:00:26 -0700139 panic(fmt.Errorf("Arch %q not found in firstArchVersions", arch.ArchType))
Dan Albert2e5d7d42017-03-29 18:22:39 -0700140 }
141
142 if apiLevel == "minimum" {
143 return strconv.Itoa(firstArchVersion), nil
144 }
145
Dan Albert914449f2016-06-17 16:45:24 -0700146 // If the NDK drops support for a platform version, we don't want to have to
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700147 // fix up every module that was using it as its SDK version. Clip to the
Dan Albert914449f2016-06-17 16:45:24 -0700148 // supported version here instead.
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700149 version, err := strconv.Atoi(apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700150 if err != nil {
Dan Albert90f7a4d2016-11-08 14:34:24 -0800151 return "", fmt.Errorf("API level must be an integer (is %q)", apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700152 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700153 version = intMax(version, minVersion)
154
Dan Albert90f7a4d2016-11-08 14:34:24 -0800155 return strconv.Itoa(intMax(version, firstArchVersion)), nil
156}
157
158func getFirstGeneratedVersion(firstSupportedVersion string, platformVersion int) (int, error) {
159 if firstSupportedVersion == "current" {
160 return platformVersion + 1, nil
161 }
162
163 return strconv.Atoi(firstSupportedVersion)
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700164}
165
Dan Alberte67144e2018-05-03 15:42:34 -0700166func shouldUseVersionScript(ctx android.BaseContext, stub *stubDecorator) (bool, error) {
Ryan Prichard37ebbde2018-07-24 12:37:24 -0700167 // unversioned_until is normally empty, in which case we should use the version script.
168 if String(stub.properties.Unversioned_until) == "" {
169 return true, nil
170 }
Dan Albert98dbb3b2017-01-03 15:16:29 -0800171
Nan Zhang0007d812017-11-07 10:57:05 -0800172 if String(stub.properties.Unversioned_until) == "current" {
Dan Albert022e7a32017-01-05 15:49:09 -0800173 if stub.properties.ApiLevel == "current" {
174 return true, nil
175 } else {
176 return false, nil
177 }
178 }
179
Dan Albert98dbb3b2017-01-03 15:16:29 -0800180 if stub.properties.ApiLevel == "current" {
181 return true, nil
182 }
183
Dan Alberte67144e2018-05-03 15:42:34 -0700184 unversionedUntil, err := android.ApiStrToNum(ctx, String(stub.properties.Unversioned_until))
Dan Albert98dbb3b2017-01-03 15:16:29 -0800185 if err != nil {
186 return true, err
187 }
188
Ryan Prichard37ebbde2018-07-24 12:37:24 -0700189 version, err := android.ApiStrToNum(ctx, stub.properties.ApiLevel)
190 if err != nil {
191 return true, err
Dan Alberte67144e2018-05-03 15:42:34 -0700192 }
193
Dan Albert98dbb3b2017-01-03 15:16:29 -0800194 return version >= unversionedUntil, nil
195}
196
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700197func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) {
Colin Cross6510f912017-11-29 00:27:14 -0800198 platformVersion := mctx.Config().PlatformSdkVersionInt()
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700199
Nan Zhang0007d812017-11-07 10:57:05 -0800200 firstSupportedVersion, err := normalizeNdkApiLevel(mctx, String(c.properties.First_version),
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700201 mctx.Arch())
202 if err != nil {
203 mctx.PropertyErrorf("first_version", err.Error())
Dan Albert914449f2016-06-17 16:45:24 -0700204 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700205
Dan Albert90f7a4d2016-11-08 14:34:24 -0800206 firstGenVersion, err := getFirstGeneratedVersion(firstSupportedVersion, platformVersion)
207 if err != nil {
208 // In theory this is impossible because we've already run this through
209 // normalizeNdkApiLevel above.
210 mctx.PropertyErrorf("first_version", err.Error())
211 }
212
Dan Albertfd86e9e2016-11-08 13:35:12 -0800213 var versionStrs []string
Dan Albert90f7a4d2016-11-08 14:34:24 -0800214 for version := firstGenVersion; version <= platformVersion; version++ {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800215 versionStrs = append(versionStrs, strconv.Itoa(version))
Dan Albert914449f2016-06-17 16:45:24 -0700216 }
Colin Cross6510f912017-11-29 00:27:14 -0800217 versionStrs = append(versionStrs, mctx.Config().PlatformVersionActiveCodenames()...)
Dan Albertfd86e9e2016-11-08 13:35:12 -0800218 versionStrs = append(versionStrs, "current")
Dan Albert914449f2016-06-17 16:45:24 -0700219
220 modules := mctx.CreateVariations(versionStrs...)
221 for i, module := range modules {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800222 module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = versionStrs[i]
Dan Albert914449f2016-06-17 16:45:24 -0700223 }
224}
225
226func ndkApiMutator(mctx android.BottomUpMutatorContext) {
227 if m, ok := mctx.Module().(*Module); ok {
Colin Crossd4025822017-04-13 12:53:07 -0700228 if m.Enabled() {
229 if compiler, ok := m.compiler.(*stubDecorator); ok {
230 generateStubApiVariants(mctx, compiler)
231 }
Dan Albert914449f2016-06-17 16:45:24 -0700232 }
233 }
234}
235
Colin Crossb916a382016-07-29 17:28:03 -0700236func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700237 c.baseCompiler.compilerInit(ctx)
238
Dan Willemsen01a90592017-04-07 15:21:13 -0700239 name := ctx.baseModuleName()
240 if strings.HasSuffix(name, ndkLibrarySuffix) {
241 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
242 }
243
Colin Crosse8a67a72016-08-07 21:17:54 -0700244 ndkMigratedLibsLock.Lock()
245 defer ndkMigratedLibsLock.Unlock()
Dan Albert7e9d2952016-08-04 13:02:36 -0700246 for _, lib := range ndkMigratedLibs {
247 if lib == name {
248 return
249 }
250 }
251 ndkMigratedLibs = append(ndkMigratedLibs, name)
252}
253
George Burgess IVf5310e32017-07-19 11:39:53 -0700254func addStubLibraryCompilerFlags(flags Flags) Flags {
255 flags.CFlags = append(flags.CFlags,
256 // We're knowingly doing some otherwise unsightly things with builtin
257 // functions here. We're just generating stub libraries, so ignore it.
258 "-Wno-incompatible-library-redeclaration",
259 "-Wno-builtin-requires-header",
260 "-Wno-invalid-noreturn",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800261 "-Wall",
262 "-Werror",
George Burgess IVf5310e32017-07-19 11:39:53 -0700263 // These libraries aren't actually used. Don't worry about unwinding
264 // (avoids the need to link an unwinder into a fake library).
265 "-fno-unwind-tables",
266 )
267 return flags
268}
269
Colin Crossf18e1102017-11-16 14:33:08 -0800270func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
271 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700272 return addStubLibraryCompilerFlags(flags)
273}
274
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900275func compileStubLibrary(ctx ModuleContext, flags Flags, symbolFile, apiLevel, genstubFlags string) (Objects, android.ModuleGenPath) {
Dan Albert914449f2016-06-17 16:45:24 -0700276 arch := ctx.Arch().ArchType.String()
277
Dan Willemsenb916b802017-03-19 13:44:32 -0700278 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
279 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
280 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albert49927d22017-03-28 15:00:46 -0700281 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700282 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700283 Rule: genStubSrc,
284 Description: "generate stubs " + symbolFilePath.Rel(),
285 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
286 Input: symbolFilePath,
287 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700288 Args: map[string]string{
289 "arch": arch,
Dan Willemsenb916b802017-03-19 13:44:32 -0700290 "apiLevel": apiLevel,
Dan Albert49927d22017-03-28 15:00:46 -0700291 "apiMap": apiLevelsJson.String(),
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900292 "flags": genstubFlags,
Dan Albert914449f2016-06-17 16:45:24 -0700293 },
294 })
295
Dan Albert914449f2016-06-17 16:45:24 -0700296 subdir := ""
Colin Cross2f336352016-10-26 10:03:47 -0700297 srcs := []android.Path{stubSrcPath}
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800298 return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil, nil), versionScriptPath
Dan Willemsenb916b802017-03-19 13:44:32 -0700299}
300
301func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800302 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700303 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
304 }
305
Nan Zhang0007d812017-11-07 10:57:05 -0800306 objs, versionScript := compileStubLibrary(ctx, flags, String(c.properties.Symbol_file),
307 c.properties.ApiLevel, "")
Dan Willemsenb916b802017-03-19 13:44:32 -0700308 c.versionScriptPath = versionScript
309 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700310}
311
Colin Cross37047f12016-12-13 17:06:13 -0800312func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700313 return Deps{}
314}
315
Dan Willemsen01a90592017-04-07 15:21:13 -0700316func (linker *stubDecorator) Name(name string) string {
317 return name + ndkLibrarySuffix
318}
319
Colin Crossb916a382016-07-29 17:28:03 -0700320func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700321 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700322 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700323}
324
Colin Crossb916a382016-07-29 17:28:03 -0700325func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700326 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700327
Dan Alberte67144e2018-05-03 15:42:34 -0700328 useVersionScript, err := shouldUseVersionScript(ctx, stub)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800329 if err != nil {
330 ctx.ModuleErrorf(err.Error())
331 }
332
333 if useVersionScript {
334 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
335 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
336 }
337
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700338 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700339}
340
Colin Crossb916a382016-07-29 17:28:03 -0700341func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700342 arch := ctx.Target().Arch.ArchType.Name
Colin Crossb916a382016-07-29 17:28:03 -0700343 apiLevel := stub.properties.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700344
345 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
346 // architectures it's just installed to lib.
347 libDir := "lib"
348 if ctx.toolchain().Is64Bit() && arch != "arm64" {
349 libDir = "lib64"
350 }
351
352 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albertfd86e9e2016-11-08 13:35:12 -0800353 "platforms/android-%s/arch-%s/usr/%s", apiLevel, arch, libDir))
Colin Cross0875c522017-11-28 17:34:01 -0800354 stub.installPath = ctx.InstallFile(installDir, path.Base(), path)
Dan Albert914449f2016-06-17 16:45:24 -0700355}
356
Colin Cross36242852017-06-23 15:06:31 -0700357func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800358 module, library := NewLibrary(android.DeviceSupported)
359 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700360 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700361 module.sanitize = nil
Nan Zhang0007d812017-11-07 10:57:05 -0800362 library.StripProperties.Strip.None = BoolPtr(true)
Dan Albert914449f2016-06-17 16:45:24 -0700363
Colin Crossb916a382016-07-29 17:28:03 -0700364 stub := &stubDecorator{
365 libraryDecorator: library,
366 }
367 module.compiler = stub
368 module.linker = stub
369 module.installer = stub
Dan Albert914449f2016-06-17 16:45:24 -0700370
Colin Cross36242852017-06-23 15:06:31 -0700371 module.AddProperties(&stub.properties, &library.MutatedProperties)
372
373 return module
Dan Albert914449f2016-06-17 16:45:24 -0700374}
375
Colin Cross36242852017-06-23 15:06:31 -0700376func ndkLibraryFactory() android.Module {
377 module := newStubLibrary()
378 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
379 return module
Dan Albert914449f2016-06-17 16:45:24 -0700380}