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 | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | // Contains support for processing hiddenAPI in a modular fashion. |
| 26 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 27 | type hiddenAPIStubsDependencyTag struct { |
| 28 | blueprint.BaseDependencyTag |
| 29 | sdkKind android.SdkKind |
| 30 | } |
| 31 | |
| 32 | func (b hiddenAPIStubsDependencyTag) ExcludeFromApexContents() { |
| 33 | } |
| 34 | |
| 35 | func (b hiddenAPIStubsDependencyTag) ReplaceSourceWithPrebuilt() bool { |
| 36 | return false |
| 37 | } |
| 38 | |
Paul Duffin | 976b0e5 | 2021-04-27 23:20:26 +0100 | [diff] [blame] | 39 | func (b hiddenAPIStubsDependencyTag) SdkMemberType(child android.Module) android.SdkMemberType { |
| 40 | // If the module is a java_sdk_library then treat it as if it was specific in the java_sdk_libs |
| 41 | // property, otherwise treat if it was specified in the java_header_libs property. |
| 42 | if javaSdkLibrarySdkMemberType.IsInstance(child) { |
| 43 | return javaSdkLibrarySdkMemberType |
| 44 | } |
| 45 | |
| 46 | return javaHeaderLibsSdkMemberType |
| 47 | } |
| 48 | |
| 49 | func (b hiddenAPIStubsDependencyTag) ExportMember() bool { |
| 50 | // Export the module added via this dependency tag from the sdk. |
| 51 | return true |
| 52 | } |
| 53 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 54 | // Avoid having to make stubs content explicitly visible to dependent modules. |
| 55 | // |
| 56 | // This is a temporary workaround to make it easier to migrate to bootclasspath_fragment modules |
| 57 | // with proper dependencies. |
| 58 | // TODO(b/177892522): Remove this and add needed visibility. |
| 59 | func (b hiddenAPIStubsDependencyTag) ExcludeFromVisibilityEnforcement() { |
| 60 | } |
| 61 | |
| 62 | var _ android.ExcludeFromVisibilityEnforcementTag = hiddenAPIStubsDependencyTag{} |
| 63 | var _ android.ReplaceSourceWithPrebuilt = hiddenAPIStubsDependencyTag{} |
| 64 | var _ android.ExcludeFromApexContentsTag = hiddenAPIStubsDependencyTag{} |
Paul Duffin | 976b0e5 | 2021-04-27 23:20:26 +0100 | [diff] [blame] | 65 | var _ android.SdkMemberTypeDependencyTag = hiddenAPIStubsDependencyTag{} |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 66 | |
Paul Duffin | 3e7fcc3 | 2021-04-15 13:31:38 +0100 | [diff] [blame] | 67 | // hiddenAPIRelevantSdkKinds lists all the android.SdkKind instances that are needed by the hidden |
| 68 | // API processing. |
| 69 | var hiddenAPIRelevantSdkKinds = []android.SdkKind{ |
| 70 | android.SdkPublic, |
| 71 | android.SdkSystem, |
| 72 | android.SdkTest, |
| 73 | android.SdkCorePlatform, |
| 74 | } |
| 75 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 76 | // hiddenAPIComputeMonolithicStubLibModules computes the set of module names that provide stubs |
| 77 | // needed to produce the hidden API monolithic stub flags file. |
| 78 | func hiddenAPIComputeMonolithicStubLibModules(config android.Config) map[android.SdkKind][]string { |
| 79 | var publicStubModules []string |
| 80 | var systemStubModules []string |
| 81 | var testStubModules []string |
| 82 | var corePlatformStubModules []string |
| 83 | |
| 84 | if config.AlwaysUsePrebuiltSdks() { |
| 85 | // Build configuration mandates using prebuilt stub modules |
| 86 | publicStubModules = append(publicStubModules, "sdk_public_current_android") |
| 87 | systemStubModules = append(systemStubModules, "sdk_system_current_android") |
| 88 | testStubModules = append(testStubModules, "sdk_test_current_android") |
| 89 | } else { |
| 90 | // Use stub modules built from source |
| 91 | publicStubModules = append(publicStubModules, "android_stubs_current") |
| 92 | systemStubModules = append(systemStubModules, "android_system_stubs_current") |
| 93 | testStubModules = append(testStubModules, "android_test_stubs_current") |
| 94 | } |
| 95 | // We do not have prebuilts of the core platform api yet |
| 96 | corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs") |
| 97 | |
| 98 | // Allow products to define their own stubs for custom product jars that apps can use. |
| 99 | publicStubModules = append(publicStubModules, config.ProductHiddenAPIStubs()...) |
| 100 | systemStubModules = append(systemStubModules, config.ProductHiddenAPIStubsSystem()...) |
| 101 | testStubModules = append(testStubModules, config.ProductHiddenAPIStubsTest()...) |
| 102 | if config.IsEnvTrue("EMMA_INSTRUMENT") { |
Paul Duffin | 098c878 | 2021-05-14 10:45:25 +0100 | [diff] [blame] | 103 | // Add jacoco-stubs to public, system and test. It doesn't make any real difference as public |
| 104 | // allows everyone access but it is needed to ensure consistent flags between the |
| 105 | // bootclasspath fragment generated flags and the platform_bootclasspath generated flags. |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 106 | publicStubModules = append(publicStubModules, "jacoco-stubs") |
Paul Duffin | 098c878 | 2021-05-14 10:45:25 +0100 | [diff] [blame] | 107 | systemStubModules = append(systemStubModules, "jacoco-stubs") |
| 108 | testStubModules = append(testStubModules, "jacoco-stubs") |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | m := map[android.SdkKind][]string{} |
| 112 | m[android.SdkPublic] = publicStubModules |
| 113 | m[android.SdkSystem] = systemStubModules |
| 114 | m[android.SdkTest] = testStubModules |
| 115 | m[android.SdkCorePlatform] = corePlatformStubModules |
| 116 | return m |
| 117 | } |
| 118 | |
| 119 | // hiddenAPIAddStubLibDependencies adds dependencies onto the modules specified in |
| 120 | // sdkKindToStubLibModules. It adds them in a well known order and uses an SdkKind specific tag to |
| 121 | // identify the source of the dependency. |
| 122 | func hiddenAPIAddStubLibDependencies(ctx android.BottomUpMutatorContext, sdkKindToStubLibModules map[android.SdkKind][]string) { |
| 123 | module := ctx.Module() |
| 124 | for _, sdkKind := range hiddenAPIRelevantSdkKinds { |
| 125 | modules := sdkKindToStubLibModules[sdkKind] |
| 126 | ctx.AddDependency(module, hiddenAPIStubsDependencyTag{sdkKind: sdkKind}, modules...) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // hiddenAPIGatherStubLibDexJarPaths gathers the paths to the dex jars from the dependencies added |
| 131 | // in hiddenAPIAddStubLibDependencies. |
Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 132 | func hiddenAPIGatherStubLibDexJarPaths(ctx android.ModuleContext, contents []android.Module) map[android.SdkKind]android.Paths { |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 133 | m := map[android.SdkKind]android.Paths{} |
Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 134 | |
| 135 | // If the contents includes any java_sdk_library modules then add them to the stubs. |
| 136 | for _, module := range contents { |
| 137 | if _, ok := module.(SdkLibraryDependency); ok { |
| 138 | for _, kind := range []android.SdkKind{android.SdkPublic, android.SdkSystem, android.SdkTest} { |
| 139 | dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, kind) |
| 140 | if dexJar != nil { |
| 141 | m[kind] = append(m[kind], dexJar) |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 147 | ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) { |
| 148 | tag := ctx.OtherModuleDependencyTag(module) |
| 149 | if hiddenAPIStubsTag, ok := tag.(hiddenAPIStubsDependencyTag); ok { |
| 150 | kind := hiddenAPIStubsTag.sdkKind |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 151 | dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, kind) |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 152 | if dexJar != nil { |
| 153 | m[kind] = append(m[kind], dexJar) |
| 154 | } |
| 155 | } |
| 156 | }) |
Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 157 | |
| 158 | // Normalize the paths, i.e. remove duplicates and sort. |
| 159 | for k, v := range m { |
| 160 | m[k] = android.SortedUniquePaths(v) |
| 161 | } |
| 162 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 163 | return m |
| 164 | } |
| 165 | |
| 166 | // hiddenAPIRetrieveDexJarBuildPath retrieves the DexJarBuildPath from the specified module, if |
| 167 | // available, or reports an error. |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 168 | func hiddenAPIRetrieveDexJarBuildPath(ctx android.ModuleContext, module android.Module, kind android.SdkKind) android.Path { |
| 169 | var dexJar android.Path |
| 170 | if sdkLibrary, ok := module.(SdkLibraryDependency); ok { |
| 171 | dexJar = sdkLibrary.SdkApiStubDexJar(ctx, kind) |
| 172 | } else if j, ok := module.(UsesLibraryDependency); ok { |
| 173 | dexJar = j.DexJarBuildPath() |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 174 | } else { |
| 175 | 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] | 176 | return nil |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 177 | } |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 178 | |
| 179 | if dexJar == nil { |
| 180 | ctx.ModuleErrorf("dependency %s does not provide a dex jar, consider setting compile_dex: true", module) |
| 181 | } |
| 182 | return dexJar |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | var sdkKindToHiddenapiListOption = map[android.SdkKind]string{ |
| 186 | android.SdkPublic: "public-stub-classpath", |
| 187 | android.SdkSystem: "system-stub-classpath", |
| 188 | android.SdkTest: "test-stub-classpath", |
| 189 | android.SdkCorePlatform: "core-platform-stub-classpath", |
| 190 | } |
| 191 | |
| 192 | // ruleToGenerateHiddenAPIStubFlagsFile creates a rule to create a hidden API stub flags file. |
| 193 | // |
| 194 | // The rule is initialized but not built so that the caller can modify it and select an appropriate |
| 195 | // name. |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 196 | func ruleToGenerateHiddenAPIStubFlagsFile(ctx android.BuilderContext, outputPath android.WritablePath, bootDexJars android.Paths, sdkKindToPathList map[android.SdkKind]android.Paths) *android.RuleBuilder { |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 197 | // Singleton rule which applies hiddenapi on all boot class path dex files. |
| 198 | rule := android.NewRuleBuilder(pctx, ctx) |
| 199 | |
| 200 | tempPath := tempPathForRestat(ctx, outputPath) |
| 201 | |
| 202 | command := rule.Command(). |
| 203 | Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")). |
| 204 | Text("list"). |
| 205 | FlagForEachInput("--boot-dex=", bootDexJars) |
| 206 | |
| 207 | // Iterate over the sdk kinds in a fixed order. |
| 208 | for _, sdkKind := range hiddenAPIRelevantSdkKinds { |
| 209 | paths := sdkKindToPathList[sdkKind] |
| 210 | if len(paths) > 0 { |
| 211 | option := sdkKindToHiddenapiListOption[sdkKind] |
| 212 | command.FlagWithInputList("--"+option+"=", paths, ":") |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Add the output path. |
| 217 | command.FlagWithOutput("--out-api-flags=", tempPath) |
| 218 | |
| 219 | commitChangeForRestat(rule, tempPath, outputPath) |
| 220 | return rule |
| 221 | } |
| 222 | |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 223 | // HiddenAPIFlagFileProperties contains paths to the flag files that can be used to augment the |
| 224 | // information obtained from annotations within the source code in order to create the complete set |
| 225 | // 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] | 226 | // |
| 227 | // Each property contains a list of paths. With the exception of the Unsupported_packages the paths |
| 228 | // of each property reference a plain text file that contains a java signature per line. The flags |
| 229 | // for each of those signatures will be updated in a property specific way. |
| 230 | // |
| 231 | // The Unsupported_packages property contains a list of paths, each of which is a plain text file |
| 232 | // with one Java package per line. All members of all classes within that package (but not nested |
| 233 | // packages) will be updated in a property specific way. |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 234 | type HiddenAPIFlagFileProperties struct { |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 235 | // Marks each signature in the referenced files as being unsupported. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 236 | Unsupported []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 237 | |
| 238 | // Marks each signature in the referenced files as being unsupported because it has been removed. |
| 239 | // Any conflicts with other flags are ignored. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 240 | Removed []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 241 | |
| 242 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= R |
| 243 | // and low priority. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 244 | Max_target_r_low_priority []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 245 | |
| 246 | // 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] | 247 | Max_target_q []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 248 | |
| 249 | // 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] | 250 | Max_target_p []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 251 | |
| 252 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= O |
| 253 | // and low priority. Any conflicts with other flags are ignored. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 254 | Max_target_o_low_priority []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 255 | |
| 256 | // Marks each signature in the referenced files as being blocked. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 257 | Blocked []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 258 | |
| 259 | // 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] | 260 | Unsupported_packages []string `android:"path"` |
| 261 | } |
| 262 | |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 263 | func (p *HiddenAPIFlagFileProperties) hiddenAPIFlagFileInfo(ctx android.ModuleContext) hiddenAPIFlagFileInfo { |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 264 | info := hiddenAPIFlagFileInfo{FlagFilesByCategory: FlagFilesByCategory{}} |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 265 | for _, category := range hiddenAPIFlagFileCategories { |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 266 | paths := android.PathsForModuleSrc(ctx, category.propertyValueReader(p)) |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 267 | info.FlagFilesByCategory[category] = paths |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 268 | } |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 269 | return info |
| 270 | } |
| 271 | |
| 272 | type hiddenAPIFlagFileCategory struct { |
| 273 | // propertyName is the name of the property for this category. |
| 274 | propertyName string |
| 275 | |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 276 | // 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] | 277 | // properties. |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 278 | propertyValueReader func(properties *HiddenAPIFlagFileProperties) []string |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 279 | |
| 280 | // commandMutator adds the appropriate command line options for this category to the supplied |
| 281 | // command |
| 282 | commandMutator func(command *android.RuleBuilderCommand, path android.Path) |
| 283 | } |
| 284 | |
| 285 | var hiddenAPIFlagFileCategories = []*hiddenAPIFlagFileCategory{ |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 286 | // See HiddenAPIFlagFileProperties.Unsupported |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 287 | { |
| 288 | propertyName: "unsupported", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 289 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 290 | return properties.Unsupported |
| 291 | }, |
| 292 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 293 | command.FlagWithInput("--unsupported ", path) |
| 294 | }, |
| 295 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 296 | // See HiddenAPIFlagFileProperties.Removed |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 297 | { |
| 298 | propertyName: "removed", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 299 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 300 | return properties.Removed |
| 301 | }, |
| 302 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 303 | command.FlagWithInput("--unsupported ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed") |
| 304 | }, |
| 305 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 306 | // See HiddenAPIFlagFileProperties.Max_target_r_low_priority |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 307 | { |
| 308 | propertyName: "max_target_r_low_priority", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 309 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 310 | return properties.Max_target_r_low_priority |
| 311 | }, |
| 312 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 313 | command.FlagWithInput("--max-target-r ", path).FlagWithArg("--tag ", "lo-prio") |
| 314 | }, |
| 315 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 316 | // See HiddenAPIFlagFileProperties.Max_target_q |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 317 | { |
| 318 | propertyName: "max_target_q", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 319 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 320 | return properties.Max_target_q |
| 321 | }, |
| 322 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 323 | command.FlagWithInput("--max-target-q ", path) |
| 324 | }, |
| 325 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 326 | // See HiddenAPIFlagFileProperties.Max_target_p |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 327 | { |
| 328 | propertyName: "max_target_p", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 329 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 330 | return properties.Max_target_p |
| 331 | }, |
| 332 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 333 | command.FlagWithInput("--max-target-p ", path) |
| 334 | }, |
| 335 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 336 | // See HiddenAPIFlagFileProperties.Max_target_o_low_priority |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 337 | { |
| 338 | propertyName: "max_target_o_low_priority", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 339 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 340 | return properties.Max_target_o_low_priority |
| 341 | }, |
| 342 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 343 | command.FlagWithInput("--max-target-o ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio") |
| 344 | }, |
| 345 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 346 | // See HiddenAPIFlagFileProperties.Blocked |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 347 | { |
| 348 | propertyName: "blocked", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 349 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 350 | return properties.Blocked |
| 351 | }, |
| 352 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 353 | command.FlagWithInput("--blocked ", path) |
| 354 | }, |
| 355 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 356 | // See HiddenAPIFlagFileProperties.Unsupported_packages |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 357 | { |
| 358 | propertyName: "unsupported_packages", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 359 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 360 | return properties.Unsupported_packages |
| 361 | }, |
| 362 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 363 | command.FlagWithInput("--unsupported ", path).Flag("--packages ") |
| 364 | }, |
| 365 | }, |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 368 | // FlagFilesByCategory maps a hiddenAPIFlagFileCategory to the paths to the files in that category. |
| 369 | type FlagFilesByCategory map[*hiddenAPIFlagFileCategory]android.Paths |
| 370 | |
| 371 | // append appends the supplied flags files to the corresponding category in this map. |
| 372 | func (s FlagFilesByCategory) append(other FlagFilesByCategory) { |
| 373 | for _, category := range hiddenAPIFlagFileCategories { |
| 374 | s[category] = append(s[category], other[category]...) |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // dedup removes duplicates in the flag files, while maintaining the order in which they were |
| 379 | // appended. |
| 380 | func (s FlagFilesByCategory) dedup() { |
| 381 | for category, paths := range s { |
| 382 | s[category] = android.FirstUniquePaths(paths) |
| 383 | } |
| 384 | } |
| 385 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 386 | // hiddenAPIFlagFileInfo contains paths resolved from HiddenAPIFlagFileProperties and also generated |
| 387 | // by hidden API processing. |
| 388 | // |
| 389 | // This is used both for an individual bootclasspath_fragment to provide it to other modules and |
| 390 | // for a module to collate the files from the fragments it depends upon. That is why the fields are |
| 391 | // all Paths even though they are initialized with a single path. |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 392 | type hiddenAPIFlagFileInfo struct { |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 393 | // FlagFilesByCategory maps from the flag file category to the paths containing information for |
| 394 | // that category. |
| 395 | FlagFilesByCategory FlagFilesByCategory |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 396 | |
Paul Duffin | 1e6f5c4 | 2021-05-21 16:15:31 +0100 | [diff] [blame^] | 397 | // The output from the hidden API processing needs to be made available to other modules. |
| 398 | HiddenAPIFlagOutput |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 399 | } |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 400 | |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 401 | var hiddenAPIFlagFileInfoProvider = blueprint.NewProvider(hiddenAPIFlagFileInfo{}) |
| 402 | |
Paul Duffin | 1e6f5c4 | 2021-05-21 16:15:31 +0100 | [diff] [blame^] | 403 | // HiddenAPIFlagOutput contains paths to output files from the hidden API flag generation for a |
| 404 | // bootclasspath_fragment module. |
| 405 | type HiddenAPIFlagOutput struct { |
| 406 | // The path to the generated stub-flags.csv file. |
| 407 | StubFlagsPath android.Path |
| 408 | |
| 409 | // The path to the generated annotation-flags.csv file. |
| 410 | AnnotationFlagsPath android.Path |
| 411 | |
| 412 | // The path to the generated metadata.csv file. |
| 413 | MetadataPath android.Path |
| 414 | |
| 415 | // The path to the generated index.csv file. |
| 416 | IndexPath android.Path |
| 417 | |
| 418 | // The path to the generated all-flags.csv file. |
| 419 | AllFlagsPath android.Path |
| 420 | } |
| 421 | |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 422 | // pathForValidation creates a path of the same type as the supplied type but with a name of |
| 423 | // <path>.valid. |
| 424 | // |
| 425 | // e.g. If path is an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv then this will return |
| 426 | // an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv.valid |
| 427 | func pathForValidation(ctx android.PathContext, path android.WritablePath) android.WritablePath { |
| 428 | extWithoutLeadingDot := strings.TrimPrefix(path.Ext(), ".") |
| 429 | return path.ReplaceExtension(ctx, extWithoutLeadingDot+".valid") |
| 430 | } |
| 431 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 432 | // buildRuleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from |
| 433 | // the flags from all the modules, the stub flags, augmented with some additional configuration |
| 434 | // files. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 435 | // |
| 436 | // baseFlagsPath is the path to the flags file containing all the information from the stubs plus |
| 437 | // an entry for every single member in the dex implementation jars of the individual modules. Every |
| 438 | // signature in any of the other files MUST be included in this file. |
| 439 | // |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 440 | // annotationFlags is the path to the annotation flags file generated from annotation information |
| 441 | // in each module. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 442 | // |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 443 | // flagFileInfo is a struct containing paths to files that augment the information provided by |
| 444 | // the annotationFlags. |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 445 | func buildRuleToGenerateHiddenApiFlags(ctx android.BuilderContext, name, desc string, outputPath android.WritablePath, baseFlagsPath android.Path, annotationFlags android.Path, flagFilesByCategory FlagFilesByCategory, allFlagsPaths android.Paths) { |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 446 | |
| 447 | // The file which is used to record that the flags file is valid. |
| 448 | var validFile android.WritablePath |
| 449 | |
| 450 | // If there are flag files that have been generated by fragments on which this depends then use |
| 451 | // 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] | 452 | if len(allFlagsPaths) > 0 { |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 453 | // The flags file generated by the rule created by this method needs to be validated to ensure |
| 454 | // that it is consistent with the flag files generated by the individual fragments. |
| 455 | |
| 456 | validFile = pathForValidation(ctx, outputPath) |
| 457 | |
| 458 | // Create a rule to validate the output from the following rule. |
| 459 | rule := android.NewRuleBuilder(pctx, ctx) |
| 460 | rule.Command(). |
| 461 | BuiltTool("verify_overlaps"). |
| 462 | Input(outputPath). |
| 463 | Inputs(allFlagsPaths). |
| 464 | // If validation passes then update the file that records that. |
| 465 | Text("&& touch").Output(validFile) |
| 466 | rule.Build(name+"Validation", desc+" validation") |
| 467 | } |
| 468 | |
| 469 | // Create the rule that will generate the flag files. |
Paul Duffin | d3c1513 | 2021-04-21 22:12:35 +0100 | [diff] [blame] | 470 | tempPath := tempPathForRestat(ctx, outputPath) |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 471 | rule := android.NewRuleBuilder(pctx, ctx) |
| 472 | command := rule.Command(). |
| 473 | BuiltTool("generate_hiddenapi_lists"). |
| 474 | FlagWithInput("--csv ", baseFlagsPath). |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 475 | Input(annotationFlags). |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 476 | FlagWithOutput("--output ", tempPath) |
| 477 | |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 478 | // Add the options for the different categories of flag files. |
| 479 | for _, category := range hiddenAPIFlagFileCategories { |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 480 | paths := flagFilesByCategory[category] |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 481 | for _, path := range paths { |
| 482 | category.commandMutator(command, path) |
| 483 | } |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | commitChangeForRestat(rule, tempPath, outputPath) |
| 487 | |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 488 | if validFile != nil { |
| 489 | // Add the file that indicates that the file generated by this is valid. |
| 490 | // |
| 491 | // This will cause the validation rule above to be run any time that the output of this rule |
| 492 | // changes but the validation will run in parallel with other rules that depend on this file. |
| 493 | command.Validation(validFile) |
| 494 | } |
| 495 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 496 | rule.Build(name, desc) |
| 497 | } |
| 498 | |
| 499 | // hiddenAPIGenerateAllFlagsForBootclasspathFragment will generate all the flags for a fragment |
| 500 | // of the bootclasspath. |
| 501 | // |
| 502 | // It takes: |
| 503 | // * Map from android.SdkKind to stub dex jar paths defining the API for that sdk kind. |
| 504 | // * The list of modules that are the contents of the fragment. |
| 505 | // * The additional manually curated flag files to use. |
| 506 | // |
| 507 | // It generates: |
| 508 | // * stub-flags.csv |
| 509 | // * annotation-flags.csv |
| 510 | // * metadata.csv |
| 511 | // * index.csv |
| 512 | // * all-flags.csv |
Paul Duffin | 1e6f5c4 | 2021-05-21 16:15:31 +0100 | [diff] [blame^] | 513 | func hiddenAPIGenerateAllFlagsForBootclasspathFragment(ctx android.ModuleContext, contents []hiddenAPIModule, stubJarsByKind map[android.SdkKind]android.Paths, flagFileInfo *hiddenAPIFlagFileInfo) *HiddenAPIFlagOutput { |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 514 | hiddenApiSubDir := "modular-hiddenapi" |
| 515 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 516 | // Generate the stub-flags.csv. |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 517 | bootDexJars := extractBootDexJarsFromHiddenAPIModules(ctx, contents) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 518 | stubFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "stub-flags.csv") |
| 519 | rule := ruleToGenerateHiddenAPIStubFlagsFile(ctx, stubFlagsCSV, bootDexJars, stubJarsByKind) |
| 520 | rule.Build("modularHiddenAPIStubFlagsFile", "modular hiddenapi stub flags") |
| 521 | |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 522 | // Extract the classes jars from the contents. |
| 523 | classesJars := extractClassJarsFromHiddenAPIModules(ctx, contents) |
| 524 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 525 | // Generate the set of flags from the annotations in the source code. |
| 526 | annotationFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "annotation-flags.csv") |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 527 | buildRuleToGenerateAnnotationFlags(ctx, "modular hiddenapi annotation flags", classesJars, stubFlagsCSV, annotationFlagsCSV) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 528 | |
| 529 | // Generate the metadata from the annotations in the source code. |
| 530 | metadataCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "metadata.csv") |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 531 | buildRuleToGenerateMetadata(ctx, "modular hiddenapi metadata", classesJars, stubFlagsCSV, metadataCSV) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 532 | |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 533 | // Generate the index file from the CSV files in the classes jars. |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 534 | indexCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "index.csv") |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 535 | buildRuleToGenerateIndex(ctx, "modular hiddenapi index", classesJars, indexCSV) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 536 | |
| 537 | // Removed APIs need to be marked and in order to do that the flagFileInfo needs to specify files |
| 538 | // containing dex signatures of all the removed APIs. In the monolithic files that is done by |
| 539 | // manually combining all the removed.txt files for each API and then converting them to dex |
| 540 | // signatures, see the combined-removed-dex module. That will all be done automatically in future. |
| 541 | // For now removed APIs are ignored. |
| 542 | // TODO(b/179354495): handle removed apis automatically. |
| 543 | |
| 544 | // Generate the all-flags.csv which are the flags that will, in future, be encoded into the dex |
| 545 | // files. |
| 546 | outputPath := android.PathForModuleOut(ctx, hiddenApiSubDir, "all-flags.csv") |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 547 | buildRuleToGenerateHiddenApiFlags(ctx, "modularHiddenApiAllFlags", "modular hiddenapi all flags", outputPath, stubFlagsCSV, annotationFlagsCSV, flagFileInfo.FlagFilesByCategory, nil) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 548 | |
| 549 | // Store the paths in the info for use by other modules and sdk snapshot generation. |
Paul Duffin | 1e6f5c4 | 2021-05-21 16:15:31 +0100 | [diff] [blame^] | 550 | output := HiddenAPIFlagOutput{ |
| 551 | StubFlagsPath: stubFlagsCSV, |
| 552 | AnnotationFlagsPath: annotationFlagsCSV, |
| 553 | MetadataPath: metadataCSV, |
| 554 | IndexPath: indexCSV, |
| 555 | AllFlagsPath: outputPath, |
| 556 | } |
| 557 | return &output |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 558 | } |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 559 | |
| 560 | // gatherHiddenAPIModuleFromContents gathers the hiddenAPIModule from the supplied contents. |
| 561 | func gatherHiddenAPIModuleFromContents(ctx android.ModuleContext, contents []android.Module) []hiddenAPIModule { |
| 562 | hiddenAPIModules := []hiddenAPIModule{} |
| 563 | for _, module := range contents { |
| 564 | if hiddenAPI, ok := module.(hiddenAPIModule); ok { |
| 565 | hiddenAPIModules = append(hiddenAPIModules, hiddenAPI) |
| 566 | } else if _, ok := module.(*DexImport); ok { |
| 567 | // Ignore this for the purposes of hidden API processing |
| 568 | } else { |
| 569 | ctx.ModuleErrorf("module %s does not implement hiddenAPIModule", module) |
| 570 | } |
| 571 | } |
| 572 | return hiddenAPIModules |
| 573 | } |
| 574 | |
| 575 | // extractBootDexJarsFromHiddenAPIModules extracts the boot dex jars from the supplied modules. |
| 576 | func extractBootDexJarsFromHiddenAPIModules(ctx android.ModuleContext, contents []hiddenAPIModule) android.Paths { |
| 577 | bootDexJars := android.Paths{} |
| 578 | for _, module := range contents { |
| 579 | bootDexJar := module.bootDexJar() |
| 580 | if bootDexJar == nil { |
Paul Duffin | 7487a7a | 2021-05-19 09:36:09 +0100 | [diff] [blame] | 581 | if ctx.Config().AlwaysUsePrebuiltSdks() { |
| 582 | // TODO(b/179354495): Remove this work around when it is unnecessary. |
| 583 | // Prebuilt modules like framework-wifi do not yet provide dex implementation jars. So, |
| 584 | // create a fake one that will cause a build error only if it is used. |
| 585 | fake := android.PathForModuleOut(ctx, "fake/boot-dex/%s.jar", module.Name()) |
| 586 | |
| 587 | // Create an error rule that pretends to create the output file but will actually fail if it |
| 588 | // is run. |
| 589 | ctx.Build(pctx, android.BuildParams{ |
| 590 | Rule: android.ErrorRule, |
| 591 | Output: fake, |
| 592 | Args: map[string]string{ |
| 593 | "error": fmt.Sprintf("missing dependencies: boot dex jar for %s", module), |
| 594 | }, |
| 595 | }) |
| 596 | bootDexJars = append(bootDexJars, fake) |
| 597 | } else { |
| 598 | ctx.ModuleErrorf("module %s does not provide a dex jar", module) |
| 599 | } |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 600 | } else { |
| 601 | bootDexJars = append(bootDexJars, bootDexJar) |
| 602 | } |
| 603 | } |
| 604 | return bootDexJars |
| 605 | } |
| 606 | |
| 607 | // extractClassJarsFromHiddenAPIModules extracts the class jars from the supplied modules. |
| 608 | func extractClassJarsFromHiddenAPIModules(ctx android.ModuleContext, contents []hiddenAPIModule) android.Paths { |
| 609 | classesJars := android.Paths{} |
| 610 | for _, module := range contents { |
| 611 | classesJars = append(classesJars, module.classesJars()...) |
| 612 | } |
| 613 | return classesJars |
| 614 | } |