| 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 | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 19 | "path/filepath" | 
| Dan Albert | ad66593 | 2021-06-07 13:19:49 -0700 | [diff] [blame] | 20 | "runtime" | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 21 | "strings" | 
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 22 | "sync" | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 23 |  | 
|  | 24 | "github.com/google/blueprint" | 
| Jiyong Park | ee9b117 | 2021-04-06 17:40:32 +0900 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | "android/soong/android" | 
| Jingwen Chen | 341f735 | 2022-01-11 05:42:49 +0000 | [diff] [blame] | 28 | "android/soong/cc/config" | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 29 | ) | 
|  | 30 |  | 
| sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 31 | func init() { | 
| Dan Albert | 06f58af | 2020-06-22 15:10:31 -0700 | [diff] [blame] | 32 | pctx.HostBinToolVariable("ndkStubGenerator", "ndkstubgen") | 
| Matthias Maennich | ca8ae65 | 2023-06-30 21:52:17 +0100 | [diff] [blame] | 33 | pctx.HostBinToolVariable("stg", "stg") | 
| Matthias Maennich | 658bb4d | 2023-06-30 23:28:15 +0100 | [diff] [blame] | 34 | pctx.HostBinToolVariable("stgdiff", "stgdiff") | 
| sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 35 | } | 
|  | 36 |  | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 37 | var ( | 
| Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 38 | genStubSrc = pctx.AndroidStaticRule("genStubSrc", | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 39 | blueprint.RuleParams{ | 
| Dan Albert | 06f58af | 2020-06-22 15:10:31 -0700 | [diff] [blame] | 40 | Command: "$ndkStubGenerator --arch $arch --api $apiLevel " + | 
|  | 41 | "--api-map $apiMap $flags $in $out", | 
|  | 42 | CommandDeps: []string{"$ndkStubGenerator"}, | 
| Jiyong Park | 3fd0baf | 2018-12-07 16:25:39 +0900 | [diff] [blame] | 43 | }, "arch", "apiLevel", "apiMap", "flags") | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 44 |  | 
| Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 45 | // $headersList should include paths to public headers. All types | 
|  | 46 | // that are defined outside of public headers will be excluded from | 
|  | 47 | // ABI monitoring. | 
|  | 48 | // | 
|  | 49 | // STG tool doesn't access content of files listed in $headersList, | 
|  | 50 | // so there is no need to add them to dependencies. | 
| Matthias Maennich | 55486f8 | 2023-07-03 12:25:27 +0100 | [diff] [blame] | 51 | stg = pctx.AndroidStaticRule("stg", | 
|  | 52 | blueprint.RuleParams{ | 
| Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 53 | Command:     "$stg -S :$symbolList --file-filter :$headersList --elf $in -o $out", | 
| Matthias Maennich | 55486f8 | 2023-07-03 12:25:27 +0100 | [diff] [blame] | 54 | CommandDeps: []string{"$stg"}, | 
| Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 55 | }, "symbolList", "headersList") | 
| Matthias Maennich | 55486f8 | 2023-07-03 12:25:27 +0100 | [diff] [blame] | 56 |  | 
| Matthias Maennich | 658bb4d | 2023-06-30 23:28:15 +0100 | [diff] [blame] | 57 | stgdiff = pctx.AndroidStaticRule("stgdiff", | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 58 | blueprint.RuleParams{ | 
|  | 59 | // Need to create *some* output for ninja. We don't want to use tee | 
|  | 60 | // because we don't want to spam the build output with "nothing | 
|  | 61 | // changed" messages, so redirect output message to $out, and if | 
|  | 62 | // changes were detected print the output and fail. | 
| Dan Albert | 5b2fd58 | 2023-10-05 21:43:41 +0000 | [diff] [blame] | 63 | Command:     "$stgdiff $args --stg $in -o $out || (cat $out && echo 'Run $$ANDROID_BUILD_TOP/development/tools/ndk/update_ndk_abi.sh to update the ABI dumps.' && false)", | 
| Matthias Maennich | 658bb4d | 2023-06-30 23:28:15 +0100 | [diff] [blame] | 64 | CommandDeps: []string{"$stgdiff"}, | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 65 | }, "args") | 
|  | 66 |  | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 67 | ndkLibrarySuffix = ".ndk" | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 68 |  | 
| Colin Cross | 95f1ca0 | 2020-10-29 20:47:22 -0700 | [diff] [blame] | 69 | ndkKnownLibsKey = android.NewOnceKey("ndkKnownLibsKey") | 
| Dan Albert | de5aade | 2020-06-30 12:32:51 -0700 | [diff] [blame] | 70 | // protects ndkKnownLibs writes during parallel BeginMutator. | 
|  | 71 | ndkKnownLibsLock sync.Mutex | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | stubImplementation = dependencyTag{name: "stubImplementation"} | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 74 | ) | 
|  | 75 |  | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 76 | // The First_version and Unversioned_until properties of this struct should not | 
|  | 77 | // be used directly, but rather through the ApiLevel returning methods | 
|  | 78 | // firstVersion() and unversionedUntil(). | 
|  | 79 |  | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 80 | // Creates a stub shared library based on the provided version file. | 
|  | 81 | // | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 82 | // Example: | 
|  | 83 | // | 
| Spandan Das | 73bcafc | 2022-08-18 23:26:00 +0000 | [diff] [blame] | 84 | // ndk_library { | 
|  | 85 | // | 
|  | 86 | //	name: "libfoo", | 
|  | 87 | //	symbol_file: "libfoo.map.txt", | 
|  | 88 | //	first_version: "9", | 
|  | 89 | // | 
|  | 90 | // } | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 91 | type libraryProperties struct { | 
|  | 92 | // Relative path to the symbol map. | 
|  | 93 | // An example file can be seen here: TODO(danalbert): Make an example. | 
| Inseob Kim | 5eb7ee9 | 2022-04-27 10:30:34 +0900 | [diff] [blame] | 94 | Symbol_file *string `android:"path"` | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 95 |  | 
|  | 96 | // The first API level a library was available. A library will be generated | 
|  | 97 | // for every API level beginning with this one. | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 98 | First_version *string | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 99 |  | 
| Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 100 | // The first API level that library should have the version script applied. | 
|  | 101 | // This defaults to the value of first_version, and should almost never be | 
|  | 102 | // used. This is only needed to work around platform bugs like | 
|  | 103 | // https://github.com/android-ndk/ndk/issues/265. | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 104 | Unversioned_until *string | 
| Dan Albert | 604086f | 2021-06-15 13:23:44 -0700 | [diff] [blame] | 105 |  | 
| Colin Cross | a158a71 | 2024-09-04 15:33:04 +0000 | [diff] [blame] | 106 | // DO NOT USE THIS | 
|  | 107 | // NDK libraries should not export their headers. Headers belonging to NDK | 
|  | 108 | // libraries should be added to the NDK with an ndk_headers module. | 
| Spandan Das | 73bcafc | 2022-08-18 23:26:00 +0000 | [diff] [blame] | 109 | Export_header_libs []string | 
| Colin Cross | a158a71 | 2024-09-04 15:33:04 +0000 | [diff] [blame] | 110 |  | 
|  | 111 | // Do not add other export_* properties without consulting with danalbert@. | 
|  | 112 | // Consumers of ndk_library modules should emulate the typical NDK build | 
|  | 113 | // behavior as closely as possible (that is, all NDK APIs are exposed to | 
|  | 114 | // builds via --sysroot). Export behaviors used in Soong will not be present | 
|  | 115 | // for app developers as they don't use Soong, and reliance on these export | 
|  | 116 | // behaviors can mask issues with the NDK sysroot. | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 119 | type stubDecorator struct { | 
|  | 120 | *libraryDecorator | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 121 |  | 
|  | 122 | properties libraryProperties | 
| Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 123 |  | 
| sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 124 | versionScriptPath     android.ModuleGenPath | 
|  | 125 | parsedCoverageXmlPath android.ModuleOutPath | 
|  | 126 | installPath           android.Path | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 127 | abiDumpPath           android.OutputPath | 
| Dan Albert | 2d0dce1 | 2024-09-18 19:34:32 +0000 | [diff] [blame] | 128 | hasAbiDump            bool | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 129 | abiDiffPaths          android.Paths | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 130 |  | 
|  | 131 | apiLevel         android.ApiLevel | 
|  | 132 | firstVersion     android.ApiLevel | 
|  | 133 | unversionedUntil android.ApiLevel | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
| Colin Cross | 0477b42 | 2020-10-13 18:43:54 -0700 | [diff] [blame] | 136 | var _ versionedInterface = (*stubDecorator)(nil) | 
|  | 137 |  | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 138 | func shouldUseVersionScript(ctx BaseModuleContext, stub *stubDecorator) bool { | 
|  | 139 | return stub.apiLevel.GreaterThanOrEqualTo(stub.unversionedUntil) | 
| Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
| Colin Cross | 0477b42 | 2020-10-13 18:43:54 -0700 | [diff] [blame] | 142 | func (stub *stubDecorator) implementationModuleName(name string) string { | 
|  | 143 | return strings.TrimSuffix(name, ndkLibrarySuffix) | 
|  | 144 | } | 
|  | 145 |  | 
| Colin Cross | add04a8 | 2024-05-22 09:57:59 -0700 | [diff] [blame] | 146 | func ndkLibraryVersions(ctx android.BaseModuleContext, from android.ApiLevel) []string { | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 147 | versionStrs := []string{} | 
| Dan Albert | 499c92e | 2024-10-31 22:15:40 +0000 | [diff] [blame] | 148 | for _, version := range ctx.Config().FinalApiLevels() { | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 149 | if version.GreaterThanOrEqualTo(from) { | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 150 | versionStrs = append(versionStrs, version.String()) | 
|  | 151 | } | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 152 | } | 
| Dan Albert | 0b176c8 | 2020-07-23 16:43:25 -0700 | [diff] [blame] | 153 | versionStrs = append(versionStrs, android.FutureApiLevel.String()) | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 154 |  | 
| Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 155 | return versionStrs | 
|  | 156 | } | 
|  | 157 |  | 
| Colin Cross | add04a8 | 2024-05-22 09:57:59 -0700 | [diff] [blame] | 158 | func (this *stubDecorator) stubsVersions(ctx android.BaseModuleContext) []string { | 
| Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 159 | if !ctx.Module().Enabled(ctx) { | 
| Colin Cross | 3572cf7 | 2020-10-01 15:58:11 -0700 | [diff] [blame] | 160 | return nil | 
|  | 161 | } | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 162 | if ctx.Target().NativeBridge == android.NativeBridgeEnabled { | 
|  | 163 | ctx.Module().Disable() | 
|  | 164 | return nil | 
|  | 165 | } | 
| Colin Cross | 3572cf7 | 2020-10-01 15:58:11 -0700 | [diff] [blame] | 166 | firstVersion, err := nativeApiLevelFromUser(ctx, | 
|  | 167 | String(this.properties.First_version)) | 
|  | 168 | if err != nil { | 
|  | 169 | ctx.PropertyErrorf("first_version", err.Error()) | 
|  | 170 | return nil | 
|  | 171 | } | 
|  | 172 | return ndkLibraryVersions(ctx, firstVersion) | 
|  | 173 | } | 
|  | 174 |  | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 175 | func (this *stubDecorator) initializeProperties(ctx BaseModuleContext) bool { | 
| Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 176 | this.apiLevel = nativeApiLevelOrPanic(ctx, this.stubsVersion()) | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 177 |  | 
|  | 178 | var err error | 
|  | 179 | this.firstVersion, err = nativeApiLevelFromUser(ctx, | 
|  | 180 | String(this.properties.First_version)) | 
|  | 181 | if err != nil { | 
|  | 182 | ctx.PropertyErrorf("first_version", err.Error()) | 
|  | 183 | return false | 
|  | 184 | } | 
|  | 185 |  | 
| Jiyong Park | ee9b117 | 2021-04-06 17:40:32 +0900 | [diff] [blame] | 186 | str := proptools.StringDefault(this.properties.Unversioned_until, "minimum") | 
|  | 187 | this.unversionedUntil, err = nativeApiLevelFromUser(ctx, str) | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 188 | if err != nil { | 
|  | 189 | ctx.PropertyErrorf("unversioned_until", err.Error()) | 
|  | 190 | return false | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | return true | 
|  | 194 | } | 
|  | 195 |  | 
| Colin Cross | 95f1ca0 | 2020-10-29 20:47:22 -0700 | [diff] [blame] | 196 | func getNDKKnownLibs(config android.Config) *[]string { | 
|  | 197 | return config.Once(ndkKnownLibsKey, func() interface{} { | 
|  | 198 | return &[]string{} | 
|  | 199 | }).(*[]string) | 
|  | 200 | } | 
|  | 201 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 202 | func (c *stubDecorator) compilerInit(ctx BaseModuleContext) { | 
| Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 203 | c.baseCompiler.compilerInit(ctx) | 
|  | 204 |  | 
| Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 205 | name := ctx.baseModuleName() | 
|  | 206 | if strings.HasSuffix(name, ndkLibrarySuffix) { | 
|  | 207 | ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", ndkLibrarySuffix) | 
|  | 208 | } | 
|  | 209 |  | 
| Dan Albert | de5aade | 2020-06-30 12:32:51 -0700 | [diff] [blame] | 210 | ndkKnownLibsLock.Lock() | 
|  | 211 | defer ndkKnownLibsLock.Unlock() | 
| Colin Cross | 95f1ca0 | 2020-10-29 20:47:22 -0700 | [diff] [blame] | 212 | ndkKnownLibs := getNDKKnownLibs(ctx.Config()) | 
|  | 213 | for _, lib := range *ndkKnownLibs { | 
| Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 214 | if lib == name { | 
|  | 215 | return | 
|  | 216 | } | 
|  | 217 | } | 
| Colin Cross | 95f1ca0 | 2020-10-29 20:47:22 -0700 | [diff] [blame] | 218 | *ndkKnownLibs = append(*ndkKnownLibs, name) | 
| Dan Albert | 7e9d295 | 2016-08-04 13:02:36 -0700 | [diff] [blame] | 219 | } | 
|  | 220 |  | 
| Jingwen Chen | 341f735 | 2022-01-11 05:42:49 +0000 | [diff] [blame] | 221 | var stubLibraryCompilerFlags = []string{ | 
|  | 222 | // We're knowingly doing some otherwise unsightly things with builtin | 
|  | 223 | // functions here. We're just generating stub libraries, so ignore it. | 
|  | 224 | "-Wno-incompatible-library-redeclaration", | 
|  | 225 | "-Wno-incomplete-setjmp-declaration", | 
|  | 226 | "-Wno-builtin-requires-header", | 
|  | 227 | "-Wno-invalid-noreturn", | 
|  | 228 | "-Wall", | 
|  | 229 | "-Werror", | 
|  | 230 | // These libraries aren't actually used. Don't worry about unwinding | 
|  | 231 | // (avoids the need to link an unwinder into a fake library). | 
|  | 232 | "-fno-unwind-tables", | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | func init() { | 
| Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 236 | pctx.StaticVariable("StubLibraryCompilerFlags", strings.Join(stubLibraryCompilerFlags, " ")) | 
| Jingwen Chen | 341f735 | 2022-01-11 05:42:49 +0000 | [diff] [blame] | 237 | } | 
|  | 238 |  | 
| George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 239 | func addStubLibraryCompilerFlags(flags Flags) Flags { | 
| Jingwen Chen | 341f735 | 2022-01-11 05:42:49 +0000 | [diff] [blame] | 240 | flags.Global.CFlags = append(flags.Global.CFlags, stubLibraryCompilerFlags...) | 
| Jiyong Park | 48d75ef | 2019-11-21 15:11:49 +0900 | [diff] [blame] | 241 | // All symbols in the stubs library should be visible. | 
|  | 242 | if inList("-fvisibility=hidden", flags.Local.CFlags) { | 
|  | 243 | flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default") | 
|  | 244 | } | 
| George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 245 | return flags | 
|  | 246 | } | 
|  | 247 |  | 
| Colin Cross | f18e110 | 2017-11-16 14:33:08 -0800 | [diff] [blame] | 248 | func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags { | 
|  | 249 | flags = stub.baseCompiler.compilerFlags(ctx, flags, deps) | 
| George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 250 | return addStubLibraryCompilerFlags(flags) | 
|  | 251 | } | 
|  | 252 |  | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 253 | type ndkApiOutputs struct { | 
|  | 254 | stubSrc       android.ModuleGenPath | 
|  | 255 | versionScript android.ModuleGenPath | 
|  | 256 | symbolList    android.ModuleGenPath | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | func parseNativeAbiDefinition(ctx ModuleContext, symbolFile string, | 
|  | 260 | apiLevel android.ApiLevel, genstubFlags string) ndkApiOutputs { | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 261 |  | 
| Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 262 | stubSrcPath := android.PathForModuleGen(ctx, "stub.c") | 
|  | 263 | versionScriptPath := android.PathForModuleGen(ctx, "stub.map") | 
|  | 264 | symbolFilePath := android.PathForModuleSrc(ctx, symbolFile) | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 265 | symbolListPath := android.PathForModuleGen(ctx, "abi_symbol_list.txt") | 
| Dan Albert | 49927d2 | 2017-03-28 15:00:46 -0700 | [diff] [blame] | 266 | apiLevelsJson := android.GetApiLevelsJson(ctx) | 
| Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 267 | ctx.Build(pctx, android.BuildParams{ | 
| Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 268 | Rule:        genStubSrc, | 
|  | 269 | Description: "generate stubs " + symbolFilePath.Rel(), | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 270 | Outputs: []android.WritablePath{stubSrcPath, versionScriptPath, | 
|  | 271 | symbolListPath}, | 
|  | 272 | Input:     symbolFilePath, | 
|  | 273 | Implicits: []android.Path{apiLevelsJson}, | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 274 | Args: map[string]string{ | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 275 | "arch":     ctx.Arch().ArchType.String(), | 
|  | 276 | "apiLevel": apiLevel.String(), | 
| Dan Albert | 49927d2 | 2017-03-28 15:00:46 -0700 | [diff] [blame] | 277 | "apiMap":   apiLevelsJson.String(), | 
| Jiyong Park | 3fd0baf | 2018-12-07 16:25:39 +0900 | [diff] [blame] | 278 | "flags":    genstubFlags, | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 279 | }, | 
|  | 280 | }) | 
|  | 281 |  | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 282 | return ndkApiOutputs{ | 
|  | 283 | stubSrc:       stubSrcPath, | 
|  | 284 | versionScript: versionScriptPath, | 
|  | 285 | symbolList:    symbolListPath, | 
|  | 286 | } | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | func compileStubLibrary(ctx ModuleContext, flags Flags, src android.Path) Objects { | 
| Mitch Phillips | 4e5f9a1 | 2022-04-29 13:12:28 -0700 | [diff] [blame] | 290 | // libc/libm stubs libraries end up mismatching with clang's internal definition of these | 
|  | 291 | // functions (which have noreturn attributes and other things). Because we just want to create a | 
|  | 292 | // stub with symbol definitions, and types aren't important in C, ignore the mismatch. | 
|  | 293 | flags.Local.ConlyFlags = append(flags.Local.ConlyFlags, "-fno-builtin") | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 294 | return compileObjs(ctx, flagsToBuilderFlags(flags), "", | 
| Chih-Hung Hsieh | 9db8a0c | 2022-02-17 12:54:45 -0800 | [diff] [blame] | 295 | android.Paths{src}, nil, nil, nil, nil) | 
| Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 296 | } | 
|  | 297 |  | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 298 | func (this *stubDecorator) findImplementationLibrary(ctx ModuleContext) android.Path { | 
|  | 299 | dep := ctx.GetDirectDepWithTag(strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix), | 
|  | 300 | stubImplementation) | 
|  | 301 | if dep == nil { | 
|  | 302 | ctx.ModuleErrorf("Could not find implementation for stub") | 
|  | 303 | return nil | 
|  | 304 | } | 
|  | 305 | impl, ok := dep.(*Module) | 
|  | 306 | if !ok { | 
|  | 307 | ctx.ModuleErrorf("Implementation for stub is not correct module type") | 
| Alan Stokes | 73d3245 | 2022-11-01 14:05:08 +0000 | [diff] [blame] | 308 | return nil | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 309 | } | 
|  | 310 | output := impl.UnstrippedOutputFile() | 
|  | 311 | if output == nil { | 
|  | 312 | ctx.ModuleErrorf("implementation module (%s) has no output", impl) | 
|  | 313 | return nil | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | return output | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | func (this *stubDecorator) libraryName(ctx ModuleContext) string { | 
|  | 320 | return strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix) | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | func (this *stubDecorator) findPrebuiltAbiDump(ctx ModuleContext, | 
|  | 324 | apiLevel android.ApiLevel) android.OptionalPath { | 
|  | 325 |  | 
|  | 326 | subpath := filepath.Join("prebuilts/abi-dumps/ndk", apiLevel.String(), | 
| Matthias Maennich | ca8ae65 | 2023-06-30 21:52:17 +0100 | [diff] [blame] | 327 | ctx.Arch().ArchType.String(), this.libraryName(ctx), "abi.stg") | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 328 | return android.ExistentPathForSource(ctx, subpath) | 
|  | 329 | } | 
|  | 330 |  | 
| Dan Albert | 499c92e | 2024-10-31 22:15:40 +0000 | [diff] [blame] | 331 | func (this *stubDecorator) builtAbiDumpLocation(ctx ModuleContext, apiLevel android.ApiLevel) android.OutputPath { | 
|  | 332 | return getNdkAbiDumpInstallBase(ctx).Join(ctx, | 
|  | 333 | apiLevel.String(), ctx.Arch().ArchType.String(), | 
|  | 334 | this.libraryName(ctx), "abi.stg") | 
|  | 335 | } | 
|  | 336 |  | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 337 | // Feature flag. | 
| Dan Albert | 2d0dce1 | 2024-09-18 19:34:32 +0000 | [diff] [blame] | 338 | func (this *stubDecorator) canDumpAbi(ctx ModuleContext) bool { | 
| Dan Albert | f71006a | 2022-04-14 23:08:51 +0000 | [diff] [blame] | 339 | if runtime.GOOS == "darwin" { | 
|  | 340 | return false | 
|  | 341 | } | 
| Dan Albert | 2d0dce1 | 2024-09-18 19:34:32 +0000 | [diff] [blame] | 342 | if strings.HasPrefix(ctx.ModuleDir(), "bionic/") { | 
| Dan Albert | fbe5780 | 2024-08-09 20:39:39 +0000 | [diff] [blame] | 343 | // Bionic has enough uncommon implementation details like ifuncs and asm | 
|  | 344 | // code that the ABI tracking here has a ton of false positives. That's | 
|  | 345 | // causing pretty extreme friction for development there, so disabling | 
|  | 346 | // it until the workflow can be improved. | 
|  | 347 | // | 
|  | 348 | // http://b/358653811 | 
|  | 349 | return false | 
|  | 350 | } | 
| Dan Albert | 2d0dce1 | 2024-09-18 19:34:32 +0000 | [diff] [blame] | 351 |  | 
| Dan Albert | 326ab24 | 2023-04-20 17:38:29 +0000 | [diff] [blame] | 352 | // http://b/156513478 | 
| Dan Albert | 2d0dce1 | 2024-09-18 19:34:32 +0000 | [diff] [blame] | 353 | return ctx.Config().ReleaseNdkAbiMonitored() | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 354 | } | 
|  | 355 |  | 
|  | 356 | // Feature flag to disable diffing against prebuilts. | 
| Dan Albert | 499c92e | 2024-10-31 22:15:40 +0000 | [diff] [blame] | 357 | func (this *stubDecorator) canDiffAbi(config android.Config) bool { | 
|  | 358 | if this.apiLevel.IsCurrent() { | 
|  | 359 | // Diffs are performed from this to next, and there's nothing after | 
|  | 360 | // current. | 
|  | 361 | return false | 
|  | 362 | } | 
|  | 363 |  | 
| Aleksei Vetrov | 146e982 | 2023-11-24 19:54:26 +0000 | [diff] [blame] | 364 | return config.ReleaseNdkAbiMonitored() | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 365 | } | 
|  | 366 |  | 
| Matthias Maennich | 55486f8 | 2023-07-03 12:25:27 +0100 | [diff] [blame] | 367 | func (this *stubDecorator) dumpAbi(ctx ModuleContext, symbolList android.Path) { | 
|  | 368 | implementationLibrary := this.findImplementationLibrary(ctx) | 
| Dan Albert | 499c92e | 2024-10-31 22:15:40 +0000 | [diff] [blame] | 369 | this.abiDumpPath = this.builtAbiDumpLocation(ctx, this.apiLevel) | 
| Dan Albert | 2d0dce1 | 2024-09-18 19:34:32 +0000 | [diff] [blame] | 370 | this.hasAbiDump = true | 
| Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 371 | headersList := getNdkABIHeadersFile(ctx) | 
| Matthias Maennich | 55486f8 | 2023-07-03 12:25:27 +0100 | [diff] [blame] | 372 | ctx.Build(pctx, android.BuildParams{ | 
|  | 373 | Rule:        stg, | 
|  | 374 | Description: fmt.Sprintf("stg %s", implementationLibrary), | 
|  | 375 | Input:       implementationLibrary, | 
| Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 376 | Implicits: []android.Path{ | 
|  | 377 | symbolList, | 
|  | 378 | headersList, | 
|  | 379 | }, | 
|  | 380 | Output: this.abiDumpPath, | 
| Matthias Maennich | 55486f8 | 2023-07-03 12:25:27 +0100 | [diff] [blame] | 381 | Args: map[string]string{ | 
| Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 382 | "symbolList":  symbolList.String(), | 
|  | 383 | "headersList": headersList.String(), | 
| Matthias Maennich | 55486f8 | 2023-07-03 12:25:27 +0100 | [diff] [blame] | 384 | }, | 
|  | 385 | }) | 
|  | 386 | } | 
|  | 387 |  | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 388 | func findNextApiLevel(ctx ModuleContext, apiLevel android.ApiLevel) *android.ApiLevel { | 
| Dan Albert | 499c92e | 2024-10-31 22:15:40 +0000 | [diff] [blame] | 389 | apiLevels := append(ctx.Config().FinalApiLevels(), | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 390 | android.FutureApiLevel) | 
|  | 391 | for _, api := range apiLevels { | 
|  | 392 | if api.GreaterThan(apiLevel) { | 
|  | 393 | return &api | 
|  | 394 | } | 
|  | 395 | } | 
|  | 396 | return nil | 
|  | 397 | } | 
|  | 398 |  | 
|  | 399 | func (this *stubDecorator) diffAbi(ctx ModuleContext) { | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 400 | // Catch any ABI changes compared to the checked-in definition of this API | 
|  | 401 | // level. | 
| Matthias Maennich | 658bb4d | 2023-06-30 23:28:15 +0100 | [diff] [blame] | 402 | abiDiffPath := android.PathForModuleOut(ctx, "stgdiff.timestamp") | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 403 | prebuiltAbiDump := this.findPrebuiltAbiDump(ctx, this.apiLevel) | 
| Aleksei Vetrov | 9c8c5ab | 2023-09-20 13:09:25 +0000 | [diff] [blame] | 404 | missingPrebuiltErrorTemplate := | 
|  | 405 | "Did not find prebuilt ABI dump for %q (%q). Generate with " + | 
|  | 406 | "//development/tools/ndk/update_ndk_abi.sh." | 
| Dan Albert | f7cb563 | 2022-11-29 17:20:16 +0000 | [diff] [blame] | 407 | missingPrebuiltError := fmt.Sprintf( | 
| Aleksei Vetrov | 9c8c5ab | 2023-09-20 13:09:25 +0000 | [diff] [blame] | 408 | missingPrebuiltErrorTemplate, this.libraryName(ctx), | 
| Dan Albert | f7cb563 | 2022-11-29 17:20:16 +0000 | [diff] [blame] | 409 | prebuiltAbiDump.InvalidReason()) | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 410 | if !prebuiltAbiDump.Valid() { | 
|  | 411 | ctx.Build(pctx, android.BuildParams{ | 
|  | 412 | Rule:   android.ErrorRule, | 
|  | 413 | Output: abiDiffPath, | 
|  | 414 | Args: map[string]string{ | 
|  | 415 | "error": missingPrebuiltError, | 
|  | 416 | }, | 
|  | 417 | }) | 
|  | 418 | } else { | 
|  | 419 | ctx.Build(pctx, android.BuildParams{ | 
| Matthias Maennich | 658bb4d | 2023-06-30 23:28:15 +0100 | [diff] [blame] | 420 | Rule: stgdiff, | 
|  | 421 | Description: fmt.Sprintf("Comparing ABI %s %s", prebuiltAbiDump, | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 422 | this.abiDumpPath), | 
|  | 423 | Output: abiDiffPath, | 
|  | 424 | Inputs: android.Paths{prebuiltAbiDump.Path(), this.abiDumpPath}, | 
| Matthias Maennich | 658bb4d | 2023-06-30 23:28:15 +0100 | [diff] [blame] | 425 | Args: map[string]string{ | 
|  | 426 | "args": "--format=small", | 
|  | 427 | }, | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 428 | }) | 
|  | 429 | } | 
|  | 430 | this.abiDiffPaths = append(this.abiDiffPaths, abiDiffPath) | 
|  | 431 |  | 
|  | 432 | // Also ensure that the ABI of the next API level (if there is one) matches | 
|  | 433 | // this API level. *New* ABI is allowed, but any changes to APIs that exist | 
|  | 434 | // in this API level are disallowed. | 
| Dan Albert | 9c8e0c4 | 2024-09-18 20:45:44 +0000 | [diff] [blame] | 435 | if prebuiltAbiDump.Valid() { | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 436 | nextApiLevel := findNextApiLevel(ctx, this.apiLevel) | 
|  | 437 | if nextApiLevel == nil { | 
|  | 438 | panic(fmt.Errorf("could not determine which API level follows "+ | 
|  | 439 | "non-current API level %s", this.apiLevel)) | 
|  | 440 | } | 
| Dan Albert | 9c8e0c4 | 2024-09-18 20:45:44 +0000 | [diff] [blame] | 441 |  | 
| Dan Albert | 5352776 | 2024-11-01 19:15:27 +0000 | [diff] [blame] | 442 | // Preview ABI levels are not recorded in prebuilts. ABI compatibility | 
| Dan Albert | 499c92e | 2024-10-31 22:15:40 +0000 | [diff] [blame] | 443 | // for preview APIs is still monitored via "current" so we have early | 
|  | 444 | // warning rather than learning about an ABI break during finalization, | 
|  | 445 | // but is only checked against the "current" API dumps in the out | 
|  | 446 | // directory. | 
|  | 447 | nextAbiDiffPath := android.PathForModuleOut(ctx, | 
|  | 448 | "abidiff_next.timestamp") | 
|  | 449 |  | 
|  | 450 | var nextAbiDump android.OptionalPath | 
|  | 451 | if nextApiLevel.IsCurrent() { | 
|  | 452 | nextAbiDump = android.OptionalPathForPath( | 
|  | 453 | this.builtAbiDumpLocation(ctx, *nextApiLevel), | 
|  | 454 | ) | 
|  | 455 | } else { | 
|  | 456 | nextAbiDump = this.findPrebuiltAbiDump(ctx, *nextApiLevel) | 
|  | 457 | } | 
|  | 458 |  | 
|  | 459 | if !nextAbiDump.Valid() { | 
| Dan Albert | 9c8e0c4 | 2024-09-18 20:45:44 +0000 | [diff] [blame] | 460 | missingNextPrebuiltError := fmt.Sprintf( | 
|  | 461 | missingPrebuiltErrorTemplate, this.libraryName(ctx), | 
|  | 462 | nextAbiDump.InvalidReason()) | 
| Dan Albert | 499c92e | 2024-10-31 22:15:40 +0000 | [diff] [blame] | 463 | ctx.Build(pctx, android.BuildParams{ | 
|  | 464 | Rule:   android.ErrorRule, | 
|  | 465 | Output: nextAbiDiffPath, | 
|  | 466 | Args: map[string]string{ | 
|  | 467 | "error": missingNextPrebuiltError, | 
|  | 468 | }, | 
|  | 469 | }) | 
|  | 470 | } else { | 
|  | 471 | ctx.Build(pctx, android.BuildParams{ | 
|  | 472 | Rule: stgdiff, | 
|  | 473 | Description: fmt.Sprintf( | 
|  | 474 | "Comparing ABI to the next API level %s %s", | 
|  | 475 | prebuiltAbiDump, nextAbiDump), | 
|  | 476 | Output: nextAbiDiffPath, | 
|  | 477 | Inputs: android.Paths{ | 
|  | 478 | prebuiltAbiDump.Path(), nextAbiDump.Path()}, | 
|  | 479 | Args: map[string]string{ | 
|  | 480 | "args": "--format=small --ignore=interface_addition", | 
|  | 481 | }, | 
|  | 482 | }) | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 483 | } | 
| Dan Albert | 499c92e | 2024-10-31 22:15:40 +0000 | [diff] [blame] | 484 | this.abiDiffPaths = append(this.abiDiffPaths, nextAbiDiffPath) | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 485 | } | 
|  | 486 | } | 
|  | 487 |  | 
| Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 488 | func (c *stubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects { | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 489 | if !strings.HasSuffix(String(c.properties.Symbol_file), ".map.txt") { | 
| Dan Albert | 15be0c6 | 2017-06-13 15:14:56 -0700 | [diff] [blame] | 490 | ctx.PropertyErrorf("symbol_file", "must end with .map.txt") | 
|  | 491 | } | 
|  | 492 |  | 
| Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 493 | if !c.buildStubs() { | 
|  | 494 | // NDK libraries have no implementation variant, nothing to do | 
|  | 495 | return Objects{} | 
|  | 496 | } | 
|  | 497 |  | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 498 | if !c.initializeProperties(ctx) { | 
|  | 499 | // Emits its own errors, so we don't need to. | 
|  | 500 | return Objects{} | 
|  | 501 | } | 
|  | 502 |  | 
| sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 503 | symbolFile := String(c.properties.Symbol_file) | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 504 | nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile, c.apiLevel, "") | 
|  | 505 | objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc) | 
|  | 506 | c.versionScriptPath = nativeAbiResult.versionScript | 
| Dan Albert | 2d0dce1 | 2024-09-18 19:34:32 +0000 | [diff] [blame] | 507 | if c.canDumpAbi(ctx) { | 
| Matthias Maennich | e914f2d | 2023-07-03 12:29:27 +0100 | [diff] [blame] | 508 | c.dumpAbi(ctx, nativeAbiResult.symbolList) | 
| Dan Albert | 499c92e | 2024-10-31 22:15:40 +0000 | [diff] [blame] | 509 | if c.canDiffAbi(ctx.Config()) { | 
| Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 510 | c.diffAbi(ctx) | 
|  | 511 | } | 
|  | 512 | } | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 513 | if c.apiLevel.IsCurrent() && ctx.PrimaryArch() { | 
| sophiez | 4c4f803 | 2021-08-16 22:54:00 -0700 | [diff] [blame] | 514 | c.parsedCoverageXmlPath = parseSymbolFileForAPICoverage(ctx, symbolFile) | 
| sophiez | 58cabb7 | 2020-05-29 13:37:12 -0700 | [diff] [blame] | 515 | } | 
| Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 516 | return objs | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 517 | } | 
|  | 518 |  | 
| Spandan Das | 73bcafc | 2022-08-18 23:26:00 +0000 | [diff] [blame] | 519 | // Add a dependency on the header modules of this ndk_library | 
| Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 520 | func (linker *stubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { | 
| Spandan Das | 73bcafc | 2022-08-18 23:26:00 +0000 | [diff] [blame] | 521 | return Deps{ | 
| Steven Moreland | bedc997 | 2024-09-03 21:46:14 +0000 | [diff] [blame] | 522 | ReexportHeaderLibHeaders: linker.properties.Export_header_libs, | 
|  | 523 | HeaderLibs:               linker.properties.Export_header_libs, | 
| Spandan Das | 73bcafc | 2022-08-18 23:26:00 +0000 | [diff] [blame] | 524 | } | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 525 | } | 
|  | 526 |  | 
| Colin Cross | 4a9e6ec | 2023-12-18 15:29:41 -0800 | [diff] [blame] | 527 | func (linker *stubDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) { | 
|  | 528 | linker.libraryDecorator.moduleInfoJSON(ctx, moduleInfoJSON) | 
|  | 529 | // Overwrites the SubName computed by libraryDecorator | 
|  | 530 | moduleInfoJSON.SubName = ndkLibrarySuffix + "." + linker.apiLevel.String() | 
|  | 531 | } | 
|  | 532 |  | 
| Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 533 | func (linker *stubDecorator) Name(name string) string { | 
|  | 534 | return name + ndkLibrarySuffix | 
|  | 535 | } | 
|  | 536 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 537 | func (stub *stubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { | 
| Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 538 | stub.libraryDecorator.libName = ctx.baseModuleName() | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 539 | return stub.libraryDecorator.linkerFlags(ctx, flags) | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 540 | } | 
|  | 541 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 542 | func (stub *stubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, | 
| Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 543 | objs Objects) android.Path { | 
| Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 544 |  | 
| Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 545 | if !stub.buildStubs() { | 
|  | 546 | // NDK libraries have no implementation variant, nothing to do | 
|  | 547 | return nil | 
|  | 548 | } | 
|  | 549 |  | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 550 | if shouldUseVersionScript(ctx, stub) { | 
| Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 551 | linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String() | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 552 | flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag) | 
| Dan Willemsen | 939408a | 2019-06-10 18:02:25 -0700 | [diff] [blame] | 553 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath) | 
| Dan Albert | 98dbb3b | 2017-01-03 15:16:29 -0800 | [diff] [blame] | 554 | } | 
|  | 555 |  | 
| Colin Cross | 5ec407b | 2020-09-30 11:41:33 -0700 | [diff] [blame] | 556 | stub.libraryDecorator.skipAPIDefine = true | 
| Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 557 | return stub.libraryDecorator.link(ctx, flags, deps, objs) | 
| Dan Albert | 2bc91ba | 2016-07-28 17:40:28 -0700 | [diff] [blame] | 558 | } | 
|  | 559 |  | 
| Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 560 | func (stub *stubDecorator) nativeCoverage() bool { | 
|  | 561 | return false | 
|  | 562 | } | 
|  | 563 |  | 
| Dan Albert | 4048bb0 | 2023-04-03 20:19:07 +0000 | [diff] [blame] | 564 | // Returns the install path for unversioned NDK libraries (currently only static | 
|  | 565 | // libraries). | 
| Spandan Das | f280b23 | 2024-04-04 21:25:51 +0000 | [diff] [blame] | 566 | func getUnversionedLibraryInstallPath(ctx ModuleContext) android.OutputPath { | 
| Dan Albert | 4048bb0 | 2023-04-03 20:19:07 +0000 | [diff] [blame] | 567 | return getNdkSysrootBase(ctx).Join(ctx, "usr/lib", config.NDKTriple(ctx.toolchain())) | 
|  | 568 | } | 
| Rebecca Chyung | 961cf1c | 2023-04-03 05:17:17 +0000 | [diff] [blame] | 569 |  | 
| Dan Albert | 4048bb0 | 2023-04-03 20:19:07 +0000 | [diff] [blame] | 570 | // Returns the install path for versioned NDK libraries. These are most often | 
|  | 571 | // stubs, but the same paths are used for CRT objects. | 
| Spandan Das | f280b23 | 2024-04-04 21:25:51 +0000 | [diff] [blame] | 572 | func getVersionedLibraryInstallPath(ctx ModuleContext, apiLevel android.ApiLevel) android.OutputPath { | 
| Dan Albert | 4048bb0 | 2023-04-03 20:19:07 +0000 | [diff] [blame] | 573 | return getUnversionedLibraryInstallPath(ctx).Join(ctx, apiLevel.String()) | 
|  | 574 | } | 
|  | 575 |  | 
|  | 576 | func (stub *stubDecorator) install(ctx ModuleContext, path android.Path) { | 
|  | 577 | installDir := getVersionedLibraryInstallPath(ctx, stub.apiLevel) | 
| Spandan Das | f280b23 | 2024-04-04 21:25:51 +0000 | [diff] [blame] | 578 | out := installDir.Join(ctx, path.Base()) | 
|  | 579 | ctx.Build(pctx, android.BuildParams{ | 
|  | 580 | Rule:   android.Cp, | 
|  | 581 | Input:  path, | 
|  | 582 | Output: out, | 
|  | 583 | }) | 
|  | 584 | stub.installPath = out | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 585 | } | 
|  | 586 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 587 | func newStubLibrary() *Module { | 
| Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 588 | module, library := NewLibrary(android.DeviceSupported) | 
|  | 589 | library.BuildOnlyShared() | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 590 | module.stl = nil | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 591 | module.sanitize = nil | 
| ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 592 | library.disableStripping() | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 593 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 594 | stub := &stubDecorator{ | 
|  | 595 | libraryDecorator: library, | 
|  | 596 | } | 
|  | 597 | module.compiler = stub | 
|  | 598 | module.linker = stub | 
|  | 599 | module.installer = stub | 
| Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 600 | module.library = stub | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 601 |  | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 602 | module.Properties.AlwaysSdk = true | 
|  | 603 | module.Properties.Sdk_version = StringPtr("current") | 
|  | 604 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 605 | module.AddProperties(&stub.properties, &library.MutatedProperties) | 
|  | 606 |  | 
|  | 607 | return module | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 608 | } | 
|  | 609 |  | 
| Dan Albert | f740ed0 | 2020-07-24 14:19:06 -0700 | [diff] [blame] | 610 | // ndk_library creates a library that exposes a stub implementation of functions | 
|  | 611 | // and variables for use at build time only. | 
| Jooyung Han | b90e491 | 2019-12-09 18:21:48 +0900 | [diff] [blame] | 612 | func NdkLibraryFactory() android.Module { | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 613 | module := newStubLibrary() | 
|  | 614 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth) | 
|  | 615 | return module | 
| Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 616 | } |