blob: fe52b0e62ff507ef37cb5ddc5bf998f1c3faaf92 [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"
21
22 "github.com/google/blueprint"
23
24 "android/soong/android"
25)
26
27var (
28 toolPath = pctx.SourcePathVariable("toolPath", "build/soong/cc/gen_stub_libs.py")
29
30 genStubSrc = pctx.StaticRule("genStubSrc",
31 blueprint.RuleParams{
32 Command: "$toolPath --arch $arch --api $apiLevel $in $out",
33 Description: "genStubSrc $out",
34 CommandDeps: []string{"$toolPath"},
35 }, "arch", "apiLevel")
36
37 ndkLibrarySuffix = ".ndk"
Colin Cross4d9c2d12016-07-29 12:48:20 -070038
39 ndkPrebuiltSharedLibs = []string{
40 "android",
41 "c",
42 "dl",
43 "EGL",
44 "GLESv1_CM",
45 "GLESv2",
46 "GLESv3",
47 "jnigraphics",
48 "log",
49 "mediandk",
50 "m",
51 "OpenMAXAL",
52 "OpenSLES",
53 "stdc++",
54 "vulkan",
55 "z",
56 }
57 ndkPrebuiltSharedLibraries = addPrefix(append([]string(nil), ndkPrebuiltSharedLibs...), "lib")
58
59 // These libraries have migrated over to the new ndk_library, which is added
60 // as a variation dependency via depsMutator.
61 ndkMigratedLibs = []string{}
Dan Albert914449f2016-06-17 16:45:24 -070062)
63
64// Creates a stub shared library based on the provided version file.
65//
66// The name of the generated file will be based on the module name by stripping
67// the ".ndk" suffix from the module name. Module names must end with ".ndk"
68// (as a convention to allow soong to guess the NDK name of a dependency when
69// needed). "libfoo.ndk" will generate "libfoo.so.
70//
71// Example:
72//
73// ndk_library {
74// name: "libfoo.ndk",
75// symbol_file: "libfoo.map.txt",
76// first_version: "9",
77// }
78//
79type libraryProperties struct {
80 // Relative path to the symbol map.
81 // An example file can be seen here: TODO(danalbert): Make an example.
82 Symbol_file string
83
84 // The first API level a library was available. A library will be generated
85 // for every API level beginning with this one.
86 First_version string
87
88 // Private property for use by the mutator that splits per-API level.
89 ApiLevel int `blueprint:"mutated"`
90}
91
Colin Crossb916a382016-07-29 17:28:03 -070092type stubDecorator struct {
93 *libraryDecorator
Dan Albert914449f2016-06-17 16:45:24 -070094
95 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -070096
Colin Crossb916a382016-07-29 17:28:03 -070097 versionScriptPath android.ModuleGenPath
98 installPath string
Dan Albert914449f2016-06-17 16:45:24 -070099}
100
101// OMG GO
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700102func intMax(a int, b int) int {
103 if a > b {
Dan Albert914449f2016-06-17 16:45:24 -0700104 return a
105 } else {
106 return b
107 }
108}
109
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700110func normalizeNdkApiLevel(apiLevel string, arch android.Arch) (int, error) {
Dan Albert914449f2016-06-17 16:45:24 -0700111 minVersion := 9 // Minimum version supported by the NDK.
Dan Albert914449f2016-06-17 16:45:24 -0700112 firstArchVersions := map[string]int{
113 "arm": 9,
114 "arm64": 21,
115 "mips": 9,
116 "mips64": 21,
117 "x86": 9,
118 "x86_64": 21,
119 }
120
121 // If the NDK drops support for a platform version, we don't want to have to
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700122 // fix up every module that was using it as its SDK version. Clip to the
Dan Albert914449f2016-06-17 16:45:24 -0700123 // supported version here instead.
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700124 version, err := strconv.Atoi(apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700125 if err != nil {
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700126 return -1, fmt.Errorf("API level must be an integer (is %q)", apiLevel)
Dan Albert914449f2016-06-17 16:45:24 -0700127 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700128 version = intMax(version, minVersion)
129
130 archStr := arch.ArchType.String()
131 firstArchVersion, ok := firstArchVersions[archStr]
132 if !ok {
133 panic(fmt.Errorf("Arch %q not found in firstArchVersions", archStr))
Dan Albert914449f2016-06-17 16:45:24 -0700134 }
135
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700136 return intMax(version, firstArchVersion), nil
137}
138
139func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) {
140 // TODO(danalbert): Use PlatformSdkVersion when possible.
141 // This is an interesting case because for the moment we actually need 24
142 // even though the latest released version in aosp is 23. prebuilts/ndk/r11
143 // has android-24 versions of libraries, and as platform libraries get
144 // migrated the libraries in prebuilts will need to depend on them.
145 //
146 // Once everything is all moved over to the new stuff (when there isn't a
147 // prebuilts/ndk any more) then this should be fixable, but for now I think
148 // it needs to remain as-is.
149 maxVersion := 24
150
151 firstVersion, err := normalizeNdkApiLevel(c.properties.First_version,
152 mctx.Arch())
153 if err != nil {
154 mctx.PropertyErrorf("first_version", err.Error())
Dan Albert914449f2016-06-17 16:45:24 -0700155 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700156
157 versionStrs := make([]string, maxVersion-firstVersion+1)
158 for version := firstVersion; version <= maxVersion; version++ {
159 versionStrs[version-firstVersion] = strconv.Itoa(version)
Dan Albert914449f2016-06-17 16:45:24 -0700160 }
161
162 modules := mctx.CreateVariations(versionStrs...)
163 for i, module := range modules {
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700164 module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = firstVersion + i
Dan Albert914449f2016-06-17 16:45:24 -0700165 }
166}
167
168func ndkApiMutator(mctx android.BottomUpMutatorContext) {
169 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700170 if compiler, ok := m.compiler.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700171 generateStubApiVariants(mctx, compiler)
172 }
173 }
174}
175
Colin Crossb916a382016-07-29 17:28:03 -0700176func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700177 c.baseCompiler.compilerInit(ctx)
178
179 name := strings.TrimSuffix(ctx.ModuleName(), ".ndk")
180 for _, lib := range ndkMigratedLibs {
181 if lib == name {
182 return
183 }
184 }
185 ndkMigratedLibs = append(ndkMigratedLibs, name)
186}
187
Colin Crossb916a382016-07-29 17:28:03 -0700188func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Dan Albert914449f2016-06-17 16:45:24 -0700189 arch := ctx.Arch().ArchType.String()
190
191 if !strings.HasSuffix(ctx.ModuleName(), ndkLibrarySuffix) {
192 ctx.ModuleErrorf("ndk_library modules names must be suffixed with %q\n",
193 ndkLibrarySuffix)
194 }
195 libName := strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix)
196 fileBase := fmt.Sprintf("%s.%s.%d", libName, arch, c.properties.ApiLevel)
197 stubSrcName := fileBase + ".c"
198 stubSrcPath := android.PathForModuleGen(ctx, stubSrcName)
199 versionScriptName := fileBase + ".map"
200 versionScriptPath := android.PathForModuleGen(ctx, versionScriptName)
Colin Crossb916a382016-07-29 17:28:03 -0700201 c.versionScriptPath = versionScriptPath
Dan Albert914449f2016-06-17 16:45:24 -0700202 symbolFilePath := android.PathForModuleSrc(ctx, c.properties.Symbol_file)
203 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
204 Rule: genStubSrc,
205 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
206 Input: symbolFilePath,
207 Args: map[string]string{
208 "arch": arch,
209 "apiLevel": strconv.Itoa(c.properties.ApiLevel),
210 },
211 })
212
213 flags.CFlags = append(flags.CFlags,
214 // We're knowingly doing some otherwise unsightly things with builtin
215 // functions here. We're just generating stub libraries, so ignore it.
216 "-Wno-incompatible-library-redeclaration",
217 "-Wno-builtin-requires-header",
218 "-Wno-invalid-noreturn",
219
220 // These libraries aren't actually used. Don't worry about unwinding
221 // (avoids the need to link an unwinder into a fake library).
222 "-fno-unwind-tables",
223 )
224
225 subdir := ""
226 srcs := []string{}
227 excludeSrcs := []string{}
228 extraSrcs := []android.Path{stubSrcPath}
229 extraDeps := []android.Path{}
Colin Crossb916a382016-07-29 17:28:03 -0700230 return compileObjs(ctx, flags, subdir, srcs, excludeSrcs,
Dan Albert914449f2016-06-17 16:45:24 -0700231 extraSrcs, extraDeps)
232}
233
Colin Crossb916a382016-07-29 17:28:03 -0700234func (linker *stubDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700235 return Deps{}
236}
237
Colin Crossb916a382016-07-29 17:28:03 -0700238func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
239 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(),
Dan Albert914449f2016-06-17 16:45:24 -0700240 ndkLibrarySuffix)
Colin Crossb916a382016-07-29 17:28:03 -0700241 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700242}
243
Colin Crossb916a382016-07-29 17:28:03 -0700244func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Albert2bc91ba2016-07-28 17:40:28 -0700245 objFiles android.Paths) android.Path {
246
Colin Crossb916a382016-07-29 17:28:03 -0700247 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Dan Albert2bc91ba2016-07-28 17:40:28 -0700248 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
Colin Crossb916a382016-07-29 17:28:03 -0700249 return stub.libraryDecorator.link(ctx, flags, deps, objFiles)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700250}
251
Colin Crossb916a382016-07-29 17:28:03 -0700252func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700253 arch := ctx.Target().Arch.ArchType.Name
Colin Crossb916a382016-07-29 17:28:03 -0700254 apiLevel := stub.properties.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700255
256 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
257 // architectures it's just installed to lib.
258 libDir := "lib"
259 if ctx.toolchain().Is64Bit() && arch != "arm64" {
260 libDir = "lib64"
261 }
262
263 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
264 "platforms/android-%d/arch-%s/usr/%s", apiLevel, arch, libDir))
Colin Crossb916a382016-07-29 17:28:03 -0700265 stub.installPath = ctx.InstallFile(installDir, path).String()
Dan Albert914449f2016-06-17 16:45:24 -0700266}
267
Dan Albert705c84b2016-08-08 10:45:03 -0700268func newStubLibrary() (*Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700269 module, library := NewLibrary(android.DeviceSupported, true, false)
Dan Albert914449f2016-06-17 16:45:24 -0700270 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700271 module.sanitize = nil
272 library.StripProperties.Strip.None = true
Dan Albert914449f2016-06-17 16:45:24 -0700273
Colin Crossb916a382016-07-29 17:28:03 -0700274 stub := &stubDecorator{
275 libraryDecorator: library,
276 }
277 module.compiler = stub
278 module.linker = stub
279 module.installer = stub
Dan Albert914449f2016-06-17 16:45:24 -0700280
Dan Albert705c84b2016-08-08 10:45:03 -0700281 return module, []interface{}{&stub.properties}
Dan Albert914449f2016-06-17 16:45:24 -0700282}
283
284func ndkLibraryFactory() (blueprint.Module, []interface{}) {
Dan Albert705c84b2016-08-08 10:45:03 -0700285 module, properties := newStubLibrary()
286 return android.InitAndroidArchModule(module, android.DeviceSupported,
287 android.MultilibBoth, properties...)
Dan Albert914449f2016-06-17 16:45:24 -0700288}