blob: d341591d36e50e51315825643a721f1359f936d1 [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"
22)
23
24func init() {
25 registerPlatformBootclasspathBuildComponents(android.InitRegistrationContext)
26}
27
28func registerPlatformBootclasspathBuildComponents(ctx android.RegistrationContext) {
Paul Duffine3ecce62021-04-29 10:34:11 +010029 ctx.RegisterSingletonModuleType("platform_bootclasspath", platformBootclasspathFactory)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010030}
31
Paul Duffin01b463b2021-04-26 20:05:39 +010032// The tags used for the dependencies between the platform bootclasspath and any configured boot
33// jars.
34var (
35 platformBootclasspathArtBootJarDepTag = bootclasspathDependencyTag{name: "art-boot-jar"}
36 platformBootclasspathNonUpdatableBootJarDepTag = bootclasspathDependencyTag{name: "non-updatable-boot-jar"}
37 platformBootclasspathUpdatableBootJarDepTag = bootclasspathDependencyTag{name: "updatable-boot-jar"}
38)
Paul Duffinb432df92021-03-22 22:09:42 +000039
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010040type platformBootclasspathModule struct {
Paul Duffine3ecce62021-04-29 10:34:11 +010041 android.SingletonModuleBase
Artur Satayev97259dc2021-04-07 15:17:14 +010042 ClasspathFragmentBase
Paul Duffinb432df92021-03-22 22:09:42 +000043
Paul Duffin62d8c3b2021-04-07 20:35:11 +010044 properties platformBootclasspathProperties
45
Paul Duffinb432df92021-03-22 22:09:42 +000046 // The apex:module pairs obtained from the configured modules.
47 //
48 // Currently only for testing.
49 configuredModules []android.Module
Paul Duffin62d8c3b2021-04-07 20:35:11 +010050
51 // The apex:module pairs obtained from the fragments.
52 //
53 // Currently only for testing.
54 fragments []android.Module
Paul Duffin6a766452021-04-12 14:15:22 +010055
56 // Path to the monolithic hiddenapi-flags.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010057 hiddenAPIFlagsCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010058
59 // Path to the monolithic hiddenapi-index.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010060 hiddenAPIIndexCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010061
62 // Path to the monolithic hiddenapi-unsupported.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010063 hiddenAPIMetadataCSV android.OutputPath
Paul Duffin62d8c3b2021-04-07 20:35:11 +010064}
65
Paul Duffin62d8c3b2021-04-07 20:35:11 +010066type platformBootclasspathProperties struct {
Paul Duffinb67d8782021-04-22 11:49:41 +010067 BootclasspathFragmentsDepsProperties
Paul Duffin702210b2021-04-08 20:12:41 +010068
Paul Duffin46169772021-04-14 15:01:56 +010069 Hidden_api HiddenAPIFlagFileProperties
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010070}
71
Paul Duffine3ecce62021-04-29 10:34:11 +010072func platformBootclasspathFactory() android.SingletonModule {
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010073 m := &platformBootclasspathModule{}
Paul Duffin62d8c3b2021-04-07 20:35:11 +010074 m.AddProperties(&m.properties)
Artur Satayev97259dc2021-04-07 15:17:14 +010075 // TODO(satayev): split systemserver and apex jars into separate configs.
76 initClasspathFragment(m)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010077 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
78 return m
79}
80
Paul Duffin6a766452021-04-12 14:15:22 +010081var _ android.OutputFileProducer = (*platformBootclasspathModule)(nil)
82
Artur Satayev97259dc2021-04-07 15:17:14 +010083func (b *platformBootclasspathModule) AndroidMkEntries() (entries []android.AndroidMkEntries) {
84 entries = append(entries, android.AndroidMkEntries{
85 Class: "FAKE",
86 // Need at least one output file in order for this to take effect.
87 OutputFile: android.OptionalPathForPath(b.hiddenAPIFlagsCSV),
88 Include: "$(BUILD_PHONY_PACKAGE)",
89 })
90 entries = append(entries, b.classpathFragmentBase().getAndroidMkEntries()...)
91 return
Paul Duffin6a766452021-04-12 14:15:22 +010092}
93
94// Make the hidden API files available from the platform-bootclasspath module.
95func (b *platformBootclasspathModule) OutputFiles(tag string) (android.Paths, error) {
96 switch tag {
97 case "hiddenapi-flags.csv":
98 return android.Paths{b.hiddenAPIFlagsCSV}, nil
99 case "hiddenapi-index.csv":
100 return android.Paths{b.hiddenAPIIndexCSV}, nil
101 case "hiddenapi-metadata.csv":
102 return android.Paths{b.hiddenAPIMetadataCSV}, nil
103 }
104
105 return nil, fmt.Errorf("unknown tag %s", tag)
106}
107
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100108func (b *platformBootclasspathModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin74431d52021-04-21 14:10:42 +0100109 b.hiddenAPIDepsMutator(ctx)
110
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100111 if SkipDexpreoptBootJars(ctx) {
112 return
113 }
114
115 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
116 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
117 dexpreopt.RegisterToolDeps(ctx)
118}
119
Paul Duffin74431d52021-04-21 14:10:42 +0100120func (b *platformBootclasspathModule) hiddenAPIDepsMutator(ctx android.BottomUpMutatorContext) {
121 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
122 return
123 }
124
125 // Add dependencies onto the stub lib modules.
126 sdkKindToStubLibModules := hiddenAPIComputeMonolithicStubLibModules(ctx.Config())
127 hiddenAPIAddStubLibDependencies(ctx, sdkKindToStubLibModules)
128}
129
Paul Duffin4994d262021-04-22 12:08:59 +0100130func (b *platformBootclasspathModule) BootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
131 // Add dependencies on all the modules configured in the "art" boot image.
132 artImageConfig := genBootImageConfigs(ctx)[artBootImageName]
Paul Duffin01b463b2021-04-26 20:05:39 +0100133 addDependenciesOntoBootImageModules(ctx, artImageConfig.modules, platformBootclasspathArtBootJarDepTag)
Paul Duffinb432df92021-03-22 22:09:42 +0000134
Paul Duffin01b463b2021-04-26 20:05:39 +0100135 // Add dependencies on all the non-updatable module configured in the "boot" boot image. That does
136 // not include modules configured in the "art" boot image.
Paul Duffin4994d262021-04-22 12:08:59 +0100137 bootImageConfig := b.getImageConfig(ctx)
Paul Duffin01b463b2021-04-26 20:05:39 +0100138 addDependenciesOntoBootImageModules(ctx, bootImageConfig.modules, platformBootclasspathNonUpdatableBootJarDepTag)
Paul Duffinb432df92021-03-22 22:09:42 +0000139
Paul Duffin4994d262021-04-22 12:08:59 +0100140 // Add dependencies on all the updatable modules.
141 updatableModules := dexpreopt.GetGlobalConfig(ctx).UpdatableBootJars
Paul Duffin01b463b2021-04-26 20:05:39 +0100142 addDependenciesOntoBootImageModules(ctx, updatableModules, platformBootclasspathUpdatableBootJarDepTag)
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100143
Paul Duffin4994d262021-04-22 12:08:59 +0100144 // Add dependencies on all the fragments.
145 b.properties.BootclasspathFragmentsDepsProperties.addDependenciesOntoFragments(ctx)
Paul Duffinb432df92021-03-22 22:09:42 +0000146}
147
Paul Duffin01b463b2021-04-26 20:05:39 +0100148func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList, tag bootclasspathDependencyTag) {
Paul Duffinb432df92021-03-22 22:09:42 +0000149 for i := 0; i < modules.Len(); i++ {
150 apex := modules.Apex(i)
151 name := modules.Jar(i)
152
Paul Duffin01b463b2021-04-26 20:05:39 +0100153 addDependencyOntoApexModulePair(ctx, apex, name, tag)
Paul Duffinb432df92021-03-22 22:09:42 +0000154 }
155}
156
Paul Duffine3ecce62021-04-29 10:34:11 +0100157// GenerateSingletonBuildActions does nothing and must never do anything.
158//
159// This module only implements android.SingletonModule so that it can implement
160// android.SingletonMakeVarsProvider.
161func (b *platformBootclasspathModule) GenerateSingletonBuildActions(android.SingletonContext) {
162 // Keep empty
163}
164
165func (d *platformBootclasspathModule) MakeVars(ctx android.MakeVarsContext) {
166 // Placeholder for now.
167}
168
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100169func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayev97259dc2021-04-07 15:17:14 +0100170 b.classpathFragmentBase().generateAndroidBuildActions(ctx)
171
Paul Duffin01b463b2021-04-26 20:05:39 +0100172 // Gather all the dependencies from the art, updatable and non-updatable boot jars.
173 artModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathArtBootJarDepTag)
174 nonUpdatableModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathNonUpdatableBootJarDepTag)
175 updatableModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathUpdatableBootJarDepTag)
176
177 // Concatenate them all, in order as they would appear on the bootclasspath.
178 var allModules []android.Module
179 allModules = append(allModules, artModules...)
180 allModules = append(allModules, nonUpdatableModules...)
181 allModules = append(allModules, updatableModules...)
182 b.configuredModules = allModules
183
184 // Gather all the fragments dependencies.
Paul Duffin9bacf562021-04-28 21:16:02 +0100185 b.fragments = gatherApexModulePairDepsWithTag(ctx, bootclasspathFragmentDepTag)
Paul Duffinb432df92021-03-22 22:09:42 +0000186
Paul Duffin9b381ef2021-04-08 23:01:37 +0100187 b.generateHiddenAPIBuildActions(ctx, b.configuredModules, b.fragments)
Paul Duffin702210b2021-04-08 20:12:41 +0100188
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100189 // Nothing to do if skipping the dexpreopt of boot image jars.
190 if SkipDexpreoptBootJars(ctx) {
191 return
192 }
193
Paul Duffin4c094422021-04-26 20:10:48 +0100194 b.generateBootImageBuildActions(ctx, updatableModules)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100195}
196
197func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
198 return defaultBootImageConfig(ctx)
199}
Paul Duffin702210b2021-04-08 20:12:41 +0100200
201// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffin9b381ef2021-04-08 23:01:37 +0100202func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module, fragments []android.Module) {
Paul Duffin702210b2021-04-08 20:12:41 +0100203
Paul Duffin90b8ad32021-04-13 12:25:01 +0100204 // Save the paths to the monolithic files for retrieval via OutputFiles().
205 b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
206 b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
207 b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
Paul Duffin6a766452021-04-12 14:15:22 +0100208
Paul Duffin0b659862021-04-13 13:02:29 +0100209 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true. This is a performance
210 // optimization that can be used to reduce the incremental build time but as its name suggests it
211 // can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath.
Vishnu Nair0dbd02a2021-04-30 00:24:07 +0000212 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
Paul Duffin0b659862021-04-13 13:02:29 +0100213 paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV}
214 for _, path := range paths {
215 ctx.Build(pctx, android.BuildParams{
216 Rule: android.Touch,
217 Output: path,
218 })
219 }
220 return
221 }
222
Paul Duffin1ba24672021-04-12 23:26:14 +0100223 hiddenAPISupportingModules := []hiddenAPISupportingModule{}
Paul Duffin702210b2021-04-08 20:12:41 +0100224 for _, module := range modules {
Paul Duffin1ba24672021-04-12 23:26:14 +0100225 if h, ok := module.(hiddenAPISupportingModule); ok {
226 if h.bootDexJar() == nil {
227 ctx.ModuleErrorf("module %s does not provide a bootDexJar file", module)
Paul Duffin702210b2021-04-08 20:12:41 +0100228 }
Paul Duffin1ba24672021-04-12 23:26:14 +0100229 if h.flagsCSV() == nil {
230 ctx.ModuleErrorf("module %s does not provide a flagsCSV file", module)
231 }
232 if h.indexCSV() == nil {
233 ctx.ModuleErrorf("module %s does not provide an indexCSV file", module)
234 }
235 if h.metadataCSV() == nil {
236 ctx.ModuleErrorf("module %s does not provide a metadataCSV file", module)
237 }
238
239 if ctx.Failed() {
240 continue
241 }
242
243 hiddenAPISupportingModules = append(hiddenAPISupportingModules, h)
Paul Duffin702210b2021-04-08 20:12:41 +0100244 } else {
Paul Duffin1ba24672021-04-12 23:26:14 +0100245 ctx.ModuleErrorf("module %s of type %s does not support hidden API processing", module, ctx.OtherModuleType(module))
Paul Duffin702210b2021-04-08 20:12:41 +0100246 }
247 }
248
Paul Duffin1ba24672021-04-12 23:26:14 +0100249 moduleSpecificFlagsPaths := android.Paths{}
250 for _, module := range hiddenAPISupportingModules {
251 moduleSpecificFlagsPaths = append(moduleSpecificFlagsPaths, module.flagsCSV())
252 }
253
Paul Duffin9b381ef2021-04-08 23:01:37 +0100254 flagFileInfo := b.properties.Hidden_api.hiddenAPIFlagFileInfo(ctx)
255 for _, fragment := range fragments {
256 if ctx.OtherModuleHasProvider(fragment, hiddenAPIFlagFileInfoProvider) {
257 info := ctx.OtherModuleProvider(fragment, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo)
258 flagFileInfo.append(info)
259 }
260 }
261
262 // Store the information for testing.
263 ctx.SetProvider(hiddenAPIFlagFileInfoProvider, flagFileInfo)
Paul Duffin702210b2021-04-08 20:12:41 +0100264
265 outputPath := hiddenAPISingletonPaths(ctx).flags
266 baseFlagsPath := hiddenAPISingletonPaths(ctx).stubFlags
Paul Duffin9b381ef2021-04-08 23:01:37 +0100267 ruleToGenerateHiddenApiFlags(ctx, outputPath, baseFlagsPath, moduleSpecificFlagsPaths, flagFileInfo)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100268
Paul Duffin74431d52021-04-21 14:10:42 +0100269 b.generateHiddenAPIStubFlagsRules(ctx, hiddenAPISupportingModules)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100270 b.generateHiddenAPIIndexRules(ctx, hiddenAPISupportingModules)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100271 b.generatedHiddenAPIMetadataRules(ctx, hiddenAPISupportingModules)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100272}
273
Paul Duffin74431d52021-04-21 14:10:42 +0100274func (b *platformBootclasspathModule) generateHiddenAPIStubFlagsRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
275 bootDexJars := android.Paths{}
276 for _, module := range modules {
277 bootDexJars = append(bootDexJars, module.bootDexJar())
278 }
279
280 sdkKindToStubPaths := hiddenAPIGatherStubLibDexJarPaths(ctx)
281
282 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
283 rule := ruleToGenerateHiddenAPIStubFlagsFile(ctx, outputPath, bootDexJars, sdkKindToStubPaths)
284 rule.Build("platform-bootclasspath-monolithic-hiddenapi-stub-flags", "monolithic hidden API stub flags")
285}
286
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100287func (b *platformBootclasspathModule) generateHiddenAPIIndexRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
288 indexes := android.Paths{}
289 for _, module := range modules {
290 indexes = append(indexes, module.indexCSV())
291 }
292
293 rule := android.NewRuleBuilder(pctx, ctx)
294 rule.Command().
295 BuiltTool("merge_csv").
296 Flag("--key_field signature").
297 FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
298 FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index).
299 Inputs(indexes)
300 rule.Build("platform-bootclasspath-monolithic-hiddenapi-index", "monolithic hidden API index")
Paul Duffin702210b2021-04-08 20:12:41 +0100301}
Paul Duffin85dee5d2021-04-13 00:14:38 +0100302
303func (b *platformBootclasspathModule) generatedHiddenAPIMetadataRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
304 metadataCSVFiles := android.Paths{}
305 for _, module := range modules {
306 metadataCSVFiles = append(metadataCSVFiles, module.metadataCSV())
307 }
308
309 rule := android.NewRuleBuilder(pctx, ctx)
310
311 outputPath := hiddenAPISingletonPaths(ctx).metadata
312
313 rule.Command().
314 BuiltTool("merge_csv").
315 Flag("--key_field signature").
316 FlagWithOutput("--output=", outputPath).
317 Inputs(metadataCSVFiles)
318
319 rule.Build("platform-bootclasspath-monolithic-hiddenapi-metadata", "monolithic hidden API metadata")
320}
Paul Duffinad19d382021-04-26 16:44:00 +0100321
322// generateBootImageBuildActions generates ninja rules related to the boot image creation.
Paul Duffin4c094422021-04-26 20:10:48 +0100323func (b *platformBootclasspathModule) generateBootImageBuildActions(ctx android.ModuleContext, updatableModules []android.Module) {
Paul Duffinad19d382021-04-26 16:44:00 +0100324 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
325 // GenerateSingletonBuildActions method as it cannot create it for itself.
326 dexpreopt.GetGlobalSoongConfig(ctx)
327
328 imageConfig := b.getImageConfig(ctx)
329 if imageConfig == nil {
330 return
331 }
332
333 global := dexpreopt.GetGlobalConfig(ctx)
334 if !shouldBuildBootImages(ctx.Config(), global) {
335 return
336 }
337
338 // Generate the framework profile rule
339 bootFrameworkProfileRule(ctx, imageConfig)
Paul Duffin4c094422021-04-26 20:10:48 +0100340
341 // Generate the updatable bootclasspath packages rule.
342 generateUpdatableBcpPackagesRule(ctx, imageConfig, updatableModules)
Paul Duffinf7a55922021-04-26 23:09:15 +0100343
344 dumpOatRules(ctx, imageConfig)
Paul Duffinad19d382021-04-26 16:44:00 +0100345}