blob: 63d9f2994be235371832e2000d8e782ce8279010 [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",
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 Albert914449f2016-06-17 16:45:24 -070094}
95
Colin Crossb916a382016-07-29 17:28:03 -070096type stubDecorator struct {
97 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -070098
99 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -0700100
Colin Crossb916a382016-07-29 17:28:03 -0700101 versionScriptPath android.ModuleGenPath
Colin Cross0875c522017-11-28 17:34:01 -0800102 installPath android.Path
Dan Albert914449f2016-06-17 16:45:24 -0700103}
104
105// OMG GO
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700106func intMax(a int, b int) int {
107 if a > b {
Dan Albert914449f2016-06-17 16:45:24 -0700108 return a
109 } else {
110 return b
111 }
112}
113
Dan Albertf5415d72017-08-17 16:19:59 -0700114func normalizeNdkApiLevel(ctx android.BaseContext, apiLevel string,
115 arch android.Arch) (string, error) {
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.Mips: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700126 android.Mips64: 21,
Dan Albertf5415d72017-08-17 16:19:59 -0700127 android.X86: minVersion,
Colin Crossce87b802017-04-13 13:00:26 -0700128 android.X86_64: 21,
Dan Albert914449f2016-06-17 16:45:24 -0700129 }
130
Colin Crossce87b802017-04-13 13:00:26 -0700131 firstArchVersion, ok := firstArchVersions[arch.ArchType]
Dan Albert2e5d7d42017-03-29 18:22:39 -0700132 if !ok {
Colin Crossce87b802017-04-13 13:00:26 -0700133 panic(fmt.Errorf("Arch %q not found in firstArchVersions", arch.ArchType))
Dan Albert2e5d7d42017-03-29 18:22:39 -0700134 }
135
136 if apiLevel == "minimum" {
137 return strconv.Itoa(firstArchVersion), nil
138 }
139
Dan Albert914449f2016-06-17 16:45:24 -0700140 // If the NDK drops support for a platform version, we don't want to have to
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700141 // fix up every module that was using it as its SDK version. Clip to the
Dan Albert914449f2016-06-17 16:45:24 -0700142 // supported version here instead.
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700143 version, err := strconv.Atoi(apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700144 if err != nil {
Dan Albert90f7a4d2016-11-08 14:34:24 -0800145 return "", fmt.Errorf("API level must be an integer (is %q)", apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700146 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700147 version = intMax(version, minVersion)
148
Dan Albert90f7a4d2016-11-08 14:34:24 -0800149 return strconv.Itoa(intMax(version, firstArchVersion)), nil
150}
151
152func getFirstGeneratedVersion(firstSupportedVersion string, platformVersion int) (int, error) {
153 if firstSupportedVersion == "current" {
154 return platformVersion + 1, nil
155 }
156
157 return strconv.Atoi(firstSupportedVersion)
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700158}
159
Dan Alberte67144e2018-05-03 15:42:34 -0700160func shouldUseVersionScript(ctx android.BaseContext, stub *stubDecorator) (bool, error) {
Ryan Prichard37ebbde2018-07-24 12:37:24 -0700161 // unversioned_until is normally empty, in which case we should use the version script.
162 if String(stub.properties.Unversioned_until) == "" {
163 return true, nil
164 }
Dan Albert98dbb3b2017-01-03 15:16:29 -0800165
Nan Zhang0007d812017-11-07 10:57:05 -0800166 if String(stub.properties.Unversioned_until) == "current" {
Dan Albert022e7a32017-01-05 15:49:09 -0800167 if stub.properties.ApiLevel == "current" {
168 return true, nil
169 } else {
170 return false, nil
171 }
172 }
173
Dan Albert98dbb3b2017-01-03 15:16:29 -0800174 if stub.properties.ApiLevel == "current" {
175 return true, nil
176 }
177
Dan Alberte67144e2018-05-03 15:42:34 -0700178 unversionedUntil, err := android.ApiStrToNum(ctx, String(stub.properties.Unversioned_until))
Dan Albert98dbb3b2017-01-03 15:16:29 -0800179 if err != nil {
180 return true, err
181 }
182
Ryan Prichard37ebbde2018-07-24 12:37:24 -0700183 version, err := android.ApiStrToNum(ctx, stub.properties.ApiLevel)
184 if err != nil {
185 return true, err
Dan Alberte67144e2018-05-03 15:42:34 -0700186 }
187
Dan Albert98dbb3b2017-01-03 15:16:29 -0800188 return version >= unversionedUntil, nil
189}
190
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700191func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) {
Colin Cross6510f912017-11-29 00:27:14 -0800192 platformVersion := mctx.Config().PlatformSdkVersionInt()
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700193
Nan Zhang0007d812017-11-07 10:57:05 -0800194 firstSupportedVersion, err := normalizeNdkApiLevel(mctx, String(c.properties.First_version),
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700195 mctx.Arch())
196 if err != nil {
197 mctx.PropertyErrorf("first_version", err.Error())
Dan Albert914449f2016-06-17 16:45:24 -0700198 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700199
Dan Albert90f7a4d2016-11-08 14:34:24 -0800200 firstGenVersion, err := getFirstGeneratedVersion(firstSupportedVersion, platformVersion)
201 if err != nil {
202 // In theory this is impossible because we've already run this through
203 // normalizeNdkApiLevel above.
204 mctx.PropertyErrorf("first_version", err.Error())
205 }
206
Dan Albertfd86e9e2016-11-08 13:35:12 -0800207 var versionStrs []string
Dan Albert90f7a4d2016-11-08 14:34:24 -0800208 for version := firstGenVersion; version <= platformVersion; version++ {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800209 versionStrs = append(versionStrs, strconv.Itoa(version))
Dan Albert914449f2016-06-17 16:45:24 -0700210 }
Colin Cross6510f912017-11-29 00:27:14 -0800211 versionStrs = append(versionStrs, mctx.Config().PlatformVersionActiveCodenames()...)
Dan Albertfd86e9e2016-11-08 13:35:12 -0800212 versionStrs = append(versionStrs, "current")
Dan Albert914449f2016-06-17 16:45:24 -0700213
214 modules := mctx.CreateVariations(versionStrs...)
215 for i, module := range modules {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800216 module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = versionStrs[i]
Dan Albert914449f2016-06-17 16:45:24 -0700217 }
218}
219
220func ndkApiMutator(mctx android.BottomUpMutatorContext) {
221 if m, ok := mctx.Module().(*Module); ok {
Colin Crossd4025822017-04-13 12:53:07 -0700222 if m.Enabled() {
223 if compiler, ok := m.compiler.(*stubDecorator); ok {
224 generateStubApiVariants(mctx, compiler)
225 }
Dan Albert914449f2016-06-17 16:45:24 -0700226 }
227 }
228}
229
Colin Crossb916a382016-07-29 17:28:03 -0700230func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700231 c.baseCompiler.compilerInit(ctx)
232
Dan Willemsen01a90592017-04-07 15:21:13 -0700233 name := ctx.baseModuleName()
234 if strings.HasSuffix(name, ndkLibrarySuffix) {
235 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
236 }
237
Colin Crosse8a67a72016-08-07 21:17:54 -0700238 ndkMigratedLibsLock.Lock()
239 defer ndkMigratedLibsLock.Unlock()
Dan Albert7e9d2952016-08-04 13:02:36 -0700240 for _, lib := range ndkMigratedLibs {
241 if lib == name {
242 return
243 }
244 }
245 ndkMigratedLibs = append(ndkMigratedLibs, name)
246}
247
George Burgess IVf5310e32017-07-19 11:39:53 -0700248func addStubLibraryCompilerFlags(flags Flags) Flags {
249 flags.CFlags = append(flags.CFlags,
250 // We're knowingly doing some otherwise unsightly things with builtin
251 // functions here. We're just generating stub libraries, so ignore it.
252 "-Wno-incompatible-library-redeclaration",
253 "-Wno-builtin-requires-header",
254 "-Wno-invalid-noreturn",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800255 "-Wall",
256 "-Werror",
George Burgess IVf5310e32017-07-19 11:39:53 -0700257 // These libraries aren't actually used. Don't worry about unwinding
258 // (avoids the need to link an unwinder into a fake library).
259 "-fno-unwind-tables",
260 )
261 return flags
262}
263
Colin Crossf18e1102017-11-16 14:33:08 -0800264func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
265 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -0700266 return addStubLibraryCompilerFlags(flags)
267}
268
Dan Willemsenb916b802017-03-19 13:44:32 -0700269func compileStubLibrary(ctx ModuleContext, flags Flags, symbolFile, apiLevel, vndk string) (Objects, android.ModuleGenPath) {
Dan Albert914449f2016-06-17 16:45:24 -0700270 arch := ctx.Arch().ArchType.String()
271
Dan Willemsenb916b802017-03-19 13:44:32 -0700272 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
273 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
274 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albert49927d22017-03-28 15:00:46 -0700275 apiLevelsJson := android.GetApiLevelsJson(ctx)
Colin Crossae887032017-10-23 17:16:14 -0700276 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700277 Rule: genStubSrc,
278 Description: "generate stubs " + symbolFilePath.Rel(),
279 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
280 Input: symbolFilePath,
281 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700282 Args: map[string]string{
283 "arch": arch,
Dan Willemsenb916b802017-03-19 13:44:32 -0700284 "apiLevel": apiLevel,
Dan Albert49927d22017-03-28 15:00:46 -0700285 "apiMap": apiLevelsJson.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700286 "vndk": vndk,
Dan Albert914449f2016-06-17 16:45:24 -0700287 },
288 })
289
Dan Albert914449f2016-06-17 16:45:24 -0700290 subdir := ""
Colin Cross2f336352016-10-26 10:03:47 -0700291 srcs := []android.Path{stubSrcPath}
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800292 return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil, nil), versionScriptPath
Dan Willemsenb916b802017-03-19 13:44:32 -0700293}
294
295func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Nan Zhang0007d812017-11-07 10:57:05 -0800296 if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") {
Dan Albert15be0c62017-06-13 15:14:56 -0700297 ctx.PropertyErrorf("symbol_file", "must end with .map.txt")
298 }
299
Nan Zhang0007d812017-11-07 10:57:05 -0800300 objs, versionScript := compileStubLibrary(ctx, flags, String(c.properties.Symbol_file),
301 c.properties.ApiLevel, "")
Dan Willemsenb916b802017-03-19 13:44:32 -0700302 c.versionScriptPath = versionScript
303 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700304}
305
Colin Cross37047f12016-12-13 17:06:13 -0800306func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700307 return Deps{}
308}
309
Dan Willemsen01a90592017-04-07 15:21:13 -0700310func (linker *stubDecorator) Name(name string) string {
311 return name + ndkLibrarySuffix
312}
313
Colin Crossb916a382016-07-29 17:28:03 -0700314func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700315 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700316 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700317}
318
Colin Crossb916a382016-07-29 17:28:03 -0700319func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700320 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700321
Dan Alberte67144e2018-05-03 15:42:34 -0700322 useVersionScript, err := shouldUseVersionScript(ctx, stub)
Dan Albert98dbb3b2017-01-03 15:16:29 -0800323 if err != nil {
324 ctx.ModuleErrorf(err.Error())
325 }
326
327 if useVersionScript {
328 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
329 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
330 }
331
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700332 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700333}
334
Colin Crossb916a382016-07-29 17:28:03 -0700335func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700336 arch := ctx.Target().Arch.ArchType.Name
Colin Crossb916a382016-07-29 17:28:03 -0700337 apiLevel := stub.properties.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700338
339 // 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 Albertfd86e9e2016-11-08 13:35:12 -0800347 "platforms/android-%s/arch-%s/usr/%s", 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
Nan Zhang0007d812017-11-07 10:57:05 -0800356 library.StripProperties.Strip.None = BoolPtr(true)
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 Cross36242852017-06-23 15:06:31 -0700365 module.AddProperties(&stub.properties, &library.MutatedProperties)
366
367 return module
Dan Albert914449f2016-06-17 16:45:24 -0700368}
369
Colin Cross36242852017-06-23 15:06:31 -0700370func ndkLibraryFactory() android.Module {
371 module := newStubLibrary()
372 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
373 return module
Dan Albert914449f2016-06-17 16:45:24 -0700374}