blob: d681b772bbcb49d93c113fefe3c86ce94650b6f2 [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
92type stubCompiler struct {
93 baseCompiler
94
95 properties libraryProperties
Dan Albert2bc91ba2016-07-28 17:40:28 -070096
97 linker *stubLinker
Dan Albert914449f2016-06-17 16:45:24 -070098}
99
100// OMG GO
101func intMin(a int, b int) int {
102 if a < b {
103 return a
104 } else {
105 return b
106 }
107}
108
109func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubCompiler) {
110 minVersion := 9 // Minimum version supported by the NDK.
111 // TODO(danalbert): Use PlatformSdkVersion when possible.
112 // This is an interesting case because for the moment we actually need 24
113 // even though the latest released version in aosp is 23. prebuilts/ndk/r11
114 // has android-24 versions of libraries, and as platform libraries get
115 // migrated the libraries in prebuilts will need to depend on them.
116 //
117 // Once everything is all moved over to the new stuff (when there isn't a
118 // prebuilts/ndk any more) then this should be fixable, but for now I think
119 // it needs to remain as-is.
120 maxVersion := 24
121 firstArchVersions := map[string]int{
122 "arm": 9,
123 "arm64": 21,
124 "mips": 9,
125 "mips64": 21,
126 "x86": 9,
127 "x86_64": 21,
128 }
129
130 // If the NDK drops support for a platform version, we don't want to have to
131 // fix up every module that was using it as its minimum version. Clip to the
132 // supported version here instead.
133 firstVersion, err := strconv.Atoi(c.properties.First_version)
134 if err != nil {
135 mctx.ModuleErrorf("Invalid first_version value (must be int): %q",
136 c.properties.First_version)
137 }
138 if firstVersion < minVersion {
139 firstVersion = minVersion
140 }
141
142 arch := mctx.Arch().ArchType.String()
143 firstArchVersion, ok := firstArchVersions[arch]
144 if !ok {
145 panic(fmt.Errorf("Arch %q not found in firstArchVersions", arch))
146 }
147 firstGenVersion := intMin(firstVersion, firstArchVersion)
148 versionStrs := make([]string, maxVersion-firstGenVersion+1)
149 for version := firstGenVersion; version <= maxVersion; version++ {
150 versionStrs[version-firstGenVersion] = strconv.Itoa(version)
151 }
152
153 modules := mctx.CreateVariations(versionStrs...)
154 for i, module := range modules {
155 module.(*Module).compiler.(*stubCompiler).properties.ApiLevel = firstGenVersion + i
156 }
157}
158
159func ndkApiMutator(mctx android.BottomUpMutatorContext) {
160 if m, ok := mctx.Module().(*Module); ok {
161 if compiler, ok := m.compiler.(*stubCompiler); ok {
162 generateStubApiVariants(mctx, compiler)
163 }
164 }
165}
166
167func (c *stubCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
168 arch := ctx.Arch().ArchType.String()
169
170 if !strings.HasSuffix(ctx.ModuleName(), ndkLibrarySuffix) {
171 ctx.ModuleErrorf("ndk_library modules names must be suffixed with %q\n",
172 ndkLibrarySuffix)
173 }
174 libName := strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix)
175 fileBase := fmt.Sprintf("%s.%s.%d", libName, arch, c.properties.ApiLevel)
176 stubSrcName := fileBase + ".c"
177 stubSrcPath := android.PathForModuleGen(ctx, stubSrcName)
178 versionScriptName := fileBase + ".map"
179 versionScriptPath := android.PathForModuleGen(ctx, versionScriptName)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700180 c.linker.versionScriptPath = versionScriptPath
Dan Albert914449f2016-06-17 16:45:24 -0700181 symbolFilePath := android.PathForModuleSrc(ctx, c.properties.Symbol_file)
182 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
183 Rule: genStubSrc,
184 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
185 Input: symbolFilePath,
186 Args: map[string]string{
187 "arch": arch,
188 "apiLevel": strconv.Itoa(c.properties.ApiLevel),
189 },
190 })
191
192 flags.CFlags = append(flags.CFlags,
193 // We're knowingly doing some otherwise unsightly things with builtin
194 // functions here. We're just generating stub libraries, so ignore it.
195 "-Wno-incompatible-library-redeclaration",
196 "-Wno-builtin-requires-header",
197 "-Wno-invalid-noreturn",
198
199 // These libraries aren't actually used. Don't worry about unwinding
200 // (avoids the need to link an unwinder into a fake library).
201 "-fno-unwind-tables",
202 )
203
204 subdir := ""
205 srcs := []string{}
206 excludeSrcs := []string{}
207 extraSrcs := []android.Path{stubSrcPath}
208 extraDeps := []android.Path{}
209 return c.baseCompiler.compileObjs(ctx, flags, subdir, srcs, excludeSrcs,
210 extraSrcs, extraDeps)
211}
212
213type stubLinker struct {
214 libraryLinker
Dan Albert2bc91ba2016-07-28 17:40:28 -0700215
216 versionScriptPath android.ModuleGenPath
Dan Albert914449f2016-06-17 16:45:24 -0700217}
218
Colin Cross42742b82016-08-01 13:20:05 -0700219func (linker *stubLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700220 return Deps{}
221}
222
Colin Cross42742b82016-08-01 13:20:05 -0700223func (linker *stubLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Albert914449f2016-06-17 16:45:24 -0700224 linker.libraryLinker.libName = strings.TrimSuffix(ctx.ModuleName(),
225 ndkLibrarySuffix)
Colin Cross42742b82016-08-01 13:20:05 -0700226 return linker.libraryLinker.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700227}
228
Dan Albert2bc91ba2016-07-28 17:40:28 -0700229func (linker *stubLinker) link(ctx ModuleContext, flags Flags, deps PathDeps,
230 objFiles android.Paths) android.Path {
231
232 linkerScriptFlag := "-Wl,--version-script," + linker.versionScriptPath.String()
233 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
234 return linker.libraryLinker.link(ctx, flags, deps, objFiles)
235}
236
Dan Albert914449f2016-06-17 16:45:24 -0700237type stubInstaller struct {
238 baseInstaller
239
240 compiler *stubCompiler
241
242 installPath string
243}
244
245var _ installer = (*stubInstaller)(nil)
246
247func (installer *stubInstaller) install(ctx ModuleContext, path android.Path) {
248 arch := ctx.Target().Arch.ArchType.Name
249 apiLevel := installer.compiler.properties.ApiLevel
250
251 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
252 // architectures it's just installed to lib.
253 libDir := "lib"
254 if ctx.toolchain().Is64Bit() && arch != "arm64" {
255 libDir = "lib64"
256 }
257
258 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
259 "platforms/android-%d/arch-%s/usr/%s", apiLevel, arch, libDir))
260 installer.installPath = ctx.InstallFile(installDir, path).String()
261}
262
263func newStubLibrary() *Module {
264 module := newModule(android.DeviceSupported, android.MultilibBoth)
265 module.stl = nil
266
267 linker := &stubLinker{}
268 linker.dynamicProperties.BuildShared = true
269 linker.dynamicProperties.BuildStatic = false
270 linker.stripper.StripProperties.Strip.None = true
271 module.linker = linker
272
273 compiler := &stubCompiler{}
Dan Albert2bc91ba2016-07-28 17:40:28 -0700274 compiler.linker = linker
Dan Albert914449f2016-06-17 16:45:24 -0700275 module.compiler = compiler
276 module.installer = &stubInstaller{baseInstaller{
277 dir: "lib",
278 dir64: "lib64",
279 }, compiler, ""}
280
281 return module
282}
283
284func ndkLibraryFactory() (blueprint.Module, []interface{}) {
285 module := newStubLibrary()
286 return android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth,
287 &module.compiler.(*stubCompiler).properties)
288}