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