blob: 6503eca87c99a12fd21929a83e7c46d7a11df537 [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 Duffinb432df92021-03-22 22:09:42 +0000155 ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) {
156 tag := ctx.OtherModuleDependencyTag(module)
157 if tag == platformBootclasspathModuleDepTag {
158 b.configuredModules = append(b.configuredModules, module)
Paul Duffinb67d8782021-04-22 11:49:41 +0100159 } else if tag == bootclasspathFragmentDepTag {
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100160 b.fragments = append(b.fragments, module)
Paul Duffinb432df92021-03-22 22:09:42 +0000161 }
162 })
163
Paul Duffin9b381ef2021-04-08 23:01:37 +0100164 b.generateHiddenAPIBuildActions(ctx, b.configuredModules, b.fragments)
Paul Duffin702210b2021-04-08 20:12:41 +0100165
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100166 // Nothing to do if skipping the dexpreopt of boot image jars.
167 if SkipDexpreoptBootJars(ctx) {
168 return
169 }
170
Paul Duffinad19d382021-04-26 16:44:00 +0100171 b.generateBootImageBuildActions(ctx)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100172}
173
174func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
175 return defaultBootImageConfig(ctx)
176}
Paul Duffin702210b2021-04-08 20:12:41 +0100177
178// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffin9b381ef2021-04-08 23:01:37 +0100179func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module, fragments []android.Module) {
Paul Duffin702210b2021-04-08 20:12:41 +0100180
Paul Duffin90b8ad32021-04-13 12:25:01 +0100181 // Save the paths to the monolithic files for retrieval via OutputFiles().
182 b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
183 b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
184 b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
Paul Duffin6a766452021-04-12 14:15:22 +0100185
Paul Duffin0b659862021-04-13 13:02:29 +0100186 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true. This is a performance
187 // optimization that can be used to reduce the incremental build time but as its name suggests it
188 // can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath.
189 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
190 paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV}
191 for _, path := range paths {
192 ctx.Build(pctx, android.BuildParams{
193 Rule: android.Touch,
194 Output: path,
195 })
196 }
197 return
198 }
199
Paul Duffin1ba24672021-04-12 23:26:14 +0100200 hiddenAPISupportingModules := []hiddenAPISupportingModule{}
Paul Duffin702210b2021-04-08 20:12:41 +0100201 for _, module := range modules {
Paul Duffin1ba24672021-04-12 23:26:14 +0100202 if h, ok := module.(hiddenAPISupportingModule); ok {
203 if h.bootDexJar() == nil {
204 ctx.ModuleErrorf("module %s does not provide a bootDexJar file", module)
Paul Duffin702210b2021-04-08 20:12:41 +0100205 }
Paul Duffin1ba24672021-04-12 23:26:14 +0100206 if h.flagsCSV() == nil {
207 ctx.ModuleErrorf("module %s does not provide a flagsCSV file", module)
208 }
209 if h.indexCSV() == nil {
210 ctx.ModuleErrorf("module %s does not provide an indexCSV file", module)
211 }
212 if h.metadataCSV() == nil {
213 ctx.ModuleErrorf("module %s does not provide a metadataCSV file", module)
214 }
215
216 if ctx.Failed() {
217 continue
218 }
219
220 hiddenAPISupportingModules = append(hiddenAPISupportingModules, h)
Paul Duffin702210b2021-04-08 20:12:41 +0100221 } else {
Paul Duffin1ba24672021-04-12 23:26:14 +0100222 ctx.ModuleErrorf("module %s of type %s does not support hidden API processing", module, ctx.OtherModuleType(module))
Paul Duffin702210b2021-04-08 20:12:41 +0100223 }
224 }
225
Paul Duffin1ba24672021-04-12 23:26:14 +0100226 moduleSpecificFlagsPaths := android.Paths{}
227 for _, module := range hiddenAPISupportingModules {
228 moduleSpecificFlagsPaths = append(moduleSpecificFlagsPaths, module.flagsCSV())
229 }
230
Paul Duffin9b381ef2021-04-08 23:01:37 +0100231 flagFileInfo := b.properties.Hidden_api.hiddenAPIFlagFileInfo(ctx)
232 for _, fragment := range fragments {
233 if ctx.OtherModuleHasProvider(fragment, hiddenAPIFlagFileInfoProvider) {
234 info := ctx.OtherModuleProvider(fragment, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo)
235 flagFileInfo.append(info)
236 }
237 }
238
239 // Store the information for testing.
240 ctx.SetProvider(hiddenAPIFlagFileInfoProvider, flagFileInfo)
Paul Duffin702210b2021-04-08 20:12:41 +0100241
242 outputPath := hiddenAPISingletonPaths(ctx).flags
243 baseFlagsPath := hiddenAPISingletonPaths(ctx).stubFlags
Paul Duffin9b381ef2021-04-08 23:01:37 +0100244 ruleToGenerateHiddenApiFlags(ctx, outputPath, baseFlagsPath, moduleSpecificFlagsPaths, flagFileInfo)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100245
Paul Duffin74431d52021-04-21 14:10:42 +0100246 b.generateHiddenAPIStubFlagsRules(ctx, hiddenAPISupportingModules)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100247 b.generateHiddenAPIIndexRules(ctx, hiddenAPISupportingModules)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100248 b.generatedHiddenAPIMetadataRules(ctx, hiddenAPISupportingModules)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100249}
250
Paul Duffin74431d52021-04-21 14:10:42 +0100251func (b *platformBootclasspathModule) generateHiddenAPIStubFlagsRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
252 bootDexJars := android.Paths{}
253 for _, module := range modules {
254 bootDexJars = append(bootDexJars, module.bootDexJar())
255 }
256
257 sdkKindToStubPaths := hiddenAPIGatherStubLibDexJarPaths(ctx)
258
259 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
260 rule := ruleToGenerateHiddenAPIStubFlagsFile(ctx, outputPath, bootDexJars, sdkKindToStubPaths)
261 rule.Build("platform-bootclasspath-monolithic-hiddenapi-stub-flags", "monolithic hidden API stub flags")
262}
263
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100264func (b *platformBootclasspathModule) generateHiddenAPIIndexRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
265 indexes := android.Paths{}
266 for _, module := range modules {
267 indexes = append(indexes, module.indexCSV())
268 }
269
270 rule := android.NewRuleBuilder(pctx, ctx)
271 rule.Command().
272 BuiltTool("merge_csv").
273 Flag("--key_field signature").
274 FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
275 FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index).
276 Inputs(indexes)
277 rule.Build("platform-bootclasspath-monolithic-hiddenapi-index", "monolithic hidden API index")
Paul Duffin702210b2021-04-08 20:12:41 +0100278}
Paul Duffin85dee5d2021-04-13 00:14:38 +0100279
280func (b *platformBootclasspathModule) generatedHiddenAPIMetadataRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
281 metadataCSVFiles := android.Paths{}
282 for _, module := range modules {
283 metadataCSVFiles = append(metadataCSVFiles, module.metadataCSV())
284 }
285
286 rule := android.NewRuleBuilder(pctx, ctx)
287
288 outputPath := hiddenAPISingletonPaths(ctx).metadata
289
290 rule.Command().
291 BuiltTool("merge_csv").
292 Flag("--key_field signature").
293 FlagWithOutput("--output=", outputPath).
294 Inputs(metadataCSVFiles)
295
296 rule.Build("platform-bootclasspath-monolithic-hiddenapi-metadata", "monolithic hidden API metadata")
297}
Paul Duffinad19d382021-04-26 16:44:00 +0100298
299// generateBootImageBuildActions generates ninja rules related to the boot image creation.
300func (b *platformBootclasspathModule) generateBootImageBuildActions(ctx android.ModuleContext) {
301 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
302 // GenerateSingletonBuildActions method as it cannot create it for itself.
303 dexpreopt.GetGlobalSoongConfig(ctx)
304
305 imageConfig := b.getImageConfig(ctx)
306 if imageConfig == nil {
307 return
308 }
309
310 global := dexpreopt.GetGlobalConfig(ctx)
311 if !shouldBuildBootImages(ctx.Config(), global) {
312 return
313 }
314
315 // Generate the framework profile rule
316 bootFrameworkProfileRule(ctx, imageConfig)
317}