blob: b1a0ac43fa4684a518818785b8f91a53770752b8 [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
171 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
172 // GenerateSingletonBuildActions method as it cannot create it for itself.
173 dexpreopt.GetGlobalSoongConfig(ctx)
174
175 imageConfig := b.getImageConfig(ctx)
176 if imageConfig == nil {
177 return
178 }
179
180 // Construct the boot image info from the config.
181 info := BootImageInfo{imageConfig: imageConfig}
182
183 // Make it available for other modules.
184 ctx.SetProvider(BootImageInfoProvider, info)
185}
186
187func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
188 return defaultBootImageConfig(ctx)
189}
Paul Duffin702210b2021-04-08 20:12:41 +0100190
191// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffin9b381ef2021-04-08 23:01:37 +0100192func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module, fragments []android.Module) {
Paul Duffin702210b2021-04-08 20:12:41 +0100193
Paul Duffin90b8ad32021-04-13 12:25:01 +0100194 // Save the paths to the monolithic files for retrieval via OutputFiles().
195 b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
196 b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
197 b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
Paul Duffin6a766452021-04-12 14:15:22 +0100198
Paul Duffin0b659862021-04-13 13:02:29 +0100199 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true. This is a performance
200 // optimization that can be used to reduce the incremental build time but as its name suggests it
201 // can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath.
202 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
203 paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV}
204 for _, path := range paths {
205 ctx.Build(pctx, android.BuildParams{
206 Rule: android.Touch,
207 Output: path,
208 })
209 }
210 return
211 }
212
Paul Duffin1ba24672021-04-12 23:26:14 +0100213 hiddenAPISupportingModules := []hiddenAPISupportingModule{}
Paul Duffin702210b2021-04-08 20:12:41 +0100214 for _, module := range modules {
Paul Duffin1ba24672021-04-12 23:26:14 +0100215 if h, ok := module.(hiddenAPISupportingModule); ok {
216 if h.bootDexJar() == nil {
217 ctx.ModuleErrorf("module %s does not provide a bootDexJar file", module)
Paul Duffin702210b2021-04-08 20:12:41 +0100218 }
Paul Duffin1ba24672021-04-12 23:26:14 +0100219 if h.flagsCSV() == nil {
220 ctx.ModuleErrorf("module %s does not provide a flagsCSV file", module)
221 }
222 if h.indexCSV() == nil {
223 ctx.ModuleErrorf("module %s does not provide an indexCSV file", module)
224 }
225 if h.metadataCSV() == nil {
226 ctx.ModuleErrorf("module %s does not provide a metadataCSV file", module)
227 }
228
229 if ctx.Failed() {
230 continue
231 }
232
233 hiddenAPISupportingModules = append(hiddenAPISupportingModules, h)
Paul Duffin702210b2021-04-08 20:12:41 +0100234 } else {
Paul Duffin1ba24672021-04-12 23:26:14 +0100235 ctx.ModuleErrorf("module %s of type %s does not support hidden API processing", module, ctx.OtherModuleType(module))
Paul Duffin702210b2021-04-08 20:12:41 +0100236 }
237 }
238
Paul Duffin1ba24672021-04-12 23:26:14 +0100239 moduleSpecificFlagsPaths := android.Paths{}
240 for _, module := range hiddenAPISupportingModules {
241 moduleSpecificFlagsPaths = append(moduleSpecificFlagsPaths, module.flagsCSV())
242 }
243
Paul Duffin9b381ef2021-04-08 23:01:37 +0100244 flagFileInfo := b.properties.Hidden_api.hiddenAPIFlagFileInfo(ctx)
245 for _, fragment := range fragments {
246 if ctx.OtherModuleHasProvider(fragment, hiddenAPIFlagFileInfoProvider) {
247 info := ctx.OtherModuleProvider(fragment, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo)
248 flagFileInfo.append(info)
249 }
250 }
251
252 // Store the information for testing.
253 ctx.SetProvider(hiddenAPIFlagFileInfoProvider, flagFileInfo)
Paul Duffin702210b2021-04-08 20:12:41 +0100254
255 outputPath := hiddenAPISingletonPaths(ctx).flags
256 baseFlagsPath := hiddenAPISingletonPaths(ctx).stubFlags
Paul Duffin9b381ef2021-04-08 23:01:37 +0100257 ruleToGenerateHiddenApiFlags(ctx, outputPath, baseFlagsPath, moduleSpecificFlagsPaths, flagFileInfo)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100258
Paul Duffin74431d52021-04-21 14:10:42 +0100259 b.generateHiddenAPIStubFlagsRules(ctx, hiddenAPISupportingModules)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100260 b.generateHiddenAPIIndexRules(ctx, hiddenAPISupportingModules)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100261 b.generatedHiddenAPIMetadataRules(ctx, hiddenAPISupportingModules)
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100262}
263
Paul Duffin74431d52021-04-21 14:10:42 +0100264func (b *platformBootclasspathModule) generateHiddenAPIStubFlagsRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
265 bootDexJars := android.Paths{}
266 for _, module := range modules {
267 bootDexJars = append(bootDexJars, module.bootDexJar())
268 }
269
270 sdkKindToStubPaths := hiddenAPIGatherStubLibDexJarPaths(ctx)
271
272 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
273 rule := ruleToGenerateHiddenAPIStubFlagsFile(ctx, outputPath, bootDexJars, sdkKindToStubPaths)
274 rule.Build("platform-bootclasspath-monolithic-hiddenapi-stub-flags", "monolithic hidden API stub flags")
275}
276
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100277func (b *platformBootclasspathModule) generateHiddenAPIIndexRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
278 indexes := android.Paths{}
279 for _, module := range modules {
280 indexes = append(indexes, module.indexCSV())
281 }
282
283 rule := android.NewRuleBuilder(pctx, ctx)
284 rule.Command().
285 BuiltTool("merge_csv").
286 Flag("--key_field signature").
287 FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
288 FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index).
289 Inputs(indexes)
290 rule.Build("platform-bootclasspath-monolithic-hiddenapi-index", "monolithic hidden API index")
Paul Duffin702210b2021-04-08 20:12:41 +0100291}
Paul Duffin85dee5d2021-04-13 00:14:38 +0100292
293func (b *platformBootclasspathModule) generatedHiddenAPIMetadataRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
294 metadataCSVFiles := android.Paths{}
295 for _, module := range modules {
296 metadataCSVFiles = append(metadataCSVFiles, module.metadataCSV())
297 }
298
299 rule := android.NewRuleBuilder(pctx, ctx)
300
301 outputPath := hiddenAPISingletonPaths(ctx).metadata
302
303 rule.Command().
304 BuiltTool("merge_csv").
305 Flag("--key_field signature").
306 FlagWithOutput("--output=", outputPath).
307 Inputs(metadataCSVFiles)
308
309 rule.Build("platform-bootclasspath-monolithic-hiddenapi-metadata", "monolithic hidden API metadata")
310}