Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 1 | // Copyright 2021 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame^] | 18 | "fmt" |
| 19 | |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 20 | "android/soong/android" |
| 21 | "android/soong/dexpreopt" |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func init() { |
| 27 | registerPlatformBootclasspathBuildComponents(android.InitRegistrationContext) |
| 28 | } |
| 29 | |
| 30 | func registerPlatformBootclasspathBuildComponents(ctx android.RegistrationContext) { |
| 31 | ctx.RegisterModuleType("platform_bootclasspath", platformBootclasspathFactory) |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 32 | |
| 33 | ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 34 | ctx.BottomUp("platform_bootclasspath_deps", platformBootclasspathDepsMutator) |
| 35 | }) |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 38 | type platformBootclasspathDependencyTag struct { |
| 39 | blueprint.BaseDependencyTag |
| 40 | |
| 41 | name string |
| 42 | } |
| 43 | |
| 44 | // Avoid having to make platform bootclasspath content visible to the platform bootclasspath. |
| 45 | // |
| 46 | // This is a temporary workaround to make it easier to migrate to platform bootclasspath with proper |
| 47 | // dependencies. |
| 48 | // TODO(b/177892522): Remove this and add needed visibility. |
| 49 | func (t platformBootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() { |
| 50 | } |
| 51 | |
| 52 | // The tag used for the dependency between the platform bootclasspath and any configured boot jars. |
| 53 | var platformBootclasspathModuleDepTag = platformBootclasspathDependencyTag{name: "module"} |
| 54 | |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 55 | // The tag used for the dependency between the platform bootclasspath and bootclasspath_fragments. |
| 56 | var platformBootclasspathFragmentDepTag = platformBootclasspathDependencyTag{name: "fragment"} |
| 57 | |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 58 | var _ android.ExcludeFromVisibilityEnforcementTag = platformBootclasspathDependencyTag{} |
| 59 | |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 60 | type platformBootclasspathModule struct { |
| 61 | android.ModuleBase |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 62 | |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 63 | properties platformBootclasspathProperties |
| 64 | |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 65 | // The apex:module pairs obtained from the configured modules. |
| 66 | // |
| 67 | // Currently only for testing. |
| 68 | configuredModules []android.Module |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 69 | |
| 70 | // The apex:module pairs obtained from the fragments. |
| 71 | // |
| 72 | // Currently only for testing. |
| 73 | fragments []android.Module |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame^] | 74 | |
| 75 | // Path to the monolithic hiddenapi-flags.csv file. |
| 76 | hiddenAPIFlagsCSV android.Path |
| 77 | |
| 78 | // Path to the monolithic hiddenapi-index.csv file. |
| 79 | hiddenAPIIndexCSV android.Path |
| 80 | |
| 81 | // Path to the monolithic hiddenapi-unsupported.csv file. |
| 82 | hiddenAPIMetadataCSV android.Path |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // ApexVariantReference specifies a particular apex variant of a module. |
| 86 | type ApexVariantReference struct { |
| 87 | // The name of the module apex variant, i.e. the apex containing the module variant. |
| 88 | // |
| 89 | // If this is not specified then it defaults to "platform" which will cause a dependency to be |
| 90 | // added to the module's platform variant. |
| 91 | Apex *string |
| 92 | |
| 93 | // The name of the module. |
| 94 | Module *string |
| 95 | } |
| 96 | |
| 97 | type platformBootclasspathProperties struct { |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 98 | // The names of the bootclasspath_fragment modules that form part of this |
| 99 | // platform_bootclasspath. |
| 100 | Fragments []ApexVariantReference |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 101 | |
| 102 | Hidden_api HiddenAPIAugmentationProperties |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | func platformBootclasspathFactory() android.Module { |
| 106 | m := &platformBootclasspathModule{} |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 107 | m.AddProperties(&m.properties) |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 108 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 109 | return m |
| 110 | } |
| 111 | |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame^] | 112 | var _ android.OutputFileProducer = (*platformBootclasspathModule)(nil) |
| 113 | |
| 114 | // A minimal AndroidMkEntries is needed in order to support the dists property. |
| 115 | func (b *platformBootclasspathModule) AndroidMkEntries() []android.AndroidMkEntries { |
| 116 | return []android.AndroidMkEntries{ |
| 117 | { |
| 118 | Class: "FAKE", |
| 119 | // Need at least one output file in order for this to take effect. |
| 120 | OutputFile: android.OptionalPathForPath(b.hiddenAPIFlagsCSV), |
| 121 | Include: "$(BUILD_PHONY_PACKAGE)", |
| 122 | }, |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Make the hidden API files available from the platform-bootclasspath module. |
| 127 | func (b *platformBootclasspathModule) OutputFiles(tag string) (android.Paths, error) { |
| 128 | switch tag { |
| 129 | case "hiddenapi-flags.csv": |
| 130 | return android.Paths{b.hiddenAPIFlagsCSV}, nil |
| 131 | case "hiddenapi-index.csv": |
| 132 | return android.Paths{b.hiddenAPIIndexCSV}, nil |
| 133 | case "hiddenapi-metadata.csv": |
| 134 | return android.Paths{b.hiddenAPIMetadataCSV}, nil |
| 135 | } |
| 136 | |
| 137 | return nil, fmt.Errorf("unknown tag %s", tag) |
| 138 | } |
| 139 | |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 140 | func (b *platformBootclasspathModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 141 | if SkipDexpreoptBootJars(ctx) { |
| 142 | return |
| 143 | } |
| 144 | |
| 145 | // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The |
| 146 | // path is retrieved from the dependency by GetGlobalSoongConfig(ctx). |
| 147 | dexpreopt.RegisterToolDeps(ctx) |
| 148 | } |
| 149 | |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 150 | func platformBootclasspathDepsMutator(ctx android.BottomUpMutatorContext) { |
| 151 | m := ctx.Module() |
| 152 | if p, ok := m.(*platformBootclasspathModule); ok { |
| 153 | // Add dependencies on all the modules configured in the "art" boot image. |
| 154 | artImageConfig := genBootImageConfigs(ctx)[artBootImageName] |
| 155 | addDependenciesOntoBootImageModules(ctx, artImageConfig.modules) |
| 156 | |
| 157 | // Add dependencies on all the modules configured in the "boot" boot image. That does not |
| 158 | // include modules configured in the "art" boot image. |
| 159 | bootImageConfig := p.getImageConfig(ctx) |
| 160 | addDependenciesOntoBootImageModules(ctx, bootImageConfig.modules) |
| 161 | |
| 162 | // Add dependencies on all the updatable modules. |
| 163 | updatableModules := dexpreopt.GetGlobalConfig(ctx).UpdatableBootJars |
| 164 | addDependenciesOntoBootImageModules(ctx, updatableModules) |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 165 | |
| 166 | // Add dependencies on all the fragments. |
| 167 | addDependencyOntoApexVariants(ctx, "fragments", p.properties.Fragments, platformBootclasspathFragmentDepTag) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func addDependencyOntoApexVariants(ctx android.BottomUpMutatorContext, propertyName string, refs []ApexVariantReference, tag blueprint.DependencyTag) { |
| 172 | for i, ref := range refs { |
| 173 | apex := proptools.StringDefault(ref.Apex, "platform") |
| 174 | |
| 175 | if ref.Module == nil { |
| 176 | ctx.PropertyErrorf(propertyName, "missing module name at position %d", i) |
| 177 | continue |
| 178 | } |
| 179 | name := proptools.String(ref.Module) |
| 180 | |
| 181 | addDependencyOntoApexModulePair(ctx, apex, name, tag) |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | func addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tag blueprint.DependencyTag) { |
| 186 | var variations []blueprint.Variation |
| 187 | if apex != "platform" { |
| 188 | // Pick the correct apex variant. |
| 189 | variations = []blueprint.Variation{ |
| 190 | {Mutator: "apex", Variation: apex}, |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | addedDep := false |
| 195 | if ctx.OtherModuleDependencyVariantExists(variations, name) { |
| 196 | ctx.AddFarVariationDependencies(variations, tag, name) |
| 197 | addedDep = true |
| 198 | } |
| 199 | |
| 200 | // Add a dependency on the prebuilt module if it exists. |
| 201 | prebuiltName := android.PrebuiltNameFromSource(name) |
| 202 | if ctx.OtherModuleDependencyVariantExists(variations, prebuiltName) { |
| 203 | ctx.AddVariationDependencies(variations, tag, prebuiltName) |
| 204 | addedDep = true |
| 205 | } |
| 206 | |
| 207 | // If no appropriate variant existing for this, so no dependency could be added, then it is an |
| 208 | // error, unless missing dependencies are allowed. The simplest way to handle that is to add a |
| 209 | // dependency that will not be satisfied and the default behavior will handle it. |
| 210 | if !addedDep { |
| 211 | ctx.AddFarVariationDependencies(variations, tag, name) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList) { |
| 216 | for i := 0; i < modules.Len(); i++ { |
| 217 | apex := modules.Apex(i) |
| 218 | name := modules.Jar(i) |
| 219 | |
| 220 | addDependencyOntoApexModulePair(ctx, apex, name, platformBootclasspathModuleDepTag) |
| 221 | } |
| 222 | } |
| 223 | |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 224 | func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 225 | ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) { |
| 226 | tag := ctx.OtherModuleDependencyTag(module) |
| 227 | if tag == platformBootclasspathModuleDepTag { |
| 228 | b.configuredModules = append(b.configuredModules, module) |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 229 | } else if tag == platformBootclasspathFragmentDepTag { |
| 230 | b.fragments = append(b.fragments, module) |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 231 | } |
| 232 | }) |
| 233 | |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 234 | b.generateHiddenAPIBuildActions(ctx, b.configuredModules) |
| 235 | |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 236 | // Nothing to do if skipping the dexpreopt of boot image jars. |
| 237 | if SkipDexpreoptBootJars(ctx) { |
| 238 | return |
| 239 | } |
| 240 | |
| 241 | // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars |
| 242 | // GenerateSingletonBuildActions method as it cannot create it for itself. |
| 243 | dexpreopt.GetGlobalSoongConfig(ctx) |
| 244 | |
| 245 | imageConfig := b.getImageConfig(ctx) |
| 246 | if imageConfig == nil { |
| 247 | return |
| 248 | } |
| 249 | |
| 250 | // Construct the boot image info from the config. |
| 251 | info := BootImageInfo{imageConfig: imageConfig} |
| 252 | |
| 253 | // Make it available for other modules. |
| 254 | ctx.SetProvider(BootImageInfoProvider, info) |
| 255 | } |
| 256 | |
| 257 | func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig { |
| 258 | return defaultBootImageConfig(ctx) |
| 259 | } |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 260 | |
| 261 | // generateHiddenAPIBuildActions generates all the hidden API related build rules. |
| 262 | func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module) { |
| 263 | |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame^] | 264 | // Save the paths to the monolithic files for retrieval via OutputFiles() |
| 265 | // Make the paths relative to the out/soong/hiddenapi directory instead of to the out/soong/ |
| 266 | // directory. This ensures that if they are used as java_resources they do not end up in a |
| 267 | // hiddenapi directory in the resulting APK. |
| 268 | relToHiddenapiDir := func(path android.OutputPath) android.Path { |
| 269 | return path |
| 270 | } |
| 271 | b.hiddenAPIFlagsCSV = relToHiddenapiDir(hiddenAPISingletonPaths(ctx).flags) |
| 272 | b.hiddenAPIIndexCSV = relToHiddenapiDir(hiddenAPISingletonPaths(ctx).index) |
| 273 | b.hiddenAPIMetadataCSV = relToHiddenapiDir(hiddenAPISingletonPaths(ctx).metadata) |
| 274 | |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 275 | moduleSpecificFlagsPaths := android.Paths{} |
| 276 | for _, module := range modules { |
| 277 | if h, ok := module.(hiddenAPIIntf); ok { |
| 278 | if csv := h.flagsCSV(); csv != nil { |
| 279 | moduleSpecificFlagsPaths = append(moduleSpecificFlagsPaths, csv) |
| 280 | } |
| 281 | } else { |
| 282 | ctx.ModuleErrorf("module %s of type %s does not implement hiddenAPIIntf", module, ctx.OtherModuleType(module)) |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | augmentationInfo := b.properties.Hidden_api.hiddenAPIAugmentationInfo(ctx) |
| 287 | |
| 288 | outputPath := hiddenAPISingletonPaths(ctx).flags |
| 289 | baseFlagsPath := hiddenAPISingletonPaths(ctx).stubFlags |
| 290 | ruleToGenerateHiddenApiFlags(ctx, outputPath, baseFlagsPath, moduleSpecificFlagsPaths, augmentationInfo) |
| 291 | } |