blob: 1bd63a9853cfaa320b5963ffddce6a2c8b5f447c [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 Description: "genStubSrc $out",
36 CommandDeps: []string{"$toolPath"},
Dan Albert49927d22017-03-28 15:00:46 -070037 }, "arch", "apiLevel", "apiMap", "vndk")
Dan Albert914449f2016-06-17 16:45:24 -070038
39 ndkLibrarySuffix = ".ndk"
Colin Cross4d9c2d12016-07-29 12:48:20 -070040
41 ndkPrebuiltSharedLibs = []string{
42 "android",
43 "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{}
64 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.
80 Symbol_file string
81
82 // The first API level a library was available. A library will be generated
83 // for every API level beginning with this one.
84 First_version string
85
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.
90 Unversioned_until string
91
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
102 installPath string
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 Albert90f7a4d2016-11-08 14:34:24 -0800114func normalizeNdkApiLevel(apiLevel string, arch android.Arch) (string, error) {
115 if apiLevel == "current" {
116 return apiLevel, nil
117 }
118
Dan Albert914449f2016-06-17 16:45:24 -0700119 minVersion := 9 // Minimum version supported by the NDK.
Colin Crossce87b802017-04-13 13:00:26 -0700120 firstArchVersions := map[android.ArchType]int{
121 android.Arm: 9,
122 android.Arm64: 21,
123 android.Mips: 9,
124 android.Mips64: 21,
125 android.X86: 9,
126 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 Albert90f7a4d2016-11-08 14:34:24 -0800143 return "", fmt.Errorf("API level must be an integer (is %q)", apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700144 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700145 version = intMax(version, minVersion)
146
Dan Albert90f7a4d2016-11-08 14:34:24 -0800147 return strconv.Itoa(intMax(version, firstArchVersion)), nil
148}
149
150func getFirstGeneratedVersion(firstSupportedVersion string, platformVersion int) (int, error) {
151 if firstSupportedVersion == "current" {
152 return platformVersion + 1, nil
153 }
154
155 return strconv.Atoi(firstSupportedVersion)
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700156}
157
Dan Albert98dbb3b2017-01-03 15:16:29 -0800158func shouldUseVersionScript(stub *stubDecorator) (bool, error) {
159 // unversioned_until is normally empty, in which case we should use the version script.
160 if stub.properties.Unversioned_until == "" {
161 return true, nil
162 }
163
Dan Albert022e7a32017-01-05 15:49:09 -0800164 if stub.properties.Unversioned_until == "current" {
165 if stub.properties.ApiLevel == "current" {
166 return true, nil
167 } else {
168 return false, nil
169 }
170 }
171
Dan Albert98dbb3b2017-01-03 15:16:29 -0800172 if stub.properties.ApiLevel == "current" {
173 return true, nil
174 }
175
176 unversionedUntil, err := strconv.Atoi(stub.properties.Unversioned_until)
177 if err != nil {
178 return true, err
179 }
180
181 version, err := strconv.Atoi(stub.properties.ApiLevel)
182 if err != nil {
183 return true, err
184 }
185
186 return version >= unversionedUntil, nil
187}
188
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700189func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) {
Dan Albert90f7a4d2016-11-08 14:34:24 -0800190 platformVersion := mctx.AConfig().PlatformSdkVersionInt()
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700191
Dan Albert90f7a4d2016-11-08 14:34:24 -0800192 firstSupportedVersion, err := normalizeNdkApiLevel(c.properties.First_version,
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700193 mctx.Arch())
194 if err != nil {
195 mctx.PropertyErrorf("first_version", err.Error())
Dan Albert914449f2016-06-17 16:45:24 -0700196 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700197
Dan Albert90f7a4d2016-11-08 14:34:24 -0800198 firstGenVersion, err := getFirstGeneratedVersion(firstSupportedVersion, platformVersion)
199 if err != nil {
200 // In theory this is impossible because we've already run this through
201 // normalizeNdkApiLevel above.
202 mctx.PropertyErrorf("first_version", err.Error())
203 }
204
Dan Albertfd86e9e2016-11-08 13:35:12 -0800205 var versionStrs []string
Dan Albert90f7a4d2016-11-08 14:34:24 -0800206 for version := firstGenVersion; version <= platformVersion; version++ {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800207 versionStrs = append(versionStrs, strconv.Itoa(version))
Dan Albert914449f2016-06-17 16:45:24 -0700208 }
Dan Albert49927d22017-03-28 15:00:46 -0700209 versionStrs = append(versionStrs, mctx.AConfig().PlatformVersionAllCodenames()...)
Dan Albertfd86e9e2016-11-08 13:35:12 -0800210 versionStrs = append(versionStrs, "current")
Dan Albert914449f2016-06-17 16:45:24 -0700211
212 modules := mctx.CreateVariations(versionStrs...)
213 for i, module := range modules {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800214 module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = versionStrs[i]
Dan Albert914449f2016-06-17 16:45:24 -0700215 }
216}
217
218func ndkApiMutator(mctx android.BottomUpMutatorContext) {
219 if m, ok := mctx.Module().(*Module); ok {
Colin Crossd4025822017-04-13 12:53:07 -0700220 if m.Enabled() {
221 if compiler, ok := m.compiler.(*stubDecorator); ok {
222 generateStubApiVariants(mctx, compiler)
223 }
Dan Albert914449f2016-06-17 16:45:24 -0700224 }
225 }
226}
227
Colin Crossb916a382016-07-29 17:28:03 -0700228func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700229 c.baseCompiler.compilerInit(ctx)
230
Dan Willemsen01a90592017-04-07 15:21:13 -0700231 name := ctx.baseModuleName()
232 if strings.HasSuffix(name, ndkLibrarySuffix) {
233 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
234 }
235
Colin Crosse8a67a72016-08-07 21:17:54 -0700236 ndkMigratedLibsLock.Lock()
237 defer ndkMigratedLibsLock.Unlock()
Dan Albert7e9d2952016-08-04 13:02:36 -0700238 for _, lib := range ndkMigratedLibs {
239 if lib == name {
240 return
241 }
242 }
243 ndkMigratedLibs = append(ndkMigratedLibs, name)
244}
245
Dan Willemsenb916b802017-03-19 13:44:32 -0700246func compileStubLibrary(ctx ModuleContext, flags Flags, symbolFile, apiLevel, vndk string) (Objects, android.ModuleGenPath) {
Dan Albert914449f2016-06-17 16:45:24 -0700247 arch := ctx.Arch().ArchType.String()
248
Dan Willemsenb916b802017-03-19 13:44:32 -0700249 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
250 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
251 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albert49927d22017-03-28 15:00:46 -0700252 apiLevelsJson := android.GetApiLevelsJson(ctx)
Dan Albert914449f2016-06-17 16:45:24 -0700253 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Albert49927d22017-03-28 15:00:46 -0700254 Rule: genStubSrc,
255 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
256 Input: symbolFilePath,
257 Implicits: []android.Path{apiLevelsJson},
Dan Albert914449f2016-06-17 16:45:24 -0700258 Args: map[string]string{
259 "arch": arch,
Dan Willemsenb916b802017-03-19 13:44:32 -0700260 "apiLevel": apiLevel,
Dan Albert49927d22017-03-28 15:00:46 -0700261 "apiMap": apiLevelsJson.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700262 "vndk": vndk,
Dan Albert914449f2016-06-17 16:45:24 -0700263 },
264 })
265
266 flags.CFlags = append(flags.CFlags,
267 // We're knowingly doing some otherwise unsightly things with builtin
268 // functions here. We're just generating stub libraries, so ignore it.
269 "-Wno-incompatible-library-redeclaration",
270 "-Wno-builtin-requires-header",
271 "-Wno-invalid-noreturn",
272
273 // These libraries aren't actually used. Don't worry about unwinding
274 // (avoids the need to link an unwinder into a fake library).
275 "-fno-unwind-tables",
276 )
277
278 subdir := ""
Colin Cross2f336352016-10-26 10:03:47 -0700279 srcs := []android.Path{stubSrcPath}
Dan Willemsenb916b802017-03-19 13:44:32 -0700280 return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil), versionScriptPath
281}
282
283func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Dan Willemsenb916b802017-03-19 13:44:32 -0700284 objs, versionScript := compileStubLibrary(ctx, flags, c.properties.Symbol_file, c.properties.ApiLevel, "")
285 c.versionScriptPath = versionScript
286 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700287}
288
Colin Cross37047f12016-12-13 17:06:13 -0800289func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700290 return Deps{}
291}
292
Dan Willemsen01a90592017-04-07 15:21:13 -0700293func (linker *stubDecorator) Name(name string) string {
294 return name + ndkLibrarySuffix
295}
296
Colin Crossb916a382016-07-29 17:28:03 -0700297func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700298 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700299 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700300}
301
Colin Crossb916a382016-07-29 17:28:03 -0700302func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700303 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700304
Dan Albert98dbb3b2017-01-03 15:16:29 -0800305 useVersionScript, err := shouldUseVersionScript(stub)
306 if err != nil {
307 ctx.ModuleErrorf(err.Error())
308 }
309
310 if useVersionScript {
311 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
312 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
313 }
314
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700315 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700316}
317
Colin Crossb916a382016-07-29 17:28:03 -0700318func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700319 arch := ctx.Target().Arch.ArchType.Name
Colin Crossb916a382016-07-29 17:28:03 -0700320 apiLevel := stub.properties.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700321
322 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
323 // architectures it's just installed to lib.
324 libDir := "lib"
325 if ctx.toolchain().Is64Bit() && arch != "arm64" {
326 libDir = "lib64"
327 }
328
329 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albertfd86e9e2016-11-08 13:35:12 -0800330 "platforms/android-%s/arch-%s/usr/%s", apiLevel, arch, libDir))
Colin Crossb916a382016-07-29 17:28:03 -0700331 stub.installPath = ctx.InstallFile(installDir, path).String()
Dan Albert914449f2016-06-17 16:45:24 -0700332}
333
Dan Albert705c84b2016-08-08 10:45:03 -0700334func newStubLibrary() (*Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800335 module, library := NewLibrary(android.DeviceSupported)
336 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700337 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700338 module.sanitize = nil
339 library.StripProperties.Strip.None = true
Dan Albert914449f2016-06-17 16:45:24 -0700340
Colin Crossb916a382016-07-29 17:28:03 -0700341 stub := &stubDecorator{
342 libraryDecorator: library,
343 }
344 module.compiler = stub
345 module.linker = stub
346 module.installer = stub
Dan Albert914449f2016-06-17 16:45:24 -0700347
Colin Crossa48ab5b2017-02-14 15:28:44 -0800348 return module, []interface{}{&stub.properties, &library.MutatedProperties}
Dan Albert914449f2016-06-17 16:45:24 -0700349}
350
351func ndkLibraryFactory() (blueprint.Module, []interface{}) {
Dan Albert705c84b2016-08-08 10:45:03 -0700352 module, properties := newStubLibrary()
353 return android.InitAndroidArchModule(module, android.DeviceSupported,
354 android.MultilibBoth, properties...)
Dan Albert914449f2016-06-17 16:45:24 -0700355}