blob: d769fea83e1987b8641ac82d139c576653b7d5bf [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 Willemsenb916b802017-03-19 13:44:32 -070033 Command: "$toolPath --arch $arch --api $apiLevel $vndk $in $out",
Dan Albert914449f2016-06-17 16:45:24 -070034 Description: "genStubSrc $out",
35 CommandDeps: []string{"$toolPath"},
Dan Willemsenb916b802017-03-19 13:44:32 -070036 }, "arch", "apiLevel", "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.
79 Symbol_file string
80
81 // The first API level a library was available. A library will be generated
82 // for every API level beginning with this one.
83 First_version string
84
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.
89 Unversioned_until string
90
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
101 installPath string
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 Albert90f7a4d2016-11-08 14:34:24 -0800113func normalizeNdkApiLevel(apiLevel string, arch android.Arch) (string, error) {
114 if apiLevel == "current" {
115 return apiLevel, nil
116 }
117
Dan Albert914449f2016-06-17 16:45:24 -0700118 minVersion := 9 // Minimum version supported by the NDK.
Colin Crossce87b802017-04-13 13:00:26 -0700119 firstArchVersions := map[android.ArchType]int{
120 android.Arm: 9,
121 android.Arm64: 21,
122 android.Mips: 9,
123 android.Mips64: 21,
124 android.X86: 9,
125 android.X86_64: 21,
Dan Albert914449f2016-06-17 16:45:24 -0700126 }
127
Colin Crossce87b802017-04-13 13:00:26 -0700128 firstArchVersion, ok := firstArchVersions[arch.ArchType]
Dan Albert2e5d7d42017-03-29 18:22:39 -0700129 if !ok {
Colin Crossce87b802017-04-13 13:00:26 -0700130 panic(fmt.Errorf("Arch %q not found in firstArchVersions", arch.ArchType))
Dan Albert2e5d7d42017-03-29 18:22:39 -0700131 }
132
133 if apiLevel == "minimum" {
134 return strconv.Itoa(firstArchVersion), nil
135 }
136
Dan Albert914449f2016-06-17 16:45:24 -0700137 // If the NDK drops support for a platform version, we don't want to have to
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700138 // fix up every module that was using it as its SDK version. Clip to the
Dan Albert914449f2016-06-17 16:45:24 -0700139 // supported version here instead.
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700140 version, err := strconv.Atoi(apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700141 if err != nil {
Dan Albert90f7a4d2016-11-08 14:34:24 -0800142 return "", fmt.Errorf("API level must be an integer (is %q)", apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700143 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700144 version = intMax(version, minVersion)
145
Dan Albert90f7a4d2016-11-08 14:34:24 -0800146 return strconv.Itoa(intMax(version, firstArchVersion)), nil
147}
148
149func getFirstGeneratedVersion(firstSupportedVersion string, platformVersion int) (int, error) {
150 if firstSupportedVersion == "current" {
151 return platformVersion + 1, nil
152 }
153
154 return strconv.Atoi(firstSupportedVersion)
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700155}
156
Dan Albert98dbb3b2017-01-03 15:16:29 -0800157func shouldUseVersionScript(stub *stubDecorator) (bool, error) {
158 // unversioned_until is normally empty, in which case we should use the version script.
159 if stub.properties.Unversioned_until == "" {
160 return true, nil
161 }
162
Dan Albert022e7a32017-01-05 15:49:09 -0800163 if stub.properties.Unversioned_until == "current" {
164 if stub.properties.ApiLevel == "current" {
165 return true, nil
166 } else {
167 return false, nil
168 }
169 }
170
Dan Albert98dbb3b2017-01-03 15:16:29 -0800171 if stub.properties.ApiLevel == "current" {
172 return true, nil
173 }
174
175 unversionedUntil, err := strconv.Atoi(stub.properties.Unversioned_until)
176 if err != nil {
177 return true, err
178 }
179
180 version, err := strconv.Atoi(stub.properties.ApiLevel)
181 if err != nil {
182 return true, err
183 }
184
185 return version >= unversionedUntil, nil
186}
187
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700188func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) {
Dan Albert90f7a4d2016-11-08 14:34:24 -0800189 platformVersion := mctx.AConfig().PlatformSdkVersionInt()
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700190
Dan Albert90f7a4d2016-11-08 14:34:24 -0800191 firstSupportedVersion, err := normalizeNdkApiLevel(c.properties.First_version,
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700192 mctx.Arch())
193 if err != nil {
194 mctx.PropertyErrorf("first_version", err.Error())
Dan Albert914449f2016-06-17 16:45:24 -0700195 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700196
Dan Albert90f7a4d2016-11-08 14:34:24 -0800197 firstGenVersion, err := getFirstGeneratedVersion(firstSupportedVersion, platformVersion)
198 if err != nil {
199 // In theory this is impossible because we've already run this through
200 // normalizeNdkApiLevel above.
201 mctx.PropertyErrorf("first_version", err.Error())
202 }
203
Dan Albertfd86e9e2016-11-08 13:35:12 -0800204 var versionStrs []string
Dan Albert90f7a4d2016-11-08 14:34:24 -0800205 for version := firstGenVersion; version <= platformVersion; version++ {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800206 versionStrs = append(versionStrs, strconv.Itoa(version))
Dan Albert914449f2016-06-17 16:45:24 -0700207 }
Dan Albertfd86e9e2016-11-08 13:35:12 -0800208 versionStrs = append(versionStrs, "current")
Dan Albert914449f2016-06-17 16:45:24 -0700209
210 modules := mctx.CreateVariations(versionStrs...)
211 for i, module := range modules {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800212 module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = versionStrs[i]
Dan Albert914449f2016-06-17 16:45:24 -0700213 }
214}
215
216func ndkApiMutator(mctx android.BottomUpMutatorContext) {
217 if m, ok := mctx.Module().(*Module); ok {
Colin Crossd4025822017-04-13 12:53:07 -0700218 if m.Enabled() {
219 if compiler, ok := m.compiler.(*stubDecorator); ok {
220 generateStubApiVariants(mctx, compiler)
221 }
Dan Albert914449f2016-06-17 16:45:24 -0700222 }
223 }
224}
225
Colin Crossb916a382016-07-29 17:28:03 -0700226func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700227 c.baseCompiler.compilerInit(ctx)
228
Dan Willemsen01a90592017-04-07 15:21:13 -0700229 name := ctx.baseModuleName()
230 if strings.HasSuffix(name, ndkLibrarySuffix) {
231 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix)
232 }
233
Colin Crosse8a67a72016-08-07 21:17:54 -0700234 ndkMigratedLibsLock.Lock()
235 defer ndkMigratedLibsLock.Unlock()
Dan Albert7e9d2952016-08-04 13:02:36 -0700236 for _, lib := range ndkMigratedLibs {
237 if lib == name {
238 return
239 }
240 }
241 ndkMigratedLibs = append(ndkMigratedLibs, name)
242}
243
Dan Willemsenb916b802017-03-19 13:44:32 -0700244func compileStubLibrary(ctx ModuleContext, flags Flags, symbolFile, apiLevel, vndk string) (Objects, android.ModuleGenPath) {
Dan Albert914449f2016-06-17 16:45:24 -0700245 arch := ctx.Arch().ArchType.String()
246
Dan Willemsenb916b802017-03-19 13:44:32 -0700247 stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
248 versionScriptPath := android.PathForModuleGen(ctx, "stub.map")
249 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
Dan Albert914449f2016-06-17 16:45:24 -0700250 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
251 Rule: genStubSrc,
252 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
253 Input: symbolFilePath,
254 Args: map[string]string{
255 "arch": arch,
Dan Willemsenb916b802017-03-19 13:44:32 -0700256 "apiLevel": apiLevel,
257 "vndk": vndk,
Dan Albert914449f2016-06-17 16:45:24 -0700258 },
259 })
260
261 flags.CFlags = append(flags.CFlags,
262 // We're knowingly doing some otherwise unsightly things with builtin
263 // functions here. We're just generating stub libraries, so ignore it.
264 "-Wno-incompatible-library-redeclaration",
265 "-Wno-builtin-requires-header",
266 "-Wno-invalid-noreturn",
267
268 // These libraries aren't actually used. Don't worry about unwinding
269 // (avoids the need to link an unwinder into a fake library).
270 "-fno-unwind-tables",
271 )
272
273 subdir := ""
Colin Cross2f336352016-10-26 10:03:47 -0700274 srcs := []android.Path{stubSrcPath}
Dan Willemsenb916b802017-03-19 13:44:32 -0700275 return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil), versionScriptPath
276}
277
278func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Dan Willemsenb916b802017-03-19 13:44:32 -0700279 objs, versionScript := compileStubLibrary(ctx, flags, c.properties.Symbol_file, c.properties.ApiLevel, "")
280 c.versionScriptPath = versionScript
281 return objs
Dan Albert914449f2016-06-17 16:45:24 -0700282}
283
Colin Cross37047f12016-12-13 17:06:13 -0800284func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700285 return Deps{}
286}
287
Dan Willemsen01a90592017-04-07 15:21:13 -0700288func (linker *stubDecorator) Name(name string) string {
289 return name + ndkLibrarySuffix
290}
291
Colin Crossb916a382016-07-29 17:28:03 -0700292func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen01a90592017-04-07 15:21:13 -0700293 stub.libraryDecorator.libName = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700294 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700295}
296
Colin Crossb916a382016-07-29 17:28:03 -0700297func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700298 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700299
Dan Albert98dbb3b2017-01-03 15:16:29 -0800300 useVersionScript, err := shouldUseVersionScript(stub)
301 if err != nil {
302 ctx.ModuleErrorf(err.Error())
303 }
304
305 if useVersionScript {
306 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
307 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
308 }
309
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700310 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700311}
312
Colin Crossb916a382016-07-29 17:28:03 -0700313func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700314 arch := ctx.Target().Arch.ArchType.Name
Colin Crossb916a382016-07-29 17:28:03 -0700315 apiLevel := stub.properties.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700316
317 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
318 // architectures it's just installed to lib.
319 libDir := "lib"
320 if ctx.toolchain().Is64Bit() && arch != "arm64" {
321 libDir = "lib64"
322 }
323
324 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albertfd86e9e2016-11-08 13:35:12 -0800325 "platforms/android-%s/arch-%s/usr/%s", apiLevel, arch, libDir))
Colin Crossb916a382016-07-29 17:28:03 -0700326 stub.installPath = ctx.InstallFile(installDir, path).String()
Dan Albert914449f2016-06-17 16:45:24 -0700327}
328
Dan Albert705c84b2016-08-08 10:45:03 -0700329func newStubLibrary() (*Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800330 module, library := NewLibrary(android.DeviceSupported)
331 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700332 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700333 module.sanitize = nil
334 library.StripProperties.Strip.None = true
Dan Albert914449f2016-06-17 16:45:24 -0700335
Colin Crossb916a382016-07-29 17:28:03 -0700336 stub := &stubDecorator{
337 libraryDecorator: library,
338 }
339 module.compiler = stub
340 module.linker = stub
341 module.installer = stub
Dan Albert914449f2016-06-17 16:45:24 -0700342
Colin Crossa48ab5b2017-02-14 15:28:44 -0800343 return module, []interface{}{&stub.properties, &library.MutatedProperties}
Dan Albert914449f2016-06-17 16:45:24 -0700344}
345
346func ndkLibraryFactory() (blueprint.Module, []interface{}) {
Dan Albert705c84b2016-08-08 10:45:03 -0700347 module, properties := newStubLibrary()
348 return android.InitAndroidArchModule(module, android.DeviceSupported,
349 android.MultilibBoth, properties...)
Dan Albert914449f2016-06-17 16:45:24 -0700350}