blob: c28b411d8782b30d1a51c211966b51591568eb3c [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{
33 Command: "$toolPath --arch $arch --api $apiLevel $in $out",
34 Description: "genStubSrc $out",
35 CommandDeps: []string{"$toolPath"},
36 }, "arch", "apiLevel")
37
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//
68// The name of the generated file will be based on the module name by stripping
69// the ".ndk" suffix from the module name. Module names must end with ".ndk"
70// (as a convention to allow soong to guess the NDK name of a dependency when
71// needed). "libfoo.ndk" will generate "libfoo.so.
72//
73// Example:
74//
75// ndk_library {
76// name: "libfoo.ndk",
77// symbol_file: "libfoo.map.txt",
78// first_version: "9",
79// }
80//
81type libraryProperties struct {
82 // Relative path to the symbol map.
83 // An example file can be seen here: TODO(danalbert): Make an example.
84 Symbol_file string
85
86 // The first API level a library was available. A library will be generated
87 // for every API level beginning with this one.
88 First_version string
89
Dan Albert98dbb3b2017-01-03 15:16:29 -080090 // The first API level that library should have the version script applied.
91 // This defaults to the value of first_version, and should almost never be
92 // used. This is only needed to work around platform bugs like
93 // https://github.com/android-ndk/ndk/issues/265.
94 Unversioned_until string
95
Dan Albert914449f2016-06-17 16:45:24 -070096 // Private property for use by the mutator that splits per-API level.
Dan Albertfd86e9e2016-11-08 13:35:12 -080097 ApiLevel string `blueprint:"mutated"`
Dan Albert914449f2016-06-17 16:45:24 -070098}
99
Colin Crossb916a382016-07-29 17:28:03 -0700100type stubDecorator struct {
101 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -0700102
103 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -0700104
Colin Crossb916a382016-07-29 17:28:03 -0700105 versionScriptPath android.ModuleGenPath
106 installPath string
Dan Albert914449f2016-06-17 16:45:24 -0700107}
108
109// OMG GO
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700110func intMax(a int, b int) int {
111 if a > b {
Dan Albert914449f2016-06-17 16:45:24 -0700112 return a
113 } else {
114 return b
115 }
116}
117
Dan Albert90f7a4d2016-11-08 14:34:24 -0800118func normalizeNdkApiLevel(apiLevel string, arch android.Arch) (string, error) {
119 if apiLevel == "current" {
120 return apiLevel, nil
121 }
122
Dan Albert914449f2016-06-17 16:45:24 -0700123 minVersion := 9 // Minimum version supported by the NDK.
Dan Albert914449f2016-06-17 16:45:24 -0700124 firstArchVersions := map[string]int{
125 "arm": 9,
126 "arm64": 21,
127 "mips": 9,
128 "mips64": 21,
129 "x86": 9,
130 "x86_64": 21,
131 }
132
Dan Albert2e5d7d42017-03-29 18:22:39 -0700133 archStr := arch.ArchType.String()
134 firstArchVersion, ok := firstArchVersions[archStr]
135 if !ok {
136 panic(fmt.Errorf("Arch %q not found in firstArchVersions", archStr))
137 }
138
139 if apiLevel == "minimum" {
140 return strconv.Itoa(firstArchVersion), nil
141 }
142
Dan Albert914449f2016-06-17 16:45:24 -0700143 // If the NDK drops support for a platform version, we don't want to have to
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700144 // fix up every module that was using it as its SDK version. Clip to the
Dan Albert914449f2016-06-17 16:45:24 -0700145 // supported version here instead.
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700146 version, err := strconv.Atoi(apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700147 if err != nil {
Dan Albert90f7a4d2016-11-08 14:34:24 -0800148 return "", fmt.Errorf("API level must be an integer (is %q)", apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700149 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700150 version = intMax(version, minVersion)
151
Dan Albert90f7a4d2016-11-08 14:34:24 -0800152 return strconv.Itoa(intMax(version, firstArchVersion)), nil
153}
154
155func getFirstGeneratedVersion(firstSupportedVersion string, platformVersion int) (int, error) {
156 if firstSupportedVersion == "current" {
157 return platformVersion + 1, nil
158 }
159
160 return strconv.Atoi(firstSupportedVersion)
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700161}
162
Dan Albert98dbb3b2017-01-03 15:16:29 -0800163func shouldUseVersionScript(stub *stubDecorator) (bool, error) {
164 // unversioned_until is normally empty, in which case we should use the version script.
165 if stub.properties.Unversioned_until == "" {
166 return true, nil
167 }
168
Dan Albert022e7a32017-01-05 15:49:09 -0800169 if stub.properties.Unversioned_until == "current" {
170 if stub.properties.ApiLevel == "current" {
171 return true, nil
172 } else {
173 return false, nil
174 }
175 }
176
Dan Albert98dbb3b2017-01-03 15:16:29 -0800177 if stub.properties.ApiLevel == "current" {
178 return true, nil
179 }
180
181 unversionedUntil, err := strconv.Atoi(stub.properties.Unversioned_until)
182 if err != nil {
183 return true, err
184 }
185
186 version, err := strconv.Atoi(stub.properties.ApiLevel)
187 if err != nil {
188 return true, err
189 }
190
191 return version >= unversionedUntil, nil
192}
193
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700194func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) {
Dan Albert90f7a4d2016-11-08 14:34:24 -0800195 platformVersion := mctx.AConfig().PlatformSdkVersionInt()
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700196
Dan Albert90f7a4d2016-11-08 14:34:24 -0800197 firstSupportedVersion, err := normalizeNdkApiLevel(c.properties.First_version,
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700198 mctx.Arch())
199 if err != nil {
200 mctx.PropertyErrorf("first_version", err.Error())
Dan Albert914449f2016-06-17 16:45:24 -0700201 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700202
Dan Albert90f7a4d2016-11-08 14:34:24 -0800203 firstGenVersion, err := getFirstGeneratedVersion(firstSupportedVersion, platformVersion)
204 if err != nil {
205 // In theory this is impossible because we've already run this through
206 // normalizeNdkApiLevel above.
207 mctx.PropertyErrorf("first_version", err.Error())
208 }
209
Dan Albertfd86e9e2016-11-08 13:35:12 -0800210 var versionStrs []string
Dan Albert90f7a4d2016-11-08 14:34:24 -0800211 for version := firstGenVersion; version <= platformVersion; version++ {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800212 versionStrs = append(versionStrs, strconv.Itoa(version))
Dan Albert914449f2016-06-17 16:45:24 -0700213 }
Dan Albertfd86e9e2016-11-08 13:35:12 -0800214 versionStrs = append(versionStrs, "current")
Dan Albert914449f2016-06-17 16:45:24 -0700215
216 modules := mctx.CreateVariations(versionStrs...)
217 for i, module := range modules {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800218 module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = versionStrs[i]
Dan Albert914449f2016-06-17 16:45:24 -0700219 }
220}
221
222func ndkApiMutator(mctx android.BottomUpMutatorContext) {
223 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700224 if compiler, ok := m.compiler.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700225 generateStubApiVariants(mctx, compiler)
226 }
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
233 name := strings.TrimSuffix(ctx.ModuleName(), ".ndk")
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 Willemsen5cb580f2016-09-26 17:33:01 -0700244func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Dan Albert914449f2016-06-17 16:45:24 -0700245 arch := ctx.Arch().ArchType.String()
246
247 if !strings.HasSuffix(ctx.ModuleName(), ndkLibrarySuffix) {
248 ctx.ModuleErrorf("ndk_library modules names must be suffixed with %q\n",
249 ndkLibrarySuffix)
250 }
251 libName := strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix)
Dan Albertfd86e9e2016-11-08 13:35:12 -0800252 fileBase := fmt.Sprintf("%s.%s.%s", libName, arch, c.properties.ApiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700253 stubSrcName := fileBase + ".c"
254 stubSrcPath := android.PathForModuleGen(ctx, stubSrcName)
255 versionScriptName := fileBase + ".map"
256 versionScriptPath := android.PathForModuleGen(ctx, versionScriptName)
Colin Crossb916a382016-07-29 17:28:03 -0700257 c.versionScriptPath = versionScriptPath
Dan Albert914449f2016-06-17 16:45:24 -0700258 symbolFilePath := android.PathForModuleSrc(ctx, c.properties.Symbol_file)
259 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
260 Rule: genStubSrc,
261 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
262 Input: symbolFilePath,
263 Args: map[string]string{
264 "arch": arch,
Dan Albertfd86e9e2016-11-08 13:35:12 -0800265 "apiLevel": c.properties.ApiLevel,
Dan Albert914449f2016-06-17 16:45:24 -0700266 },
267 })
268
269 flags.CFlags = append(flags.CFlags,
270 // We're knowingly doing some otherwise unsightly things with builtin
271 // functions here. We're just generating stub libraries, so ignore it.
272 "-Wno-incompatible-library-redeclaration",
273 "-Wno-builtin-requires-header",
274 "-Wno-invalid-noreturn",
275
276 // These libraries aren't actually used. Don't worry about unwinding
277 // (avoids the need to link an unwinder into a fake library).
278 "-fno-unwind-tables",
279 )
280
281 subdir := ""
Colin Cross2f336352016-10-26 10:03:47 -0700282 srcs := []android.Path{stubSrcPath}
283 return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil)
Dan Albert914449f2016-06-17 16:45:24 -0700284}
285
Colin Cross37047f12016-12-13 17:06:13 -0800286func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700287 return Deps{}
288}
289
Colin Crossb916a382016-07-29 17:28:03 -0700290func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
291 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(),
Dan Albert914449f2016-06-17 16:45:24 -0700292 ndkLibrarySuffix)
Colin Crossb916a382016-07-29 17:28:03 -0700293 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700294}
295
Colin Crossb916a382016-07-29 17:28:03 -0700296func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700297 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700298
Dan Albert98dbb3b2017-01-03 15:16:29 -0800299 useVersionScript, err := shouldUseVersionScript(stub)
300 if err != nil {
301 ctx.ModuleErrorf(err.Error())
302 }
303
304 if useVersionScript {
305 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
306 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
307 }
308
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700309 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700310}
311
Colin Crossb916a382016-07-29 17:28:03 -0700312func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700313 arch := ctx.Target().Arch.ArchType.Name
Colin Crossb916a382016-07-29 17:28:03 -0700314 apiLevel := stub.properties.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700315
316 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
317 // architectures it's just installed to lib.
318 libDir := "lib"
319 if ctx.toolchain().Is64Bit() && arch != "arm64" {
320 libDir = "lib64"
321 }
322
323 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
Dan Albertfd86e9e2016-11-08 13:35:12 -0800324 "platforms/android-%s/arch-%s/usr/%s", apiLevel, arch, libDir))
Colin Crossb916a382016-07-29 17:28:03 -0700325 stub.installPath = ctx.InstallFile(installDir, path).String()
Dan Albert914449f2016-06-17 16:45:24 -0700326}
327
Dan Albert705c84b2016-08-08 10:45:03 -0700328func newStubLibrary() (*Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800329 module, library := NewLibrary(android.DeviceSupported)
330 library.BuildOnlyShared()
Dan Albert914449f2016-06-17 16:45:24 -0700331 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700332 module.sanitize = nil
333 library.StripProperties.Strip.None = true
Dan Albert914449f2016-06-17 16:45:24 -0700334
Colin Crossb916a382016-07-29 17:28:03 -0700335 stub := &stubDecorator{
336 libraryDecorator: library,
337 }
338 module.compiler = stub
339 module.linker = stub
340 module.installer = stub
Dan Albert914449f2016-06-17 16:45:24 -0700341
Colin Crossa48ab5b2017-02-14 15:28:44 -0800342 return module, []interface{}{&stub.properties, &library.MutatedProperties}
Dan Albert914449f2016-06-17 16:45:24 -0700343}
344
345func ndkLibraryFactory() (blueprint.Module, []interface{}) {
Dan Albert705c84b2016-08-08 10:45:03 -0700346 module, properties := newStubLibrary()
347 return android.InitAndroidArchModule(module, android.DeviceSupported,
348 android.MultilibBoth, properties...)
Dan Albert914449f2016-06-17 16:45:24 -0700349}