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