blob: db96325759d3aa782b17a9de6adf8475e34d73d2 [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 " +
34 "$apiMap $vndk $in $out",
Dan Albert914449f2016-06-17 16:45:24 -070035 CommandDeps: []string{"$toolPath"},
Dan Albert49927d22017-03-28 15:00:46 -070036 }, "arch", "apiLevel", "apiMap", "vndk")
Dan Albert914449f2016-06-17 16:45:24 -070037
38 ndkLibrarySuffix = ".ndk"
Colin Cross4d9c2d12016-07-29 12:48:20 -070039
40 ndkPrebuiltSharedLibs = []string{
41 "android",
42 "c",
43 "dl",
44 "EGL",
45 "GLESv1_CM",
46 "GLESv2",
47 "GLESv3",
48 "jnigraphics",
49 "log",
50 "mediandk",
51 "m",
52 "OpenMAXAL",
53 "OpenSLES",
54 "stdc++",
55 "vulkan",
56 "z",
57 }
58 ndkPrebuiltSharedLibraries = addPrefix(append([]string(nil), ndkPrebuiltSharedLibs...), "lib")
59
60 // These libraries have migrated over to the new ndk_library, which is added
61 // as a variation dependency via depsMutator.
Colin Crosse8a67a72016-08-07 21:17:54 -070062 ndkMigratedLibs = []string{}
63 ndkMigratedLibsLock sync.Mutex // protects ndkMigratedLibs writes during parallel beginMutator
Dan Albert914449f2016-06-17 16:45:24 -070064)
65
66// Creates a stub shared library based on the provided version file.
67//
Dan Albert914449f2016-06-17 16:45:24 -070068// Example:
69//
70// ndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070071// name: "libfoo",
Dan Albert914449f2016-06-17 16:45:24 -070072// symbol_file: "libfoo.map.txt",
73// first_version: "9",
74// }
75//
76type libraryProperties struct {
77 // Relative path to the symbol map.
78 // An example file can be seen here: TODO(danalbert): Make an example.
Nan Zhang0007d812017-11-07 10:57:05 -080079 Symbol_file *string
Dan Albert914449f2016-06-17 16:45:24 -070080
81 // The first API level a library was available. A library will be generated
82 // for every API level beginning with this one.
Nan Zhang0007d812017-11-07 10:57:05 -080083 First_version *string
Dan Albert914449f2016-06-17 16:45:24 -070084
Dan Albert98dbb3b2017-01-03 15:16:29 -080085 // The first API level that library should have the version script applied.
86 // This defaults to the value of first_version, and should almost never be
87 // used. This is only needed to work around platform bugs like
88 // https://github.com/android-ndk/ndk/issues/265.
Nan Zhang0007d812017-11-07 10:57:05 -080089 Unversioned_until *string
Dan Albert98dbb3b2017-01-03 15:16:29 -080090
Dan Albert914449f2016-06-17 16:45:24 -070091 // Private property for use by the mutator that splits per-API level.
Dan Albertfd86e9e2016-11-08 13:35:12 -080092 ApiLevel string `blueprint:"mutated"`
Dan Albert914449f2016-06-17 16:45:24 -070093}
94
Colin Crossb916a382016-07-29 17:28:03 -070095type stubDecorator struct {
96 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -070097
98 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -070099
Colin Crossb916a382016-07-29 17:28:03 -0700100 versionScriptPath android.ModuleGenPath
Colin Cross0875c522017-11-28 17:34:01 -0800101 installPath android.Path
Dan Albert914449f2016-06-17 16:45:24 -0700102}
103
104// OMG GO
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700105func intMax(a int, b int) int {
106 if a > b {
Dan Albert914449f2016-06-17 16:45:24 -0700107 return a
108 } else {
109 return b
110 }
111}
112
Dan Albertf5415d72017-08-17 16:19:59 -0700113func normalizeNdkApiLevel(ctx android.BaseContext, apiLevel string,
114 arch android.Arch) (string, error) {
115
Dan Albert90f7a4d2016-11-08 14:34:24 -0800116 if apiLevel == "current" {
117 return apiLevel, nil
118 }
119
Colin Cross6510f912017-11-29 00:27:14 -0800120 minVersion := ctx.Config().MinSupportedSdkVersion()
Colin Crossce87b802017-04-13 13:00:26 -0700121 firstArchVersions := map[android.ArchType]int{
Dan Albertf5415d72017-08-17 16:19:59 -0700122 android.Arm: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700123 android.Arm64: 21,
Dan Albertf5415d72017-08-17 16:19:59 -0700124 android.Mips: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700125 android.Mips64: 21,
Dan Albertf5415d72017-08-17 16:19:59 -0700126 android.X86: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700127 android.X86_64: 21,
Dan Albert914449f2016-06-17 16:45:24 -0700128 }
129
Colin Crossce87b802017-04-13 13:00:26 -0700130 firstArchVersion, ok := firstArchVersions[arch.ArchType]
Dan Albert2e5d7d42017-03-29 18:22:39 -0700131 if !ok {
Colin Crossce87b802017-04-13 13:00:26 -0700132 panic(fmt.Errorf("Arch %q not found in firstArchVersions", arch.ArchType))
Dan Albert2e5d7d42017-03-29 18:22:39 -0700133 }
134
135 if apiLevel == "minimum" {
136 return strconv.Itoa(firstArchVersion), nil
137 }
138
Dan Albert914449f2016-06-17 16:45:24 -0700139 // If the NDK drops support for a platform version, we don't want to have to
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700140 // fix up every module that was using it as its SDK version. Clip to the
Dan Albert914449f2016-06-17 16:45:24 -0700141 // supported version here instead.
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700142 version, err := strconv.Atoi(apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700143 if err != nil {
Dan Albert90f7a4d2016-11-08 14:34:24 -0800144 return "", fmt.Errorf("API level must be an integer (is %q)", apiLevel)
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
Dan Alberte67144e2018-05-03 15:42:34 -0700159func shouldUseVersionScript(ctx android.BaseContext, stub *stubDecorator) (bool, error) {
160 // https://github.com/android-ndk/ndk/issues/622
161 // The loader spews warnings to stderr on L-MR1 when loading a library that
162 // has symbol versioning.
163 firstVersionSupportingRelease := 23
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 version, err := android.ApiStrToNum(ctx, stub.properties.ApiLevel)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800178 if err != nil {
179 return true, err
180 }
181
Dan Alberte67144e2018-05-03 15:42:34 -0700182 // unversioned_until is normally empty, in which case we use the version
183 // script as long as we are on a supported API level.
184 if String(stub.properties.Unversioned_until) == "" {
185 return version >= firstVersionSupportingRelease, nil
186 }
187
188 unversionedUntil, err := android.ApiStrToNum(ctx, String(stub.properties.Unversioned_until))
Dan Albert98dbb3b2017-01-03 15:16:29 -0800189 if err != nil {
190 return true, err
191 }
192
Dan Alberte67144e2018-05-03 15:42:34 -0700193 if unversionedUntil < firstVersionSupportingRelease {
194 return true, fmt.Errorf("unversioned_until must be at least %d",
195 firstVersionSupportingRelease)
196 }
197
198 if version < firstVersionSupportingRelease {
199 return false, nil
200 }
201
Dan Albert98dbb3b2017-01-03 15:16:29 -0800202 return version >= unversionedUntil, nil
203}
204
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700205func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) {
Colin Cross6510f912017-11-29 00:27:14 -0800206 platformVersion := mctx.Config().PlatformSdkVersionInt()
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700207
Nan Zhang0007d812017-11-07 10:57:05 -0800208 firstSupportedVersion, err := normalizeNdkApiLevel(mctx, String(c.properties.First_version),
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700209 mctx.Arch())
210 if err != nil {
211 mctx.PropertyErrorf("first_version", err.Error())
Dan Albert914449f2016-06-17 16:45:24 -0700212 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700213
Dan Albert90f7a4d2016-11-08 14:34:24 -0800214 firstGenVersion, err := getFirstGeneratedVersion(firstSupportedVersion, platformVersion)
215 if err != nil {
216 // In theory this is impossible because we've already run this through
217 // normalizeNdkApiLevel above.
218 mctx.PropertyErrorf("first_version", err.Error())
219 }
220
Dan Albertfd86e9e2016-11-08 13:35:12 -0800221 var versionStrs []string
Dan Albert90f7a4d2016-11-08 14:34:24 -0800222 for version := firstGenVersion; version <= platformVersion; version++ {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800223 versionStrs = append(versionStrs, strconv.Itoa(version))
Dan Albert914449f2016-06-17 16:45:24 -0700224 }
Colin Cross6510f912017-11-29 00:27:14 -0800225 versionStrs = append(versionStrs, mctx.Config().PlatformVersionActiveCodenames()...)
Dan Albertfd86e9e2016-11-08 13:35:12 -0800226 versionStrs = append(versionStrs, "current")
Dan Albert914449f2016-06-17 16:45:24 -0700227
228 modules := mctx.CreateVariations(versionStrs...)
229 for i, module := range modules {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800230 module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = versionStrs[i]
Dan Albert914449f2016-06-17 16:45:24 -0700231 }
232}
233
234func ndkApiMutator(mctx android.BottomUpMutatorContext) {
235 if m, ok := mctx.Module().(*Module); ok {
Colin Crossd4025822017-04-13 12:53:07 -0700236 if m.Enabled() {
237 if compiler, ok := m.compiler.(*stubDecorator); ok {
238 generateStubApiVariants(mctx, compiler)
239 }
Dan Albert914449f2016-06-17 16:45:24 -0700240 }
241 }
242}
243
Colin Crossb916a382016-07-29 17:28:03 -0700244func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700245 c.baseCompiler.compilerInit(ctx)
246
Dan Willemsen01a90592017-04-07 15:21:13 -0700247 name := ctx.baseModuleName()
248 if strings.HasSuffix(name, ndkLibrarySuffix) {
249 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
250 }
251
Colin Crosse8a67a72016-08-07 21:17:54 -0700252 ndkMigratedLibsLock.Lock()
253 defer ndkMigratedLibsLock.Unlock()
Dan Albert7e9d2952016-08-04 13:02:36 -0700254 for _, lib := range ndkMigratedLibs {
255 if lib == name {
256 return
257 }
258 }
259 ndkMigratedLibs = append(ndkMigratedLibs, name)
260}
261
George Burgess IVf5310e32017-07-19 11:39:53 -0700262func addStubLibraryCompilerFlags(flags Flags) Flags {
263 flags.CFlags = append(flags.CFlags,
264 // We're knowingly doing some otherwise unsightly things with builtin
265 // functions here. We're just generating stub libraries, so ignore it.
266 "-Wno-incompatible-library-redeclaration",
267 "-Wno-builtin-requires-header",
268 "-Wno-invalid-noreturn",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800269 "-Wall",
270 "-Werror",
George Burgess IVf5310e32017-07-19 11:39:53 -0700271 // These libraries aren't actually used. Don't worry about unwinding
272 // (avoids the need to link an unwinder into a fake library).
273 "-fno-unwind-tables",
274 )
275 return flags
276}
277
Colin Crossf18e1102017-11-16 14:33:08 -0800278func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
279 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700280 return addStubLibraryCompilerFlags(flags)
281}
282
Dan Willemsenb916b802017-03-19 13:44:32 -0700283func compileStubLibrary(ctx ModuleContext, flags Flags, symbolFile, apiLevel, vndk string) (Objects, android.ModuleGenPath) {
Dan Albert914449f2016-06-17 16:45:24 -0700284 arch := ctx.Arch().ArchType.String()
285
Dan Willemsenb916b802017-03-19 13:44:32 -0700286 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
287 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
288 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albert49927d22017-03-28 15:00:46 -0700289 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700290 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700291 Rule: genStubSrc,
292 Description: "generate stubs " + symbolFilePath.Rel(),
293 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
294 Input: symbolFilePath,
295 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700296 Args: map[string]string{
297 "arch": arch,
Dan Willemsenb916b802017-03-19 13:44:32 -0700298 "apiLevel": apiLevel,
Dan Albert49927d22017-03-28 15:00:46 -0700299 "apiMap": apiLevelsJson.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700300 "vndk": vndk,
Dan Albert914449f2016-06-17 16:45:24 -0700301 },
302 })
303
Dan Albert914449f2016-06-17 16:45:24 -0700304 subdir := ""
Colin Cross2f336352016-10-26 10:03:47 -0700305 srcs := []android.Path{stubSrcPath}
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800306 return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil, nil), versionScriptPath
Dan Willemsenb916b802017-03-19 13:44:32 -0700307}
308
309func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800310 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700311 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
312 }
313
Nan Zhang0007d812017-11-07 10:57:05 -0800314 objs, versionScript := compileStubLibrary(ctx, flags, String(c.properties.Symbol_file),
315 c.properties.ApiLevel, "")
Dan Willemsenb916b802017-03-19 13:44:32 -0700316 c.versionScriptPath = versionScript
317 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700318}
319
Colin Cross37047f12016-12-13 17:06:13 -0800320func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700321 return Deps{}
322}
323
Dan Willemsen01a90592017-04-07 15:21:13 -0700324func (linker *stubDecorator) Name(name string) string {
325 return name + ndkLibrarySuffix
326}
327
Colin Crossb916a382016-07-29 17:28:03 -0700328func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700329 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700330 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700331}
332
Colin Crossb916a382016-07-29 17:28:03 -0700333func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700334 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700335
Dan Alberte67144e2018-05-03 15:42:34 -0700336 useVersionScript, err := shouldUseVersionScript(ctx, stub)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800337 if err != nil {
338 ctx.ModuleErrorf(err.Error())
339 }
340
341 if useVersionScript {
342 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
343 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
344 }
345
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700346 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700347}
348
Colin Crossb916a382016-07-29 17:28:03 -0700349func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700350 arch := ctx.Target().Arch.ArchType.Name
Colin Crossb916a382016-07-29 17:28:03 -0700351 apiLevel := stub.properties.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700352
353 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
354 // architectures it's just installed to lib.
355 libDir := "lib"
356 if ctx.toolchain().Is64Bit() && arch != "arm64" {
357 libDir = "lib64"
358 }
359
360 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albertfd86e9e2016-11-08 13:35:12 -0800361 "platforms/android-%s/arch-%s/usr/%s", apiLevel, arch, libDir))
Colin Cross0875c522017-11-28 17:34:01 -0800362 stub.installPath = ctx.InstallFile(installDir, path.Base(), path)
Dan Albert914449f2016-06-17 16:45:24 -0700363}
364
Colin Cross36242852017-06-23 15:06:31 -0700365func newStubLibrary() *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800366 module, library := NewLibrary(android.DeviceSupported)
367 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700368 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700369 module.sanitize = nil
Nan Zhang0007d812017-11-07 10:57:05 -0800370 library.StripProperties.Strip.None = BoolPtr(true)
Dan Albert914449f2016-06-17 16:45:24 -0700371
Colin Crossb916a382016-07-29 17:28:03 -0700372 stub := &stubDecorator{
373 libraryDecorator: library,
374 }
375 module.compiler = stub
376 module.linker = stub
377 module.installer = stub
Dan Albert914449f2016-06-17 16:45:24 -0700378
Colin Cross36242852017-06-23 15:06:31 -0700379 module.AddProperties(&stub.properties, &library.MutatedProperties)
380
381 return module
Dan Albert914449f2016-06-17 16:45:24 -0700382}
383
Colin Cross36242852017-06-23 15:06:31 -0700384func ndkLibraryFactory() android.Module {
385 module := newStubLibrary()
386 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
387 return module
Dan Albert914449f2016-06-17 16:45:24 -0700388}