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 |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame^] | 62 | ClasspathFragmentBase |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 63 | |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 64 | properties platformBootclasspathProperties |
| 65 | |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 66 | // The apex:module pairs obtained from the configured modules. |
| 67 | // |
| 68 | // Currently only for testing. |
| 69 | configuredModules []android.Module |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 70 | |
| 71 | // The apex:module pairs obtained from the fragments. |
| 72 | // |
| 73 | // Currently only for testing. |
| 74 | fragments []android.Module |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 75 | |
| 76 | // Path to the monolithic hiddenapi-flags.csv file. |
Paul Duffin | 0b65986 | 2021-04-13 13:02:29 +0100 | [diff] [blame] | 77 | hiddenAPIFlagsCSV android.OutputPath |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 78 | |
| 79 | // Path to the monolithic hiddenapi-index.csv file. |
Paul Duffin | 0b65986 | 2021-04-13 13:02:29 +0100 | [diff] [blame] | 80 | hiddenAPIIndexCSV android.OutputPath |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 81 | |
| 82 | // Path to the monolithic hiddenapi-unsupported.csv file. |
Paul Duffin | 0b65986 | 2021-04-13 13:02:29 +0100 | [diff] [blame] | 83 | hiddenAPIMetadataCSV android.OutputPath |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // ApexVariantReference specifies a particular apex variant of a module. |
| 87 | type ApexVariantReference struct { |
| 88 | // The name of the module apex variant, i.e. the apex containing the module variant. |
| 89 | // |
| 90 | // If this is not specified then it defaults to "platform" which will cause a dependency to be |
| 91 | // added to the module's platform variant. |
| 92 | Apex *string |
| 93 | |
| 94 | // The name of the module. |
| 95 | Module *string |
| 96 | } |
| 97 | |
| 98 | type platformBootclasspathProperties struct { |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 99 | // The names of the bootclasspath_fragment modules that form part of this |
| 100 | // platform_bootclasspath. |
| 101 | Fragments []ApexVariantReference |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 102 | |
| 103 | Hidden_api HiddenAPIAugmentationProperties |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | func platformBootclasspathFactory() android.Module { |
| 107 | m := &platformBootclasspathModule{} |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 108 | m.AddProperties(&m.properties) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame^] | 109 | // TODO(satayev): split systemserver and apex jars into separate configs. |
| 110 | initClasspathFragment(m) |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 111 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 112 | return m |
| 113 | } |
| 114 | |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 115 | var _ android.OutputFileProducer = (*platformBootclasspathModule)(nil) |
| 116 | |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame^] | 117 | func (b *platformBootclasspathModule) AndroidMkEntries() (entries []android.AndroidMkEntries) { |
| 118 | entries = append(entries, android.AndroidMkEntries{ |
| 119 | Class: "FAKE", |
| 120 | // Need at least one output file in order for this to take effect. |
| 121 | OutputFile: android.OptionalPathForPath(b.hiddenAPIFlagsCSV), |
| 122 | Include: "$(BUILD_PHONY_PACKAGE)", |
| 123 | }) |
| 124 | entries = append(entries, b.classpathFragmentBase().getAndroidMkEntries()...) |
| 125 | return |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Make the hidden API files available from the platform-bootclasspath module. |
| 129 | func (b *platformBootclasspathModule) OutputFiles(tag string) (android.Paths, error) { |
| 130 | switch tag { |
| 131 | case "hiddenapi-flags.csv": |
| 132 | return android.Paths{b.hiddenAPIFlagsCSV}, nil |
| 133 | case "hiddenapi-index.csv": |
| 134 | return android.Paths{b.hiddenAPIIndexCSV}, nil |
| 135 | case "hiddenapi-metadata.csv": |
| 136 | return android.Paths{b.hiddenAPIMetadataCSV}, nil |
| 137 | } |
| 138 | |
| 139 | return nil, fmt.Errorf("unknown tag %s", tag) |
| 140 | } |
| 141 | |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 142 | func (b *platformBootclasspathModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 143 | if SkipDexpreoptBootJars(ctx) { |
| 144 | return |
| 145 | } |
| 146 | |
| 147 | // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The |
| 148 | // path is retrieved from the dependency by GetGlobalSoongConfig(ctx). |
| 149 | dexpreopt.RegisterToolDeps(ctx) |
| 150 | } |
| 151 | |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 152 | func platformBootclasspathDepsMutator(ctx android.BottomUpMutatorContext) { |
| 153 | m := ctx.Module() |
| 154 | if p, ok := m.(*platformBootclasspathModule); ok { |
| 155 | // Add dependencies on all the modules configured in the "art" boot image. |
| 156 | artImageConfig := genBootImageConfigs(ctx)[artBootImageName] |
| 157 | addDependenciesOntoBootImageModules(ctx, artImageConfig.modules) |
| 158 | |
| 159 | // Add dependencies on all the modules configured in the "boot" boot image. That does not |
| 160 | // include modules configured in the "art" boot image. |
| 161 | bootImageConfig := p.getImageConfig(ctx) |
| 162 | addDependenciesOntoBootImageModules(ctx, bootImageConfig.modules) |
| 163 | |
| 164 | // Add dependencies on all the updatable modules. |
| 165 | updatableModules := dexpreopt.GetGlobalConfig(ctx).UpdatableBootJars |
| 166 | addDependenciesOntoBootImageModules(ctx, updatableModules) |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 167 | |
| 168 | // Add dependencies on all the fragments. |
| 169 | addDependencyOntoApexVariants(ctx, "fragments", p.properties.Fragments, platformBootclasspathFragmentDepTag) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | func addDependencyOntoApexVariants(ctx android.BottomUpMutatorContext, propertyName string, refs []ApexVariantReference, tag blueprint.DependencyTag) { |
| 174 | for i, ref := range refs { |
| 175 | apex := proptools.StringDefault(ref.Apex, "platform") |
| 176 | |
| 177 | if ref.Module == nil { |
| 178 | ctx.PropertyErrorf(propertyName, "missing module name at position %d", i) |
| 179 | continue |
| 180 | } |
| 181 | name := proptools.String(ref.Module) |
| 182 | |
| 183 | addDependencyOntoApexModulePair(ctx, apex, name, tag) |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | func addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tag blueprint.DependencyTag) { |
| 188 | var variations []blueprint.Variation |
| 189 | if apex != "platform" { |
| 190 | // Pick the correct apex variant. |
| 191 | variations = []blueprint.Variation{ |
| 192 | {Mutator: "apex", Variation: apex}, |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | addedDep := false |
| 197 | if ctx.OtherModuleDependencyVariantExists(variations, name) { |
| 198 | ctx.AddFarVariationDependencies(variations, tag, name) |
| 199 | addedDep = true |
| 200 | } |
| 201 | |
| 202 | // Add a dependency on the prebuilt module if it exists. |
| 203 | prebuiltName := android.PrebuiltNameFromSource(name) |
| 204 | if ctx.OtherModuleDependencyVariantExists(variations, prebuiltName) { |
| 205 | ctx.AddVariationDependencies(variations, tag, prebuiltName) |
| 206 | addedDep = true |
| 207 | } |
| 208 | |
| 209 | // If no appropriate variant existing for this, so no dependency could be added, then it is an |
| 210 | // error, unless missing dependencies are allowed. The simplest way to handle that is to add a |
| 211 | // dependency that will not be satisfied and the default behavior will handle it. |
| 212 | if !addedDep { |
| 213 | ctx.AddFarVariationDependencies(variations, tag, name) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList) { |
| 218 | for i := 0; i < modules.Len(); i++ { |
| 219 | apex := modules.Apex(i) |
| 220 | name := modules.Jar(i) |
| 221 | |
| 222 | addDependencyOntoApexModulePair(ctx, apex, name, platformBootclasspathModuleDepTag) |
| 223 | } |
| 224 | } |
| 225 | |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 226 | func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame^] | 227 | b.classpathFragmentBase().generateAndroidBuildActions(ctx) |
| 228 | |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 229 | ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) { |
| 230 | tag := ctx.OtherModuleDependencyTag(module) |
| 231 | if tag == platformBootclasspathModuleDepTag { |
| 232 | b.configuredModules = append(b.configuredModules, module) |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 233 | } else if tag == platformBootclasspathFragmentDepTag { |
| 234 | b.fragments = append(b.fragments, module) |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 235 | } |
| 236 | }) |
| 237 | |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 238 | b.generateHiddenAPIBuildActions(ctx, b.configuredModules) |
| 239 | |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 240 | // Nothing to do if skipping the dexpreopt of boot image jars. |
| 241 | if SkipDexpreoptBootJars(ctx) { |
| 242 | return |
| 243 | } |
| 244 | |
| 245 | // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars |
| 246 | // GenerateSingletonBuildActions method as it cannot create it for itself. |
| 247 | dexpreopt.GetGlobalSoongConfig(ctx) |
| 248 | |
| 249 | imageConfig := b.getImageConfig(ctx) |
| 250 | if imageConfig == nil { |
| 251 | return |
| 252 | } |
| 253 | |
| 254 | // Construct the boot image info from the config. |
| 255 | info := BootImageInfo{imageConfig: imageConfig} |
| 256 | |
| 257 | // Make it available for other modules. |
| 258 | ctx.SetProvider(BootImageInfoProvider, info) |
| 259 | } |
| 260 | |
| 261 | func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig { |
| 262 | return defaultBootImageConfig(ctx) |
| 263 | } |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 264 | |
| 265 | // generateHiddenAPIBuildActions generates all the hidden API related build rules. |
| 266 | func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module) { |
| 267 | |
Paul Duffin | 90b8ad3 | 2021-04-13 12:25:01 +0100 | [diff] [blame] | 268 | // Save the paths to the monolithic files for retrieval via OutputFiles(). |
| 269 | b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags |
| 270 | b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index |
| 271 | b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 272 | |
Paul Duffin | 0b65986 | 2021-04-13 13:02:29 +0100 | [diff] [blame] | 273 | // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true. This is a performance |
| 274 | // optimization that can be used to reduce the incremental build time but as its name suggests it |
| 275 | // can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath. |
| 276 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 277 | paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV} |
| 278 | for _, path := range paths { |
| 279 | ctx.Build(pctx, android.BuildParams{ |
| 280 | Rule: android.Touch, |
| 281 | Output: path, |
| 282 | }) |
| 283 | } |
| 284 | return |
| 285 | } |
| 286 | |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 287 | moduleSpecificFlagsPaths := android.Paths{} |
| 288 | for _, module := range modules { |
| 289 | if h, ok := module.(hiddenAPIIntf); ok { |
| 290 | if csv := h.flagsCSV(); csv != nil { |
| 291 | moduleSpecificFlagsPaths = append(moduleSpecificFlagsPaths, csv) |
| 292 | } |
| 293 | } else { |
| 294 | ctx.ModuleErrorf("module %s of type %s does not implement hiddenAPIIntf", module, ctx.OtherModuleType(module)) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | augmentationInfo := b.properties.Hidden_api.hiddenAPIAugmentationInfo(ctx) |
| 299 | |
| 300 | outputPath := hiddenAPISingletonPaths(ctx).flags |
| 301 | baseFlagsPath := hiddenAPISingletonPaths(ctx).stubFlags |
| 302 | ruleToGenerateHiddenApiFlags(ctx, outputPath, baseFlagsPath, moduleSpecificFlagsPaths, augmentationInfo) |
| 303 | } |