blob: 6f242a149c77dc961f3ba5eb2b58591c5fde3e46 [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
Artur Satayev97259dc2021-04-07 15:17:14 +010062 ClasspathFragmentBase
Paul Duffinb432df92021-03-22 22:09:42 +000063
Paul Duffin62d8c3b2021-04-07 20:35:11 +010064 properties platformBootclasspathProperties
65
Paul Duffinb432df92021-03-22 22:09:42 +000066 // The apex:module pairs obtained from the configured modules.
67 //
68 // Currently only for testing.
69 configuredModules []android.Module
Paul Duffin62d8c3b2021-04-07 20:35:11 +010070
71 // The apex:module pairs obtained from the fragments.
72 //
73 // Currently only for testing.
74 fragments []android.Module
Paul Duffin6a766452021-04-12 14:15:22 +010075
76 // Path to the monolithic hiddenapi-flags.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010077 hiddenAPIFlagsCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010078
79 // Path to the monolithic hiddenapi-index.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010080 hiddenAPIIndexCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010081
82 // Path to the monolithic hiddenapi-unsupported.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010083 hiddenAPIMetadataCSV android.OutputPath
Paul Duffin62d8c3b2021-04-07 20:35:11 +010084}
85
86// ApexVariantReference specifies a particular apex variant of a module.
87type 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
98type platformBootclasspathProperties struct {
Paul Duffin62d8c3b2021-04-07 20:35:11 +010099 // The names of the bootclasspath_fragment modules that form part of this
100 // platform_bootclasspath.
101 Fragments []ApexVariantReference
Paul Duffin702210b2021-04-08 20:12:41 +0100102
103 Hidden_api HiddenAPIAugmentationProperties
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100104}
105
106func platformBootclasspathFactory() android.Module {
107 m := &platformBootclasspathModule{}
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100108 m.AddProperties(&m.properties)
Artur Satayev97259dc2021-04-07 15:17:14 +0100109 // TODO(satayev): split systemserver and apex jars into separate configs.
110 initClasspathFragment(m)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100111 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
112 return m
113}
114
Paul Duffin6a766452021-04-12 14:15:22 +0100115var _ android.OutputFileProducer = (*platformBootclasspathModule)(nil)
116
Artur Satayev97259dc2021-04-07 15:17:14 +0100117func (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 Duffin6a766452021-04-12 14:15:22 +0100126}
127
128// Make the hidden API files available from the platform-bootclasspath module.
129func (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 Duffinbb7f1ac2021-03-29 22:18:45 +0100142func (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 Duffinb432df92021-03-22 22:09:42 +0000152func 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 Duffin62d8c3b2021-04-07 20:35:11 +0100167
168 // Add dependencies on all the fragments.
169 addDependencyOntoApexVariants(ctx, "fragments", p.properties.Fragments, platformBootclasspathFragmentDepTag)
170 }
171}
172
173func 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 Duffinb432df92021-03-22 22:09:42 +0000184 }
185}
186
187func 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
217func 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 Duffinbb7f1ac2021-03-29 22:18:45 +0100226func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayev97259dc2021-04-07 15:17:14 +0100227 b.classpathFragmentBase().generateAndroidBuildActions(ctx)
228
Paul Duffinb432df92021-03-22 22:09:42 +0000229 ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) {
230 tag := ctx.OtherModuleDependencyTag(module)
231 if tag == platformBootclasspathModuleDepTag {
232 b.configuredModules = append(b.configuredModules, module)
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100233 } else if tag == platformBootclasspathFragmentDepTag {
234 b.fragments = append(b.fragments, module)
Paul Duffinb432df92021-03-22 22:09:42 +0000235 }
236 })
237
Paul Duffin702210b2021-04-08 20:12:41 +0100238 b.generateHiddenAPIBuildActions(ctx, b.configuredModules)
239
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100240 // 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
261func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
262 return defaultBootImageConfig(ctx)
263}
Paul Duffin702210b2021-04-08 20:12:41 +0100264
265// generateHiddenAPIBuildActions generates all the hidden API related build rules.
266func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module) {
267
Paul Duffin90b8ad32021-04-13 12:25:01 +0100268 // 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 Duffin6a766452021-04-12 14:15:22 +0100272
Paul Duffin0b659862021-04-13 13:02:29 +0100273 // 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 Duffin702210b2021-04-08 20:12:41 +0100287 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}