blob: fa13332dc6658a3fbcaf6efbbcbae5caa02275c8 [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 (
Paul Duffin6a766452021-04-12 14:15:22 +010018 "fmt"
19
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010020 "android/soong/android"
21 "android/soong/dexpreopt"
Paul Duffinb432df92021-03-22 22:09:42 +000022 "github.com/google/blueprint"
Paul Duffin62d8c3b2021-04-07 20:35:11 +010023 "github.com/google/blueprint/proptools"
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010024)
25
26func init() {
27 registerPlatformBootclasspathBuildComponents(android.InitRegistrationContext)
28}
29
30func registerPlatformBootclasspathBuildComponents(ctx android.RegistrationContext) {
31 ctx.RegisterModuleType("platform_bootclasspath", platformBootclasspathFactory)
Paul Duffinb432df92021-03-22 22:09:42 +000032
33 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
34 ctx.BottomUp("platform_bootclasspath_deps", platformBootclasspathDepsMutator)
35 })
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010036}
37
Paul Duffinb432df92021-03-22 22:09:42 +000038type 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.
49func (t platformBootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() {
50}
51
52// The tag used for the dependency between the platform bootclasspath and any configured boot jars.
53var platformBootclasspathModuleDepTag = platformBootclasspathDependencyTag{name: "module"}
54
Paul Duffin62d8c3b2021-04-07 20:35:11 +010055// The tag used for the dependency between the platform bootclasspath and bootclasspath_fragments.
56var platformBootclasspathFragmentDepTag = platformBootclasspathDependencyTag{name: "fragment"}
57
Paul Duffinb432df92021-03-22 22:09:42 +000058var _ android.ExcludeFromVisibilityEnforcementTag = platformBootclasspathDependencyTag{}
59
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010060type platformBootclasspathModule struct {
61 android.ModuleBase
Paul Duffinb432df92021-03-22 22:09:42 +000062
Paul Duffin62d8c3b2021-04-07 20:35:11 +010063 properties platformBootclasspathProperties
64
Paul Duffinb432df92021-03-22 22:09:42 +000065 // The apex:module pairs obtained from the configured modules.
66 //
67 // Currently only for testing.
68 configuredModules []android.Module
Paul Duffin62d8c3b2021-04-07 20:35:11 +010069
70 // The apex:module pairs obtained from the fragments.
71 //
72 // Currently only for testing.
73 fragments []android.Module
Paul Duffin6a766452021-04-12 14:15:22 +010074
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 Duffin62d8c3b2021-04-07 20:35:11 +010083}
84
85// ApexVariantReference specifies a particular apex variant of a module.
86type 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
97type platformBootclasspathProperties struct {
Paul Duffin62d8c3b2021-04-07 20:35:11 +010098 // The names of the bootclasspath_fragment modules that form part of this
99 // platform_bootclasspath.
100 Fragments []ApexVariantReference
Paul Duffin702210b2021-04-08 20:12:41 +0100101
102 Hidden_api HiddenAPIAugmentationProperties
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100103}
104
105func platformBootclasspathFactory() android.Module {
106 m := &platformBootclasspathModule{}
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100107 m.AddProperties(&m.properties)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100108 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
109 return m
110}
111
Paul Duffin6a766452021-04-12 14:15:22 +0100112var _ android.OutputFileProducer = (*platformBootclasspathModule)(nil)
113
114// A minimal AndroidMkEntries is needed in order to support the dists property.
115func (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.
127func (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 Duffinbb7f1ac2021-03-29 22:18:45 +0100140func (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 Duffinb432df92021-03-22 22:09:42 +0000150func 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 Duffin62d8c3b2021-04-07 20:35:11 +0100165
166 // Add dependencies on all the fragments.
167 addDependencyOntoApexVariants(ctx, "fragments", p.properties.Fragments, platformBootclasspathFragmentDepTag)
168 }
169}
170
171func 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 Duffinb432df92021-03-22 22:09:42 +0000182 }
183}
184
185func 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
215func 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 Duffinbb7f1ac2021-03-29 22:18:45 +0100224func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffinb432df92021-03-22 22:09:42 +0000225 ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) {
226 tag := ctx.OtherModuleDependencyTag(module)
227 if tag == platformBootclasspathModuleDepTag {
228 b.configuredModules = append(b.configuredModules, module)
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100229 } else if tag == platformBootclasspathFragmentDepTag {
230 b.fragments = append(b.fragments, module)
Paul Duffinb432df92021-03-22 22:09:42 +0000231 }
232 })
233
Paul Duffin702210b2021-04-08 20:12:41 +0100234 b.generateHiddenAPIBuildActions(ctx, b.configuredModules)
235
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100236 // 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
257func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
258 return defaultBootImageConfig(ctx)
259}
Paul Duffin702210b2021-04-08 20:12:41 +0100260
261// generateHiddenAPIBuildActions generates all the hidden API related build rules.
262func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module) {
263
Paul Duffin90b8ad32021-04-13 12:25:01 +0100264 // Save the paths to the monolithic files for retrieval via OutputFiles().
265 b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
266 b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
267 b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
Paul Duffin6a766452021-04-12 14:15:22 +0100268
Paul Duffin702210b2021-04-08 20:12:41 +0100269 moduleSpecificFlagsPaths := android.Paths{}
270 for _, module := range modules {
271 if h, ok := module.(hiddenAPIIntf); ok {
272 if csv := h.flagsCSV(); csv != nil {
273 moduleSpecificFlagsPaths = append(moduleSpecificFlagsPaths, csv)
274 }
275 } else {
276 ctx.ModuleErrorf("module %s of type %s does not implement hiddenAPIIntf", module, ctx.OtherModuleType(module))
277 }
278 }
279
280 augmentationInfo := b.properties.Hidden_api.hiddenAPIAugmentationInfo(ctx)
281
282 outputPath := hiddenAPISingletonPaths(ctx).flags
283 baseFlagsPath := hiddenAPISingletonPaths(ctx).stubFlags
284 ruleToGenerateHiddenApiFlags(ctx, outputPath, baseFlagsPath, moduleSpecificFlagsPaths, augmentationInfo)
285}