blob: f126e13b6e442c9c12745aba66b864cc06abd297 [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) {
29 ctx.RegisterModuleType("platform_bootclasspath", platformBootclasspathFactory)
30}
31
Paul Duffinb432df92021-03-22 22:09:42 +000032// The tag used for the dependency between the platform bootclasspath and any configured boot jars.
Paul Duffinb67d8782021-04-22 11:49:41 +010033var platformBootclasspathModuleDepTag = bootclasspathDependencyTag{name: "module"}
Paul Duffinb432df92021-03-22 22:09:42 +000034
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010035type platformBootclasspathModule struct {
36 android.ModuleBase
Artur Satayev97259dc2021-04-07 15:17:14 +010037 ClasspathFragmentBase
Paul Duffinb432df92021-03-22 22:09:42 +000038
Paul Duffin62d8c3b2021-04-07 20:35:11 +010039 properties platformBootclasspathProperties
40
Paul Duffinb432df92021-03-22 22:09:42 +000041 // The apex:module pairs obtained from the configured modules.
42 //
43 // Currently only for testing.
44 configuredModules []android.Module
Paul Duffin62d8c3b2021-04-07 20:35:11 +010045
46 // The apex:module pairs obtained from the fragments.
47 //
48 // Currently only for testing.
49 fragments []android.Module
Paul Duffin6a766452021-04-12 14:15:22 +010050
51 // Path to the monolithic hiddenapi-flags.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010052 hiddenAPIFlagsCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010053
54 // Path to the monolithic hiddenapi-index.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010055 hiddenAPIIndexCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010056
57 // Path to the monolithic hiddenapi-unsupported.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010058 hiddenAPIMetadataCSV android.OutputPath
Paul Duffin62d8c3b2021-04-07 20:35:11 +010059}
60
Paul Duffin62d8c3b2021-04-07 20:35:11 +010061type platformBootclasspathProperties struct {
Paul Duffinb67d8782021-04-22 11:49:41 +010062 BootclasspathFragmentsDepsProperties
Paul Duffin702210b2021-04-08 20:12:41 +010063
Paul Duffin46169772021-04-14 15:01:56 +010064 Hidden_api HiddenAPIFlagFileProperties
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010065}
66
67func platformBootclasspathFactory() android.Module {
68 m := &platformBootclasspathModule{}
Paul Duffin62d8c3b2021-04-07 20:35:11 +010069 m.AddProperties(&m.properties)
Artur Satayev97259dc2021-04-07 15:17:14 +010070 // TODO(satayev): split systemserver and apex jars into separate configs.
71 initClasspathFragment(m)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010072 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
73 return m
74}
75
Paul Duffin6a766452021-04-12 14:15:22 +010076var _ android.OutputFileProducer = (*platformBootclasspathModule)(nil)
77
Artur Satayev97259dc2021-04-07 15:17:14 +010078func (b *platformBootclasspathModule) AndroidMkEntries() (entries []android.AndroidMkEntries) {
79 entries = append(entries, android.AndroidMkEntries{
80 Class: "FAKE",
81 // Need at least one output file in order for this to take effect.
82 OutputFile: android.OptionalPathForPath(b.hiddenAPIFlagsCSV),
83 Include: "$(BUILD_PHONY_PACKAGE)",
84 })
85 entries = append(entries, b.classpathFragmentBase().getAndroidMkEntries()...)
86 return
Paul Duffin6a766452021-04-12 14:15:22 +010087}
88
89// Make the hidden API files available from the platform-bootclasspath module.
90func (b *platformBootclasspathModule) OutputFiles(tag string) (android.Paths, error) {
91 switch tag {
92 case "hiddenapi-flags.csv":
93 return android.Paths{b.hiddenAPIFlagsCSV}, nil
94 case "hiddenapi-index.csv":
95 return android.Paths{b.hiddenAPIIndexCSV}, nil
96 case "hiddenapi-metadata.csv":
97 return android.Paths{b.hiddenAPIMetadataCSV}, nil
98 }
99
100 return nil, fmt.Errorf("unknown tag %s", tag)
101}
102
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100103func (b *platformBootclasspathModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin74431d52021-04-21 14:10:42 +0100104 b.hiddenAPIDepsMutator(ctx)
105
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100106 if SkipDexpreoptBootJars(ctx) {
107 return
108 }
109
110 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
111 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
112 dexpreopt.RegisterToolDeps(ctx)
113}
114
Paul Duffin74431d52021-04-21 14:10:42 +0100115func (b *platformBootclasspathModule) hiddenAPIDepsMutator(ctx android.BottomUpMutatorContext) {
116 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
117 return
118 }
119
120 // Add dependencies onto the stub lib modules.
121 sdkKindToStubLibModules := hiddenAPIComputeMonolithicStubLibModules(ctx.Config())
122 hiddenAPIAddStubLibDependencies(ctx, sdkKindToStubLibModules)
123}
124
Paul Duffin4994d262021-04-22 12:08:59 +0100125func (b *platformBootclasspathModule) BootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
126 // Add dependencies on all the modules configured in the "art" boot image.
127 artImageConfig := genBootImageConfigs(ctx)[artBootImageName]
128 addDependenciesOntoBootImageModules(ctx, artImageConfig.modules)
Paul Duffinb432df92021-03-22 22:09:42 +0000129
Paul Duffin4994d262021-04-22 12:08:59 +0100130 // Add dependencies on all the modules configured in the "boot" boot image. That does not
131 // include modules configured in the "art" boot image.
132 bootImageConfig := b.getImageConfig(ctx)
133 addDependenciesOntoBootImageModules(ctx, bootImageConfig.modules)
Paul Duffinb432df92021-03-22 22:09:42 +0000134
Paul Duffin4994d262021-04-22 12:08:59 +0100135 // Add dependencies on all the updatable modules.
136 updatableModules := dexpreopt.GetGlobalConfig(ctx).UpdatableBootJars
137 addDependenciesOntoBootImageModules(ctx, updatableModules)
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100138
Paul Duffin4994d262021-04-22 12:08:59 +0100139 // Add dependencies on all the fragments.
140 b.properties.BootclasspathFragmentsDepsProperties.addDependenciesOntoFragments(ctx)
Paul Duffinb432df92021-03-22 22:09:42 +0000141}
142
143func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList) {
144 for i := 0; i < modules.Len(); i++ {
145 apex := modules.Apex(i)
146 name := modules.Jar(i)
147
148 addDependencyOntoApexModulePair(ctx, apex, name, platformBootclasspathModuleDepTag)
149 }
150}
151
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100152func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayev97259dc2021-04-07 15:17:14 +0100153 b.classpathFragmentBase().generateAndroidBuildActions(ctx)
154
Paul Duffin9bacf562021-04-28 21:16:02 +0100155 b.configuredModules = gatherApexModulePairDepsWithTag(ctx, platformBootclasspathModuleDepTag)
156 b.fragments = gatherApexModulePairDepsWithTag(ctx, bootclasspathFragmentDepTag)
Paul Duffinb432df92021-03-22 22:09:42 +0000157
Paul Duffin9b381ef2021-04-08 23:01:37 +0100158 b.generateHiddenAPIBuildActions(ctx, b.configuredModules, b.fragments)
Paul Duffin702210b2021-04-08 20:12:41 +0100159
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100160 // Nothing to do if skipping the dexpreopt of boot image jars.
161 if SkipDexpreoptBootJars(ctx) {
162 return
163 }
164
Paul Duffinad19d382021-04-26 16:44:00 +0100165 b.generateBootImageBuildActions(ctx)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100166}
167
168func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
169 return defaultBootImageConfig(ctx)
170}
Paul Duffin702210b2021-04-08 20:12:41 +0100171
172// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffin9b381ef2021-04-08 23:01:37 +0100173func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module, fragments []android.Module) {
Paul Duffin702210b2021-04-08 20:12:41 +0100174
Paul Duffin90b8ad32021-04-13 12:25:01 +0100175 // Save the paths to the monolithic files for retrieval via OutputFiles().
176 b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
177 b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
178 b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
Paul Duffin6a766452021-04-12 14:15:22 +0100179
Paul Duffin0b659862021-04-13 13:02:29 +0100180 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true. This is a performance
181 // optimization that can be used to reduce the incremental build time but as its name suggests it
182 // can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath.
183 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
184 paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV}
185 for _, path := range paths {
186 ctx.Build(pctx, android.BuildParams{
187 Rule: android.Touch,
188 Output: path,
189 })
190 }
191 return
192 }
193
Paul Duffin1ba24672021-04-12 23:26:14 +0100194 hiddenAPISupportingModules := []hiddenAPISupportingModule{}
Paul Duffin702210b2021-04-08 20:12:41 +0100195 for _, module := range modules {
Paul Duffin1ba24672021-04-12 23:26:14 +0100196 if h, ok := module.(hiddenAPISupportingModule); ok {
197 if h.bootDexJar() == nil {
198 ctx.ModuleErrorf("module %s does not provide a bootDexJar file", module)
Paul Duffin702210b2021-04-08 20:12:41 +0100199 }
Paul Duffin1ba24672021-04-12 23:26:14 +0100200 if h.flagsCSV() == nil {
201 ctx.ModuleErrorf("module %s does not provide a flagsCSV file", module)
202 }
203 if h.indexCSV() == nil {
204 ctx.ModuleErrorf("module %s does not provide an indexCSV file", module)
205 }
206 if h.metadataCSV() == nil {
207 ctx.ModuleErrorf("module %s does not provide a metadataCSV file", module)
208 }
209
210 if ctx.Failed() {
211 continue
212 }
213
214 hiddenAPISupportingModules = append(hiddenAPISupportingModules, h)
Paul Duffin702210b2021-04-08 20:12:41 +0100215 } else {
Paul Duffin1ba24672021-04-12 23:26:14 +0100216 ctx.ModuleErrorf("module %s of type %s does not support hidden API processing", module, ctx.OtherModuleType(module))
Paul Duffin702210b2021-04-08 20:12:41 +0100217 }
218 }
219
Paul Duffin1ba24672021-04-12 23:26:14 +0100220 moduleSpecificFlagsPaths := android.Paths{}
221 for _, module := range hiddenAPISupportingModules {
222 moduleSpecificFlagsPaths = append(moduleSpecificFlagsPaths, module.flagsCSV())
223 }
224
Paul Duffin9b381ef2021-04-08 23:01:37 +0100225 flagFileInfo := b.properties.Hidden_api.hiddenAPIFlagFileInfo(ctx)
226 for _, fragment := range fragments {
227 if ctx.OtherModuleHasProvider(fragment, hiddenAPIFlagFileInfoProvider) {
228 info := ctx.OtherModuleProvider(fragment, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo)
229 flagFileInfo.append(info)
230 }
231 }
232
233 // Store the information for testing.
234 ctx.SetProvider(hiddenAPIFlagFileInfoProvider, flagFileInfo)
Paul Duffin702210b2021-04-08 20:12:41 +0100235
236 outputPath := hiddenAPISingletonPaths(ctx).flags
237 baseFlagsPath := hiddenAPISingletonPaths(ctx).stubFlags
Paul Duffin9b381ef2021-04-08 23:01:37 +0100238 ruleToGenerateHiddenApiFlags(ctx, outputPath, baseFlagsPath, moduleSpecificFlagsPaths, flagFileInfo)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100239
Paul Duffin74431d52021-04-21 14:10:42 +0100240 b.generateHiddenAPIStubFlagsRules(ctx, hiddenAPISupportingModules)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100241 b.generateHiddenAPIIndexRules(ctx, hiddenAPISupportingModules)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100242 b.generatedHiddenAPIMetadataRules(ctx, hiddenAPISupportingModules)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100243}
244
Paul Duffin74431d52021-04-21 14:10:42 +0100245func (b *platformBootclasspathModule) generateHiddenAPIStubFlagsRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
246 bootDexJars := android.Paths{}
247 for _, module := range modules {
248 bootDexJars = append(bootDexJars, module.bootDexJar())
249 }
250
251 sdkKindToStubPaths := hiddenAPIGatherStubLibDexJarPaths(ctx)
252
253 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
254 rule := ruleToGenerateHiddenAPIStubFlagsFile(ctx, outputPath, bootDexJars, sdkKindToStubPaths)
255 rule.Build("platform-bootclasspath-monolithic-hiddenapi-stub-flags", "monolithic hidden API stub flags")
256}
257
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100258func (b *platformBootclasspathModule) generateHiddenAPIIndexRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
259 indexes := android.Paths{}
260 for _, module := range modules {
261 indexes = append(indexes, module.indexCSV())
262 }
263
264 rule := android.NewRuleBuilder(pctx, ctx)
265 rule.Command().
266 BuiltTool("merge_csv").
267 Flag("--key_field signature").
268 FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
269 FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index).
270 Inputs(indexes)
271 rule.Build("platform-bootclasspath-monolithic-hiddenapi-index", "monolithic hidden API index")
Paul Duffin702210b2021-04-08 20:12:41 +0100272}
Paul Duffin85dee5d2021-04-13 00:14:38 +0100273
274func (b *platformBootclasspathModule) generatedHiddenAPIMetadataRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
275 metadataCSVFiles := android.Paths{}
276 for _, module := range modules {
277 metadataCSVFiles = append(metadataCSVFiles, module.metadataCSV())
278 }
279
280 rule := android.NewRuleBuilder(pctx, ctx)
281
282 outputPath := hiddenAPISingletonPaths(ctx).metadata
283
284 rule.Command().
285 BuiltTool("merge_csv").
286 Flag("--key_field signature").
287 FlagWithOutput("--output=", outputPath).
288 Inputs(metadataCSVFiles)
289
290 rule.Build("platform-bootclasspath-monolithic-hiddenapi-metadata", "monolithic hidden API metadata")
291}
Paul Duffinad19d382021-04-26 16:44:00 +0100292
293// generateBootImageBuildActions generates ninja rules related to the boot image creation.
294func (b *platformBootclasspathModule) generateBootImageBuildActions(ctx android.ModuleContext) {
295 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
296 // GenerateSingletonBuildActions method as it cannot create it for itself.
297 dexpreopt.GetGlobalSoongConfig(ctx)
298
299 imageConfig := b.getImageConfig(ctx)
300 if imageConfig == nil {
301 return
302 }
303
304 global := dexpreopt.GetGlobalConfig(ctx)
305 if !shouldBuildBootImages(ctx.Config(), global) {
306 return
307 }
308
309 // Generate the framework profile rule
310 bootFrameworkProfileRule(ctx, imageConfig)
311}