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 ( |
| 18 | "android/soong/android" |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 19 | "github.com/google/blueprint" |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | // Contains support for processing hiddenAPI in a modular fashion. |
| 23 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 24 | type hiddenAPIStubsDependencyTag struct { |
| 25 | blueprint.BaseDependencyTag |
| 26 | sdkKind android.SdkKind |
| 27 | } |
| 28 | |
| 29 | func (b hiddenAPIStubsDependencyTag) ExcludeFromApexContents() { |
| 30 | } |
| 31 | |
| 32 | func (b hiddenAPIStubsDependencyTag) ReplaceSourceWithPrebuilt() bool { |
| 33 | return false |
| 34 | } |
| 35 | |
Paul Duffin | 976b0e5 | 2021-04-27 23:20:26 +0100 | [diff] [blame] | 36 | func (b hiddenAPIStubsDependencyTag) SdkMemberType(child android.Module) android.SdkMemberType { |
| 37 | // If the module is a java_sdk_library then treat it as if it was specific in the java_sdk_libs |
| 38 | // property, otherwise treat if it was specified in the java_header_libs property. |
| 39 | if javaSdkLibrarySdkMemberType.IsInstance(child) { |
| 40 | return javaSdkLibrarySdkMemberType |
| 41 | } |
| 42 | |
| 43 | return javaHeaderLibsSdkMemberType |
| 44 | } |
| 45 | |
| 46 | func (b hiddenAPIStubsDependencyTag) ExportMember() bool { |
| 47 | // Export the module added via this dependency tag from the sdk. |
| 48 | return true |
| 49 | } |
| 50 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 51 | // Avoid having to make stubs content explicitly visible to dependent modules. |
| 52 | // |
| 53 | // This is a temporary workaround to make it easier to migrate to bootclasspath_fragment modules |
| 54 | // with proper dependencies. |
| 55 | // TODO(b/177892522): Remove this and add needed visibility. |
| 56 | func (b hiddenAPIStubsDependencyTag) ExcludeFromVisibilityEnforcement() { |
| 57 | } |
| 58 | |
| 59 | var _ android.ExcludeFromVisibilityEnforcementTag = hiddenAPIStubsDependencyTag{} |
| 60 | var _ android.ReplaceSourceWithPrebuilt = hiddenAPIStubsDependencyTag{} |
| 61 | var _ android.ExcludeFromApexContentsTag = hiddenAPIStubsDependencyTag{} |
Paul Duffin | 976b0e5 | 2021-04-27 23:20:26 +0100 | [diff] [blame] | 62 | var _ android.SdkMemberTypeDependencyTag = hiddenAPIStubsDependencyTag{} |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 63 | |
Paul Duffin | 3e7fcc3 | 2021-04-15 13:31:38 +0100 | [diff] [blame] | 64 | // hiddenAPIRelevantSdkKinds lists all the android.SdkKind instances that are needed by the hidden |
| 65 | // API processing. |
| 66 | var hiddenAPIRelevantSdkKinds = []android.SdkKind{ |
| 67 | android.SdkPublic, |
| 68 | android.SdkSystem, |
| 69 | android.SdkTest, |
| 70 | android.SdkCorePlatform, |
| 71 | } |
| 72 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 73 | // hiddenAPIComputeMonolithicStubLibModules computes the set of module names that provide stubs |
| 74 | // needed to produce the hidden API monolithic stub flags file. |
| 75 | func hiddenAPIComputeMonolithicStubLibModules(config android.Config) map[android.SdkKind][]string { |
| 76 | var publicStubModules []string |
| 77 | var systemStubModules []string |
| 78 | var testStubModules []string |
| 79 | var corePlatformStubModules []string |
| 80 | |
| 81 | if config.AlwaysUsePrebuiltSdks() { |
| 82 | // Build configuration mandates using prebuilt stub modules |
| 83 | publicStubModules = append(publicStubModules, "sdk_public_current_android") |
| 84 | systemStubModules = append(systemStubModules, "sdk_system_current_android") |
| 85 | testStubModules = append(testStubModules, "sdk_test_current_android") |
| 86 | } else { |
| 87 | // Use stub modules built from source |
| 88 | publicStubModules = append(publicStubModules, "android_stubs_current") |
| 89 | systemStubModules = append(systemStubModules, "android_system_stubs_current") |
| 90 | testStubModules = append(testStubModules, "android_test_stubs_current") |
| 91 | } |
| 92 | // We do not have prebuilts of the core platform api yet |
| 93 | corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs") |
| 94 | |
| 95 | // Allow products to define their own stubs for custom product jars that apps can use. |
| 96 | publicStubModules = append(publicStubModules, config.ProductHiddenAPIStubs()...) |
| 97 | systemStubModules = append(systemStubModules, config.ProductHiddenAPIStubsSystem()...) |
| 98 | testStubModules = append(testStubModules, config.ProductHiddenAPIStubsTest()...) |
| 99 | if config.IsEnvTrue("EMMA_INSTRUMENT") { |
| 100 | publicStubModules = append(publicStubModules, "jacoco-stubs") |
| 101 | } |
| 102 | |
| 103 | m := map[android.SdkKind][]string{} |
| 104 | m[android.SdkPublic] = publicStubModules |
| 105 | m[android.SdkSystem] = systemStubModules |
| 106 | m[android.SdkTest] = testStubModules |
| 107 | m[android.SdkCorePlatform] = corePlatformStubModules |
| 108 | return m |
| 109 | } |
| 110 | |
| 111 | // hiddenAPIAddStubLibDependencies adds dependencies onto the modules specified in |
| 112 | // sdkKindToStubLibModules. It adds them in a well known order and uses an SdkKind specific tag to |
| 113 | // identify the source of the dependency. |
| 114 | func hiddenAPIAddStubLibDependencies(ctx android.BottomUpMutatorContext, sdkKindToStubLibModules map[android.SdkKind][]string) { |
| 115 | module := ctx.Module() |
| 116 | for _, sdkKind := range hiddenAPIRelevantSdkKinds { |
| 117 | modules := sdkKindToStubLibModules[sdkKind] |
| 118 | ctx.AddDependency(module, hiddenAPIStubsDependencyTag{sdkKind: sdkKind}, modules...) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // hiddenAPIGatherStubLibDexJarPaths gathers the paths to the dex jars from the dependencies added |
| 123 | // in hiddenAPIAddStubLibDependencies. |
| 124 | func hiddenAPIGatherStubLibDexJarPaths(ctx android.ModuleContext) map[android.SdkKind]android.Paths { |
| 125 | m := map[android.SdkKind]android.Paths{} |
| 126 | ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) { |
| 127 | tag := ctx.OtherModuleDependencyTag(module) |
| 128 | if hiddenAPIStubsTag, ok := tag.(hiddenAPIStubsDependencyTag); ok { |
| 129 | kind := hiddenAPIStubsTag.sdkKind |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 130 | dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, kind) |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 131 | if dexJar != nil { |
| 132 | m[kind] = append(m[kind], dexJar) |
| 133 | } |
| 134 | } |
| 135 | }) |
| 136 | return m |
| 137 | } |
| 138 | |
| 139 | // hiddenAPIRetrieveDexJarBuildPath retrieves the DexJarBuildPath from the specified module, if |
| 140 | // available, or reports an error. |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 141 | func hiddenAPIRetrieveDexJarBuildPath(ctx android.ModuleContext, module android.Module, kind android.SdkKind) android.Path { |
| 142 | var dexJar android.Path |
| 143 | if sdkLibrary, ok := module.(SdkLibraryDependency); ok { |
| 144 | dexJar = sdkLibrary.SdkApiStubDexJar(ctx, kind) |
| 145 | } else if j, ok := module.(UsesLibraryDependency); ok { |
| 146 | dexJar = j.DexJarBuildPath() |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 147 | } else { |
| 148 | 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] | 149 | return nil |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 150 | } |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 151 | |
| 152 | if dexJar == nil { |
| 153 | ctx.ModuleErrorf("dependency %s does not provide a dex jar, consider setting compile_dex: true", module) |
| 154 | } |
| 155 | return dexJar |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | var sdkKindToHiddenapiListOption = map[android.SdkKind]string{ |
| 159 | android.SdkPublic: "public-stub-classpath", |
| 160 | android.SdkSystem: "system-stub-classpath", |
| 161 | android.SdkTest: "test-stub-classpath", |
| 162 | android.SdkCorePlatform: "core-platform-stub-classpath", |
| 163 | } |
| 164 | |
| 165 | // ruleToGenerateHiddenAPIStubFlagsFile creates a rule to create a hidden API stub flags file. |
| 166 | // |
| 167 | // The rule is initialized but not built so that the caller can modify it and select an appropriate |
| 168 | // name. |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame^] | 169 | 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] | 170 | // Singleton rule which applies hiddenapi on all boot class path dex files. |
| 171 | rule := android.NewRuleBuilder(pctx, ctx) |
| 172 | |
| 173 | tempPath := tempPathForRestat(ctx, outputPath) |
| 174 | |
| 175 | command := rule.Command(). |
| 176 | Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")). |
| 177 | Text("list"). |
| 178 | FlagForEachInput("--boot-dex=", bootDexJars) |
| 179 | |
| 180 | // Iterate over the sdk kinds in a fixed order. |
| 181 | for _, sdkKind := range hiddenAPIRelevantSdkKinds { |
| 182 | paths := sdkKindToPathList[sdkKind] |
| 183 | if len(paths) > 0 { |
| 184 | option := sdkKindToHiddenapiListOption[sdkKind] |
| 185 | command.FlagWithInputList("--"+option+"=", paths, ":") |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // Add the output path. |
| 190 | command.FlagWithOutput("--out-api-flags=", tempPath) |
| 191 | |
| 192 | commitChangeForRestat(rule, tempPath, outputPath) |
| 193 | return rule |
| 194 | } |
| 195 | |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 196 | // HiddenAPIFlagFileProperties contains paths to the flag files that can be used to augment the |
| 197 | // information obtained from annotations within the source code in order to create the complete set |
| 198 | // 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] | 199 | // |
| 200 | // Each property contains a list of paths. With the exception of the Unsupported_packages the paths |
| 201 | // of each property reference a plain text file that contains a java signature per line. The flags |
| 202 | // for each of those signatures will be updated in a property specific way. |
| 203 | // |
| 204 | // The Unsupported_packages property contains a list of paths, each of which is a plain text file |
| 205 | // with one Java package per line. All members of all classes within that package (but not nested |
| 206 | // packages) will be updated in a property specific way. |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 207 | type HiddenAPIFlagFileProperties struct { |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 208 | // Marks each signature in the referenced files as being unsupported. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 209 | Unsupported []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 210 | |
| 211 | // Marks each signature in the referenced files as being unsupported because it has been removed. |
| 212 | // Any conflicts with other flags are ignored. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 213 | Removed []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 214 | |
| 215 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= R |
| 216 | // and low priority. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 217 | Max_target_r_low_priority []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 218 | |
| 219 | // 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] | 220 | Max_target_q []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 221 | |
| 222 | // 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] | 223 | Max_target_p []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 supported only for targetSdkVersion <= O |
| 226 | // and low priority. Any conflicts with other flags are ignored. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 227 | Max_target_o_low_priority []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 blocked. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 230 | Blocked []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 231 | |
| 232 | // 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] | 233 | Unsupported_packages []string `android:"path"` |
| 234 | } |
| 235 | |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 236 | func (p *HiddenAPIFlagFileProperties) hiddenAPIFlagFileInfo(ctx android.ModuleContext) hiddenAPIFlagFileInfo { |
| 237 | info := hiddenAPIFlagFileInfo{categoryToPaths: map[*hiddenAPIFlagFileCategory]android.Paths{}} |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 238 | for _, category := range hiddenAPIFlagFileCategories { |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 239 | paths := android.PathsForModuleSrc(ctx, category.propertyValueReader(p)) |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 240 | info.categoryToPaths[category] = paths |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 241 | } |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 242 | return info |
| 243 | } |
| 244 | |
| 245 | type hiddenAPIFlagFileCategory struct { |
| 246 | // propertyName is the name of the property for this category. |
| 247 | propertyName string |
| 248 | |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 249 | // 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] | 250 | // properties. |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 251 | propertyValueReader func(properties *HiddenAPIFlagFileProperties) []string |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 252 | |
| 253 | // commandMutator adds the appropriate command line options for this category to the supplied |
| 254 | // command |
| 255 | commandMutator func(command *android.RuleBuilderCommand, path android.Path) |
| 256 | } |
| 257 | |
| 258 | var hiddenAPIFlagFileCategories = []*hiddenAPIFlagFileCategory{ |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 259 | // See HiddenAPIFlagFileProperties.Unsupported |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 260 | { |
| 261 | propertyName: "unsupported", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 262 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 263 | return properties.Unsupported |
| 264 | }, |
| 265 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 266 | command.FlagWithInput("--unsupported ", path) |
| 267 | }, |
| 268 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 269 | // See HiddenAPIFlagFileProperties.Removed |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 270 | { |
| 271 | propertyName: "removed", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 272 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 273 | return properties.Removed |
| 274 | }, |
| 275 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 276 | command.FlagWithInput("--unsupported ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed") |
| 277 | }, |
| 278 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 279 | // See HiddenAPIFlagFileProperties.Max_target_r_low_priority |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 280 | { |
| 281 | propertyName: "max_target_r_low_priority", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 282 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 283 | return properties.Max_target_r_low_priority |
| 284 | }, |
| 285 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 286 | command.FlagWithInput("--max-target-r ", path).FlagWithArg("--tag ", "lo-prio") |
| 287 | }, |
| 288 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 289 | // See HiddenAPIFlagFileProperties.Max_target_q |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 290 | { |
| 291 | propertyName: "max_target_q", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 292 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 293 | return properties.Max_target_q |
| 294 | }, |
| 295 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 296 | command.FlagWithInput("--max-target-q ", path) |
| 297 | }, |
| 298 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 299 | // See HiddenAPIFlagFileProperties.Max_target_p |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 300 | { |
| 301 | propertyName: "max_target_p", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 302 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 303 | return properties.Max_target_p |
| 304 | }, |
| 305 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 306 | command.FlagWithInput("--max-target-p ", path) |
| 307 | }, |
| 308 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 309 | // See HiddenAPIFlagFileProperties.Max_target_o_low_priority |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 310 | { |
| 311 | propertyName: "max_target_o_low_priority", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 312 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 313 | return properties.Max_target_o_low_priority |
| 314 | }, |
| 315 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 316 | command.FlagWithInput("--max-target-o ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio") |
| 317 | }, |
| 318 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 319 | // See HiddenAPIFlagFileProperties.Blocked |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 320 | { |
| 321 | propertyName: "blocked", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 322 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 323 | return properties.Blocked |
| 324 | }, |
| 325 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 326 | command.FlagWithInput("--blocked ", path) |
| 327 | }, |
| 328 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 329 | // See HiddenAPIFlagFileProperties.Unsupported_packages |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 330 | { |
| 331 | propertyName: "unsupported_packages", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 332 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 333 | return properties.Unsupported_packages |
| 334 | }, |
| 335 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 336 | command.FlagWithInput("--unsupported ", path).Flag("--packages ") |
| 337 | }, |
| 338 | }, |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame^] | 341 | // hiddenAPIFlagFileInfo contains paths resolved from HiddenAPIFlagFileProperties and also generated |
| 342 | // by hidden API processing. |
| 343 | // |
| 344 | // This is used both for an individual bootclasspath_fragment to provide it to other modules and |
| 345 | // for a module to collate the files from the fragments it depends upon. That is why the fields are |
| 346 | // all Paths even though they are initialized with a single path. |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 347 | type hiddenAPIFlagFileInfo struct { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 348 | // categoryToPaths maps from the flag file category to the paths containing information for that |
| 349 | // category. |
| 350 | categoryToPaths map[*hiddenAPIFlagFileCategory]android.Paths |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame^] | 351 | |
| 352 | // The paths to the generated stub-flags.csv files. |
| 353 | StubFlagsPaths android.Paths |
| 354 | |
| 355 | // The paths to the generated annotation-flags.csv files. |
| 356 | AnnotationFlagsPaths android.Paths |
| 357 | |
| 358 | // The paths to the generated metadata.csv files. |
| 359 | MetadataPaths android.Paths |
| 360 | |
| 361 | // The paths to the generated index.csv files. |
| 362 | IndexPaths android.Paths |
| 363 | |
| 364 | // The paths to the generated all-flags.csv files. |
| 365 | AllFlagsPaths android.Paths |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 366 | } |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 367 | |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 368 | func (i *hiddenAPIFlagFileInfo) append(other hiddenAPIFlagFileInfo) { |
| 369 | for _, category := range hiddenAPIFlagFileCategories { |
| 370 | i.categoryToPaths[category] = append(i.categoryToPaths[category], other.categoryToPaths[category]...) |
| 371 | } |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame^] | 372 | i.StubFlagsPaths = append(i.StubFlagsPaths, other.StubFlagsPaths...) |
| 373 | i.AnnotationFlagsPaths = append(i.AnnotationFlagsPaths, other.AnnotationFlagsPaths...) |
| 374 | i.MetadataPaths = append(i.MetadataPaths, other.MetadataPaths...) |
| 375 | i.IndexPaths = append(i.IndexPaths, other.IndexPaths...) |
| 376 | i.AllFlagsPaths = append(i.AllFlagsPaths, other.AllFlagsPaths...) |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | var hiddenAPIFlagFileInfoProvider = blueprint.NewProvider(hiddenAPIFlagFileInfo{}) |
| 380 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame^] | 381 | // buildRuleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from |
| 382 | // the flags from all the modules, the stub flags, augmented with some additional configuration |
| 383 | // files. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 384 | // |
| 385 | // baseFlagsPath is the path to the flags file containing all the information from the stubs plus |
| 386 | // an entry for every single member in the dex implementation jars of the individual modules. Every |
| 387 | // signature in any of the other files MUST be included in this file. |
| 388 | // |
| 389 | // moduleSpecificFlagsPaths are the paths to the flags files generated by each module using |
| 390 | // information from the baseFlagsPath as well as from annotations within the source. |
| 391 | // |
| 392 | // augmentationInfo is a struct containing paths to files that augment the information provided by |
| 393 | // the moduleSpecificFlagsPaths. |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame^] | 394 | func buildRuleToGenerateHiddenApiFlags(ctx android.BuilderContext, name, desc string, outputPath android.WritablePath, baseFlagsPath android.Path, moduleSpecificFlagsPaths android.Paths, flagFileInfo *hiddenAPIFlagFileInfo) { |
Paul Duffin | d3c1513 | 2021-04-21 22:12:35 +0100 | [diff] [blame] | 395 | tempPath := tempPathForRestat(ctx, outputPath) |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 396 | rule := android.NewRuleBuilder(pctx, ctx) |
| 397 | command := rule.Command(). |
| 398 | BuiltTool("generate_hiddenapi_lists"). |
| 399 | FlagWithInput("--csv ", baseFlagsPath). |
| 400 | Inputs(moduleSpecificFlagsPaths). |
| 401 | FlagWithOutput("--output ", tempPath) |
| 402 | |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 403 | // Add the options for the different categories of flag files. |
| 404 | for _, category := range hiddenAPIFlagFileCategories { |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame^] | 405 | paths := flagFileInfo.categoryToPaths[category] |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 406 | for _, path := range paths { |
| 407 | category.commandMutator(command, path) |
| 408 | } |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | commitChangeForRestat(rule, tempPath, outputPath) |
| 412 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame^] | 413 | rule.Build(name, desc) |
| 414 | } |
| 415 | |
| 416 | // hiddenAPIGenerateAllFlagsForBootclasspathFragment will generate all the flags for a fragment |
| 417 | // of the bootclasspath. |
| 418 | // |
| 419 | // It takes: |
| 420 | // * Map from android.SdkKind to stub dex jar paths defining the API for that sdk kind. |
| 421 | // * The list of modules that are the contents of the fragment. |
| 422 | // * The additional manually curated flag files to use. |
| 423 | // |
| 424 | // It generates: |
| 425 | // * stub-flags.csv |
| 426 | // * annotation-flags.csv |
| 427 | // * metadata.csv |
| 428 | // * index.csv |
| 429 | // * all-flags.csv |
| 430 | func hiddenAPIGenerateAllFlagsForBootclasspathFragment(ctx android.ModuleContext, contents []android.Module, stubJarsByKind map[android.SdkKind]android.Paths, flagFileInfo *hiddenAPIFlagFileInfo) { |
| 431 | |
| 432 | hiddenApiSubDir := "modular-hiddenapi" |
| 433 | |
| 434 | bootDexJars := android.Paths{} |
| 435 | classesJars := android.Paths{} |
| 436 | for _, module := range contents { |
| 437 | if hiddenAPI, ok := module.(hiddenAPIIntf); ok { |
| 438 | classesJars = append(classesJars, hiddenAPI.classesJars()...) |
| 439 | bootDexJar := hiddenAPI.bootDexJar() |
| 440 | if bootDexJar == nil { |
| 441 | ctx.ModuleErrorf("module %s does not provide a dex jar", module) |
| 442 | } else { |
| 443 | bootDexJars = append(bootDexJars, bootDexJar) |
| 444 | } |
| 445 | } else { |
| 446 | ctx.ModuleErrorf("module %s does not implement hiddenAPIIntf", module) |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | // Generate the stub-flags.csv. |
| 451 | stubFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "stub-flags.csv") |
| 452 | rule := ruleToGenerateHiddenAPIStubFlagsFile(ctx, stubFlagsCSV, bootDexJars, stubJarsByKind) |
| 453 | rule.Build("modularHiddenAPIStubFlagsFile", "modular hiddenapi stub flags") |
| 454 | |
| 455 | // Generate the set of flags from the annotations in the source code. |
| 456 | annotationFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "annotation-flags.csv") |
| 457 | ctx.Build(pctx, android.BuildParams{ |
| 458 | Rule: hiddenAPIGenerateCSVRule, |
| 459 | Description: "modular hiddenapi annotation flags", |
| 460 | Inputs: classesJars, |
| 461 | Output: annotationFlagsCSV, |
| 462 | Implicit: stubFlagsCSV, |
| 463 | Args: map[string]string{ |
| 464 | "outFlag": "--write-flags-csv", |
| 465 | "stubAPIFlags": stubFlagsCSV.String(), |
| 466 | }, |
| 467 | }) |
| 468 | |
| 469 | // Generate the metadata from the annotations in the source code. |
| 470 | metadataCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "metadata.csv") |
| 471 | ctx.Build(pctx, android.BuildParams{ |
| 472 | Rule: hiddenAPIGenerateCSVRule, |
| 473 | Description: "modular hiddenapi metadata", |
| 474 | Inputs: classesJars, |
| 475 | Output: metadataCSV, |
| 476 | Implicit: stubFlagsCSV, |
| 477 | Args: map[string]string{ |
| 478 | "outFlag": "--write-metadata-csv", |
| 479 | "stubAPIFlags": stubFlagsCSV.String(), |
| 480 | }, |
| 481 | }) |
| 482 | |
| 483 | // Generate the index file from the annotations in the source code. |
| 484 | indexCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "index.csv") |
| 485 | rule = android.NewRuleBuilder(pctx, ctx) |
| 486 | rule.Command(). |
| 487 | BuiltTool("merge_csv"). |
| 488 | Flag("--zip_input"). |
| 489 | Flag("--key_field signature"). |
| 490 | FlagWithOutput("--output=", indexCSV). |
| 491 | Inputs(classesJars) |
| 492 | rule.Build("modular-hiddenapi-index", "modular hiddenapi index") |
| 493 | |
| 494 | // Removed APIs need to be marked and in order to do that the flagFileInfo needs to specify files |
| 495 | // containing dex signatures of all the removed APIs. In the monolithic files that is done by |
| 496 | // manually combining all the removed.txt files for each API and then converting them to dex |
| 497 | // signatures, see the combined-removed-dex module. That will all be done automatically in future. |
| 498 | // For now removed APIs are ignored. |
| 499 | // TODO(b/179354495): handle removed apis automatically. |
| 500 | |
| 501 | // Generate the all-flags.csv which are the flags that will, in future, be encoded into the dex |
| 502 | // files. |
| 503 | outputPath := android.PathForModuleOut(ctx, hiddenApiSubDir, "all-flags.csv") |
| 504 | buildRuleToGenerateHiddenApiFlags(ctx, "modularHiddenApiAllFlags", "modular hiddenapi all flags", outputPath, stubFlagsCSV, android.Paths{annotationFlagsCSV}, flagFileInfo) |
| 505 | |
| 506 | // Store the paths in the info for use by other modules and sdk snapshot generation. |
| 507 | flagFileInfo.StubFlagsPaths = android.Paths{stubFlagsCSV} |
| 508 | flagFileInfo.AnnotationFlagsPaths = android.Paths{annotationFlagsCSV} |
| 509 | flagFileInfo.MetadataPaths = android.Paths{metadataCSV} |
| 510 | flagFileInfo.IndexPaths = android.Paths{indexCSV} |
| 511 | flagFileInfo.AllFlagsPaths = android.Paths{outputPath} |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 512 | } |