blob: 254c40535f304c940a4bb490ae73086caf657d6f [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 (
satayevd604b212021-07-21 14:23:52 +010035 platformBootclasspathArtBootJarDepTag = bootclasspathDependencyTag{name: "art-boot-jar"}
36 platformBootclasspathBootJarDepTag = bootclasspathDependencyTag{name: "platform-boot-jar"}
37 platformBootclasspathApexBootJarDepTag = bootclasspathDependencyTag{name: "apex-boot-jar"}
Paul Duffin01b463b2021-04-26 20:05:39 +010038)
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.
Paul Duffinb432df92021-03-22 22:09:42 +000047 configuredModules []android.Module
Paul Duffin62d8c3b2021-04-07 20:35:11 +010048
49 // The apex:module pairs obtained from the fragments.
Paul Duffin62d8c3b2021-04-07 20:35:11 +010050 fragments []android.Module
Paul Duffin6a766452021-04-12 14:15:22 +010051
52 // Path to the monolithic hiddenapi-flags.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010053 hiddenAPIFlagsCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010054
55 // Path to the monolithic hiddenapi-index.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010056 hiddenAPIIndexCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010057
58 // Path to the monolithic hiddenapi-unsupported.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010059 hiddenAPIMetadataCSV android.OutputPath
Paul Duffin62d8c3b2021-04-07 20:35:11 +010060}
61
Paul Duffin62d8c3b2021-04-07 20:35:11 +010062type platformBootclasspathProperties struct {
Paul Duffinb67d8782021-04-22 11:49:41 +010063 BootclasspathFragmentsDepsProperties
Paul Duffin702210b2021-04-08 20:12:41 +010064
Paul Duffin9b61abb2022-07-27 16:16:54 +000065 HiddenAPIFlagFileProperties
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010066}
67
Paul Duffine3ecce62021-04-29 10:34:11 +010068func platformBootclasspathFactory() android.SingletonModule {
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010069 m := &platformBootclasspathModule{}
Paul Duffin62d8c3b2021-04-07 20:35:11 +010070 m.AddProperties(&m.properties)
satayev95e9c5b2021-04-29 11:50:26 +010071 initClasspathFragment(m, BOOTCLASSPATH)
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 })
satayev128ce2f2021-05-06 13:21:15 +010085 entries = append(entries, b.classpathFragmentBase().androidMkEntries()...)
Artur Satayev97259dc2021-04-07 15:17:14 +010086 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 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
107 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
108 dexpreopt.RegisterToolDeps(ctx)
109}
110
Paul Duffin74431d52021-04-21 14:10:42 +0100111func (b *platformBootclasspathModule) hiddenAPIDepsMutator(ctx android.BottomUpMutatorContext) {
112 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
113 return
114 }
115
116 // Add dependencies onto the stub lib modules.
Paul Duffin31fad802021-06-18 18:14:25 +0100117 apiLevelToStubLibModules := hiddenAPIComputeMonolithicStubLibModules(ctx.Config())
118 hiddenAPIAddStubLibDependencies(ctx, apiLevelToStubLibModules)
Paul Duffin74431d52021-04-21 14:10:42 +0100119}
120
Paul Duffin4994d262021-04-22 12:08:59 +0100121func (b *platformBootclasspathModule) BootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
122 // Add dependencies on all the modules configured in the "art" boot image.
123 artImageConfig := genBootImageConfigs(ctx)[artBootImageName]
Paul Duffin01b463b2021-04-26 20:05:39 +0100124 addDependenciesOntoBootImageModules(ctx, artImageConfig.modules, platformBootclasspathArtBootJarDepTag)
Paul Duffinb432df92021-03-22 22:09:42 +0000125
Paul Duffin01b463b2021-04-26 20:05:39 +0100126 // Add dependencies on all the non-updatable module configured in the "boot" boot image. That does
127 // not include modules configured in the "art" boot image.
Jiakai Zhangd49324d2023-02-24 17:05:02 +0000128 bootImageConfig := defaultBootImageConfig(ctx)
satayevd604b212021-07-21 14:23:52 +0100129 addDependenciesOntoBootImageModules(ctx, bootImageConfig.modules, platformBootclasspathBootJarDepTag)
Paul Duffinb432df92021-03-22 22:09:42 +0000130
satayevd604b212021-07-21 14:23:52 +0100131 // Add dependencies on all the apex jars.
132 apexJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
133 addDependenciesOntoBootImageModules(ctx, apexJars, platformBootclasspathApexBootJarDepTag)
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100134
Paul Duffin4994d262021-04-22 12:08:59 +0100135 // Add dependencies on all the fragments.
136 b.properties.BootclasspathFragmentsDepsProperties.addDependenciesOntoFragments(ctx)
Paul Duffinb432df92021-03-22 22:09:42 +0000137}
138
Paul Duffin01b463b2021-04-26 20:05:39 +0100139func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList, tag bootclasspathDependencyTag) {
Paul Duffinb432df92021-03-22 22:09:42 +0000140 for i := 0; i < modules.Len(); i++ {
141 apex := modules.Apex(i)
142 name := modules.Jar(i)
143
Paul Duffin01b463b2021-04-26 20:05:39 +0100144 addDependencyOntoApexModulePair(ctx, apex, name, tag)
Paul Duffinb432df92021-03-22 22:09:42 +0000145 }
146}
147
Paul Duffine3ecce62021-04-29 10:34:11 +0100148// GenerateSingletonBuildActions does nothing and must never do anything.
149//
150// This module only implements android.SingletonModule so that it can implement
151// android.SingletonMakeVarsProvider.
152func (b *platformBootclasspathModule) GenerateSingletonBuildActions(android.SingletonContext) {
153 // Keep empty
154}
155
156func (d *platformBootclasspathModule) MakeVars(ctx android.MakeVarsContext) {
Paul Duffin12d29b72021-04-29 13:50:01 +0100157 d.generateHiddenApiMakeVars(ctx)
Paul Duffine3ecce62021-04-29 10:34:11 +0100158}
159
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100160func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayevd604b212021-07-21 14:23:52 +0100161 // Gather all the dependencies from the art, platform, and apex boot jars.
Paul Duffin01b463b2021-04-26 20:05:39 +0100162 artModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathArtBootJarDepTag)
satayevd604b212021-07-21 14:23:52 +0100163 platformModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathBootJarDepTag)
164 apexModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathApexBootJarDepTag)
Paul Duffin01b463b2021-04-26 20:05:39 +0100165
166 // Concatenate them all, in order as they would appear on the bootclasspath.
167 var allModules []android.Module
168 allModules = append(allModules, artModules...)
satayevd604b212021-07-21 14:23:52 +0100169 allModules = append(allModules, platformModules...)
170 allModules = append(allModules, apexModules...)
Paul Duffin01b463b2021-04-26 20:05:39 +0100171 b.configuredModules = allModules
172
173 // Gather all the fragments dependencies.
Paul Duffin9bacf562021-04-28 21:16:02 +0100174 b.fragments = gatherApexModulePairDepsWithTag(ctx, bootclasspathFragmentDepTag)
Paul Duffinb432df92021-03-22 22:09:42 +0000175
Paul Duffinf23bc472021-04-27 12:42:20 +0100176 // Check the configuration of the boot modules.
177 // ART modules are checked by the art-bootclasspath-fragment.
satayevd604b212021-07-21 14:23:52 +0100178 b.checkPlatformModules(ctx, platformModules)
179 b.checkApexModules(ctx, apexModules)
Paul Duffinf23bc472021-04-27 12:42:20 +0100180
satayev013485b2021-05-06 23:38:10 +0100181 b.generateClasspathProtoBuildActions(ctx)
182
Paul Duffinc8ead412021-06-07 19:28:15 +0100183 bootDexJarByModule := b.generateHiddenAPIBuildActions(ctx, b.configuredModules, b.fragments)
184 buildRuleForBootJarsPackageCheck(ctx, bootDexJarByModule)
Paul Duffin702210b2021-04-08 20:12:41 +0100185
satayevd604b212021-07-21 14:23:52 +0100186 b.generateBootImageBuildActions(ctx, platformModules, apexModules)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100187}
188
satayev013485b2021-05-06 23:38:10 +0100189// Generate classpaths.proto config
190func (b *platformBootclasspathModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
satayevb3090502021-06-15 17:49:10 +0100191 configuredJars := b.configuredJars(ctx)
satayev013485b2021-05-06 23:38:10 +0100192 // ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
satayevb3090502021-06-15 17:49:10 +0100193 classpathJars := configuredJarListToClasspathJars(ctx, configuredJars, BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
194 b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, configuredJars, classpathJars)
satayev013485b2021-05-06 23:38:10 +0100195}
196
satayev142ed272021-06-15 16:21:17 +0100197func (b *platformBootclasspathModule) configuredJars(ctx android.ModuleContext) android.ConfiguredJarList {
satayevb3090502021-06-15 17:49:10 +0100198 // Include all non APEX jars
Jiakai Zhangd49324d2023-02-24 17:05:02 +0000199 jars := defaultBootImageConfig(ctx).modules
satayevb3090502021-06-15 17:49:10 +0100200
201 // Include jars from APEXes that don't populate their classpath proto config.
satayevd604b212021-07-21 14:23:52 +0100202 remainingJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
satayevb3090502021-06-15 17:49:10 +0100203 for _, fragment := range b.fragments {
204 info := ctx.OtherModuleProvider(fragment, ClasspathFragmentProtoContentInfoProvider).(ClasspathFragmentProtoContentInfo)
205 if info.ClasspathFragmentProtoGenerated {
206 remainingJars = remainingJars.RemoveList(info.ClasspathFragmentProtoContents)
207 }
208 }
209 for i := 0; i < remainingJars.Len(); i++ {
210 jars = jars.Append(remainingJars.Apex(i), remainingJars.Jar(i))
211 }
212
213 return jars
satayev013485b2021-05-06 23:38:10 +0100214}
215
satayevd604b212021-07-21 14:23:52 +0100216// checkPlatformModules ensures that the non-updatable modules supplied are not part of an
217// apex module.
218func (b *platformBootclasspathModule) checkPlatformModules(ctx android.ModuleContext, modules []android.Module) {
219 // TODO(satayev): change this check to only allow core-icu4j, all apex jars should not be here.
Paul Duffinf23bc472021-04-27 12:42:20 +0100220 for _, m := range modules {
221 apexInfo := ctx.OtherModuleProvider(m, android.ApexInfoProvider).(android.ApexInfo)
222 fromUpdatableApex := apexInfo.Updatable
223 if fromUpdatableApex {
224 // error: this jar is part of an updatable apex
Jiyong Parkab50b072021-05-12 17:13:56 +0900225 ctx.ModuleErrorf("module %q from updatable apexes %q is not allowed in the framework boot image", ctx.OtherModuleName(m), apexInfo.InApexVariants)
Paul Duffinf23bc472021-04-27 12:42:20 +0100226 } else {
227 // ok: this jar is part of the platform or a non-updatable apex
228 }
229 }
230}
231
satayevd604b212021-07-21 14:23:52 +0100232// checkApexModules ensures that the apex modules supplied are not from the platform.
233func (b *platformBootclasspathModule) checkApexModules(ctx android.ModuleContext, modules []android.Module) {
Paul Duffinf23bc472021-04-27 12:42:20 +0100234 for _, m := range modules {
235 apexInfo := ctx.OtherModuleProvider(m, android.ApexInfoProvider).(android.ApexInfo)
236 fromUpdatableApex := apexInfo.Updatable
237 if fromUpdatableApex {
238 // ok: this jar is part of an updatable apex
239 } else {
240 name := ctx.OtherModuleName(m)
241 if apexInfo.IsForPlatform() {
Paul Duffin7487a7a2021-05-19 09:36:09 +0100242 // If AlwaysUsePrebuiltSdks() returns true then it is possible that the updatable list will
243 // include platform variants of a prebuilt module due to workarounds elsewhere. In that case
244 // do not treat this as an error.
245 // TODO(b/179354495): Always treat this as an error when migration to bootclasspath_fragment
246 // modules is complete.
247 if !ctx.Config().AlwaysUsePrebuiltSdks() {
248 // error: this jar is part of the platform
satayevd604b212021-07-21 14:23:52 +0100249 ctx.ModuleErrorf("module %q from platform is not allowed in the apex boot jars list", name)
Paul Duffin7487a7a2021-05-19 09:36:09 +0100250 }
Paul Duffinf23bc472021-04-27 12:42:20 +0100251 } else {
252 // TODO(b/177892522): Treat this as an error.
253 // Cannot do that at the moment because framework-wifi and framework-tethering are in the
satayevd604b212021-07-21 14:23:52 +0100254 // PRODUCT_APEX_BOOT_JARS but not marked as updatable in AOSP.
Paul Duffinf23bc472021-04-27 12:42:20 +0100255 }
256 }
257 }
258}
259
Paul Duffin702210b2021-04-08 20:12:41 +0100260// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffinc8ead412021-06-07 19:28:15 +0100261func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module, fragments []android.Module) bootDexJarByModule {
Paul Duffin702210b2021-04-08 20:12:41 +0100262
Paul Duffin90b8ad32021-04-13 12:25:01 +0100263 // Save the paths to the monolithic files for retrieval via OutputFiles().
264 b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
265 b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
266 b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
Paul Duffin6a766452021-04-12 14:15:22 +0100267
Adrian Roose95a15e2021-06-21 16:03:11 +0200268 bootDexJarByModule := extractBootDexJarsFromModules(ctx, modules)
269
Paul Duffin0b659862021-04-13 13:02:29 +0100270 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true. This is a performance
271 // optimization that can be used to reduce the incremental build time but as its name suggests it
272 // can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath.
Vishnu Nair0dbd02a2021-04-30 00:24:07 +0000273 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
Paul Duffin0b659862021-04-13 13:02:29 +0100274 paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV}
275 for _, path := range paths {
276 ctx.Build(pctx, android.BuildParams{
277 Rule: android.Touch,
278 Output: path,
279 })
280 }
Adrian Roose95a15e2021-06-21 16:03:11 +0200281 return bootDexJarByModule
Paul Duffin0b659862021-04-13 13:02:29 +0100282 }
283
Paul Duffin89f570a2021-06-16 01:42:33 +0100284 // Construct a list of ClasspathElement objects from the modules and fragments.
285 classpathElements := CreateClasspathElements(ctx, modules, fragments)
286
287 monolithicInfo := b.createAndProvideMonolithicHiddenAPIInfo(ctx, classpathElements)
288
289 // Extract the classes jars only from those libraries that do not have corresponding fragments as
290 // the fragments will have already provided the flags that are needed.
291 classesJars := monolithicInfo.ClassesJars
292
Paul Duffin4539a372021-06-23 23:20:43 +0100293 // Create the input to pass to buildRuleToGenerateHiddenAPIStubFlagsFile
Paul Duffin1352f7c2021-05-21 22:18:49 +0100294 input := newHiddenAPIFlagInput()
295
296 // Gather stub library information from the dependencies on modules provided by
297 // hiddenAPIComputeMonolithicStubLibModules.
298 input.gatherStubLibInfo(ctx, nil)
299
300 // Use the flag files from this module and all the fragments.
301 input.FlagFilesByCategory = monolithicInfo.FlagsFilesByCategory
Paul Duffin74431d52021-04-21 14:10:42 +0100302
Paul Duffin537ea3d2021-05-14 10:38:00 +0100303 // Generate the monolithic stub-flags.csv file.
Paul Duffin537ea3d2021-05-14 10:38:00 +0100304 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
Paul Duffin67b9d612021-07-21 17:38:47 +0100305 buildRuleToGenerateHiddenAPIStubFlagsFile(ctx, "platform-bootclasspath-monolithic-hiddenapi-stub-flags", "monolithic hidden API stub flags", stubFlags, bootDexJarByModule.bootDexJars(), input, monolithicInfo.StubFlagSubsets)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100306
Paul Duffin537ea3d2021-05-14 10:38:00 +0100307 // Generate the annotation-flags.csv file from all the module annotations.
Paul Duffind061d402021-06-07 21:36:01 +0100308 annotationFlags := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "annotation-flags-from-classes.csv")
309 buildRuleToGenerateAnnotationFlags(ctx, "intermediate hidden API flags", classesJars, stubFlags, annotationFlags)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100310
Paul Duffind061d402021-06-07 21:36:01 +0100311 // Generate the monolithic hiddenapi-flags.csv file.
312 //
313 // Use annotation flags generated directly from the classes jars as well as annotation flag files
314 // provided by prebuilts.
315 allAnnotationFlagFiles := android.Paths{annotationFlags}
316 allAnnotationFlagFiles = append(allAnnotationFlagFiles, monolithicInfo.AnnotationFlagsPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100317 allFlags := hiddenAPISingletonPaths(ctx).flags
Paul Duffin67b9d612021-07-21 17:38:47 +0100318 buildRuleToGenerateHiddenApiFlags(ctx, "hiddenAPIFlagsFile", "monolithic hidden API flags", allFlags, stubFlags, allAnnotationFlagFiles, monolithicInfo.FlagsFilesByCategory, monolithicInfo.FlagSubsets, android.OptionalPath{})
Paul Duffin537ea3d2021-05-14 10:38:00 +0100319
320 // Generate an intermediate monolithic hiddenapi-metadata.csv file directly from the annotations
321 // in the source code.
Paul Duffind061d402021-06-07 21:36:01 +0100322 intermediateMetadataCSV := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "metadata-from-classes.csv")
323 buildRuleToGenerateMetadata(ctx, "intermediate hidden API metadata", classesJars, stubFlags, intermediateMetadataCSV)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100324
Paul Duffind061d402021-06-07 21:36:01 +0100325 // Generate the monolithic hiddenapi-metadata.csv file.
326 //
327 // Use metadata files generated directly from the classes jars as well as metadata files provided
328 // by prebuilts.
329 //
330 // This has the side effect of ensuring that the output file uses | quotes just in case that is
331 // important for the tools that consume the metadata file.
332 allMetadataFlagFiles := android.Paths{intermediateMetadataCSV}
333 allMetadataFlagFiles = append(allMetadataFlagFiles, monolithicInfo.MetadataPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100334 metadataCSV := hiddenAPISingletonPaths(ctx).metadata
Paul Duffind061d402021-06-07 21:36:01 +0100335 b.buildRuleMergeCSV(ctx, "monolithic hidden API metadata", allMetadataFlagFiles, metadataCSV)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100336
Paul Duffind061d402021-06-07 21:36:01 +0100337 // Generate an intermediate monolithic hiddenapi-index.csv file directly from the CSV files in the
338 // classes jars.
339 intermediateIndexCSV := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "index-from-classes.csv")
340 buildRuleToGenerateIndex(ctx, "intermediate hidden API index", classesJars, intermediateIndexCSV)
341
342 // Generate the monolithic hiddenapi-index.csv file.
343 //
344 // Use index files generated directly from the classes jars as well as index files provided
345 // by prebuilts.
346 allIndexFlagFiles := android.Paths{intermediateIndexCSV}
347 allIndexFlagFiles = append(allIndexFlagFiles, monolithicInfo.IndexPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100348 indexCSV := hiddenAPISingletonPaths(ctx).index
Paul Duffind061d402021-06-07 21:36:01 +0100349 b.buildRuleMergeCSV(ctx, "monolithic hidden API index", allIndexFlagFiles, indexCSV)
Paul Duffinc8ead412021-06-07 19:28:15 +0100350
351 return bootDexJarByModule
Paul Duffin74431d52021-04-21 14:10:42 +0100352}
353
Paul Duffin438eb572021-05-21 16:58:23 +0100354// createAndProvideMonolithicHiddenAPIInfo creates a MonolithicHiddenAPIInfo and provides it for
355// testing.
Paul Duffin89f570a2021-06-16 01:42:33 +0100356func (b *platformBootclasspathModule) createAndProvideMonolithicHiddenAPIInfo(ctx android.ModuleContext, classpathElements ClasspathElements) MonolithicHiddenAPIInfo {
Paul Duffin1352f7c2021-05-21 22:18:49 +0100357 // Create a temporary input structure in which to collate information provided directly by this
358 // module, either through properties or direct dependencies.
359 temporaryInput := newHiddenAPIFlagInput()
360
361 // Create paths to the flag files specified in the properties.
Paul Duffin9b61abb2022-07-27 16:16:54 +0000362 temporaryInput.extractFlagFilesFromProperties(ctx, &b.properties.HiddenAPIFlagFileProperties)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100363
364 // Create the monolithic info, by starting with the flag files specified on this and then merging
365 // in information from all the fragment dependencies of this.
Paul Duffin89f570a2021-06-16 01:42:33 +0100366 monolithicInfo := newMonolithicHiddenAPIInfo(ctx, temporaryInput.FlagFilesByCategory, classpathElements)
Paul Duffin438eb572021-05-21 16:58:23 +0100367
368 // Store the information for testing.
Paul Duffin524c82c2021-06-09 14:39:28 +0100369 ctx.SetProvider(MonolithicHiddenAPIInfoProvider, monolithicInfo)
Paul Duffin438eb572021-05-21 16:58:23 +0100370 return monolithicInfo
371}
372
Paul Duffin537ea3d2021-05-14 10:38:00 +0100373func (b *platformBootclasspathModule) buildRuleMergeCSV(ctx android.ModuleContext, desc string, inputPaths android.Paths, outputPath android.WritablePath) {
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100374 rule := android.NewRuleBuilder(pctx, ctx)
375 rule.Command().
376 BuiltTool("merge_csv").
377 Flag("--key_field signature").
Paul Duffin85dee5d2021-04-13 00:14:38 +0100378 FlagWithOutput("--output=", outputPath).
Paul Duffin537ea3d2021-05-14 10:38:00 +0100379 Inputs(inputPaths)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100380
Paul Duffin537ea3d2021-05-14 10:38:00 +0100381 rule.Build(desc, desc)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100382}
Paul Duffinad19d382021-04-26 16:44:00 +0100383
Paul Duffin12d29b72021-04-29 13:50:01 +0100384// generateHiddenApiMakeVars generates make variables needed by hidden API related make rules, e.g.
385// veridex and run-appcompat.
386func (b *platformBootclasspathModule) generateHiddenApiMakeVars(ctx android.MakeVarsContext) {
387 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
388 return
389 }
390 // INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
391 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", b.hiddenAPIFlagsCSV.String())
392}
393
Paul Duffinad19d382021-04-26 16:44:00 +0100394// generateBootImageBuildActions generates ninja rules related to the boot image creation.
satayevd604b212021-07-21 14:23:52 +0100395func (b *platformBootclasspathModule) generateBootImageBuildActions(ctx android.ModuleContext, platformModules, apexModules []android.Module) {
Paul Duffinad19d382021-04-26 16:44:00 +0100396 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
397 // GenerateSingletonBuildActions method as it cannot create it for itself.
398 dexpreopt.GetGlobalSoongConfig(ctx)
399
Paul Duffinad19d382021-04-26 16:44:00 +0100400 global := dexpreopt.GetGlobalConfig(ctx)
401 if !shouldBuildBootImages(ctx.Config(), global) {
402 return
403 }
404
Jiakai Zhangd49324d2023-02-24 17:05:02 +0000405 frameworkBootImageConfig := defaultBootImageConfig(ctx)
406 bootFrameworkProfileRule(ctx, frameworkBootImageConfig)
407 b.generateBootImage(ctx, frameworkBootImageName, platformModules)
Jiakai Zhangb8796202023-03-06 19:16:48 +0000408 b.generateBootImage(ctx, mainlineBootImageName, apexModules)
Jiakai Zhangd49324d2023-02-24 17:05:02 +0000409 b.copyApexBootJarsForAppsDexpreopt(ctx, apexModules)
410 dumpOatRules(ctx, frameworkBootImageConfig)
411}
Paul Duffin4c094422021-04-26 20:10:48 +0100412
Jiakai Zhangd49324d2023-02-24 17:05:02 +0000413func (b *platformBootclasspathModule) generateBootImage(ctx android.ModuleContext, imageName string, modules []android.Module) {
414 imageConfig := genBootImageConfigs(ctx)[imageName]
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100415
Jiakai Zhangd49324d2023-02-24 17:05:02 +0000416 // Copy module dex jars to their predefined locations.
417 bootDexJarsByModule := extractEncodedDexJarsFromModules(ctx, modules)
418 copyBootJarsToPredefinedLocations(ctx, bootDexJarsByModule, imageConfig.dexPathsByModule)
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100419
Paul Duffin2fc82ad2021-04-29 23:36:12 +0100420 // Build a profile for the image config and then use that to build the boot image.
421 profile := bootImageProfileRule(ctx, imageConfig)
Paul Duffin56afb272021-07-01 22:04:22 +0100422
Jiakai Zhangb95f8342023-05-02 14:35:44 +0100423 // If dexpreopt of boot image jars should be skipped, generate only a profile.
424 global := dexpreopt.GetGlobalConfig(ctx)
425 if global.DisablePreoptBootImages {
426 return
427 }
428
Paul Duffina56be7d2021-07-02 13:00:43 +0100429 // Build boot image files for the android variants.
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100430 androidBootImageFiles := buildBootImageVariantsForAndroidOs(ctx, imageConfig, profile)
Paul Duffina56be7d2021-07-02 13:00:43 +0100431
432 // Zip the android variant boot image files up.
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100433 buildBootImageZipInPredefinedLocation(ctx, imageConfig, androidBootImageFiles.byArch)
Paul Duffina56be7d2021-07-02 13:00:43 +0100434
435 // Build boot image files for the host variants. There are use directly by ART host side tests.
436 buildBootImageVariantsForBuildOs(ctx, imageConfig, profile)
Jiakai Zhangd49324d2023-02-24 17:05:02 +0000437}
Paul Duffin2fc82ad2021-04-29 23:36:12 +0100438
Jiakai Zhangd49324d2023-02-24 17:05:02 +0000439// Copy apex module dex jars to their predefined locations. They will be used for dexpreopt for apps.
440func (b *platformBootclasspathModule) copyApexBootJarsForAppsDexpreopt(ctx android.ModuleContext, apexModules []android.Module) {
441 config := GetApexBootConfig(ctx)
442 apexBootDexJarsByModule := extractEncodedDexJarsFromModules(ctx, apexModules)
443 copyBootJarsToPredefinedLocations(ctx, apexBootDexJarsByModule, config.dexPathsByModule)
Paul Duffinad19d382021-04-26 16:44:00 +0100444}