Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 1 | // Copyright (C) 2021 The Android Open Source Project |
| 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 java |
| 16 | |
| 17 | import ( |
Paul Duffin | 7487a7a | 2021-05-19 09:36:09 +0100 | [diff] [blame] | 18 | "fmt" |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 19 | "strings" |
| 20 | |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 21 | "android/soong/android" |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Paul Duffin | 6237092 | 2021-05-23 16:55:37 +0100 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // Contains support for processing hiddenAPI in a modular fashion. |
| 27 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 28 | // HiddenAPIScope encapsulates all the information that the hidden API processing needs about API |
| 29 | // scopes, i.e. what is called android.SdkKind and apiScope. It does not just use those as they do |
| 30 | // not provide the information needed by hidden API processing. |
| 31 | type HiddenAPIScope struct { |
| 32 | // The name of the scope, used for debug purposes. |
| 33 | name string |
| 34 | |
| 35 | // The corresponding android.SdkKind, used for retrieving paths from java_sdk_library* modules. |
| 36 | sdkKind android.SdkKind |
| 37 | |
| 38 | // The option needed to passed to "hiddenapi list". |
| 39 | hiddenAPIListOption string |
| 40 | } |
| 41 | |
| 42 | // initHiddenAPIScope initializes the scope. |
| 43 | func initHiddenAPIScope(apiScope *HiddenAPIScope) *HiddenAPIScope { |
| 44 | return apiScope |
| 45 | } |
| 46 | |
| 47 | func (l *HiddenAPIScope) String() string { |
| 48 | return fmt.Sprintf("HiddenAPIScope{%s}", l.name) |
| 49 | } |
| 50 | |
| 51 | var ( |
| 52 | PublicHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{ |
| 53 | name: "public", |
| 54 | sdkKind: android.SdkPublic, |
| 55 | hiddenAPIListOption: "--public-stub-classpath", |
| 56 | }) |
| 57 | SystemHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{ |
| 58 | name: "system", |
| 59 | sdkKind: android.SdkSystem, |
| 60 | hiddenAPIListOption: "--system-stub-classpath", |
| 61 | }) |
| 62 | TestHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{ |
| 63 | name: "test", |
| 64 | sdkKind: android.SdkTest, |
| 65 | hiddenAPIListOption: "--test-stub-classpath", |
| 66 | }) |
Paul Duffin | b51db2e | 2021-06-21 14:08:08 +0100 | [diff] [blame] | 67 | ModuleLibHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{ |
| 68 | name: "module-lib", |
| 69 | sdkKind: android.SdkModule, |
| 70 | }) |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 71 | CorePlatformHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{ |
| 72 | name: "core-platform", |
| 73 | sdkKind: android.SdkCorePlatform, |
| 74 | hiddenAPIListOption: "--core-platform-stub-classpath", |
| 75 | }) |
| 76 | |
| 77 | // hiddenAPIRelevantSdkKinds lists all the android.SdkKind instances that are needed by the hidden |
| 78 | // API processing. |
| 79 | // |
| 80 | // These are roughly in order from narrowest API surface to widest. Widest means the API stubs |
| 81 | // with the biggest API surface, e.g. test is wider than system is wider than public. |
| 82 | // |
Paul Duffin | b51db2e | 2021-06-21 14:08:08 +0100 | [diff] [blame] | 83 | // Core platform is considered wider than system/module-lib because those modules that provide |
| 84 | // core platform APIs either do not have any system/module-lib APIs at all, or if they do it is |
| 85 | // because the core platform API is being converted to system/module-lib APIs. In either case the |
| 86 | // system/module-lib APIs are subsets of the core platform API. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 87 | // |
| 88 | // This is not strictly in order from narrowest to widest as the Test API is wider than system but |
Paul Duffin | b51db2e | 2021-06-21 14:08:08 +0100 | [diff] [blame] | 89 | // is neither wider or narrower than the module-lib or core platform APIs. However, this works |
| 90 | // well enough at the moment. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 91 | // TODO(b/191644675): Correctly reflect the sub/superset relationships between APIs. |
| 92 | hiddenAPIScopes = []*HiddenAPIScope{ |
| 93 | PublicHiddenAPIScope, |
| 94 | SystemHiddenAPIScope, |
| 95 | TestHiddenAPIScope, |
Paul Duffin | b51db2e | 2021-06-21 14:08:08 +0100 | [diff] [blame] | 96 | ModuleLibHiddenAPIScope, |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 97 | CorePlatformHiddenAPIScope, |
| 98 | } |
| 99 | |
| 100 | // The HiddenAPIScope instances that are supported by a java_sdk_library. |
| 101 | // |
| 102 | // CorePlatformHiddenAPIScope is not used as the java_sdk_library does not have special support |
| 103 | // for core_platform API, instead it is implemented as a customized form of PublicHiddenAPIScope. |
| 104 | hiddenAPISdkLibrarySupportedScopes = []*HiddenAPIScope{ |
| 105 | PublicHiddenAPIScope, |
| 106 | SystemHiddenAPIScope, |
| 107 | TestHiddenAPIScope, |
Paul Duffin | b51db2e | 2021-06-21 14:08:08 +0100 | [diff] [blame] | 108 | ModuleLibHiddenAPIScope, |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // The HiddenAPIScope instances that are supported by the `hiddenapi list`. |
| 112 | hiddenAPIFlagScopes = []*HiddenAPIScope{ |
| 113 | PublicHiddenAPIScope, |
| 114 | SystemHiddenAPIScope, |
| 115 | TestHiddenAPIScope, |
| 116 | CorePlatformHiddenAPIScope, |
| 117 | } |
| 118 | ) |
| 119 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 120 | type hiddenAPIStubsDependencyTag struct { |
| 121 | blueprint.BaseDependencyTag |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 122 | |
| 123 | // The api scope for which this dependency was added. |
| 124 | apiScope *HiddenAPIScope |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | func (b hiddenAPIStubsDependencyTag) ExcludeFromApexContents() { |
| 128 | } |
| 129 | |
| 130 | func (b hiddenAPIStubsDependencyTag) ReplaceSourceWithPrebuilt() bool { |
| 131 | return false |
| 132 | } |
| 133 | |
Paul Duffin | 976b0e5 | 2021-04-27 23:20:26 +0100 | [diff] [blame] | 134 | func (b hiddenAPIStubsDependencyTag) SdkMemberType(child android.Module) android.SdkMemberType { |
| 135 | // If the module is a java_sdk_library then treat it as if it was specific in the java_sdk_libs |
| 136 | // property, otherwise treat if it was specified in the java_header_libs property. |
| 137 | if javaSdkLibrarySdkMemberType.IsInstance(child) { |
| 138 | return javaSdkLibrarySdkMemberType |
| 139 | } |
| 140 | |
| 141 | return javaHeaderLibsSdkMemberType |
| 142 | } |
| 143 | |
| 144 | func (b hiddenAPIStubsDependencyTag) ExportMember() bool { |
| 145 | // Export the module added via this dependency tag from the sdk. |
| 146 | return true |
| 147 | } |
| 148 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 149 | // Avoid having to make stubs content explicitly visible to dependent modules. |
| 150 | // |
| 151 | // This is a temporary workaround to make it easier to migrate to bootclasspath_fragment modules |
| 152 | // with proper dependencies. |
| 153 | // TODO(b/177892522): Remove this and add needed visibility. |
| 154 | func (b hiddenAPIStubsDependencyTag) ExcludeFromVisibilityEnforcement() { |
| 155 | } |
| 156 | |
| 157 | var _ android.ExcludeFromVisibilityEnforcementTag = hiddenAPIStubsDependencyTag{} |
| 158 | var _ android.ReplaceSourceWithPrebuilt = hiddenAPIStubsDependencyTag{} |
| 159 | var _ android.ExcludeFromApexContentsTag = hiddenAPIStubsDependencyTag{} |
Paul Duffin | 976b0e5 | 2021-04-27 23:20:26 +0100 | [diff] [blame] | 160 | var _ android.SdkMemberTypeDependencyTag = hiddenAPIStubsDependencyTag{} |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 161 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 162 | // hiddenAPIComputeMonolithicStubLibModules computes the set of module names that provide stubs |
| 163 | // needed to produce the hidden API monolithic stub flags file. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 164 | func hiddenAPIComputeMonolithicStubLibModules(config android.Config) map[*HiddenAPIScope][]string { |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 165 | var publicStubModules []string |
| 166 | var systemStubModules []string |
| 167 | var testStubModules []string |
| 168 | var corePlatformStubModules []string |
| 169 | |
| 170 | if config.AlwaysUsePrebuiltSdks() { |
| 171 | // Build configuration mandates using prebuilt stub modules |
| 172 | publicStubModules = append(publicStubModules, "sdk_public_current_android") |
| 173 | systemStubModules = append(systemStubModules, "sdk_system_current_android") |
| 174 | testStubModules = append(testStubModules, "sdk_test_current_android") |
| 175 | } else { |
| 176 | // Use stub modules built from source |
| 177 | publicStubModules = append(publicStubModules, "android_stubs_current") |
| 178 | systemStubModules = append(systemStubModules, "android_system_stubs_current") |
| 179 | testStubModules = append(testStubModules, "android_test_stubs_current") |
| 180 | } |
| 181 | // We do not have prebuilts of the core platform api yet |
| 182 | corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs") |
| 183 | |
| 184 | // Allow products to define their own stubs for custom product jars that apps can use. |
| 185 | publicStubModules = append(publicStubModules, config.ProductHiddenAPIStubs()...) |
| 186 | systemStubModules = append(systemStubModules, config.ProductHiddenAPIStubsSystem()...) |
| 187 | testStubModules = append(testStubModules, config.ProductHiddenAPIStubsTest()...) |
| 188 | if config.IsEnvTrue("EMMA_INSTRUMENT") { |
Paul Duffin | 098c878 | 2021-05-14 10:45:25 +0100 | [diff] [blame] | 189 | // Add jacoco-stubs to public, system and test. It doesn't make any real difference as public |
| 190 | // allows everyone access but it is needed to ensure consistent flags between the |
| 191 | // bootclasspath fragment generated flags and the platform_bootclasspath generated flags. |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 192 | publicStubModules = append(publicStubModules, "jacoco-stubs") |
Paul Duffin | 098c878 | 2021-05-14 10:45:25 +0100 | [diff] [blame] | 193 | systemStubModules = append(systemStubModules, "jacoco-stubs") |
| 194 | testStubModules = append(testStubModules, "jacoco-stubs") |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 197 | m := map[*HiddenAPIScope][]string{} |
| 198 | m[PublicHiddenAPIScope] = publicStubModules |
| 199 | m[SystemHiddenAPIScope] = systemStubModules |
| 200 | m[TestHiddenAPIScope] = testStubModules |
| 201 | m[CorePlatformHiddenAPIScope] = corePlatformStubModules |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 202 | return m |
| 203 | } |
| 204 | |
| 205 | // hiddenAPIAddStubLibDependencies adds dependencies onto the modules specified in |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 206 | // apiScopeToStubLibModules. It adds them in a well known order and uses a HiddenAPIScope specific |
| 207 | // tag to identify the source of the dependency. |
| 208 | func hiddenAPIAddStubLibDependencies(ctx android.BottomUpMutatorContext, apiScopeToStubLibModules map[*HiddenAPIScope][]string) { |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 209 | module := ctx.Module() |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 210 | for _, apiScope := range hiddenAPIScopes { |
| 211 | modules := apiScopeToStubLibModules[apiScope] |
| 212 | ctx.AddDependency(module, hiddenAPIStubsDependencyTag{apiScope: apiScope}, modules...) |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 216 | // hiddenAPIRetrieveDexJarBuildPath retrieves the DexJarBuildPath from the specified module, if |
| 217 | // available, or reports an error. |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 218 | func hiddenAPIRetrieveDexJarBuildPath(ctx android.ModuleContext, module android.Module, kind android.SdkKind) android.Path { |
| 219 | var dexJar android.Path |
| 220 | if sdkLibrary, ok := module.(SdkLibraryDependency); ok { |
| 221 | dexJar = sdkLibrary.SdkApiStubDexJar(ctx, kind) |
| 222 | } else if j, ok := module.(UsesLibraryDependency); ok { |
| 223 | dexJar = j.DexJarBuildPath() |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 224 | } else { |
| 225 | ctx.ModuleErrorf("dependency %s of module type %s does not support providing a dex jar", module, ctx.OtherModuleType(module)) |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 226 | return nil |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 227 | } |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 228 | |
| 229 | if dexJar == nil { |
| 230 | ctx.ModuleErrorf("dependency %s does not provide a dex jar, consider setting compile_dex: true", module) |
| 231 | } |
| 232 | return dexJar |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Paul Duffin | 4539a37 | 2021-06-23 23:20:43 +0100 | [diff] [blame^] | 235 | // buildRuleToGenerateHiddenAPIStubFlagsFile creates a rule to create a hidden API stub flags file. |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 236 | // |
| 237 | // The rule is initialized but not built so that the caller can modify it and select an appropriate |
| 238 | // name. |
Paul Duffin | 4539a37 | 2021-06-23 23:20:43 +0100 | [diff] [blame^] | 239 | func buildRuleToGenerateHiddenAPIStubFlagsFile(ctx android.BuilderContext, name, desc string, outputPath android.WritablePath, bootDexJars android.Paths, input HiddenAPIFlagInput) { |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 240 | // Singleton rule which applies hiddenapi on all boot class path dex files. |
| 241 | rule := android.NewRuleBuilder(pctx, ctx) |
| 242 | |
| 243 | tempPath := tempPathForRestat(ctx, outputPath) |
| 244 | |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 245 | // Find the widest API stubs provided by the fragments on which this depends, if any. |
| 246 | var dependencyStubDexJars android.Paths |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 247 | for i := len(hiddenAPIScopes) - 1; i >= 0; i-- { |
| 248 | apiScope := hiddenAPIScopes[i] |
| 249 | stubsForAPIScope := input.DependencyStubDexJarsByScope[apiScope] |
| 250 | if len(stubsForAPIScope) != 0 { |
| 251 | dependencyStubDexJars = stubsForAPIScope |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 252 | break |
| 253 | } |
| 254 | } |
| 255 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 256 | command := rule.Command(). |
| 257 | Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")). |
| 258 | Text("list"). |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 259 | FlagForEachInput("--dependency-stub-dex=", dependencyStubDexJars). |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 260 | FlagForEachInput("--boot-dex=", bootDexJars) |
| 261 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 262 | // Iterate over the api scopes in a fixed order. |
| 263 | for _, apiScope := range hiddenAPIFlagScopes { |
| 264 | // Merge in the stub dex jar paths for this api scope from the fragments on which it depends. |
| 265 | // They will be needed to resolve dependencies from this fragment's stubs to classes in the |
| 266 | // other fragment's APIs. |
| 267 | var paths android.Paths |
| 268 | paths = append(paths, input.DependencyStubDexJarsByScope[apiScope]...) |
| 269 | paths = append(paths, input.StubDexJarsByScope[apiScope]...) |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 270 | if len(paths) > 0 { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 271 | option := apiScope.hiddenAPIListOption |
| 272 | command.FlagWithInputList(option+"=", paths, ":") |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | // Add the output path. |
| 277 | command.FlagWithOutput("--out-api-flags=", tempPath) |
| 278 | |
| 279 | commitChangeForRestat(rule, tempPath, outputPath) |
Paul Duffin | 4539a37 | 2021-06-23 23:20:43 +0100 | [diff] [blame^] | 280 | |
| 281 | rule.Build(name, desc) |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 284 | // HiddenAPIFlagFileProperties contains paths to the flag files that can be used to augment the |
| 285 | // information obtained from annotations within the source code in order to create the complete set |
| 286 | // of flags that should be applied to the dex implementation jars on the bootclasspath. |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 287 | // |
| 288 | // Each property contains a list of paths. With the exception of the Unsupported_packages the paths |
| 289 | // of each property reference a plain text file that contains a java signature per line. The flags |
| 290 | // for each of those signatures will be updated in a property specific way. |
| 291 | // |
| 292 | // The Unsupported_packages property contains a list of paths, each of which is a plain text file |
| 293 | // with one Java package per line. All members of all classes within that package (but not nested |
| 294 | // packages) will be updated in a property specific way. |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 295 | type HiddenAPIFlagFileProperties struct { |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 296 | // Marks each signature in the referenced files as being unsupported. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 297 | Unsupported []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 298 | |
| 299 | // Marks each signature in the referenced files as being unsupported because it has been removed. |
| 300 | // Any conflicts with other flags are ignored. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 301 | Removed []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 302 | |
| 303 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= R |
| 304 | // and low priority. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 305 | Max_target_r_low_priority []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 306 | |
| 307 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= Q. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 308 | Max_target_q []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 309 | |
| 310 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= P. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 311 | Max_target_p []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 312 | |
| 313 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= O |
| 314 | // and low priority. Any conflicts with other flags are ignored. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 315 | Max_target_o_low_priority []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 316 | |
| 317 | // Marks each signature in the referenced files as being blocked. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 318 | Blocked []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 319 | |
| 320 | // Marks each signature in every package in the referenced files as being unsupported. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 321 | Unsupported_packages []string `android:"path"` |
| 322 | } |
| 323 | |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 324 | type hiddenAPIFlagFileCategory struct { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 325 | // PropertyName is the name of the property for this category. |
| 326 | PropertyName string |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 327 | |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 328 | // propertyValueReader retrieves the value of the property for this category from the set of |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 329 | // properties. |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 330 | propertyValueReader func(properties *HiddenAPIFlagFileProperties) []string |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 331 | |
| 332 | // commandMutator adds the appropriate command line options for this category to the supplied |
| 333 | // command |
| 334 | commandMutator func(command *android.RuleBuilderCommand, path android.Path) |
| 335 | } |
| 336 | |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 337 | // The flag file category for removed members of the API. |
| 338 | // |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 339 | // This is extracted from HiddenAPIFlagFileCategories as it is needed to add the dex signatures |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 340 | // list of removed API members that are generated automatically from the removed.txt files provided |
| 341 | // by API stubs. |
| 342 | var hiddenAPIRemovedFlagFileCategory = &hiddenAPIFlagFileCategory{ |
| 343 | // See HiddenAPIFlagFileProperties.Removed |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 344 | PropertyName: "removed", |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 345 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
| 346 | return properties.Removed |
| 347 | }, |
| 348 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 349 | command.FlagWithInput("--unsupported ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed") |
| 350 | }, |
| 351 | } |
| 352 | |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 353 | var HiddenAPIFlagFileCategories = []*hiddenAPIFlagFileCategory{ |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 354 | // See HiddenAPIFlagFileProperties.Unsupported |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 355 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 356 | PropertyName: "unsupported", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 357 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 358 | return properties.Unsupported |
| 359 | }, |
| 360 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 361 | command.FlagWithInput("--unsupported ", path) |
| 362 | }, |
| 363 | }, |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 364 | hiddenAPIRemovedFlagFileCategory, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 365 | // See HiddenAPIFlagFileProperties.Max_target_r_low_priority |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 366 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 367 | PropertyName: "max_target_r_low_priority", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 368 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 369 | return properties.Max_target_r_low_priority |
| 370 | }, |
| 371 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 372 | command.FlagWithInput("--max-target-r ", path).FlagWithArg("--tag ", "lo-prio") |
| 373 | }, |
| 374 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 375 | // See HiddenAPIFlagFileProperties.Max_target_q |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 376 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 377 | PropertyName: "max_target_q", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 378 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 379 | return properties.Max_target_q |
| 380 | }, |
| 381 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 382 | command.FlagWithInput("--max-target-q ", path) |
| 383 | }, |
| 384 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 385 | // See HiddenAPIFlagFileProperties.Max_target_p |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 386 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 387 | PropertyName: "max_target_p", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 388 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 389 | return properties.Max_target_p |
| 390 | }, |
| 391 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 392 | command.FlagWithInput("--max-target-p ", path) |
| 393 | }, |
| 394 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 395 | // See HiddenAPIFlagFileProperties.Max_target_o_low_priority |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 396 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 397 | PropertyName: "max_target_o_low_priority", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 398 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 399 | return properties.Max_target_o_low_priority |
| 400 | }, |
| 401 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 402 | command.FlagWithInput("--max-target-o ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio") |
| 403 | }, |
| 404 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 405 | // See HiddenAPIFlagFileProperties.Blocked |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 406 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 407 | PropertyName: "blocked", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 408 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 409 | return properties.Blocked |
| 410 | }, |
| 411 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 412 | command.FlagWithInput("--blocked ", path) |
| 413 | }, |
| 414 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 415 | // See HiddenAPIFlagFileProperties.Unsupported_packages |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 416 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 417 | PropertyName: "unsupported_packages", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 418 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 419 | return properties.Unsupported_packages |
| 420 | }, |
| 421 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 422 | command.FlagWithInput("--unsupported ", path).Flag("--packages ") |
| 423 | }, |
| 424 | }, |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 427 | // FlagFilesByCategory maps a hiddenAPIFlagFileCategory to the paths to the files in that category. |
| 428 | type FlagFilesByCategory map[*hiddenAPIFlagFileCategory]android.Paths |
| 429 | |
| 430 | // append appends the supplied flags files to the corresponding category in this map. |
| 431 | func (s FlagFilesByCategory) append(other FlagFilesByCategory) { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 432 | for _, category := range HiddenAPIFlagFileCategories { |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 433 | s[category] = append(s[category], other[category]...) |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | // dedup removes duplicates in the flag files, while maintaining the order in which they were |
| 438 | // appended. |
| 439 | func (s FlagFilesByCategory) dedup() { |
| 440 | for category, paths := range s { |
| 441 | s[category] = android.FirstUniquePaths(paths) |
| 442 | } |
| 443 | } |
| 444 | |
Paul Duffin | af99afa | 2021-05-21 22:18:56 +0100 | [diff] [blame] | 445 | // HiddenAPIInfo contains information provided by the hidden API processing. |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 446 | // |
Paul Duffin | af99afa | 2021-05-21 22:18:56 +0100 | [diff] [blame] | 447 | // That includes paths resolved from HiddenAPIFlagFileProperties and also generated by hidden API |
| 448 | // processing. |
| 449 | type HiddenAPIInfo struct { |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 450 | // FlagFilesByCategory maps from the flag file category to the paths containing information for |
| 451 | // that category. |
| 452 | FlagFilesByCategory FlagFilesByCategory |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 453 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 454 | // The paths to the stub dex jars for each of the *HiddenAPIScope in hiddenAPIScopes. |
| 455 | TransitiveStubDexJarsByScope StubDexJarsByScope |
Paul Duffin | 18cf197 | 2021-05-21 22:46:59 +0100 | [diff] [blame] | 456 | |
Paul Duffin | 1e6f5c4 | 2021-05-21 16:15:31 +0100 | [diff] [blame] | 457 | // The output from the hidden API processing needs to be made available to other modules. |
| 458 | HiddenAPIFlagOutput |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 459 | } |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 460 | |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 461 | func newHiddenAPIInfo() *HiddenAPIInfo { |
| 462 | info := HiddenAPIInfo{ |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 463 | FlagFilesByCategory: FlagFilesByCategory{}, |
| 464 | TransitiveStubDexJarsByScope: StubDexJarsByScope{}, |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 465 | } |
| 466 | return &info |
| 467 | } |
| 468 | |
| 469 | func (i *HiddenAPIInfo) mergeFromFragmentDeps(ctx android.ModuleContext, fragments []android.Module) { |
| 470 | // Merge all the information from the fragments. The fragments form a DAG so it is possible that |
| 471 | // this will introduce duplicates so they will be resolved after processing all the fragments. |
| 472 | for _, fragment := range fragments { |
| 473 | if ctx.OtherModuleHasProvider(fragment, HiddenAPIInfoProvider) { |
| 474 | info := ctx.OtherModuleProvider(fragment, HiddenAPIInfoProvider).(HiddenAPIInfo) |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 475 | i.TransitiveStubDexJarsByScope.append(info.TransitiveStubDexJarsByScope) |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
| 479 | // Dedup and sort paths. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 480 | i.TransitiveStubDexJarsByScope.dedupAndSort() |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 481 | } |
| 482 | |
Paul Duffin | af99afa | 2021-05-21 22:18:56 +0100 | [diff] [blame] | 483 | var HiddenAPIInfoProvider = blueprint.NewProvider(HiddenAPIInfo{}) |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 484 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 485 | // StubDexJarsByScope maps a *HiddenAPIScope to the paths to stub dex jars appropriate for that |
| 486 | // scope. See hiddenAPIScopes for a list of the acceptable *HiddenAPIScope values. |
| 487 | type StubDexJarsByScope map[*HiddenAPIScope]android.Paths |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 488 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 489 | // append appends the API scope specific stub dex jar args to the corresponding scope in this |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 490 | // map. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 491 | func (s StubDexJarsByScope) append(other StubDexJarsByScope) { |
| 492 | for _, scope := range hiddenAPIScopes { |
| 493 | s[scope] = append(s[scope], other[scope]...) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | // dedupAndSort removes duplicates in the stub dex jar paths and sorts them into a consistent and |
| 498 | // deterministic order. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 499 | func (s StubDexJarsByScope) dedupAndSort() { |
| 500 | for apiScope, paths := range s { |
| 501 | s[apiScope] = android.SortedUniquePaths(paths) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | // HiddenAPIFlagInput encapsulates information obtained from a module and its dependencies that are |
| 506 | // needed for hidden API flag generation. |
| 507 | type HiddenAPIFlagInput struct { |
| 508 | // FlagFilesByCategory contains the flag files that override the initial flags that are derived |
| 509 | // from the stub dex files. |
| 510 | FlagFilesByCategory FlagFilesByCategory |
| 511 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 512 | // StubDexJarsByScope contains the stub dex jars for different *HiddenAPIScope and which determine |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 513 | // the initial flags for each dex member. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 514 | StubDexJarsByScope StubDexJarsByScope |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 515 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 516 | // DependencyStubDexJarsByScope contains the stub dex jars provided by the fragments on which this |
| 517 | // depends. It is the result of merging HiddenAPIInfo.TransitiveStubDexJarsByScope from each |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 518 | // fragment on which this depends. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 519 | DependencyStubDexJarsByScope StubDexJarsByScope |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 520 | |
| 521 | // RemovedTxtFiles is the list of removed.txt files provided by java_sdk_library modules that are |
| 522 | // specified in the bootclasspath_fragment's stub_libs and contents properties. |
| 523 | RemovedTxtFiles android.Paths |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | // newHiddenAPIFlagInput creates a new initialize HiddenAPIFlagInput struct. |
| 527 | func newHiddenAPIFlagInput() HiddenAPIFlagInput { |
| 528 | input := HiddenAPIFlagInput{ |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 529 | FlagFilesByCategory: FlagFilesByCategory{}, |
| 530 | StubDexJarsByScope: StubDexJarsByScope{}, |
| 531 | DependencyStubDexJarsByScope: StubDexJarsByScope{}, |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | return input |
| 535 | } |
| 536 | |
Paul Duffin | 6237092 | 2021-05-23 16:55:37 +0100 | [diff] [blame] | 537 | // canPerformHiddenAPIProcessing determines whether hidden API processing should be performed. |
| 538 | // |
| 539 | // A temporary workaround to avoid existing bootclasspath_fragments that do not provide the |
| 540 | // appropriate information needed for hidden API processing breaking the build. |
| 541 | // TODO(b/179354495): Remove this workaround. |
| 542 | func (i *HiddenAPIFlagInput) canPerformHiddenAPIProcessing(ctx android.ModuleContext, properties bootclasspathFragmentProperties) bool { |
| 543 | // Performing hidden API processing without stubs is not supported and it is unlikely to ever be |
| 544 | // required as the whole point of adding something to the bootclasspath fragment is to add it to |
| 545 | // the bootclasspath in order to be used by something else in the system. Without any stubs it |
| 546 | // cannot do that. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 547 | if len(i.StubDexJarsByScope) == 0 { |
Paul Duffin | 6237092 | 2021-05-23 16:55:37 +0100 | [diff] [blame] | 548 | return false |
| 549 | } |
| 550 | |
| 551 | // Hidden API processing is always enabled in tests. |
| 552 | if ctx.Config().TestProductVariables != nil { |
| 553 | return true |
| 554 | } |
| 555 | |
| 556 | // A module that has fragments should have access to the information it needs in order to perform |
| 557 | // hidden API processing. |
| 558 | if len(properties.Fragments) != 0 { |
| 559 | return true |
| 560 | } |
| 561 | |
| 562 | // The art bootclasspath fragment does not depend on any other fragments but already supports |
| 563 | // hidden API processing. |
| 564 | imageName := proptools.String(properties.Image_name) |
| 565 | if imageName == "art" { |
| 566 | return true |
| 567 | } |
| 568 | |
| 569 | // Disable it for everything else. |
| 570 | return false |
| 571 | } |
| 572 | |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 573 | // gatherStubLibInfo gathers information from the stub libs needed by hidden API processing from the |
| 574 | // dependencies added in hiddenAPIAddStubLibDependencies. |
| 575 | // |
| 576 | // That includes paths to the stub dex jars as well as paths to the *removed.txt files. |
| 577 | func (i *HiddenAPIFlagInput) gatherStubLibInfo(ctx android.ModuleContext, contents []android.Module) { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 578 | addFromModule := func(ctx android.ModuleContext, module android.Module, apiScope *HiddenAPIScope) { |
| 579 | sdkKind := apiScope.sdkKind |
| 580 | dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, sdkKind) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 581 | if dexJar != nil { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 582 | i.StubDexJarsByScope[apiScope] = append(i.StubDexJarsByScope[apiScope], dexJar) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 583 | } |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 584 | |
| 585 | if sdkLibrary, ok := module.(SdkLibraryDependency); ok { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 586 | removedTxtFile := sdkLibrary.SdkRemovedTxtFile(ctx, sdkKind) |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 587 | i.RemovedTxtFiles = append(i.RemovedTxtFiles, removedTxtFile.AsPaths()...) |
| 588 | } |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | // If the contents includes any java_sdk_library modules then add them to the stubs. |
| 592 | for _, module := range contents { |
| 593 | if _, ok := module.(SdkLibraryDependency); ok { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 594 | // Add information for every possible API scope needed by hidden API. |
| 595 | for _, apiScope := range hiddenAPISdkLibrarySupportedScopes { |
| 596 | addFromModule(ctx, module, apiScope) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
Paul Duffin | d061d40 | 2021-06-07 21:36:01 +0100 | [diff] [blame] | 601 | ctx.VisitDirectDeps(func(module android.Module) { |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 602 | tag := ctx.OtherModuleDependencyTag(module) |
| 603 | if hiddenAPIStubsTag, ok := tag.(hiddenAPIStubsDependencyTag); ok { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 604 | apiScope := hiddenAPIStubsTag.apiScope |
| 605 | addFromModule(ctx, module, apiScope) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 606 | } |
| 607 | }) |
| 608 | |
| 609 | // Normalize the paths, i.e. remove duplicates and sort. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 610 | i.StubDexJarsByScope.dedupAndSort() |
| 611 | i.DependencyStubDexJarsByScope.dedupAndSort() |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 612 | i.RemovedTxtFiles = android.SortedUniquePaths(i.RemovedTxtFiles) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | // extractFlagFilesFromProperties extracts the paths to flag files that are specified in the |
| 616 | // supplied properties and stores them in this struct. |
| 617 | func (i *HiddenAPIFlagInput) extractFlagFilesFromProperties(ctx android.ModuleContext, p *HiddenAPIFlagFileProperties) { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 618 | for _, category := range HiddenAPIFlagFileCategories { |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 619 | paths := android.PathsForModuleSrc(ctx, category.propertyValueReader(p)) |
| 620 | i.FlagFilesByCategory[category] = paths |
| 621 | } |
| 622 | } |
| 623 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 624 | func (i *HiddenAPIFlagInput) transitiveStubDexJarsByScope() StubDexJarsByScope { |
| 625 | transitive := i.DependencyStubDexJarsByScope |
| 626 | transitive.append(i.StubDexJarsByScope) |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 627 | return transitive |
| 628 | } |
| 629 | |
Paul Duffin | 1e6f5c4 | 2021-05-21 16:15:31 +0100 | [diff] [blame] | 630 | // HiddenAPIFlagOutput contains paths to output files from the hidden API flag generation for a |
| 631 | // bootclasspath_fragment module. |
| 632 | type HiddenAPIFlagOutput struct { |
| 633 | // The path to the generated stub-flags.csv file. |
| 634 | StubFlagsPath android.Path |
| 635 | |
| 636 | // The path to the generated annotation-flags.csv file. |
| 637 | AnnotationFlagsPath android.Path |
| 638 | |
| 639 | // The path to the generated metadata.csv file. |
| 640 | MetadataPath android.Path |
| 641 | |
| 642 | // The path to the generated index.csv file. |
| 643 | IndexPath android.Path |
| 644 | |
| 645 | // The path to the generated all-flags.csv file. |
| 646 | AllFlagsPath android.Path |
| 647 | } |
| 648 | |
Paul Duffin | 5f148ca | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 649 | // bootDexJarByModule is a map from base module name (without prebuilt_ prefix) to the boot dex |
| 650 | // path. |
| 651 | type bootDexJarByModule map[string]android.Path |
| 652 | |
| 653 | // addPath adds the path for a module to the map. |
| 654 | func (b bootDexJarByModule) addPath(module android.Module, path android.Path) { |
| 655 | b[android.RemoveOptionalPrebuiltPrefix(module.Name())] = path |
| 656 | } |
| 657 | |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 658 | // bootDexJars returns the boot dex jar paths sorted by their keys. |
| 659 | func (b bootDexJarByModule) bootDexJars() android.Paths { |
| 660 | paths := android.Paths{} |
| 661 | for _, k := range android.SortedStringKeys(b) { |
| 662 | paths = append(paths, b[k]) |
| 663 | } |
| 664 | return paths |
| 665 | } |
| 666 | |
Paul Duffin | 7f87216 | 2021-06-17 19:33:24 +0100 | [diff] [blame] | 667 | // bootDexJarsWithoutCoverage returns the boot dex jar paths sorted by their keys without coverage |
| 668 | // libraries if present. |
| 669 | func (b bootDexJarByModule) bootDexJarsWithoutCoverage() android.Paths { |
| 670 | paths := android.Paths{} |
| 671 | for _, k := range android.SortedStringKeys(b) { |
| 672 | if k == "jacocoagent" { |
| 673 | continue |
| 674 | } |
| 675 | paths = append(paths, b[k]) |
| 676 | } |
| 677 | return paths |
| 678 | } |
| 679 | |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 680 | // HiddenAPIOutput encapsulates the output from the hidden API processing. |
| 681 | type HiddenAPIOutput struct { |
| 682 | HiddenAPIFlagOutput |
| 683 | |
| 684 | // The map from base module name to the path to the encoded boot dex file. |
| 685 | EncodedBootDexFilesByModule bootDexJarByModule |
| 686 | } |
| 687 | |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 688 | // pathForValidation creates a path of the same type as the supplied type but with a name of |
| 689 | // <path>.valid. |
| 690 | // |
| 691 | // e.g. If path is an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv then this will return |
| 692 | // an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv.valid |
| 693 | func pathForValidation(ctx android.PathContext, path android.WritablePath) android.WritablePath { |
| 694 | extWithoutLeadingDot := strings.TrimPrefix(path.Ext(), ".") |
| 695 | return path.ReplaceExtension(ctx, extWithoutLeadingDot+".valid") |
| 696 | } |
| 697 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 698 | // buildRuleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from |
| 699 | // the flags from all the modules, the stub flags, augmented with some additional configuration |
| 700 | // files. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 701 | // |
| 702 | // baseFlagsPath is the path to the flags file containing all the information from the stubs plus |
| 703 | // an entry for every single member in the dex implementation jars of the individual modules. Every |
| 704 | // signature in any of the other files MUST be included in this file. |
| 705 | // |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 706 | // annotationFlags is the path to the annotation flags file generated from annotation information |
| 707 | // in each module. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 708 | // |
Paul Duffin | af99afa | 2021-05-21 22:18:56 +0100 | [diff] [blame] | 709 | // hiddenAPIInfo is a struct containing paths to files that augment the information provided by |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 710 | // the annotationFlags. |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 711 | func buildRuleToGenerateHiddenApiFlags(ctx android.BuilderContext, name, desc string, |
Paul Duffin | d061d40 | 2021-06-07 21:36:01 +0100 | [diff] [blame] | 712 | outputPath android.WritablePath, baseFlagsPath android.Path, annotationFlagPaths android.Paths, |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 713 | flagFilesByCategory FlagFilesByCategory, allFlagsPaths android.Paths, generatedRemovedDexSignatures android.OptionalPath) { |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 714 | |
| 715 | // The file which is used to record that the flags file is valid. |
| 716 | var validFile android.WritablePath |
| 717 | |
| 718 | // If there are flag files that have been generated by fragments on which this depends then use |
| 719 | // them to validate the flag file generated by the rules created by this method. |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 720 | if len(allFlagsPaths) > 0 { |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 721 | // The flags file generated by the rule created by this method needs to be validated to ensure |
| 722 | // that it is consistent with the flag files generated by the individual fragments. |
| 723 | |
| 724 | validFile = pathForValidation(ctx, outputPath) |
| 725 | |
| 726 | // Create a rule to validate the output from the following rule. |
| 727 | rule := android.NewRuleBuilder(pctx, ctx) |
| 728 | rule.Command(). |
| 729 | BuiltTool("verify_overlaps"). |
| 730 | Input(outputPath). |
| 731 | Inputs(allFlagsPaths). |
| 732 | // If validation passes then update the file that records that. |
| 733 | Text("&& touch").Output(validFile) |
| 734 | rule.Build(name+"Validation", desc+" validation") |
| 735 | } |
| 736 | |
| 737 | // Create the rule that will generate the flag files. |
Paul Duffin | d3c1513 | 2021-04-21 22:12:35 +0100 | [diff] [blame] | 738 | tempPath := tempPathForRestat(ctx, outputPath) |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 739 | rule := android.NewRuleBuilder(pctx, ctx) |
| 740 | command := rule.Command(). |
| 741 | BuiltTool("generate_hiddenapi_lists"). |
| 742 | FlagWithInput("--csv ", baseFlagsPath). |
Paul Duffin | d061d40 | 2021-06-07 21:36:01 +0100 | [diff] [blame] | 743 | Inputs(annotationFlagPaths). |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 744 | FlagWithOutput("--output ", tempPath) |
| 745 | |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 746 | // Add the options for the different categories of flag files. |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 747 | for _, category := range HiddenAPIFlagFileCategories { |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 748 | paths := flagFilesByCategory[category] |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 749 | for _, path := range paths { |
| 750 | category.commandMutator(command, path) |
| 751 | } |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 752 | } |
| 753 | |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 754 | // If available then pass the automatically generated file containing dex signatures of removed |
| 755 | // API members to the rule so they can be marked as removed. |
| 756 | if generatedRemovedDexSignatures.Valid() { |
| 757 | hiddenAPIRemovedFlagFileCategory.commandMutator(command, generatedRemovedDexSignatures.Path()) |
| 758 | } |
| 759 | |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 760 | commitChangeForRestat(rule, tempPath, outputPath) |
| 761 | |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 762 | if validFile != nil { |
| 763 | // Add the file that indicates that the file generated by this is valid. |
| 764 | // |
| 765 | // This will cause the validation rule above to be run any time that the output of this rule |
| 766 | // changes but the validation will run in parallel with other rules that depend on this file. |
| 767 | command.Validation(validFile) |
| 768 | } |
| 769 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 770 | rule.Build(name, desc) |
| 771 | } |
| 772 | |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 773 | // hiddenAPIRulesForBootclasspathFragment will generate all the flags for a fragment of the |
| 774 | // bootclasspath and then encode the flags into the boot dex files. |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 775 | // |
| 776 | // It takes: |
| 777 | // * Map from android.SdkKind to stub dex jar paths defining the API for that sdk kind. |
| 778 | // * The list of modules that are the contents of the fragment. |
| 779 | // * The additional manually curated flag files to use. |
| 780 | // |
| 781 | // It generates: |
| 782 | // * stub-flags.csv |
| 783 | // * annotation-flags.csv |
| 784 | // * metadata.csv |
| 785 | // * index.csv |
| 786 | // * all-flags.csv |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 787 | // * encoded boot dex files |
| 788 | func hiddenAPIRulesForBootclasspathFragment(ctx android.ModuleContext, contents []android.Module, input HiddenAPIFlagInput) *HiddenAPIOutput { |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 789 | hiddenApiSubDir := "modular-hiddenapi" |
| 790 | |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 791 | // Gather information about the boot dex files for the boot libraries provided by this fragment. |
| 792 | bootDexInfoByModule := extractBootDexInfoFromModules(ctx, contents) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 793 | |
| 794 | // Generate the stub-flags.csv. |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 795 | stubFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "stub-flags.csv") |
Paul Duffin | 4539a37 | 2021-06-23 23:20:43 +0100 | [diff] [blame^] | 796 | buildRuleToGenerateHiddenAPIStubFlagsFile(ctx, "modularHiddenAPIStubFlagsFile", "modular hiddenapi stub flags", stubFlagsCSV, bootDexInfoByModule.bootDexJars(), input) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 797 | |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 798 | // Extract the classes jars from the contents. |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 799 | classesJars := extractClassesJarsFromModules(contents) |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 800 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 801 | // Generate the set of flags from the annotations in the source code. |
| 802 | annotationFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "annotation-flags.csv") |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 803 | buildRuleToGenerateAnnotationFlags(ctx, "modular hiddenapi annotation flags", classesJars, stubFlagsCSV, annotationFlagsCSV) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 804 | |
| 805 | // Generate the metadata from the annotations in the source code. |
| 806 | metadataCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "metadata.csv") |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 807 | buildRuleToGenerateMetadata(ctx, "modular hiddenapi metadata", classesJars, stubFlagsCSV, metadataCSV) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 808 | |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 809 | // Generate the index file from the CSV files in the classes jars. |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 810 | indexCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "index.csv") |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 811 | buildRuleToGenerateIndex(ctx, "modular hiddenapi index", classesJars, indexCSV) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 812 | |
Paul Duffin | af99afa | 2021-05-21 22:18:56 +0100 | [diff] [blame] | 813 | // Removed APIs need to be marked and in order to do that the hiddenAPIInfo needs to specify files |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 814 | // containing dex signatures of all the removed APIs. In the monolithic files that is done by |
| 815 | // manually combining all the removed.txt files for each API and then converting them to dex |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 816 | // signatures, see the combined-removed-dex module. This does that automatically by using the |
| 817 | // *removed.txt files retrieved from the java_sdk_library modules that are specified in the |
| 818 | // stub_libs and contents properties of a bootclasspath_fragment. |
| 819 | removedDexSignatures := buildRuleToGenerateRemovedDexSignatures(ctx, input.RemovedTxtFiles) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 820 | |
| 821 | // Generate the all-flags.csv which are the flags that will, in future, be encoded into the dex |
| 822 | // files. |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 823 | allFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "all-flags.csv") |
Paul Duffin | d061d40 | 2021-06-07 21:36:01 +0100 | [diff] [blame] | 824 | buildRuleToGenerateHiddenApiFlags(ctx, "modularHiddenApiAllFlags", "modular hiddenapi all flags", allFlagsCSV, stubFlagsCSV, android.Paths{annotationFlagsCSV}, input.FlagFilesByCategory, nil, removedDexSignatures) |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 825 | |
| 826 | // Encode the flags into the boot dex files. |
| 827 | encodedBootDexJarsByModule := map[string]android.Path{} |
| 828 | outputDir := android.PathForModuleOut(ctx, "hiddenapi-modular/encoded").OutputPath |
| 829 | for _, name := range android.SortedStringKeys(bootDexInfoByModule) { |
| 830 | bootDexInfo := bootDexInfoByModule[name] |
| 831 | unencodedDex := bootDexInfo.path |
| 832 | encodedDex := hiddenAPIEncodeDex(ctx, unencodedDex, allFlagsCSV, bootDexInfo.uncompressDex, outputDir) |
| 833 | encodedBootDexJarsByModule[name] = encodedDex |
| 834 | } |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 835 | |
| 836 | // Store the paths in the info for use by other modules and sdk snapshot generation. |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 837 | output := HiddenAPIOutput{ |
| 838 | HiddenAPIFlagOutput: HiddenAPIFlagOutput{ |
| 839 | StubFlagsPath: stubFlagsCSV, |
| 840 | AnnotationFlagsPath: annotationFlagsCSV, |
| 841 | MetadataPath: metadataCSV, |
| 842 | IndexPath: indexCSV, |
| 843 | AllFlagsPath: allFlagsCSV, |
| 844 | }, |
| 845 | EncodedBootDexFilesByModule: encodedBootDexJarsByModule, |
Paul Duffin | 1e6f5c4 | 2021-05-21 16:15:31 +0100 | [diff] [blame] | 846 | } |
| 847 | return &output |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 848 | } |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 849 | |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 850 | func buildRuleToGenerateRemovedDexSignatures(ctx android.ModuleContext, removedTxtFiles android.Paths) android.OptionalPath { |
| 851 | if len(removedTxtFiles) == 0 { |
| 852 | return android.OptionalPath{} |
| 853 | } |
| 854 | |
| 855 | output := android.PathForModuleOut(ctx, "modular-hiddenapi/removed-dex-signatures.txt") |
| 856 | |
| 857 | rule := android.NewRuleBuilder(pctx, ctx) |
| 858 | rule.Command(). |
| 859 | BuiltTool("metalava"). |
| 860 | Flag("--no-banner"). |
| 861 | Inputs(removedTxtFiles). |
| 862 | FlagWithOutput("--dex-api ", output) |
| 863 | rule.Build("modular-hiddenapi-removed-dex-signatures", "modular hiddenapi removed dex signatures") |
| 864 | return android.OptionalPathForPath(output) |
| 865 | } |
| 866 | |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 867 | // extractBootDexJarsFromModules extracts the boot dex jars from the supplied modules. |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 868 | func extractBootDexJarsFromModules(ctx android.ModuleContext, contents []android.Module) bootDexJarByModule { |
| 869 | bootDexJars := bootDexJarByModule{} |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 870 | for _, module := range contents { |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 871 | hiddenAPIModule := hiddenAPIModuleFromModule(ctx, module) |
| 872 | if hiddenAPIModule == nil { |
| 873 | continue |
| 874 | } |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 875 | bootDexJar := retrieveBootDexJarFromHiddenAPIModule(ctx, hiddenAPIModule) |
| 876 | bootDexJars.addPath(module, bootDexJar) |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 877 | } |
| 878 | return bootDexJars |
| 879 | } |
| 880 | |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 881 | func hiddenAPIModuleFromModule(ctx android.BaseModuleContext, module android.Module) hiddenAPIModule { |
| 882 | if hiddenAPIModule, ok := module.(hiddenAPIModule); ok { |
| 883 | return hiddenAPIModule |
| 884 | } else if _, ok := module.(*DexImport); ok { |
| 885 | // Ignore this for the purposes of hidden API processing |
| 886 | } else { |
| 887 | ctx.ModuleErrorf("module %s does not implement hiddenAPIModule", module) |
| 888 | } |
| 889 | |
| 890 | return nil |
| 891 | } |
| 892 | |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 893 | // bootDexInfo encapsulates both the path and uncompressDex status retrieved from a hiddenAPIModule. |
| 894 | type bootDexInfo struct { |
| 895 | // The path to the dex jar that has not had hidden API flags encoded into it. |
| 896 | path android.Path |
| 897 | |
| 898 | // Indicates whether the dex jar needs uncompressing before encoding. |
| 899 | uncompressDex bool |
| 900 | } |
| 901 | |
| 902 | // bootDexInfoByModule is a map from module name (as returned by module.Name()) to the boot dex |
| 903 | // path (as returned by hiddenAPIModule.bootDexJar()) and the uncompressDex flag. |
| 904 | type bootDexInfoByModule map[string]bootDexInfo |
| 905 | |
| 906 | // bootDexJars returns the boot dex jar paths sorted by their keys. |
| 907 | func (b bootDexInfoByModule) bootDexJars() android.Paths { |
| 908 | paths := android.Paths{} |
| 909 | for _, m := range android.SortedStringKeys(b) { |
| 910 | paths = append(paths, b[m].path) |
| 911 | } |
| 912 | return paths |
| 913 | } |
| 914 | |
| 915 | // extractBootDexInfoFromModules extracts the boot dex jar and uncompress dex state from |
| 916 | // each of the supplied modules which must implement hiddenAPIModule. |
| 917 | func extractBootDexInfoFromModules(ctx android.ModuleContext, contents []android.Module) bootDexInfoByModule { |
| 918 | bootDexJarsByModule := bootDexInfoByModule{} |
| 919 | for _, module := range contents { |
| 920 | hiddenAPIModule := module.(hiddenAPIModule) |
| 921 | bootDexJar := retrieveBootDexJarFromHiddenAPIModule(ctx, hiddenAPIModule) |
| 922 | bootDexJarsByModule[module.Name()] = bootDexInfo{ |
| 923 | path: bootDexJar, |
| 924 | uncompressDex: *hiddenAPIModule.uncompressDex(), |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | return bootDexJarsByModule |
| 929 | } |
| 930 | |
| 931 | // retrieveBootDexJarFromHiddenAPIModule retrieves the boot dex jar from the hiddenAPIModule. |
| 932 | // |
| 933 | // If the module does not provide a boot dex jar, i.e. the returned boot dex jar is nil, then that |
| 934 | // create a fake path and either report an error immediately or defer reporting of the error until |
| 935 | // the path is actually used. |
| 936 | func retrieveBootDexJarFromHiddenAPIModule(ctx android.ModuleContext, module hiddenAPIModule) android.Path { |
| 937 | bootDexJar := module.bootDexJar() |
| 938 | if bootDexJar == nil { |
| 939 | fake := android.PathForModuleOut(ctx, fmt.Sprintf("fake/boot-dex/%s.jar", module.Name())) |
| 940 | bootDexJar = fake |
| 941 | |
| 942 | handleMissingDexBootFile(ctx, module, fake) |
| 943 | } |
| 944 | return bootDexJar |
| 945 | } |
| 946 | |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 947 | // extractClassesJarsFromModules extracts the class jars from the supplied modules. |
| 948 | func extractClassesJarsFromModules(contents []android.Module) android.Paths { |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 949 | classesJars := android.Paths{} |
| 950 | for _, module := range contents { |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 951 | classesJars = append(classesJars, retrieveClassesJarsFromModule(module)...) |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 952 | } |
| 953 | return classesJars |
| 954 | } |
Paul Duffin | 5f148ca | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 955 | |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 956 | // retrieveClassesJarsFromModule retrieves the classes jars from the supplied module. |
| 957 | func retrieveClassesJarsFromModule(module android.Module) android.Paths { |
| 958 | if hiddenAPIModule, ok := module.(hiddenAPIModule); ok { |
| 959 | return hiddenAPIModule.classesJars() |
| 960 | } |
| 961 | |
| 962 | return nil |
| 963 | } |
| 964 | |
Paul Duffin | 5f148ca | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 965 | // deferReportingMissingBootDexJar returns true if a missing boot dex jar should not be reported by |
| 966 | // Soong but should instead only be reported in ninja if the file is actually built. |
| 967 | func deferReportingMissingBootDexJar(ctx android.ModuleContext, module android.Module) bool { |
| 968 | // TODO(b/179354495): Remove this workaround when it is unnecessary. |
| 969 | // Prebuilt modules like framework-wifi do not yet provide dex implementation jars. So, |
| 970 | // create a fake one that will cause a build error only if it is used. |
| 971 | if ctx.Config().AlwaysUsePrebuiltSdks() { |
| 972 | return true |
| 973 | } |
| 974 | |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 975 | // Any missing dependency should be allowed. |
| 976 | if ctx.Config().AllowMissingDependencies() { |
| 977 | return true |
| 978 | } |
| 979 | |
Paul Duffin | 5f148ca | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 980 | // This is called for both platform_bootclasspath and bootclasspath_fragment modules. |
| 981 | // |
| 982 | // A bootclasspath_fragment module should only use the APEX variant of source or prebuilt modules. |
| 983 | // Ideally, a bootclasspath_fragment module should never have a platform variant created for it |
| 984 | // but unfortunately, due to b/187910671 it does. |
| 985 | // |
| 986 | // That causes issues when obtaining a boot dex jar for a prebuilt module as a prebuilt module |
| 987 | // used by a bootclasspath_fragment can only provide a boot dex jar when it is part of APEX, i.e. |
| 988 | // has an APEX variant not a platform variant. |
| 989 | // |
| 990 | // There are some other situations when a prebuilt module used by a bootclasspath_fragment cannot |
| 991 | // provide a boot dex jar: |
| 992 | // 1. If the bootclasspath_fragment is not exported by the prebuilt_apex/apex_set module then it |
| 993 | // does not have an APEX variant and only has a platform variant and neither do its content |
| 994 | // modules. |
| 995 | // 2. Some build configurations, e.g. setting TARGET_BUILD_USE_PREBUILT_SDKS causes all |
| 996 | // java_sdk_library_import modules to be treated as preferred and as many of them are not part |
| 997 | // of an apex they cannot provide a boot dex jar. |
| 998 | // |
| 999 | // The first case causes problems when the affected prebuilt modules are preferred but that is an |
| 1000 | // invalid configuration and it is ok for it to fail as the work to enable that is not yet |
| 1001 | // complete. The second case is used for building targets that do not use boot dex jars and so |
| 1002 | // deferring error reporting to ninja is fine as the affected ninja targets should never be built. |
| 1003 | // That is handled above. |
| 1004 | // |
| 1005 | // A platform_bootclasspath module can use libraries from both platform and APEX variants. Unlike |
| 1006 | // the bootclasspath_fragment it supports dex_import modules which provides the dex file. So, it |
| 1007 | // can obtain a boot dex jar from a prebuilt that is not part of an APEX. However, it is assumed |
| 1008 | // that if the library can be part of an APEX then it is the APEX variant that is used. |
| 1009 | // |
| 1010 | // This check handles the slightly different requirements of the bootclasspath_fragment and |
| 1011 | // platform_bootclasspath modules by only deferring error reporting for the platform variant of |
| 1012 | // a prebuilt modules that has other variants which are part of an APEX. |
| 1013 | // |
| 1014 | // TODO(b/187910671): Remove this once platform variants are no longer created unnecessarily. |
| 1015 | if android.IsModulePrebuilt(module) { |
| 1016 | if am, ok := module.(android.ApexModule); ok && am.InAnyApex() { |
| 1017 | apexInfo := ctx.OtherModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo) |
| 1018 | if apexInfo.IsForPlatform() { |
| 1019 | return true |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | // A bootclasspath module that is part of a versioned sdk never provides a boot dex jar as there |
| 1025 | // is no equivalently versioned prebuilt APEX file from which it can be obtained. However, |
| 1026 | // versioned bootclasspath modules are processed by Soong so in order to avoid them causing build |
| 1027 | // failures missing boot dex jars need to be deferred. |
| 1028 | if android.IsModuleInVersionedSdk(ctx.Module()) { |
| 1029 | return true |
| 1030 | } |
| 1031 | |
| 1032 | return false |
| 1033 | } |
| 1034 | |
| 1035 | // handleMissingDexBootFile will either log a warning or create an error rule to create the fake |
| 1036 | // file depending on the value returned from deferReportingMissingBootDexJar. |
| 1037 | func handleMissingDexBootFile(ctx android.ModuleContext, module android.Module, fake android.WritablePath) { |
| 1038 | if deferReportingMissingBootDexJar(ctx, module) { |
| 1039 | // Create an error rule that pretends to create the output file but will actually fail if it |
| 1040 | // is run. |
| 1041 | ctx.Build(pctx, android.BuildParams{ |
| 1042 | Rule: android.ErrorRule, |
| 1043 | Output: fake, |
| 1044 | Args: map[string]string{ |
| 1045 | "error": fmt.Sprintf("missing dependencies: boot dex jar for %s", module), |
| 1046 | }, |
| 1047 | }) |
| 1048 | } else { |
| 1049 | ctx.ModuleErrorf("module %s does not provide a dex jar", module) |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | // retrieveEncodedBootDexJarFromModule returns a path to the boot dex jar from the supplied module's |
| 1054 | // DexJarBuildPath() method. |
| 1055 | // |
| 1056 | // The returned path will usually be to a dex jar file that has been encoded with hidden API flags. |
| 1057 | // However, under certain conditions, e.g. errors, or special build configurations it will return |
| 1058 | // a path to a fake file. |
| 1059 | func retrieveEncodedBootDexJarFromModule(ctx android.ModuleContext, module android.Module) android.Path { |
| 1060 | bootDexJar := module.(interface{ DexJarBuildPath() android.Path }).DexJarBuildPath() |
| 1061 | if bootDexJar == nil { |
| 1062 | fake := android.PathForModuleOut(ctx, fmt.Sprintf("fake/encoded-dex/%s.jar", module.Name())) |
| 1063 | bootDexJar = fake |
| 1064 | |
| 1065 | handleMissingDexBootFile(ctx, module, fake) |
| 1066 | } |
| 1067 | return bootDexJar |
| 1068 | } |
| 1069 | |
| 1070 | // extractEncodedDexJarsFromModules extracts the encoded dex jars from the supplied modules. |
| 1071 | func extractEncodedDexJarsFromModules(ctx android.ModuleContext, contents []android.Module) bootDexJarByModule { |
| 1072 | encodedDexJarsByModuleName := bootDexJarByModule{} |
| 1073 | for _, module := range contents { |
| 1074 | path := retrieveEncodedBootDexJarFromModule(ctx, module) |
| 1075 | encodedDexJarsByModuleName.addPath(module, path) |
| 1076 | } |
| 1077 | return encodedDexJarsByModuleName |
| 1078 | } |