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" |
| 19 | "strconv" |
| 20 | "strings" |
Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 21 | "sync" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | |
| 25 | "android/soong/android" |
| 26 | ) |
| 27 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 28 | func init() { |
| 29 | pctx.HostBinToolVariable("ndk_api_coverage_parser", "ndk_api_coverage_parser") |
| 30 | } |
| 31 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 32 | var ( |
sophiez | b858c6d | 2020-05-06 15:57:32 -0700 | [diff] [blame] | 33 | toolPath = pctx.SourcePathVariable("toolPath", "build/soong/cc/scriptlib/gen_stub_libs.py") |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 34 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 35 | genStubSrc = pctx.AndroidStaticRule("genStubSrc", |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 36 | blueprint.RuleParams{ |
Dan Albert | 49927d2 | 2017-03-28 15:00:46 -0700 | [diff] [blame] | 37 | Command: "$toolPath --arch $arch --api $apiLevel --api-map " + |
Jiyong Park | 3fd0baf | 2018-12-07 16:25:39 +0900 | [diff] [blame] | 38 | "$apiMap $flags $in $out", |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 39 | CommandDeps: []string{"$toolPath"}, |
Jiyong Park | 3fd0baf | 2018-12-07 16:25:39 +0900 | [diff] [blame] | 40 | }, "arch", "apiLevel", "apiMap", "flags") |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 41 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 42 | parseNdkApiRule = pctx.AndroidStaticRule("parseNdkApiRule", |
| 43 | blueprint.RuleParams{ |
| 44 | Command: "$ndk_api_coverage_parser $in $out --api-map $apiMap", |
| 45 | CommandDeps: []string{"$ndk_api_coverage_parser"}, |
| 46 | }, "apiMap") |
| 47 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 48 | ndkLibrarySuffix = ".ndk" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 49 | |
Dan Albert | de5aade | 2020-06-30 12:32:51 -0700 | [diff] [blame^] | 50 | // Added as a variation dependency via depsMutator. |
| 51 | ndkKnownLibs = []string{} |
| 52 | // protects ndkKnownLibs writes during parallel BeginMutator. |
| 53 | ndkKnownLibsLock sync.Mutex |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 54 | ) |
| 55 | |
| 56 | // Creates a stub shared library based on the provided version file. |
| 57 | // |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 58 | // Example: |
| 59 | // |
| 60 | // ndk_library { |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 61 | // name: "libfoo", |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 62 | // symbol_file: "libfoo.map.txt", |
| 63 | // first_version: "9", |
| 64 | // } |
| 65 | // |
| 66 | type libraryProperties struct { |
| 67 | // Relative path to the symbol map. |
| 68 | // An example file can be seen here: TODO(danalbert): Make an example. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 69 | Symbol_file *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 70 | |
| 71 | // The first API level a library was available. A library will be generated |
| 72 | // for every API level beginning with this one. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 73 | First_version *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 74 | |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 75 | // The first API level that library should have the version script applied. |
| 76 | // This defaults to the value of first_version, and should almost never be |
| 77 | // used. This is only needed to work around platform bugs like |
| 78 | // https://github.com/android-ndk/ndk/issues/265. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 79 | Unversioned_until *string |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 80 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 81 | // Private property for use by the mutator that splits per-API level. |
Jooyung Han | aed150d | 2020-04-02 01:41:41 +0900 | [diff] [blame] | 82 | // can be one of <number:sdk_version> or <codename> or "current" |
| 83 | // passed to "gen_stub_libs.py" as it is |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 84 | ApiLevel string `blueprint:"mutated"` |
Dan Albert | 23d37e0 | 2018-11-28 08:30:10 -0800 | [diff] [blame] | 85 | |
| 86 | // True if this API is not yet ready to be shipped in the NDK. It will be |
| 87 | // available in the platform for testing, but will be excluded from the |
| 88 | // sysroot provided to the NDK proper. |
| 89 | Draft bool |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 92 | type stubDecorator struct { |
| 93 | *libraryDecorator |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 94 | |
| 95 | properties libraryProperties |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 96 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 97 | versionScriptPath android.ModuleGenPath |
| 98 | parsedCoverageXmlPath android.ModuleOutPath |
| 99 | installPath android.Path |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // OMG GO |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 103 | func intMax(a int, b int) int { |
| 104 | if a > b { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 105 | return a |
| 106 | } else { |
| 107 | return b |
| 108 | } |
| 109 | } |
| 110 | |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 111 | func normalizeNdkApiLevel(ctx android.BaseModuleContext, apiLevel string, |
Dan Albert | f5415d7 | 2017-08-17 16:19:59 -0700 | [diff] [blame] | 112 | arch android.Arch) (string, error) { |
| 113 | |
Dan Albert | 90f7a4d | 2016-11-08 14:34:24 -0800 | [diff] [blame] | 114 | if apiLevel == "current" { |
| 115 | return apiLevel, nil |
| 116 | } |
| 117 | |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 118 | minVersion := ctx.Config().MinSupportedSdkVersion() |
Colin Cross | ce87b80 | 2017-04-13 13:00:26 -0700 | [diff] [blame] | 119 | firstArchVersions := map[android.ArchType]int{ |
Dan Albert | f5415d7 | 2017-08-17 16:19:59 -0700 | [diff] [blame] | 120 | android.Arm: minVersion, |
Colin Cross | ce87b80 | 2017-04-13 13:00:26 -0700 | [diff] [blame] | 121 | android.Arm64: 21, |
Dan Albert | f5415d7 | 2017-08-17 16:19:59 -0700 | [diff] [blame] | 122 | android.X86: minVersion, |
Colin Cross | ce87b80 | 2017-04-13 13:00:26 -0700 | [diff] [blame] | 123 | android.X86_64: 21, |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Colin Cross | ce87b80 | 2017-04-13 13:00:26 -0700 | [diff] [blame] | 126 | firstArchVersion, ok := firstArchVersions[arch.ArchType] |
Dan Albert | 2e5d7d4 | 2017-03-29 18:22:39 -0700 | [diff] [blame] | 127 | if !ok { |
Colin Cross | ce87b80 | 2017-04-13 13:00:26 -0700 | [diff] [blame] | 128 | panic(fmt.Errorf("Arch %q not found in firstArchVersions", arch.ArchType)) |
Dan Albert | 2e5d7d4 | 2017-03-29 18:22:39 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if apiLevel == "minimum" { |
| 132 | return strconv.Itoa(firstArchVersion), nil |
| 133 | } |
| 134 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 135 | // If the NDK drops support for a platform version, we don't want to have to |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 136 | // fix up every module that was using it as its SDK version. Clip to the |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 137 | // supported version here instead. |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 138 | version, err := strconv.Atoi(apiLevel) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 139 | if err != nil { |
Dan Albert | 90f7a4d | 2016-11-08 14:34:24 -0800 | [diff] [blame] | 140 | return "", fmt.Errorf("API level must be an integer (is %q)", apiLevel) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 141 | } |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 142 | version = intMax(version, minVersion) |
| 143 | |
Dan Albert | 90f7a4d | 2016-11-08 14:34:24 -0800 | [diff] [blame] | 144 | return strconv.Itoa(intMax(version, firstArchVersion)), nil |
| 145 | } |
| 146 | |
| 147 | func getFirstGeneratedVersion(firstSupportedVersion string, platformVersion int) (int, error) { |
| 148 | if firstSupportedVersion == "current" { |
| 149 | return platformVersion + 1, nil |
| 150 | } |
| 151 | |
| 152 | return strconv.Atoi(firstSupportedVersion) |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 155 | func shouldUseVersionScript(ctx android.BaseModuleContext, stub *stubDecorator) (bool, error) { |
Ryan Prichard | 37ebbde | 2018-07-24 12:37:24 -0700 | [diff] [blame] | 156 | // unversioned_until is normally empty, in which case we should use the version script. |
| 157 | if String(stub.properties.Unversioned_until) == "" { |
| 158 | return true, nil |
| 159 | } |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 160 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 161 | if String(stub.properties.Unversioned_until) == "current" { |
Dan Albert | 022e7a3 | 2017-01-05 15:49:09 -0800 | [diff] [blame] | 162 | if stub.properties.ApiLevel == "current" { |
| 163 | return true, nil |
| 164 | } else { |
| 165 | return false, nil |
| 166 | } |
| 167 | } |
| 168 | |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 169 | if stub.properties.ApiLevel == "current" { |
| 170 | return true, nil |
| 171 | } |
| 172 | |
Dan Albert | e67144e | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 173 | unversionedUntil, err := android.ApiStrToNum(ctx, String(stub.properties.Unversioned_until)) |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 174 | if err != nil { |
| 175 | return true, err |
| 176 | } |
| 177 | |
Ryan Prichard | 37ebbde | 2018-07-24 12:37:24 -0700 | [diff] [blame] | 178 | version, err := android.ApiStrToNum(ctx, stub.properties.ApiLevel) |
| 179 | if err != nil { |
| 180 | return true, err |
Dan Albert | e67144e | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 183 | return version >= unversionedUntil, nil |
| 184 | } |
| 185 | |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 186 | func generateStubApiVariants(mctx android.BottomUpMutatorContext, c *stubDecorator) { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 187 | platformVersion := mctx.Config().PlatformSdkVersionInt() |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 188 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 189 | firstSupportedVersion, err := normalizeNdkApiLevel(mctx, String(c.properties.First_version), |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 190 | mctx.Arch()) |
| 191 | if err != nil { |
| 192 | mctx.PropertyErrorf("first_version", err.Error()) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 193 | } |
Dan Albert | 7fa7b2e | 2016-08-05 16:37:52 -0700 | [diff] [blame] | 194 | |
Dan Albert | 90f7a4d | 2016-11-08 14:34:24 -0800 | [diff] [blame] | 195 | firstGenVersion, err := getFirstGeneratedVersion(firstSupportedVersion, platformVersion) |
| 196 | if err != nil { |
| 197 | // In theory this is impossible because we've already run this through |
| 198 | // normalizeNdkApiLevel above. |
| 199 | mctx.PropertyErrorf("first_version", err.Error()) |
| 200 | } |
| 201 | |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 202 | var versionStrs []string |
Dan Albert | 90f7a4d | 2016-11-08 14:34:24 -0800 | [diff] [blame] | 203 | for version := firstGenVersion; version <= platformVersion; version++ { |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 204 | versionStrs = append(versionStrs, strconv.Itoa(version)) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 205 | } |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 206 | versionStrs = append(versionStrs, mctx.Config().PlatformVersionActiveCodenames()...) |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 207 | versionStrs = append(versionStrs, "current") |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 208 | |
| 209 | modules := mctx.CreateVariations(versionStrs...) |
| 210 | for i, module := range modules { |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 211 | module.(*Module).compiler.(*stubDecorator).properties.ApiLevel = versionStrs[i] |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Jooyung Han | b90e491 | 2019-12-09 18:21:48 +0900 | [diff] [blame] | 215 | func NdkApiMutator(mctx android.BottomUpMutatorContext) { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 216 | if m, ok := mctx.Module().(*Module); ok { |
Colin Cross | d402582 | 2017-04-13 12:53:07 -0700 | [diff] [blame] | 217 | if m.Enabled() { |
| 218 | if compiler, ok := m.compiler.(*stubDecorator); ok { |
| 219 | generateStubApiVariants(mctx, compiler) |
| 220 | } |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 225 | func (c *stubDecorator) compilerInit(ctx BaseModuleContext) { |
Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 226 | c.baseCompiler.compilerInit(ctx) |
| 227 | |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 228 | name := ctx.baseModuleName() |
| 229 | if strings.HasSuffix(name, ndkLibrarySuffix) { |
| 230 | ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix) |
| 231 | } |
| 232 | |
Dan Albert | de5aade | 2020-06-30 12:32:51 -0700 | [diff] [blame^] | 233 | ndkKnownLibsLock.Lock() |
| 234 | defer ndkKnownLibsLock.Unlock() |
| 235 | for _, lib := range ndkKnownLibs { |
Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 236 | if lib == name { |
| 237 | return |
| 238 | } |
| 239 | } |
Dan Albert | de5aade | 2020-06-30 12:32:51 -0700 | [diff] [blame^] | 240 | ndkKnownLibs = append(ndkKnownLibs, name) |
Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 243 | func addStubLibraryCompilerFlags(flags Flags) Flags { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 244 | flags.Global.CFlags = append(flags.Global.CFlags, |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 245 | // We're knowingly doing some otherwise unsightly things with builtin |
| 246 | // functions here. We're just generating stub libraries, so ignore it. |
| 247 | "-Wno-incompatible-library-redeclaration", |
Nick Desaulniers | eb20744 | 2019-12-12 10:15:42 -0800 | [diff] [blame] | 248 | "-Wno-incomplete-setjmp-declaration", |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 249 | "-Wno-builtin-requires-header", |
| 250 | "-Wno-invalid-noreturn", |
Chih-Hung Hsieh | 64a38dc | 2017-11-14 14:09:14 -0800 | [diff] [blame] | 251 | "-Wall", |
| 252 | "-Werror", |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 253 | // These libraries aren't actually used. Don't worry about unwinding |
| 254 | // (avoids the need to link an unwinder into a fake library). |
| 255 | "-fno-unwind-tables", |
| 256 | ) |
Jiyong Park | 48d75ef | 2019-11-21 15:11:49 +0900 | [diff] [blame] | 257 | // All symbols in the stubs library should be visible. |
| 258 | if inList("-fvisibility=hidden", flags.Local.CFlags) { |
| 259 | flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default") |
| 260 | } |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 261 | return flags |
| 262 | } |
| 263 | |
Colin Cross | f18e110 | 2017-11-16 14:33:08 -0800 | [diff] [blame] | 264 | func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags { |
| 265 | flags = stub.baseCompiler.compilerFlags(ctx, flags, deps) |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 266 | return addStubLibraryCompilerFlags(flags) |
| 267 | } |
| 268 | |
Jiyong Park | 3fd0baf | 2018-12-07 16:25:39 +0900 | [diff] [blame] | 269 | 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] | 270 | arch := ctx.Arch().ArchType.String() |
| 271 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 272 | stubSrcPath := android.PathForModuleGen(ctx, "stub.c") |
| 273 | versionScriptPath := android.PathForModuleGen(ctx, "stub.map") |
| 274 | symbolFilePath := android.PathForModuleSrc(ctx, symbolFile) |
Dan Albert | 49927d2 | 2017-03-28 15:00:46 -0700 | [diff] [blame] | 275 | apiLevelsJson := android.GetApiLevelsJson(ctx) |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 276 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 277 | Rule: genStubSrc, |
| 278 | Description: "generate stubs " + symbolFilePath.Rel(), |
| 279 | Outputs: []android.WritablePath{stubSrcPath, versionScriptPath}, |
| 280 | Input: symbolFilePath, |
| 281 | Implicits: []android.Path{apiLevelsJson}, |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 282 | Args: map[string]string{ |
| 283 | "arch": arch, |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 284 | "apiLevel": apiLevel, |
Dan Albert | 49927d2 | 2017-03-28 15:00:46 -0700 | [diff] [blame] | 285 | "apiMap": apiLevelsJson.String(), |
Jiyong Park | 3fd0baf | 2018-12-07 16:25:39 +0900 | [diff] [blame] | 286 | "flags": genstubFlags, |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 287 | }, |
| 288 | }) |
| 289 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 290 | subdir := "" |
Colin Cross | 2f33635 | 2016-10-26 10:03:47 -0700 | [diff] [blame] | 291 | srcs := []android.Path{stubSrcPath} |
Pirama Arumuga Nainar | 70ba5a3 | 2017-12-19 15:11:01 -0800 | [diff] [blame] | 292 | return compileObjs(ctx, flagsToBuilderFlags(flags), subdir, srcs, nil, nil), versionScriptPath |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 293 | } |
| 294 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 295 | func parseSymbolFileForCoverage(ctx ModuleContext, symbolFile string) android.ModuleOutPath { |
| 296 | apiLevelsJson := android.GetApiLevelsJson(ctx) |
| 297 | symbolFilePath := android.PathForModuleSrc(ctx, symbolFile) |
| 298 | outputFileName := strings.Split(symbolFilePath.Base(), ".")[0] |
| 299 | parsedApiCoveragePath := android.PathForModuleOut(ctx, outputFileName+".xml") |
| 300 | ctx.Build(pctx, android.BuildParams{ |
| 301 | Rule: parseNdkApiRule, |
| 302 | Description: "parse ndk api symbol file for api coverage: " + symbolFilePath.Rel(), |
| 303 | Outputs: []android.WritablePath{parsedApiCoveragePath}, |
| 304 | Input: symbolFilePath, |
sophiez | 148b317 | 2020-06-11 17:27:56 -0700 | [diff] [blame] | 305 | Implicits: []android.Path{apiLevelsJson}, |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 306 | Args: map[string]string{ |
| 307 | "apiMap": apiLevelsJson.String(), |
| 308 | }, |
| 309 | }) |
| 310 | return parsedApiCoveragePath |
| 311 | } |
| 312 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 313 | func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 314 | if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") { |
Dan Albert | 15be0c6 | 2017-06-13 15:14:56 -0700 | [diff] [blame] | 315 | ctx.PropertyErrorf("symbol_file", "must end with .map.txt") |
| 316 | } |
| 317 | |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 318 | symbolFile := String(c.properties.Symbol_file) |
| 319 | objs, versionScript := compileStubLibrary(ctx, flags, symbolFile, |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 320 | c.properties.ApiLevel, "") |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 321 | c.versionScriptPath = versionScript |
sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 322 | if c.properties.ApiLevel == "current" && ctx.PrimaryArch() { |
| 323 | c.parsedCoverageXmlPath = parseSymbolFileForCoverage(ctx, symbolFile) |
| 324 | } |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 325 | return objs |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 328 | func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 329 | return Deps{} |
| 330 | } |
| 331 | |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 332 | func (linker *stubDecorator) Name(name string) string { |
| 333 | return name + ndkLibrarySuffix |
| 334 | } |
| 335 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 336 | func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 337 | stub.libraryDecorator.libName = ctx.baseModuleName() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 338 | return stub.libraryDecorator.linkerFlags(ctx, flags) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 341 | func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 342 | objs Objects) android.Path { |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 343 | |
Dan Albert | e67144e | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 344 | useVersionScript, err := shouldUseVersionScript(ctx, stub) |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 345 | if err != nil { |
| 346 | ctx.ModuleErrorf(err.Error()) |
| 347 | } |
| 348 | |
| 349 | if useVersionScript { |
| 350 | linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String() |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 351 | flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag) |
Dan Willemsen | 939408a | 2019-06-10 18:02:25 -0700 | [diff] [blame] | 352 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath) |
Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 355 | return stub.libraryDecorator.link(ctx, flags, deps, objs) |
Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 358 | func (stub *stubDecorator) nativeCoverage() bool { |
| 359 | return false |
| 360 | } |
| 361 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 362 | func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 363 | arch := ctx.Target().Arch.ArchType.Name |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 364 | apiLevel := stub.properties.ApiLevel |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 365 | |
| 366 | // arm64 isn't actually a multilib toolchain, so unlike the other LP64 |
| 367 | // architectures it's just installed to lib. |
| 368 | libDir := "lib" |
| 369 | if ctx.toolchain().Is64Bit() && arch != "arm64" { |
| 370 | libDir = "lib64" |
| 371 | } |
| 372 | |
| 373 | installDir := getNdkInstallBase(ctx).Join(ctx, fmt.Sprintf( |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 374 | "platforms/android-%s/arch-%s/usr/%s", apiLevel, arch, libDir)) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 375 | stub.installPath = ctx.InstallFile(installDir, path.Base(), path) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 378 | func newStubLibrary() *Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 379 | module, library := NewLibrary(android.DeviceSupported) |
| 380 | library.BuildOnlyShared() |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 381 | module.stl = nil |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 382 | module.sanitize = nil |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 383 | library.StripProperties.Strip.None = BoolPtr(true) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 384 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 385 | stub := &stubDecorator{ |
| 386 | libraryDecorator: library, |
| 387 | } |
| 388 | module.compiler = stub |
| 389 | module.linker = stub |
| 390 | module.installer = stub |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 391 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 392 | module.Properties.AlwaysSdk = true |
| 393 | module.Properties.Sdk_version = StringPtr("current") |
| 394 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 395 | module.AddProperties(&stub.properties, &library.MutatedProperties) |
| 396 | |
| 397 | return module |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Patrice Arruda | 6ea4211 | 2019-04-03 08:43:30 -0700 | [diff] [blame] | 400 | // ndk_library creates a stub library that exposes dummy implementation |
| 401 | // of functions and variables for use at build time only. |
Jooyung Han | b90e491 | 2019-12-09 18:21:48 +0900 | [diff] [blame] | 402 | func NdkLibraryFactory() android.Module { |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 403 | module := newStubLibrary() |
| 404 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth) |
dimitry | 03dc3f6 | 2019-05-09 14:07:34 +0200 | [diff] [blame] | 405 | module.ModuleBase.EnableNativeBridgeSupportByDefault() |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 406 | return module |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 407 | } |