Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 19 | "strings" |
Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 20 | "sync" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 21 | |
| 22 | "github.com/google/blueprint" |
Jiyong Park | ee9b117 | 2021-04-06 17:40:32 +0900 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 24 | |
| 25 | "android/soong/android" |
| 26 | ) |
| 27 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 28 | func init() { |
Dan Albert | 06f58af | 2020-06-22 15:10:31 -0700 | [diff] [blame] | 29 | pctx.HostBinToolVariable("ndkStubGenerator", "ndkstubgen") |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 30 | pctx.HostBinToolVariable("ndk_api_coverage_parser", "ndk_api_coverage_parser") |
| 31 | } |
| 32 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 33 | var ( |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 34 | genStubSrc = pctx.AndroidStaticRule("genStubSrc", |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 35 | blueprint.RuleParams{ |
Dan Albert | 06f58af | 2020-06-22 15:10:31 -0700 | [diff] [blame] | 36 | Command: "$ndkStubGenerator --arch $arch --api $apiLevel " + |
| 37 | "--api-map $apiMap $flags $in $out", |
| 38 | CommandDeps: []string{"$ndkStubGenerator"}, |
Jiyong Park | 3fd0baf | 2018-12-07 16:25:39 +0900 | [diff] [blame] | 39 | }, "arch", "apiLevel", "apiMap", "flags") |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 40 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 41 | parseNdkApiRule = pctx.AndroidStaticRule("parseNdkApiRule", |
| 42 | blueprint.RuleParams{ |
| 43 | Command: "$ndk_api_coverage_parser $in $out --api-map $apiMap", |
| 44 | CommandDeps: []string{"$ndk_api_coverage_parser"}, |
| 45 | }, "apiMap") |
| 46 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 47 | ndkLibrarySuffix = ".ndk" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 48 | |
Colin Cross | 95f1ca0 | 2020-10-29 20:47:22 -0700 | [diff] [blame] | 49 | ndkKnownLibsKey = android.NewOnceKey("ndkKnownLibsKey") |
Dan Albert | de5aade | 2020-06-30 12:32:51 -0700 | [diff] [blame] | 50 | // protects ndkKnownLibs writes during parallel BeginMutator. |
| 51 | ndkKnownLibsLock sync.Mutex |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 52 | ) |
| 53 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 54 | // The First_version and Unversioned_until properties of this struct should not |
| 55 | // be used directly, but rather through the ApiLevel returning methods |
| 56 | // firstVersion() and unversionedUntil(). |
| 57 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 58 | // Creates a stub shared library based on the provided version file. |
| 59 | // |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 60 | // Example: |
| 61 | // |
| 62 | // ndk_library { |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 63 | // name: "libfoo", |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 64 | // symbol_file: "libfoo.map.txt", |
| 65 | // first_version: "9", |
| 66 | // } |
| 67 | // |
| 68 | type libraryProperties struct { |
| 69 | // Relative path to the symbol map. |
| 70 | // An example file can be seen here: TODO(danalbert): Make an example. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 71 | Symbol_file *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 72 | |
| 73 | // The first API level a library was available. A library will be generated |
| 74 | // for every API level beginning with this one. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 75 | First_version *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 76 | |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 77 | // The first API level that library should have the version script applied. |
| 78 | // This defaults to the value of first_version, and should almost never be |
| 79 | // used. This is only needed to work around platform bugs like |
| 80 | // https://github.com/android-ndk/ndk/issues/265. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 81 | Unversioned_until *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 84 | type stubDecorator struct { |
| 85 | *libraryDecorator |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 86 | |
| 87 | properties libraryProperties |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 88 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 89 | versionScriptPath android.ModuleGenPath |
| 90 | parsedCoverageXmlPath android.ModuleOutPath |
| 91 | installPath android.Path |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 92 | |
| 93 | apiLevel android.ApiLevel |
| 94 | firstVersion android.ApiLevel |
| 95 | unversionedUntil android.ApiLevel |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Colin Cross | 0477b42 | 2020-10-13 18:43:54 -0700 | [diff] [blame] | 98 | var _ versionedInterface = (*stubDecorator)(nil) |
| 99 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 100 | func shouldUseVersionScript(ctx BaseModuleContext, stub *stubDecorator) bool { |
| 101 | return stub.apiLevel.GreaterThanOrEqualTo(stub.unversionedUntil) |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Colin Cross | 0477b42 | 2020-10-13 18:43:54 -0700 | [diff] [blame] | 104 | func (stub *stubDecorator) implementationModuleName(name string) string { |
| 105 | return strings.TrimSuffix(name, ndkLibrarySuffix) |
| 106 | } |
| 107 | |
Colin Cross | 3572cf7 | 2020-10-01 15:58:11 -0700 | [diff] [blame] | 108 | func ndkLibraryVersions(ctx android.BaseMutatorContext, from android.ApiLevel) []string { |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 109 | var versions []android.ApiLevel |
| 110 | versionStrs := []string{} |
| 111 | for _, version := range ctx.Config().AllSupportedApiLevels() { |
| 112 | if version.GreaterThanOrEqualTo(from) { |
| 113 | versions = append(versions, version) |
| 114 | versionStrs = append(versionStrs, version.String()) |
| 115 | } |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 116 | } |
Dan Albert | 0b176c8 | 2020-07-23 16:43:25 -0700 | [diff] [blame] | 117 | versionStrs = append(versionStrs, android.FutureApiLevel.String()) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 118 | |
Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 119 | return versionStrs |
| 120 | } |
| 121 | |
Colin Cross | 3572cf7 | 2020-10-01 15:58:11 -0700 | [diff] [blame] | 122 | func (this *stubDecorator) stubsVersions(ctx android.BaseMutatorContext) []string { |
| 123 | if !ctx.Module().Enabled() { |
| 124 | return nil |
| 125 | } |
| 126 | firstVersion, err := nativeApiLevelFromUser(ctx, |
| 127 | String(this.properties.First_version)) |
| 128 | if err != nil { |
| 129 | ctx.PropertyErrorf("first_version", err.Error()) |
| 130 | return nil |
| 131 | } |
| 132 | return ndkLibraryVersions(ctx, firstVersion) |
| 133 | } |
| 134 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 135 | func (this *stubDecorator) initializeProperties(ctx BaseModuleContext) bool { |
Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 136 | this.apiLevel = nativeApiLevelOrPanic(ctx, this.stubsVersion()) |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 137 | |
| 138 | var err error |
| 139 | this.firstVersion, err = nativeApiLevelFromUser(ctx, |
| 140 | String(this.properties.First_version)) |
| 141 | if err != nil { |
| 142 | ctx.PropertyErrorf("first_version", err.Error()) |
| 143 | return false |
| 144 | } |
| 145 | |
Jiyong Park | ee9b117 | 2021-04-06 17:40:32 +0900 | [diff] [blame] | 146 | str := proptools.StringDefault(this.properties.Unversioned_until, "minimum") |
| 147 | this.unversionedUntil, err = nativeApiLevelFromUser(ctx, str) |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 148 | if err != nil { |
| 149 | ctx.PropertyErrorf("unversioned_until", err.Error()) |
| 150 | return false |
| 151 | } |
| 152 | |
| 153 | return true |
| 154 | } |
| 155 | |
Colin Cross | 95f1ca0 | 2020-10-29 20:47:22 -0700 | [diff] [blame] | 156 | func getNDKKnownLibs(config android.Config) *[]string { |
| 157 | return config.Once(ndkKnownLibsKey, func() interface{} { |
| 158 | return &[]string{} |
| 159 | }).(*[]string) |
| 160 | } |
| 161 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 162 | func (c *stubDecorator) compilerInit(ctx BaseModuleContext) { |
Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 163 | c.baseCompiler.compilerInit(ctx) |
| 164 | |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 165 | name := ctx.baseModuleName() |
| 166 | if strings.HasSuffix(name, ndkLibrarySuffix) { |
| 167 | ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix) |
| 168 | } |
| 169 | |
Dan Albert | de5aade | 2020-06-30 12:32:51 -0700 | [diff] [blame] | 170 | ndkKnownLibsLock.Lock() |
| 171 | defer ndkKnownLibsLock.Unlock() |
Colin Cross | 95f1ca0 | 2020-10-29 20:47:22 -0700 | [diff] [blame] | 172 | ndkKnownLibs := getNDKKnownLibs(ctx.Config()) |
| 173 | for _, lib := range *ndkKnownLibs { |
Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 174 | if lib == name { |
| 175 | return |
| 176 | } |
| 177 | } |
Colin Cross | 95f1ca0 | 2020-10-29 20:47:22 -0700 | [diff] [blame] | 178 | *ndkKnownLibs = append(*ndkKnownLibs, name) |
Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 181 | func addStubLibraryCompilerFlags(flags Flags) Flags { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 182 | flags.Global.CFlags = append(flags.Global.CFlags, |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 183 | // We're knowingly doing some otherwise unsightly things with builtin |
| 184 | // functions here. We're just generating stub libraries, so ignore it. |
| 185 | "-Wno-incompatible-library-redeclaration", |
Nick Desaulniers | eb20744 | 2019-12-12 10:15:42 -0800 | [diff] [blame] | 186 | "-Wno-incomplete-setjmp-declaration", |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 187 | "-Wno-builtin-requires-header", |
| 188 | "-Wno-invalid-noreturn", |
Chih-Hung Hsieh | 64a38dc | 2017-11-14 14:09:14 -0800 | [diff] [blame] | 189 | "-Wall", |
| 190 | "-Werror", |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 191 | // These libraries aren't actually used. Don't worry about unwinding |
| 192 | // (avoids the need to link an unwinder into a fake library). |
| 193 | "-fno-unwind-tables", |
| 194 | ) |
Jiyong Park | 48d75ef | 2019-11-21 15:11:49 +0900 | [diff] [blame] | 195 | // All symbols in the stubs library should be visible. |
| 196 | if inList("-fvisibility=hidden", flags.Local.CFlags) { |
| 197 | flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default") |
| 198 | } |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 199 | return flags |
| 200 | } |
| 201 | |
Colin Cross | f18e110 | 2017-11-16 14:33:08 -0800 | [diff] [blame] | 202 | func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags { |
| 203 | flags = stub.baseCompiler.compilerFlags(ctx, flags, deps) |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 204 | return addStubLibraryCompilerFlags(flags) |
| 205 | } |
| 206 | |
Jiyong Park | 3fd0baf | 2018-12-07 16:25:39 +0900 | [diff] [blame] | 207 | func compileStubLibrary(ctx ModuleContext, flags Flags, symbolFile, apiLevel, genstubFlags string) (Objects, android.ModuleGenPath) { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 208 | arch := ctx.Arch().ArchType.String() |
| 209 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 210 | stubSrcPath := android.PathForModuleGen(ctx, "stub.c") |
| 211 | versionScriptPath := android.PathForModuleGen(ctx, "stub.map") |
| 212 | symbolFilePath := android.PathForModuleSrc(ctx, symbolFile) |
Dan Albert | 49927d2 | 2017-03-28 15:00:46 -0700 | [diff] [blame] | 213 | apiLevelsJson := android.GetApiLevelsJson(ctx) |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 214 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 215 | Rule: genStubSrc, |
| 216 | Description: "generate stubs " + symbolFilePath.Rel(), |
| 217 | Outputs: []android.WritablePath{stubSrcPath, versionScriptPath}, |
| 218 | Input: symbolFilePath, |
| 219 | Implicits: []android.Path{apiLevelsJson}, |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 220 | Args: map[string]string{ |
| 221 | "arch": arch, |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 222 | "apiLevel": apiLevel, |
Dan Albert | 49927d2 | 2017-03-28 15:00:46 -0700 | [diff] [blame] | 223 | "apiMap": apiLevelsJson.String(), |
Jiyong Park | 3fd0baf | 2018-12-07 16:25:39 +0900 | [diff] [blame] | 224 | "flags": genstubFlags, |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 225 | }, |
| 226 | }) |
| 227 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 228 | subdir := "" |
Colin Cross | 2f33635 | 2016-10-26 10:03:47 -0700 | [diff] [blame] | 229 | srcs := []android.Path{stubSrcPath} |
Pirama Arumuga Nainar | 70ba5a3 | 2017-12-19 15:11:01 -0800 | [diff] [blame] | 230 | return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil, nil), versionScriptPath |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 231 | } |
| 232 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 233 | func parseSymbolFileForCoverage(ctx ModuleContext, symbolFile string) android.ModuleOutPath { |
| 234 | apiLevelsJson := android.GetApiLevelsJson(ctx) |
| 235 | symbolFilePath := android.PathForModuleSrc(ctx, symbolFile) |
| 236 | outputFileName := strings.Split(symbolFilePath.Base(), ".")[0] |
| 237 | parsedApiCoveragePath := android.PathForModuleOut(ctx, outputFileName+".xml") |
| 238 | ctx.Build(pctx, android.BuildParams{ |
| 239 | Rule: parseNdkApiRule, |
| 240 | Description: "parse ndk api symbol file for api coverage: " + symbolFilePath.Rel(), |
| 241 | Outputs: []android.WritablePath{parsedApiCoveragePath}, |
| 242 | Input: symbolFilePath, |
sophiez | 148b317 | 2020-06-11 17:27:56 -0700 | [diff] [blame] | 243 | Implicits: []android.Path{apiLevelsJson}, |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 244 | Args: map[string]string{ |
| 245 | "apiMap": apiLevelsJson.String(), |
| 246 | }, |
| 247 | }) |
| 248 | return parsedApiCoveragePath |
| 249 | } |
| 250 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 251 | func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 252 | if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") { |
Dan Albert | 15be0c6 | 2017-06-13 15:14:56 -0700 | [diff] [blame] | 253 | ctx.PropertyErrorf("symbol_file", "must end with .map.txt") |
| 254 | } |
| 255 | |
Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 256 | if !c.buildStubs() { |
| 257 | // NDK libraries have no implementation variant, nothing to do |
| 258 | return Objects{} |
| 259 | } |
| 260 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 261 | if !c.initializeProperties(ctx) { |
| 262 | // Emits its own errors, so we don't need to. |
| 263 | return Objects{} |
| 264 | } |
| 265 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 266 | symbolFile := String(c.properties.Symbol_file) |
| 267 | objs, versionScript := compileStubLibrary(ctx, flags, symbolFile, |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 268 | c.apiLevel.String(), "") |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 269 | c.versionScriptPath = versionScript |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 270 | if c.apiLevel.IsCurrent() && ctx.PrimaryArch() { |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 271 | c.parsedCoverageXmlPath = parseSymbolFileForCoverage(ctx, symbolFile) |
| 272 | } |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 273 | return objs |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 276 | func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 277 | return Deps{} |
| 278 | } |
| 279 | |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 280 | func (linker *stubDecorator) Name(name string) string { |
| 281 | return name + ndkLibrarySuffix |
| 282 | } |
| 283 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 284 | func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 285 | stub.libraryDecorator.libName = ctx.baseModuleName() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 286 | return stub.libraryDecorator.linkerFlags(ctx, flags) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 289 | func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 290 | objs Objects) android.Path { |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 291 | |
Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 292 | if !stub.buildStubs() { |
| 293 | // NDK libraries have no implementation variant, nothing to do |
| 294 | return nil |
| 295 | } |
| 296 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 297 | if shouldUseVersionScript(ctx, stub) { |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 298 | linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String() |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 299 | flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag) |
Dan Willemsen | 939408a | 2019-06-10 18:02:25 -0700 | [diff] [blame] | 300 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath) |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 303 | stub.libraryDecorator.skipAPIDefine = true |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 304 | return stub.libraryDecorator.link(ctx, flags, deps, objs) |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 307 | func (stub *stubDecorator) nativeCoverage() bool { |
| 308 | return false |
| 309 | } |
| 310 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 311 | func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 312 | arch := ctx.Target().Arch.ArchType.Name |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 313 | // arm64 isn't actually a multilib toolchain, so unlike the other LP64 |
| 314 | // architectures it's just installed to lib. |
| 315 | libDir := "lib" |
| 316 | if ctx.toolchain().Is64Bit() && arch != "arm64" { |
| 317 | libDir = "lib64" |
| 318 | } |
| 319 | |
| 320 | installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf( |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 321 | "platforms/android-%s/arch-%s/usr/%s", stub.apiLevel, arch, libDir)) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 322 | stub.installPath = ctx.InstallFile(installDir, path.Base(), path) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 325 | func newStubLibrary() *Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 326 | module, library := NewLibrary(android.DeviceSupported) |
| 327 | library.BuildOnlyShared() |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 328 | module.stl = nil |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 329 | module.sanitize = nil |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 330 | library.disableStripping() |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 331 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 332 | stub := &stubDecorator{ |
| 333 | libraryDecorator: library, |
| 334 | } |
| 335 | module.compiler = stub |
| 336 | module.linker = stub |
| 337 | module.installer = stub |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 338 | module.library = stub |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 339 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 340 | module.Properties.AlwaysSdk = true |
| 341 | module.Properties.Sdk_version = StringPtr("current") |
| 342 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 343 | module.AddProperties(&stub.properties, &library.MutatedProperties) |
| 344 | |
| 345 | return module |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Dan Albert | f740ed0 | 2020-07-24 14:19:06 -0700 | [diff] [blame] | 348 | // ndk_library creates a library that exposes a stub implementation of functions |
| 349 | // and variables for use at build time only. |
Jooyung Han | b90e491 | 2019-12-09 18:21:48 +0900 | [diff] [blame] | 350 | func NdkLibraryFactory() android.Module { |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 351 | module := newStubLibrary() |
| 352 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth) |
| 353 | return module |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 354 | } |