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 ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/dexpreopt" |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 20 | "github.com/google/blueprint" |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 21 | "github.com/google/blueprint/proptools" |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | func init() { |
| 25 | registerPlatformBootclasspathBuildComponents(android.InitRegistrationContext) |
| 26 | } |
| 27 | |
| 28 | func registerPlatformBootclasspathBuildComponents(ctx android.RegistrationContext) { |
| 29 | ctx.RegisterModuleType("platform_bootclasspath", platformBootclasspathFactory) |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 30 | |
| 31 | ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 32 | ctx.BottomUp("platform_bootclasspath_deps", platformBootclasspathDepsMutator) |
| 33 | }) |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 36 | type platformBootclasspathDependencyTag struct { |
| 37 | blueprint.BaseDependencyTag |
| 38 | |
| 39 | name string |
| 40 | } |
| 41 | |
| 42 | // Avoid having to make platform bootclasspath content visible to the platform bootclasspath. |
| 43 | // |
| 44 | // This is a temporary workaround to make it easier to migrate to platform bootclasspath with proper |
| 45 | // dependencies. |
| 46 | // TODO(b/177892522): Remove this and add needed visibility. |
| 47 | func (t platformBootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() { |
| 48 | } |
| 49 | |
| 50 | // The tag used for the dependency between the platform bootclasspath and any configured boot jars. |
| 51 | var platformBootclasspathModuleDepTag = platformBootclasspathDependencyTag{name: "module"} |
| 52 | |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 53 | // The tag used for the dependency between the platform bootclasspath and bootclasspath_fragments. |
| 54 | var platformBootclasspathFragmentDepTag = platformBootclasspathDependencyTag{name: "fragment"} |
| 55 | |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 56 | var _ android.ExcludeFromVisibilityEnforcementTag = platformBootclasspathDependencyTag{} |
| 57 | |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 58 | type platformBootclasspathModule struct { |
| 59 | android.ModuleBase |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 60 | |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 61 | properties platformBootclasspathProperties |
| 62 | |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 63 | // The apex:module pairs obtained from the configured modules. |
| 64 | // |
| 65 | // Currently only for testing. |
| 66 | configuredModules []android.Module |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 67 | |
| 68 | // The apex:module pairs obtained from the fragments. |
| 69 | // |
| 70 | // Currently only for testing. |
| 71 | fragments []android.Module |
| 72 | } |
| 73 | |
| 74 | // ApexVariantReference specifies a particular apex variant of a module. |
| 75 | type ApexVariantReference struct { |
| 76 | // The name of the module apex variant, i.e. the apex containing the module variant. |
| 77 | // |
| 78 | // If this is not specified then it defaults to "platform" which will cause a dependency to be |
| 79 | // added to the module's platform variant. |
| 80 | Apex *string |
| 81 | |
| 82 | // The name of the module. |
| 83 | Module *string |
| 84 | } |
| 85 | |
| 86 | type platformBootclasspathProperties struct { |
| 87 | |
| 88 | // The names of the bootclasspath_fragment modules that form part of this |
| 89 | // platform_bootclasspath. |
| 90 | Fragments []ApexVariantReference |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | func platformBootclasspathFactory() android.Module { |
| 94 | m := &platformBootclasspathModule{} |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 95 | m.AddProperties(&m.properties) |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 96 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 97 | return m |
| 98 | } |
| 99 | |
| 100 | func (b *platformBootclasspathModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 101 | if SkipDexpreoptBootJars(ctx) { |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The |
| 106 | // path is retrieved from the dependency by GetGlobalSoongConfig(ctx). |
| 107 | dexpreopt.RegisterToolDeps(ctx) |
| 108 | } |
| 109 | |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 110 | func platformBootclasspathDepsMutator(ctx android.BottomUpMutatorContext) { |
| 111 | m := ctx.Module() |
| 112 | if p, ok := m.(*platformBootclasspathModule); ok { |
| 113 | // Add dependencies on all the modules configured in the "art" boot image. |
| 114 | artImageConfig := genBootImageConfigs(ctx)[artBootImageName] |
| 115 | addDependenciesOntoBootImageModules(ctx, artImageConfig.modules) |
| 116 | |
| 117 | // Add dependencies on all the modules configured in the "boot" boot image. That does not |
| 118 | // include modules configured in the "art" boot image. |
| 119 | bootImageConfig := p.getImageConfig(ctx) |
| 120 | addDependenciesOntoBootImageModules(ctx, bootImageConfig.modules) |
| 121 | |
| 122 | // Add dependencies on all the updatable modules. |
| 123 | updatableModules := dexpreopt.GetGlobalConfig(ctx).UpdatableBootJars |
| 124 | addDependenciesOntoBootImageModules(ctx, updatableModules) |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 125 | |
| 126 | // Add dependencies on all the fragments. |
| 127 | addDependencyOntoApexVariants(ctx, "fragments", p.properties.Fragments, platformBootclasspathFragmentDepTag) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func addDependencyOntoApexVariants(ctx android.BottomUpMutatorContext, propertyName string, refs []ApexVariantReference, tag blueprint.DependencyTag) { |
| 132 | for i, ref := range refs { |
| 133 | apex := proptools.StringDefault(ref.Apex, "platform") |
| 134 | |
| 135 | if ref.Module == nil { |
| 136 | ctx.PropertyErrorf(propertyName, "missing module name at position %d", i) |
| 137 | continue |
| 138 | } |
| 139 | name := proptools.String(ref.Module) |
| 140 | |
| 141 | addDependencyOntoApexModulePair(ctx, apex, name, tag) |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | func addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tag blueprint.DependencyTag) { |
| 146 | var variations []blueprint.Variation |
| 147 | if apex != "platform" { |
| 148 | // Pick the correct apex variant. |
| 149 | variations = []blueprint.Variation{ |
| 150 | {Mutator: "apex", Variation: apex}, |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | addedDep := false |
| 155 | if ctx.OtherModuleDependencyVariantExists(variations, name) { |
| 156 | ctx.AddFarVariationDependencies(variations, tag, name) |
| 157 | addedDep = true |
| 158 | } |
| 159 | |
| 160 | // Add a dependency on the prebuilt module if it exists. |
| 161 | prebuiltName := android.PrebuiltNameFromSource(name) |
| 162 | if ctx.OtherModuleDependencyVariantExists(variations, prebuiltName) { |
| 163 | ctx.AddVariationDependencies(variations, tag, prebuiltName) |
| 164 | addedDep = true |
| 165 | } |
| 166 | |
| 167 | // If no appropriate variant existing for this, so no dependency could be added, then it is an |
| 168 | // error, unless missing dependencies are allowed. The simplest way to handle that is to add a |
| 169 | // dependency that will not be satisfied and the default behavior will handle it. |
| 170 | if !addedDep { |
| 171 | ctx.AddFarVariationDependencies(variations, tag, name) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList) { |
| 176 | for i := 0; i < modules.Len(); i++ { |
| 177 | apex := modules.Apex(i) |
| 178 | name := modules.Jar(i) |
| 179 | |
| 180 | addDependencyOntoApexModulePair(ctx, apex, name, platformBootclasspathModuleDepTag) |
| 181 | } |
| 182 | } |
| 183 | |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 184 | func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 185 | ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) { |
| 186 | tag := ctx.OtherModuleDependencyTag(module) |
| 187 | if tag == platformBootclasspathModuleDepTag { |
| 188 | b.configuredModules = append(b.configuredModules, module) |
Paul Duffin | 62d8c3b | 2021-04-07 20:35:11 +0100 | [diff] [blame] | 189 | } else if tag == platformBootclasspathFragmentDepTag { |
| 190 | b.fragments = append(b.fragments, module) |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 191 | } |
| 192 | }) |
| 193 | |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 194 | // Nothing to do if skipping the dexpreopt of boot image jars. |
| 195 | if SkipDexpreoptBootJars(ctx) { |
| 196 | return |
| 197 | } |
| 198 | |
| 199 | // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars |
| 200 | // GenerateSingletonBuildActions method as it cannot create it for itself. |
| 201 | dexpreopt.GetGlobalSoongConfig(ctx) |
| 202 | |
| 203 | imageConfig := b.getImageConfig(ctx) |
| 204 | if imageConfig == nil { |
| 205 | return |
| 206 | } |
| 207 | |
| 208 | // Construct the boot image info from the config. |
| 209 | info := BootImageInfo{imageConfig: imageConfig} |
| 210 | |
| 211 | // Make it available for other modules. |
| 212 | ctx.SetProvider(BootImageInfoProvider, info) |
| 213 | } |
| 214 | |
| 215 | func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig { |
| 216 | return defaultBootImageConfig(ctx) |
| 217 | } |