blob: 95d19b96ed270a13a141154d5ad6968b3f07c2b0 [file] [log] [blame]
Paul Duffinbb7f1ac2021-03-29 22:18:45 +01001// 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
15package java
16
17import (
18 "android/soong/android"
19 "android/soong/dexpreopt"
Paul Duffinb432df92021-03-22 22:09:42 +000020 "github.com/google/blueprint"
Paul Duffin62d8c3b2021-04-07 20:35:11 +010021 "github.com/google/blueprint/proptools"
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010022)
23
24func init() {
25 registerPlatformBootclasspathBuildComponents(android.InitRegistrationContext)
26}
27
28func registerPlatformBootclasspathBuildComponents(ctx android.RegistrationContext) {
29 ctx.RegisterModuleType("platform_bootclasspath", platformBootclasspathFactory)
Paul Duffinb432df92021-03-22 22:09:42 +000030
31 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
32 ctx.BottomUp("platform_bootclasspath_deps", platformBootclasspathDepsMutator)
33 })
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010034}
35
Paul Duffinb432df92021-03-22 22:09:42 +000036type 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.
47func (t platformBootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() {
48}
49
50// The tag used for the dependency between the platform bootclasspath and any configured boot jars.
51var platformBootclasspathModuleDepTag = platformBootclasspathDependencyTag{name: "module"}
52
Paul Duffin62d8c3b2021-04-07 20:35:11 +010053// The tag used for the dependency between the platform bootclasspath and bootclasspath_fragments.
54var platformBootclasspathFragmentDepTag = platformBootclasspathDependencyTag{name: "fragment"}
55
Paul Duffinb432df92021-03-22 22:09:42 +000056var _ android.ExcludeFromVisibilityEnforcementTag = platformBootclasspathDependencyTag{}
57
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010058type platformBootclasspathModule struct {
59 android.ModuleBase
Paul Duffinb432df92021-03-22 22:09:42 +000060
Paul Duffin62d8c3b2021-04-07 20:35:11 +010061 properties platformBootclasspathProperties
62
Paul Duffinb432df92021-03-22 22:09:42 +000063 // The apex:module pairs obtained from the configured modules.
64 //
65 // Currently only for testing.
66 configuredModules []android.Module
Paul Duffin62d8c3b2021-04-07 20:35:11 +010067
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.
75type 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
86type platformBootclasspathProperties struct {
87
88 // The names of the bootclasspath_fragment modules that form part of this
89 // platform_bootclasspath.
90 Fragments []ApexVariantReference
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010091}
92
93func platformBootclasspathFactory() android.Module {
94 m := &platformBootclasspathModule{}
Paul Duffin62d8c3b2021-04-07 20:35:11 +010095 m.AddProperties(&m.properties)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010096 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
97 return m
98}
99
100func (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 Duffinb432df92021-03-22 22:09:42 +0000110func 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 Duffin62d8c3b2021-04-07 20:35:11 +0100125
126 // Add dependencies on all the fragments.
127 addDependencyOntoApexVariants(ctx, "fragments", p.properties.Fragments, platformBootclasspathFragmentDepTag)
128 }
129}
130
131func 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 Duffinb432df92021-03-22 22:09:42 +0000142 }
143}
144
145func 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
175func 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 Duffinbb7f1ac2021-03-29 22:18:45 +0100184func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffinb432df92021-03-22 22:09:42 +0000185 ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) {
186 tag := ctx.OtherModuleDependencyTag(module)
187 if tag == platformBootclasspathModuleDepTag {
188 b.configuredModules = append(b.configuredModules, module)
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100189 } else if tag == platformBootclasspathFragmentDepTag {
190 b.fragments = append(b.fragments, module)
Paul Duffinb432df92021-03-22 22:09:42 +0000191 }
192 })
193
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100194 // 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
215func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
216 return defaultBootImageConfig(ctx)
217}