blob: 621119ef3d396b46f09ea2aa8f0db5ab996bd0ff [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.
Paul Duffin0b659862021-04-13 13:02:29 +010076 hiddenAPIFlagsCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010077
78 // Path to the monolithic hiddenapi-index.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010079 hiddenAPIIndexCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010080
81 // Path to the monolithic hiddenapi-unsupported.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010082 hiddenAPIMetadataCSV android.OutputPath
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 {
Paul Duffin8f146b92021-04-12 17:24:18 +0100211 // Add dependency on the unprefixed (i.e. source or renamed prebuilt) module which we know does
212 // not exist. The resulting error message will contain useful information about the available
213 // variants.
214 reportMissingVariationDependency(ctx, variations, name)
215
216 // Add dependency on the missing prefixed prebuilt variant too if a module with that name exists
217 // so that information about its available variants will be reported too.
218 if ctx.OtherModuleExists(prebuiltName) {
219 reportMissingVariationDependency(ctx, variations, prebuiltName)
220 }
221 }
222}
223
224// reportMissingVariationDependency intentionally adds a dependency on a missing variation in order
225// to generate an appropriate error message with information about the available variations.
226func reportMissingVariationDependency(ctx android.BottomUpMutatorContext, variations []blueprint.Variation, name string) {
227 modules := ctx.AddFarVariationDependencies(variations, nil, name)
228 if len(modules) != 1 {
229 panic(fmt.Errorf("Internal Error: expected one module, found %d", len(modules)))
230 return
231 }
232 if modules[0] != nil {
233 panic(fmt.Errorf("Internal Error: expected module to be missing but was found: %q", modules[0]))
234 return
Paul Duffinb432df92021-03-22 22:09:42 +0000235 }
236}
237
238func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList) {
239 for i := 0; i < modules.Len(); i++ {
240 apex := modules.Apex(i)
241 name := modules.Jar(i)
242
243 addDependencyOntoApexModulePair(ctx, apex, name, platformBootclasspathModuleDepTag)
244 }
245}
246
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100247func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffinb432df92021-03-22 22:09:42 +0000248 ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) {
249 tag := ctx.OtherModuleDependencyTag(module)
250 if tag == platformBootclasspathModuleDepTag {
251 b.configuredModules = append(b.configuredModules, module)
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100252 } else if tag == platformBootclasspathFragmentDepTag {
253 b.fragments = append(b.fragments, module)
Paul Duffinb432df92021-03-22 22:09:42 +0000254 }
255 })
256
Paul Duffin702210b2021-04-08 20:12:41 +0100257 b.generateHiddenAPIBuildActions(ctx, b.configuredModules)
258
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100259 // Nothing to do if skipping the dexpreopt of boot image jars.
260 if SkipDexpreoptBootJars(ctx) {
261 return
262 }
263
264 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
265 // GenerateSingletonBuildActions method as it cannot create it for itself.
266 dexpreopt.GetGlobalSoongConfig(ctx)
267
268 imageConfig := b.getImageConfig(ctx)
269 if imageConfig == nil {
270 return
271 }
272
273 // Construct the boot image info from the config.
274 info := BootImageInfo{imageConfig: imageConfig}
275
276 // Make it available for other modules.
277 ctx.SetProvider(BootImageInfoProvider, info)
278}
279
280func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
281 return defaultBootImageConfig(ctx)
282}
Paul Duffin702210b2021-04-08 20:12:41 +0100283
284// generateHiddenAPIBuildActions generates all the hidden API related build rules.
285func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module) {
286
Paul Duffin90b8ad32021-04-13 12:25:01 +0100287 // Save the paths to the monolithic files for retrieval via OutputFiles().
288 b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
289 b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
290 b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
Paul Duffin6a766452021-04-12 14:15:22 +0100291
Paul Duffin0b659862021-04-13 13:02:29 +0100292 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true. This is a performance
293 // optimization that can be used to reduce the incremental build time but as its name suggests it
294 // can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath.
295 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
296 paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV}
297 for _, path := range paths {
298 ctx.Build(pctx, android.BuildParams{
299 Rule: android.Touch,
300 Output: path,
301 })
302 }
303 return
304 }
305
Paul Duffin1ba24672021-04-12 23:26:14 +0100306 hiddenAPISupportingModules := []hiddenAPISupportingModule{}
Paul Duffin702210b2021-04-08 20:12:41 +0100307 for _, module := range modules {
Paul Duffin1ba24672021-04-12 23:26:14 +0100308 if h, ok := module.(hiddenAPISupportingModule); ok {
309 if h.bootDexJar() == nil {
310 ctx.ModuleErrorf("module %s does not provide a bootDexJar file", module)
Paul Duffin702210b2021-04-08 20:12:41 +0100311 }
Paul Duffin1ba24672021-04-12 23:26:14 +0100312 if h.flagsCSV() == nil {
313 ctx.ModuleErrorf("module %s does not provide a flagsCSV file", module)
314 }
315 if h.indexCSV() == nil {
316 ctx.ModuleErrorf("module %s does not provide an indexCSV file", module)
317 }
318 if h.metadataCSV() == nil {
319 ctx.ModuleErrorf("module %s does not provide a metadataCSV file", module)
320 }
321
322 if ctx.Failed() {
323 continue
324 }
325
326 hiddenAPISupportingModules = append(hiddenAPISupportingModules, h)
Paul Duffin702210b2021-04-08 20:12:41 +0100327 } else {
Paul Duffin1ba24672021-04-12 23:26:14 +0100328 ctx.ModuleErrorf("module %s of type %s does not support hidden API processing", module, ctx.OtherModuleType(module))
Paul Duffin702210b2021-04-08 20:12:41 +0100329 }
330 }
331
Paul Duffin1ba24672021-04-12 23:26:14 +0100332 moduleSpecificFlagsPaths := android.Paths{}
333 for _, module := range hiddenAPISupportingModules {
334 moduleSpecificFlagsPaths = append(moduleSpecificFlagsPaths, module.flagsCSV())
335 }
336
Paul Duffin702210b2021-04-08 20:12:41 +0100337 augmentationInfo := b.properties.Hidden_api.hiddenAPIAugmentationInfo(ctx)
338
339 outputPath := hiddenAPISingletonPaths(ctx).flags
340 baseFlagsPath := hiddenAPISingletonPaths(ctx).stubFlags
341 ruleToGenerateHiddenApiFlags(ctx, outputPath, baseFlagsPath, moduleSpecificFlagsPaths, augmentationInfo)
342}