blob: 891b49a5eceddd57b9aef924034bbdc8949ad5bf [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
102func intMin(a int, b int) int {
103 if a < b {
104 return a
105 } else {
106 return b
107 }
108}
109
Colin Crossb916a382016-07-29 17:28:03 -0700110func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) {
Dan Albert914449f2016-06-17 16:45:24 -0700111 minVersion := 9 // Minimum version supported by the NDK.
112 // TODO(danalbert): Use PlatformSdkVersion when possible.
113 // This is an interesting case because for the moment we actually need 24
114 // even though the latest released version in aosp is 23. prebuilts/ndk/r11
115 // has android-24 versions of libraries, and as platform libraries get
116 // migrated the libraries in prebuilts will need to depend on them.
117 //
118 // Once everything is all moved over to the new stuff (when there isn't a
119 // prebuilts/ndk any more) then this should be fixable, but for now I think
120 // it needs to remain as-is.
121 maxVersion := 24
122 firstArchVersions := map[string]int{
123 "arm": 9,
124 "arm64": 21,
125 "mips": 9,
126 "mips64": 21,
127 "x86": 9,
128 "x86_64": 21,
129 }
130
131 // If the NDK drops support for a platform version, we don't want to have to
132 // fix up every module that was using it as its minimum version. Clip to the
133 // supported version here instead.
134 firstVersion, err := strconv.Atoi(c.properties.First_version)
135 if err != nil {
136 mctx.ModuleErrorf("Invalid first_version value (must be int): %q",
137 c.properties.First_version)
138 }
139 if firstVersion < minVersion {
140 firstVersion = minVersion
141 }
142
143 arch := mctx.Arch().ArchType.String()
144 firstArchVersion, ok := firstArchVersions[arch]
145 if !ok {
146 panic(fmt.Errorf("Arch %q not found in firstArchVersions", arch))
147 }
148 firstGenVersion := intMin(firstVersion, firstArchVersion)
149 versionStrs := make([]string, maxVersion-firstGenVersion+1)
150 for version := firstGenVersion; version <= maxVersion; version++ {
151 versionStrs[version-firstGenVersion] = strconv.Itoa(version)
152 }
153
154 modules := mctx.CreateVariations(versionStrs...)
155 for i, module := range modules {
Colin Crossb916a382016-07-29 17:28:03 -0700156 module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = firstGenVersion + i
Dan Albert914449f2016-06-17 16:45:24 -0700157 }
158}
159
160func ndkApiMutator(mctx android.BottomUpMutatorContext) {
161 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700162 if compiler, ok := m.compiler.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700163 generateStubApiVariants(mctx, compiler)
164 }
165 }
166}
167
Colin Crossb916a382016-07-29 17:28:03 -0700168func (c *stubDecorator) compilerInit(ctx BaseModuleContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -0700169 c.baseCompiler.compilerInit(ctx)
170
171 name := strings.TrimSuffix(ctx.ModuleName(), ".ndk")
172 for _, lib := range ndkMigratedLibs {
173 if lib == name {
174 return
175 }
176 }
177 ndkMigratedLibs = append(ndkMigratedLibs, name)
178}
179
Colin Crossb916a382016-07-29 17:28:03 -0700180func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Dan Albert914449f2016-06-17 16:45:24 -0700181 arch := ctx.Arch().ArchType.String()
182
183 if !strings.HasSuffix(ctx.ModuleName(), ndkLibrarySuffix) {
184 ctx.ModuleErrorf("ndk_library modules names must be suffixed with %q\n",
185 ndkLibrarySuffix)
186 }
187 libName := strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix)
188 fileBase := fmt.Sprintf("%s.%s.%d", libName, arch, c.properties.ApiLevel)
189 stubSrcName := fileBase + ".c"
190 stubSrcPath := android.PathForModuleGen(ctx, stubSrcName)
191 versionScriptName := fileBase + ".map"
192 versionScriptPath := android.PathForModuleGen(ctx, versionScriptName)
Colin Crossb916a382016-07-29 17:28:03 -0700193 c.versionScriptPath = versionScriptPath
Dan Albert914449f2016-06-17 16:45:24 -0700194 symbolFilePath := android.PathForModuleSrc(ctx, c.properties.Symbol_file)
195 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
196 Rule: genStubSrc,
197 Outputs: []android.WritablePath{stubSrcPath, versionScriptPath},
198 Input: symbolFilePath,
199 Args: map[string]string{
200 "arch": arch,
201 "apiLevel": strconv.Itoa(c.properties.ApiLevel),
202 },
203 })
204
205 flags.CFlags = append(flags.CFlags,
206 // We're knowingly doing some otherwise unsightly things with builtin
207 // functions here. We're just generating stub libraries, so ignore it.
208 "-Wno-incompatible-library-redeclaration",
209 "-Wno-builtin-requires-header",
210 "-Wno-invalid-noreturn",
211
212 // These libraries aren't actually used. Don't worry about unwinding
213 // (avoids the need to link an unwinder into a fake library).
214 "-fno-unwind-tables",
215 )
216
217 subdir := ""
218 srcs := []string{}
219 excludeSrcs := []string{}
220 extraSrcs := []android.Path{stubSrcPath}
221 extraDeps := []android.Path{}
Colin Crossb916a382016-07-29 17:28:03 -0700222 return compileObjs(ctx, flags, subdir, srcs, excludeSrcs,
Dan Albert914449f2016-06-17 16:45:24 -0700223 extraSrcs, extraDeps)
224}
225
Colin Crossb916a382016-07-29 17:28:03 -0700226func (linker *stubDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albert914449f2016-06-17 16:45:24 -0700227 return Deps{}
228}
229
Colin Crossb916a382016-07-29 17:28:03 -0700230func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
231 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(),
Dan Albert914449f2016-06-17 16:45:24 -0700232 ndkLibrarySuffix)
Colin Crossb916a382016-07-29 17:28:03 -0700233 return stub.libraryDecorator.linkerFlags(ctx, flags)
Dan Albert914449f2016-06-17 16:45:24 -0700234}
235
Colin Crossb916a382016-07-29 17:28:03 -0700236func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
Dan Albert2bc91ba2016-07-28 17:40:28 -0700237 objFiles android.Paths) android.Path {
238
Colin Crossb916a382016-07-29 17:28:03 -0700239 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Dan Albert2bc91ba2016-07-28 17:40:28 -0700240 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
Colin Crossb916a382016-07-29 17:28:03 -0700241 return stub.libraryDecorator.link(ctx, flags, deps, objFiles)
Dan Albert2bc91ba2016-07-28 17:40:28 -0700242}
243
Colin Crossb916a382016-07-29 17:28:03 -0700244func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) {
Dan Albert914449f2016-06-17 16:45:24 -0700245 arch := ctx.Target().Arch.ArchType.Name
Colin Crossb916a382016-07-29 17:28:03 -0700246 apiLevel := stub.properties.ApiLevel
Dan Albert914449f2016-06-17 16:45:24 -0700247
248 // arm64 isn't actually a multilib toolchain, so unlike the other LP64
249 // architectures it's just installed to lib.
250 libDir := "lib"
251 if ctx.toolchain().Is64Bit() && arch != "arm64" {
252 libDir = "lib64"
253 }
254
255 installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf(
256 "platforms/android-%d/arch-%s/usr/%s", apiLevel, arch, libDir))
Colin Crossb916a382016-07-29 17:28:03 -0700257 stub.installPath = ctx.InstallFile(installDir, path).String()
Dan Albert914449f2016-06-17 16:45:24 -0700258}
259
260func newStubLibrary() *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700261 module, library := NewLibrary(android.DeviceSupported, true, false)
Dan Albert914449f2016-06-17 16:45:24 -0700262 module.stl = nil
Colin Crossb916a382016-07-29 17:28:03 -0700263 module.sanitize = nil
264 library.StripProperties.Strip.None = true
Dan Albert914449f2016-06-17 16:45:24 -0700265
Colin Crossb916a382016-07-29 17:28:03 -0700266 stub := &stubDecorator{
267 libraryDecorator: library,
268 }
269 module.compiler = stub
270 module.linker = stub
271 module.installer = stub
Dan Albert914449f2016-06-17 16:45:24 -0700272
273 return module
274}
275
276func ndkLibraryFactory() (blueprint.Module, []interface{}) {
277 module := newStubLibrary()
Colin Crossb916a382016-07-29 17:28:03 -0700278 return module.Init()
Dan Albert914449f2016-06-17 16:45:24 -0700279}