blob: 568f5e406c8a13f41b5662d106dba1532cc25402 [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
Paul Duffin46169772021-04-14 15:01:56 +0100103 Hidden_api HiddenAPIFlagFileProperties
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) {
Paul Duffin74431d52021-04-21 14:10:42 +0100143 b.hiddenAPIDepsMutator(ctx)
144
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100145 if SkipDexpreoptBootJars(ctx) {
146 return
147 }
148
149 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
150 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
151 dexpreopt.RegisterToolDeps(ctx)
152}
153
Paul Duffin74431d52021-04-21 14:10:42 +0100154func (b *platformBootclasspathModule) hiddenAPIDepsMutator(ctx android.BottomUpMutatorContext) {
155 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
156 return
157 }
158
159 // Add dependencies onto the stub lib modules.
160 sdkKindToStubLibModules := hiddenAPIComputeMonolithicStubLibModules(ctx.Config())
161 hiddenAPIAddStubLibDependencies(ctx, sdkKindToStubLibModules)
162}
163
Paul Duffinb432df92021-03-22 22:09:42 +0000164func platformBootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
165 m := ctx.Module()
166 if p, ok := m.(*platformBootclasspathModule); ok {
167 // Add dependencies on all the modules configured in the "art" boot image.
168 artImageConfig := genBootImageConfigs(ctx)[artBootImageName]
169 addDependenciesOntoBootImageModules(ctx, artImageConfig.modules)
170
171 // Add dependencies on all the modules configured in the "boot" boot image. That does not
172 // include modules configured in the "art" boot image.
173 bootImageConfig := p.getImageConfig(ctx)
174 addDependenciesOntoBootImageModules(ctx, bootImageConfig.modules)
175
176 // Add dependencies on all the updatable modules.
177 updatableModules := dexpreopt.GetGlobalConfig(ctx).UpdatableBootJars
178 addDependenciesOntoBootImageModules(ctx, updatableModules)
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100179
180 // Add dependencies on all the fragments.
181 addDependencyOntoApexVariants(ctx, "fragments", p.properties.Fragments, platformBootclasspathFragmentDepTag)
182 }
183}
184
185func addDependencyOntoApexVariants(ctx android.BottomUpMutatorContext, propertyName string, refs []ApexVariantReference, tag blueprint.DependencyTag) {
186 for i, ref := range refs {
187 apex := proptools.StringDefault(ref.Apex, "platform")
188
189 if ref.Module == nil {
190 ctx.PropertyErrorf(propertyName, "missing module name at position %d", i)
191 continue
192 }
193 name := proptools.String(ref.Module)
194
195 addDependencyOntoApexModulePair(ctx, apex, name, tag)
Paul Duffinb432df92021-03-22 22:09:42 +0000196 }
197}
198
199func addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tag blueprint.DependencyTag) {
200 var variations []blueprint.Variation
201 if apex != "platform" {
202 // Pick the correct apex variant.
203 variations = []blueprint.Variation{
204 {Mutator: "apex", Variation: apex},
205 }
206 }
207
208 addedDep := false
209 if ctx.OtherModuleDependencyVariantExists(variations, name) {
210 ctx.AddFarVariationDependencies(variations, tag, name)
211 addedDep = true
212 }
213
214 // Add a dependency on the prebuilt module if it exists.
215 prebuiltName := android.PrebuiltNameFromSource(name)
216 if ctx.OtherModuleDependencyVariantExists(variations, prebuiltName) {
217 ctx.AddVariationDependencies(variations, tag, prebuiltName)
218 addedDep = true
219 }
220
221 // If no appropriate variant existing for this, so no dependency could be added, then it is an
222 // error, unless missing dependencies are allowed. The simplest way to handle that is to add a
223 // dependency that will not be satisfied and the default behavior will handle it.
224 if !addedDep {
Paul Duffin8f146b92021-04-12 17:24:18 +0100225 // Add dependency on the unprefixed (i.e. source or renamed prebuilt) module which we know does
226 // not exist. The resulting error message will contain useful information about the available
227 // variants.
228 reportMissingVariationDependency(ctx, variations, name)
229
230 // Add dependency on the missing prefixed prebuilt variant too if a module with that name exists
231 // so that information about its available variants will be reported too.
232 if ctx.OtherModuleExists(prebuiltName) {
233 reportMissingVariationDependency(ctx, variations, prebuiltName)
234 }
235 }
236}
237
238// reportMissingVariationDependency intentionally adds a dependency on a missing variation in order
239// to generate an appropriate error message with information about the available variations.
240func reportMissingVariationDependency(ctx android.BottomUpMutatorContext, variations []blueprint.Variation, name string) {
241 modules := ctx.AddFarVariationDependencies(variations, nil, name)
242 if len(modules) != 1 {
243 panic(fmt.Errorf("Internal Error: expected one module, found %d", len(modules)))
244 return
245 }
246 if modules[0] != nil {
247 panic(fmt.Errorf("Internal Error: expected module to be missing but was found: %q", modules[0]))
248 return
Paul Duffinb432df92021-03-22 22:09:42 +0000249 }
250}
251
252func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList) {
253 for i := 0; i < modules.Len(); i++ {
254 apex := modules.Apex(i)
255 name := modules.Jar(i)
256
257 addDependencyOntoApexModulePair(ctx, apex, name, platformBootclasspathModuleDepTag)
258 }
259}
260
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100261func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayev97259dc2021-04-07 15:17:14 +0100262 b.classpathFragmentBase().generateAndroidBuildActions(ctx)
263
Paul Duffinb432df92021-03-22 22:09:42 +0000264 ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) {
265 tag := ctx.OtherModuleDependencyTag(module)
266 if tag == platformBootclasspathModuleDepTag {
267 b.configuredModules = append(b.configuredModules, module)
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100268 } else if tag == platformBootclasspathFragmentDepTag {
269 b.fragments = append(b.fragments, module)
Paul Duffinb432df92021-03-22 22:09:42 +0000270 }
271 })
272
Paul Duffin9b381ef2021-04-08 23:01:37 +0100273 b.generateHiddenAPIBuildActions(ctx, b.configuredModules, b.fragments)
Paul Duffin702210b2021-04-08 20:12:41 +0100274
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100275 // Nothing to do if skipping the dexpreopt of boot image jars.
276 if SkipDexpreoptBootJars(ctx) {
277 return
278 }
279
280 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
281 // GenerateSingletonBuildActions method as it cannot create it for itself.
282 dexpreopt.GetGlobalSoongConfig(ctx)
283
284 imageConfig := b.getImageConfig(ctx)
285 if imageConfig == nil {
286 return
287 }
288
289 // Construct the boot image info from the config.
290 info := BootImageInfo{imageConfig: imageConfig}
291
292 // Make it available for other modules.
293 ctx.SetProvider(BootImageInfoProvider, info)
294}
295
296func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
297 return defaultBootImageConfig(ctx)
298}
Paul Duffin702210b2021-04-08 20:12:41 +0100299
300// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffin9b381ef2021-04-08 23:01:37 +0100301func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module, fragments []android.Module) {
Paul Duffin702210b2021-04-08 20:12:41 +0100302
Paul Duffin90b8ad32021-04-13 12:25:01 +0100303 // Save the paths to the monolithic files for retrieval via OutputFiles().
304 b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
305 b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
306 b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
Paul Duffin6a766452021-04-12 14:15:22 +0100307
Paul Duffin0b659862021-04-13 13:02:29 +0100308 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true. This is a performance
309 // optimization that can be used to reduce the incremental build time but as its name suggests it
310 // can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath.
311 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
312 paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV}
313 for _, path := range paths {
314 ctx.Build(pctx, android.BuildParams{
315 Rule: android.Touch,
316 Output: path,
317 })
318 }
319 return
320 }
321
Paul Duffin1ba24672021-04-12 23:26:14 +0100322 hiddenAPISupportingModules := []hiddenAPISupportingModule{}
Paul Duffin702210b2021-04-08 20:12:41 +0100323 for _, module := range modules {
Paul Duffin1ba24672021-04-12 23:26:14 +0100324 if h, ok := module.(hiddenAPISupportingModule); ok {
325 if h.bootDexJar() == nil {
326 ctx.ModuleErrorf("module %s does not provide a bootDexJar file", module)
Paul Duffin702210b2021-04-08 20:12:41 +0100327 }
Paul Duffin1ba24672021-04-12 23:26:14 +0100328 if h.flagsCSV() == nil {
329 ctx.ModuleErrorf("module %s does not provide a flagsCSV file", module)
330 }
331 if h.indexCSV() == nil {
332 ctx.ModuleErrorf("module %s does not provide an indexCSV file", module)
333 }
334 if h.metadataCSV() == nil {
335 ctx.ModuleErrorf("module %s does not provide a metadataCSV file", module)
336 }
337
338 if ctx.Failed() {
339 continue
340 }
341
342 hiddenAPISupportingModules = append(hiddenAPISupportingModules, h)
Paul Duffin702210b2021-04-08 20:12:41 +0100343 } else {
Paul Duffin1ba24672021-04-12 23:26:14 +0100344 ctx.ModuleErrorf("module %s of type %s does not support hidden API processing", module, ctx.OtherModuleType(module))
Paul Duffin702210b2021-04-08 20:12:41 +0100345 }
346 }
347
Paul Duffin1ba24672021-04-12 23:26:14 +0100348 moduleSpecificFlagsPaths := android.Paths{}
349 for _, module := range hiddenAPISupportingModules {
350 moduleSpecificFlagsPaths = append(moduleSpecificFlagsPaths, module.flagsCSV())
351 }
352
Paul Duffin9b381ef2021-04-08 23:01:37 +0100353 flagFileInfo := b.properties.Hidden_api.hiddenAPIFlagFileInfo(ctx)
354 for _, fragment := range fragments {
355 if ctx.OtherModuleHasProvider(fragment, hiddenAPIFlagFileInfoProvider) {
356 info := ctx.OtherModuleProvider(fragment, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo)
357 flagFileInfo.append(info)
358 }
359 }
360
361 // Store the information for testing.
362 ctx.SetProvider(hiddenAPIFlagFileInfoProvider, flagFileInfo)
Paul Duffin702210b2021-04-08 20:12:41 +0100363
364 outputPath := hiddenAPISingletonPaths(ctx).flags
365 baseFlagsPath := hiddenAPISingletonPaths(ctx).stubFlags
Paul Duffin9b381ef2021-04-08 23:01:37 +0100366 ruleToGenerateHiddenApiFlags(ctx, outputPath, baseFlagsPath, moduleSpecificFlagsPaths, flagFileInfo)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100367
Paul Duffin74431d52021-04-21 14:10:42 +0100368 b.generateHiddenAPIStubFlagsRules(ctx, hiddenAPISupportingModules)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100369 b.generateHiddenAPIIndexRules(ctx, hiddenAPISupportingModules)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100370 b.generatedHiddenAPIMetadataRules(ctx, hiddenAPISupportingModules)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100371}
372
Paul Duffin74431d52021-04-21 14:10:42 +0100373func (b *platformBootclasspathModule) generateHiddenAPIStubFlagsRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
374 bootDexJars := android.Paths{}
375 for _, module := range modules {
376 bootDexJars = append(bootDexJars, module.bootDexJar())
377 }
378
379 sdkKindToStubPaths := hiddenAPIGatherStubLibDexJarPaths(ctx)
380
381 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
382 rule := ruleToGenerateHiddenAPIStubFlagsFile(ctx, outputPath, bootDexJars, sdkKindToStubPaths)
383 rule.Build("platform-bootclasspath-monolithic-hiddenapi-stub-flags", "monolithic hidden API stub flags")
384}
385
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100386func (b *platformBootclasspathModule) generateHiddenAPIIndexRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
387 indexes := android.Paths{}
388 for _, module := range modules {
389 indexes = append(indexes, module.indexCSV())
390 }
391
392 rule := android.NewRuleBuilder(pctx, ctx)
393 rule.Command().
394 BuiltTool("merge_csv").
395 Flag("--key_field signature").
396 FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
397 FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index).
398 Inputs(indexes)
399 rule.Build("platform-bootclasspath-monolithic-hiddenapi-index", "monolithic hidden API index")
Paul Duffin702210b2021-04-08 20:12:41 +0100400}
Paul Duffin85dee5d2021-04-13 00:14:38 +0100401
402func (b *platformBootclasspathModule) generatedHiddenAPIMetadataRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
403 metadataCSVFiles := android.Paths{}
404 for _, module := range modules {
405 metadataCSVFiles = append(metadataCSVFiles, module.metadataCSV())
406 }
407
408 rule := android.NewRuleBuilder(pctx, ctx)
409
410 outputPath := hiddenAPISingletonPaths(ctx).metadata
411
412 rule.Command().
413 BuiltTool("merge_csv").
414 Flag("--key_field signature").
415 FlagWithOutput("--output=", outputPath).
416 Inputs(metadataCSVFiles)
417
418 rule.Build("platform-bootclasspath-monolithic-hiddenapi-metadata", "monolithic hidden API metadata")
419}