blob: 94be17c5be4546b3b7f2041404802a8061582263 [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
90 // Private property for use by the mutator that splits per-API level.
91 ApiLevel int `blueprint:"mutated"`
92}
93
Colin Crossb916a382016-07-29 17:28:03 -070094type stubDecorator struct {
95 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -070096
97 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -070098
Colin Crossb916a382016-07-29 17:28:03 -070099 versionScriptPath android.ModuleGenPath
100 installPath string
Dan Albert914449f2016-06-17 16:45:24 -0700101}
102
103// OMG GO
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700104func intMax(a int, b int) int {
105 if a > b {
Dan Albert914449f2016-06-17 16:45:24 -0700106 return a
107 } else {
108 return b
109 }
110}
111
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700112func normalizeNdkApiLevel(apiLevel string, arch android.Arch) (int, error) {
Dan Albert914449f2016-06-17 16:45:24 -0700113 minVersion := 9 // Minimum version supported by the NDK.
Dan Albert914449f2016-06-17 16:45:24 -0700114 firstArchVersions := map[string]int{
115 "arm": 9,
116 "arm64": 21,
117 "mips": 9,
118 "mips64": 21,
119 "x86": 9,
120 "x86_64": 21,
121 }
122
123 // If the NDK drops support for a platform version, we don't want to have to
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700124 // fix up every module that was using it as its SDK version. Clip to the
Dan Albert914449f2016-06-17 16:45:24 -0700125 // supported version here instead.
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700126 version, err := strconv.Atoi(apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700127 if err != nil {
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700128 return -1, fmt.Errorf("API level must be an integer (is %q)", apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700129 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700130 version = intMax(version, minVersion)
131
132 archStr := arch.ArchType.String()
133 firstArchVersion, ok := firstArchVersions[archStr]
134 if !ok {
135 panic(fmt.Errorf("Arch %q not found in firstArchVersions", archStr))
Dan Albert914449f2016-06-17 16:45:24 -0700136 }
137
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700138 return intMax(version, firstArchVersion), nil
139}
140
141func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) {
Dan Albert073379e2016-11-10 10:46:36 -0800142 maxVersion := mctx.AConfig().PlatformSdkVersionInt()
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700143
144 firstVersion, err := normalizeNdkApiLevel(c.properties.First_version,
145 mctx.Arch())
146 if err != nil {
147 mctx.PropertyErrorf("first_version", err.Error())
Dan Albert914449f2016-06-17 16:45:24 -0700148 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700149
150 versionStrs := make([]string, maxVersion-firstVersion+1)
151 for version := firstVersion; version <= maxVersion; version++ {
152 versionStrs[version-firstVersion] = strconv.Itoa(version)
Dan Albert914449f2016-06-17 16:45:24 -0700153 }
154
155 modules := mctx.CreateVariations(versionStrs...)
156 for i, module := range modules {
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700157 module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = firstVersion + i
Dan Albert914449f2016-06-17 16:45:24 -0700158 }
159}
160
161func ndkApiMutator(mctx android.BottomUpMutatorContext) {
162 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700163 if compiler, ok := m.compiler.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700164 generateStubApiVariants(mctx, compiler)
165 }
166 }
167}
168
Colin Crossb916a382016-07-29 17:28:03 -0700169func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700170 c.baseCompiler.compilerInit(ctx)
171
172 name := strings.TrimSuffix(ctx.ModuleName(), ".ndk")
Colin Crosse8a67a72016-08-07 21:17:54 -0700173 ndkMigratedLibsLock.Lock()
174 defer ndkMigratedLibsLock.Unlock()
Dan Albert7e9d2952016-08-04 13:02:36 -0700175 for _, lib := range ndkMigratedLibs {
176 if lib == name {
177 return
178 }
179 }
180 ndkMigratedLibs = append(ndkMigratedLibs, name)
181}
182
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700183func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Dan Albert914449f2016-06-17 16:45:24 -0700184 arch := ctx.Arch().ArchType.String()
185
186 if !strings.HasSuffix(ctx.ModuleName(), ndkLibrarySuffix) {
187 ctx.ModuleErrorf("ndk_library modules names must be suffixed with %q\n",
188 ndkLibrarySuffix)
189 }
190 libName := strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix)
191 fileBase := fmt.Sprintf("%s.%s.%d", libName, arch, c.properties.ApiLevel)
192 stubSrcName := fileBase + ".c"
193 stubSrcPath := android.PathForModuleGen(ctx, stubSrcName)
194 versionScriptName := fileBase + ".map"
195 versionScriptPath := android.PathForModuleGen(ctx, versionScriptName)
Colin Crossb916a382016-07-29 17:28:03 -0700196 c.versionScriptPath = versionScriptPath
Dan Albert914449f2016-06-17 16:45:24 -0700197 symbolFilePath := android.PathForModuleSrc(ctx, c.properties.Symbol_file)
198 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
199 Rule: genStubSrc,
200 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
201 Input: symbolFilePath,
202 Args: map[string]string{
203 "arch": arch,
204 "apiLevel": strconv.Itoa(c.properties.ApiLevel),
205 },
206 })
207
208 flags.CFlags = append(flags.CFlags,
209 // We're knowingly doing some otherwise unsightly things with builtin
210 // functions here. We're just generating stub libraries, so ignore it.
211 "-Wno-incompatible-library-redeclaration",
212 "-Wno-builtin-requires-header",
213 "-Wno-invalid-noreturn",
214
215 // These libraries aren't actually used. Don't worry about unwinding
216 // (avoids the need to link an unwinder into a fake library).
217 "-fno-unwind-tables",
218 )
219
220 subdir := ""
Colin Cross2f336352016-10-26 10:03:47 -0700221 srcs := []android.Path{stubSrcPath}
222 return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil)
Dan Albert914449f2016-06-17 16:45:24 -0700223}
224
Colin Crossb916a382016-07-29 17:28:03 -0700225func (linker *stubDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700226 return Deps{}
227}
228
Colin Crossb916a382016-07-29 17:28:03 -0700229func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
230 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(),
Dan Albert914449f2016-06-17 16:45:24 -0700231 ndkLibrarySuffix)
Colin Crossb916a382016-07-29 17:28:03 -0700232 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700233}
234
Colin Crossb916a382016-07-29 17:28:03 -0700235func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700236 objs Objects) android.Path {
Dan Albert2bc91ba2016-07-28 17:40:28 -0700237
Colin Crossb916a382016-07-29 17:28:03 -0700238 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Dan Albert2bc91ba2016-07-28 17:40:28 -0700239 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700240 return stub.libraryDecorator.link(ctx, flags, deps, objs)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700241}
242
Colin Crossb916a382016-07-29 17:28:03 -0700243func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700244 arch := ctx.Target().Arch.ArchType.Name
Colin Crossb916a382016-07-29 17:28:03 -0700245 apiLevel := stub.properties.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700246
247 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
248 // architectures it's just installed to lib.
249 libDir := "lib"
250 if ctx.toolchain().Is64Bit() && arch != "arm64" {
251 libDir = "lib64"
252 }
253
254 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
255 "platforms/android-%d/arch-%s/usr/%s", apiLevel, arch, libDir))
Colin Crossb916a382016-07-29 17:28:03 -0700256 stub.installPath = ctx.InstallFile(installDir, path).String()
Dan Albert914449f2016-06-17 16:45:24 -0700257}
258
Dan Albert705c84b2016-08-08 10:45:03 -0700259func newStubLibrary() (*Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700260 module, library := NewLibrary(android.DeviceSupported, true, false)
Dan Albert914449f2016-06-17 16:45:24 -0700261 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700262 module.sanitize = nil
263 library.StripProperties.Strip.None = true
Dan Albert914449f2016-06-17 16:45:24 -0700264
Colin Crossb916a382016-07-29 17:28:03 -0700265 stub := &stubDecorator{
266 libraryDecorator: library,
267 }
268 module.compiler = stub
269 module.linker = stub
270 module.installer = stub
Dan Albert914449f2016-06-17 16:45:24 -0700271
Dan Albert705c84b2016-08-08 10:45:03 -0700272 return module, []interface{}{&stub.properties}
Dan Albert914449f2016-06-17 16:45:24 -0700273}
274
275func ndkLibraryFactory() (blueprint.Module, []interface{}) {
Dan Albert705c84b2016-08-08 10:45:03 -0700276 module, properties := newStubLibrary()
277 return android.InitAndroidArchModule(module, android.DeviceSupported,
278 android.MultilibBoth, properties...)
Dan Albert914449f2016-06-17 16:45:24 -0700279}