blob: 8d4cf682343199ec6c840257369f82088fc647a0 [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) {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000029 ctx.RegisterParallelSingletonModuleType("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
Anton Hanssonb3738ed2023-09-21 16:23:20 +000060
61 // Path to a srcjar containing all the transitive sources of the bootclasspath.
62 srcjar android.OutputPath
Paul Duffin62d8c3b2021-04-07 20:35:11 +010063}
64
Paul Duffin62d8c3b2021-04-07 20:35:11 +010065type platformBootclasspathProperties struct {
Paul Duffinb67d8782021-04-22 11:49:41 +010066 BootclasspathFragmentsDepsProperties
Paul Duffin702210b2021-04-08 20:12:41 +010067
Paul Duffin9b61abb2022-07-27 16:16:54 +000068 HiddenAPIFlagFileProperties
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010069}
70
Paul Duffine3ecce62021-04-29 10:34:11 +010071func platformBootclasspathFactory() android.SingletonModule {
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010072 m := &platformBootclasspathModule{}
Paul Duffin62d8c3b2021-04-07 20:35:11 +010073 m.AddProperties(&m.properties)
satayev95e9c5b2021-04-29 11:50:26 +010074 initClasspathFragment(m, BOOTCLASSPATH)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010075 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
76 return m
77}
78
Paul Duffin6a766452021-04-12 14:15:22 +010079var _ android.OutputFileProducer = (*platformBootclasspathModule)(nil)
80
Artur Satayev97259dc2021-04-07 15:17:14 +010081func (b *platformBootclasspathModule) AndroidMkEntries() (entries []android.AndroidMkEntries) {
82 entries = append(entries, android.AndroidMkEntries{
83 Class: "FAKE",
84 // Need at least one output file in order for this to take effect.
85 OutputFile: android.OptionalPathForPath(b.hiddenAPIFlagsCSV),
86 Include: "$(BUILD_PHONY_PACKAGE)",
87 })
satayev128ce2f2021-05-06 13:21:15 +010088 entries = append(entries, b.classpathFragmentBase().androidMkEntries()...)
Artur Satayev97259dc2021-04-07 15:17:14 +010089 return
Paul Duffin6a766452021-04-12 14:15:22 +010090}
91
92// Make the hidden API files available from the platform-bootclasspath module.
93func (b *platformBootclasspathModule) OutputFiles(tag string) (android.Paths, error) {
94 switch tag {
95 case "hiddenapi-flags.csv":
96 return android.Paths{b.hiddenAPIFlagsCSV}, nil
97 case "hiddenapi-index.csv":
98 return android.Paths{b.hiddenAPIIndexCSV}, nil
99 case "hiddenapi-metadata.csv":
100 return android.Paths{b.hiddenAPIMetadataCSV}, nil
Anton Hanssonb3738ed2023-09-21 16:23:20 +0000101 case ".srcjar":
102 return android.Paths{b.srcjar}, nil
Paul Duffin6a766452021-04-12 14:15:22 +0100103 }
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) {
Spandan Das64c9e0c2023-12-20 20:13:34 +0000109 // Create a dependency on all_apex_contributions to determine the selected mainline module
110 ctx.AddDependency(ctx.Module(), apexContributionsMetadataDepTag, "all_apex_contributions")
111
Paul Duffin74431d52021-04-21 14:10:42 +0100112 b.hiddenAPIDepsMutator(ctx)
113
Jiakai Zhangbc698cd2023-05-08 16:28:38 +0000114 if !dexpreopt.IsDex2oatNeeded(ctx) {
Qiao Yang8d8c6602023-05-05 15:03:24 +0000115 return
116 }
117
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100118 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
119 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
120 dexpreopt.RegisterToolDeps(ctx)
121}
122
Paul Duffin74431d52021-04-21 14:10:42 +0100123func (b *platformBootclasspathModule) hiddenAPIDepsMutator(ctx android.BottomUpMutatorContext) {
Pratyushfaec4db2023-07-20 11:19:04 +0000124 if ctx.Config().DisableHiddenApiChecks() {
Paul Duffin74431d52021-04-21 14:10:42 +0100125 return
126 }
127
128 // Add dependencies onto the stub lib modules.
Paul Duffin31fad802021-06-18 18:14:25 +0100129 apiLevelToStubLibModules := hiddenAPIComputeMonolithicStubLibModules(ctx.Config())
130 hiddenAPIAddStubLibDependencies(ctx, apiLevelToStubLibModules)
Paul Duffin74431d52021-04-21 14:10:42 +0100131}
132
Paul Duffin4994d262021-04-22 12:08:59 +0100133func (b *platformBootclasspathModule) BootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100134 // Add dependencies on all the ART jars.
135 global := dexpreopt.GetGlobalConfig(ctx)
Spandan Das64c9e0c2023-12-20 20:13:34 +0000136 addDependenciesOntoSelectedBootImageApexes(ctx, "com.android.art")
137 // TODO: b/308174306 - Remove the mechanism of depending on the java_sdk_library(_import) directly
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100138 addDependenciesOntoBootImageModules(ctx, global.ArtApexJars, platformBootclasspathArtBootJarDepTag)
Paul Duffinb432df92021-03-22 22:09:42 +0000139
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100140 // Add dependencies on all the non-updatable jars, which are on the platform or in non-updatable
141 // APEXes.
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100142 addDependenciesOntoBootImageModules(ctx, b.platformJars(ctx), platformBootclasspathBootJarDepTag)
Paul Duffinb432df92021-03-22 22:09:42 +0000143
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100144 // Add dependencies on all the updatable jars, except the ART jars.
satayevd604b212021-07-21 14:23:52 +0100145 apexJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
Spandan Das64c9e0c2023-12-20 20:13:34 +0000146 apexes := []string{}
147 for i := 0; i < apexJars.Len(); i++ {
148 apexes = append(apexes, apexJars.Apex(i))
149 }
150 addDependenciesOntoSelectedBootImageApexes(ctx, android.FirstUniqueStrings(apexes)...)
151 // TODO: b/308174306 - Remove the mechanism of depending on the java_sdk_library(_import) directly
satayevd604b212021-07-21 14:23:52 +0100152 addDependenciesOntoBootImageModules(ctx, apexJars, platformBootclasspathApexBootJarDepTag)
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100153
Paul Duffin4994d262021-04-22 12:08:59 +0100154 // Add dependencies on all the fragments.
155 b.properties.BootclasspathFragmentsDepsProperties.addDependenciesOntoFragments(ctx)
Paul Duffinb432df92021-03-22 22:09:42 +0000156}
157
Paul Duffin01b463b2021-04-26 20:05:39 +0100158func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList, tag bootclasspathDependencyTag) {
Paul Duffinb432df92021-03-22 22:09:42 +0000159 for i := 0; i < modules.Len(); i++ {
160 apex := modules.Apex(i)
161 name := modules.Jar(i)
162
Paul Duffin01b463b2021-04-26 20:05:39 +0100163 addDependencyOntoApexModulePair(ctx, apex, name, tag)
Paul Duffinb432df92021-03-22 22:09:42 +0000164 }
165}
166
Paul Duffine3ecce62021-04-29 10:34:11 +0100167// GenerateSingletonBuildActions does nothing and must never do anything.
168//
169// This module only implements android.SingletonModule so that it can implement
170// android.SingletonMakeVarsProvider.
171func (b *platformBootclasspathModule) GenerateSingletonBuildActions(android.SingletonContext) {
172 // Keep empty
173}
174
175func (d *platformBootclasspathModule) MakeVars(ctx android.MakeVarsContext) {
Paul Duffin12d29b72021-04-29 13:50:01 +0100176 d.generateHiddenApiMakeVars(ctx)
Paul Duffine3ecce62021-04-29 10:34:11 +0100177}
178
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100179func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayevd604b212021-07-21 14:23:52 +0100180 // Gather all the dependencies from the art, platform, and apex boot jars.
Paul Duffin01b463b2021-04-26 20:05:39 +0100181 artModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathArtBootJarDepTag)
satayevd604b212021-07-21 14:23:52 +0100182 platformModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathBootJarDepTag)
183 apexModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathApexBootJarDepTag)
Paul Duffin01b463b2021-04-26 20:05:39 +0100184
185 // Concatenate them all, in order as they would appear on the bootclasspath.
186 var allModules []android.Module
187 allModules = append(allModules, artModules...)
satayevd604b212021-07-21 14:23:52 +0100188 allModules = append(allModules, platformModules...)
189 allModules = append(allModules, apexModules...)
Paul Duffin01b463b2021-04-26 20:05:39 +0100190 b.configuredModules = allModules
191
Anton Hansson57162c52023-09-20 13:41:30 +0000192 var transitiveSrcFiles android.Paths
193 for _, module := range allModules {
Colin Cross313aa542023-12-13 13:47:44 -0800194 depInfo, _ := android.OtherModuleProvider(ctx, module, JavaInfoProvider)
Anton Hansson57162c52023-09-20 13:41:30 +0000195 if depInfo.TransitiveSrcFiles != nil {
196 transitiveSrcFiles = append(transitiveSrcFiles, depInfo.TransitiveSrcFiles.ToList()...)
197 }
198 }
199 jarArgs := resourcePathsToJarArgs(transitiveSrcFiles)
200 jarArgs = append(jarArgs, "-srcjar") // Move srcfiles to the right package
Anton Hanssonb3738ed2023-09-21 16:23:20 +0000201 b.srcjar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-transitive.srcjar").OutputPath
202 TransformResourcesToJar(ctx, b.srcjar, jarArgs, transitiveSrcFiles)
Anton Hansson57162c52023-09-20 13:41:30 +0000203
Paul Duffin01b463b2021-04-26 20:05:39 +0100204 // Gather all the fragments dependencies.
Paul Duffin9bacf562021-04-28 21:16:02 +0100205 b.fragments = gatherApexModulePairDepsWithTag(ctx, bootclasspathFragmentDepTag)
Paul Duffinb432df92021-03-22 22:09:42 +0000206
Paul Duffinf23bc472021-04-27 12:42:20 +0100207 // Check the configuration of the boot modules.
208 // ART modules are checked by the art-bootclasspath-fragment.
satayevd604b212021-07-21 14:23:52 +0100209 b.checkPlatformModules(ctx, platformModules)
210 b.checkApexModules(ctx, apexModules)
Paul Duffinf23bc472021-04-27 12:42:20 +0100211
satayev013485b2021-05-06 23:38:10 +0100212 b.generateClasspathProtoBuildActions(ctx)
213
Paul Duffinc8ead412021-06-07 19:28:15 +0100214 bootDexJarByModule := b.generateHiddenAPIBuildActions(ctx, b.configuredModules, b.fragments)
215 buildRuleForBootJarsPackageCheck(ctx, bootDexJarByModule)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100216}
217
satayev013485b2021-05-06 23:38:10 +0100218// Generate classpaths.proto config
219func (b *platformBootclasspathModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
satayevb3090502021-06-15 17:49:10 +0100220 configuredJars := b.configuredJars(ctx)
satayev013485b2021-05-06 23:38:10 +0100221 // ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
satayevb3090502021-06-15 17:49:10 +0100222 classpathJars := configuredJarListToClasspathJars(ctx, configuredJars, BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
223 b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, configuredJars, classpathJars)
Inseob Kimdd532492024-04-30 17:22:58 +0900224 b.classpathFragmentBase().installClasspathProto(ctx)
satayev013485b2021-05-06 23:38:10 +0100225}
226
satayev142ed272021-06-15 16:21:17 +0100227func (b *platformBootclasspathModule) configuredJars(ctx android.ModuleContext) android.ConfiguredJarList {
satayevb3090502021-06-15 17:49:10 +0100228 // Include all non APEX jars
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100229 jars := b.platformJars(ctx)
satayevb3090502021-06-15 17:49:10 +0100230
231 // Include jars from APEXes that don't populate their classpath proto config.
satayevd604b212021-07-21 14:23:52 +0100232 remainingJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
satayevb3090502021-06-15 17:49:10 +0100233 for _, fragment := range b.fragments {
Colin Cross313aa542023-12-13 13:47:44 -0800234 info, _ := android.OtherModuleProvider(ctx, fragment, ClasspathFragmentProtoContentInfoProvider)
satayevb3090502021-06-15 17:49:10 +0100235 if info.ClasspathFragmentProtoGenerated {
236 remainingJars = remainingJars.RemoveList(info.ClasspathFragmentProtoContents)
237 }
238 }
239 for i := 0; i < remainingJars.Len(); i++ {
240 jars = jars.Append(remainingJars.Apex(i), remainingJars.Jar(i))
241 }
242
243 return jars
satayev013485b2021-05-06 23:38:10 +0100244}
245
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100246func (b *platformBootclasspathModule) platformJars(ctx android.PathContext) android.ConfiguredJarList {
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100247 global := dexpreopt.GetGlobalConfig(ctx)
248 return global.BootJars.RemoveList(global.ArtApexJars)
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100249}
250
satayevd604b212021-07-21 14:23:52 +0100251// checkPlatformModules ensures that the non-updatable modules supplied are not part of an
252// apex module.
253func (b *platformBootclasspathModule) checkPlatformModules(ctx android.ModuleContext, modules []android.Module) {
254 // TODO(satayev): change this check to only allow core-icu4j, all apex jars should not be here.
Paul Duffinf23bc472021-04-27 12:42:20 +0100255 for _, m := range modules {
Colin Cross313aa542023-12-13 13:47:44 -0800256 apexInfo, _ := android.OtherModuleProvider(ctx, m, android.ApexInfoProvider)
Paul Duffinf23bc472021-04-27 12:42:20 +0100257 fromUpdatableApex := apexInfo.Updatable
258 if fromUpdatableApex {
259 // error: this jar is part of an updatable apex
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100260 ctx.ModuleErrorf("module %q from updatable apexes %q is not allowed in the platform bootclasspath", ctx.OtherModuleName(m), apexInfo.InApexVariants)
Paul Duffinf23bc472021-04-27 12:42:20 +0100261 } else {
262 // ok: this jar is part of the platform or a non-updatable apex
263 }
264 }
265}
266
satayevd604b212021-07-21 14:23:52 +0100267// checkApexModules ensures that the apex modules supplied are not from the platform.
268func (b *platformBootclasspathModule) checkApexModules(ctx android.ModuleContext, modules []android.Module) {
Paul Duffinf23bc472021-04-27 12:42:20 +0100269 for _, m := range modules {
Colin Cross313aa542023-12-13 13:47:44 -0800270 apexInfo, _ := android.OtherModuleProvider(ctx, m, android.ApexInfoProvider)
Paul Duffinf23bc472021-04-27 12:42:20 +0100271 fromUpdatableApex := apexInfo.Updatable
272 if fromUpdatableApex {
273 // ok: this jar is part of an updatable apex
274 } else {
275 name := ctx.OtherModuleName(m)
276 if apexInfo.IsForPlatform() {
Paul Duffin7487a7a2021-05-19 09:36:09 +0100277 // If AlwaysUsePrebuiltSdks() returns true then it is possible that the updatable list will
278 // include platform variants of a prebuilt module due to workarounds elsewhere. In that case
279 // do not treat this as an error.
280 // TODO(b/179354495): Always treat this as an error when migration to bootclasspath_fragment
281 // modules is complete.
282 if !ctx.Config().AlwaysUsePrebuiltSdks() {
283 // error: this jar is part of the platform
satayevd604b212021-07-21 14:23:52 +0100284 ctx.ModuleErrorf("module %q from platform is not allowed in the apex boot jars list", name)
Paul Duffin7487a7a2021-05-19 09:36:09 +0100285 }
Paul Duffinf23bc472021-04-27 12:42:20 +0100286 } else {
287 // TODO(b/177892522): Treat this as an error.
288 // Cannot do that at the moment because framework-wifi and framework-tethering are in the
satayevd604b212021-07-21 14:23:52 +0100289 // PRODUCT_APEX_BOOT_JARS but not marked as updatable in AOSP.
Paul Duffinf23bc472021-04-27 12:42:20 +0100290 }
291 }
292 }
293}
294
Paul Duffin702210b2021-04-08 20:12:41 +0100295// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffinc8ead412021-06-07 19:28:15 +0100296func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module, fragments []android.Module) bootDexJarByModule {
Spandan Dasa90db962024-05-20 18:37:17 +0000297 createEmptyHiddenApiFiles := func() {
298 paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV}
299 for _, path := range paths {
300 ctx.Build(pctx, android.BuildParams{
301 Rule: android.Touch,
302 Output: path,
303 })
304 }
305 }
Paul Duffin702210b2021-04-08 20:12:41 +0100306
Paul Duffin90b8ad32021-04-13 12:25:01 +0100307 // Save the paths to the monolithic files for retrieval via OutputFiles().
308 b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
309 b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
310 b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
Paul Duffin6a766452021-04-12 14:15:22 +0100311
Adrian Roose95a15e2021-06-21 16:03:11 +0200312 bootDexJarByModule := extractBootDexJarsFromModules(ctx, modules)
313
Pratyushfaec4db2023-07-20 11:19:04 +0000314 // Don't run any hiddenapi rules if hidden api checks are disabled. This is a performance
Paul Duffin0b659862021-04-13 13:02:29 +0100315 // optimization that can be used to reduce the incremental build time but as its name suggests it
316 // can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath.
Pratyushfaec4db2023-07-20 11:19:04 +0000317 if ctx.Config().DisableHiddenApiChecks() {
Spandan Dasa90db962024-05-20 18:37:17 +0000318 createEmptyHiddenApiFiles()
Adrian Roose95a15e2021-06-21 16:03:11 +0200319 return bootDexJarByModule
Paul Duffin0b659862021-04-13 13:02:29 +0100320 }
321
Paul Duffin89f570a2021-06-16 01:42:33 +0100322 // Construct a list of ClasspathElement objects from the modules and fragments.
323 classpathElements := CreateClasspathElements(ctx, modules, fragments)
324
325 monolithicInfo := b.createAndProvideMonolithicHiddenAPIInfo(ctx, classpathElements)
326
327 // Extract the classes jars only from those libraries that do not have corresponding fragments as
328 // the fragments will have already provided the flags that are needed.
329 classesJars := monolithicInfo.ClassesJars
330
Spandan Das81fe4d12024-05-15 18:43:47 +0000331 if len(classesJars) == 0 {
332 // This product does not include any monolithic jars. Monolithic hiddenapi flag generation is not required.
Spandan Dasa90db962024-05-20 18:37:17 +0000333 // However, generate an empty file so that the dist tags in f/b/boot/Android.bp can be resolved, and `m dist` works.
334 createEmptyHiddenApiFiles()
Spandan Das81fe4d12024-05-15 18:43:47 +0000335 return bootDexJarByModule
336 }
337
Paul Duffin4539a372021-06-23 23:20:43 +0100338 // Create the input to pass to buildRuleToGenerateHiddenAPIStubFlagsFile
Paul Duffin1352f7c2021-05-21 22:18:49 +0100339 input := newHiddenAPIFlagInput()
340
341 // Gather stub library information from the dependencies on modules provided by
342 // hiddenAPIComputeMonolithicStubLibModules.
343 input.gatherStubLibInfo(ctx, nil)
344
345 // Use the flag files from this module and all the fragments.
346 input.FlagFilesByCategory = monolithicInfo.FlagsFilesByCategory
Paul Duffin74431d52021-04-21 14:10:42 +0100347
Paul Duffin537ea3d2021-05-14 10:38:00 +0100348 // Generate the monolithic stub-flags.csv file.
Paul Duffin537ea3d2021-05-14 10:38:00 +0100349 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
Paul Duffin67b9d612021-07-21 17:38:47 +0100350 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 +0100351
Paul Duffin537ea3d2021-05-14 10:38:00 +0100352 // Generate the annotation-flags.csv file from all the module annotations.
Paul Duffind061d402021-06-07 21:36:01 +0100353 annotationFlags := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "annotation-flags-from-classes.csv")
354 buildRuleToGenerateAnnotationFlags(ctx, "intermediate hidden API flags", classesJars, stubFlags, annotationFlags)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100355
Paul Duffind061d402021-06-07 21:36:01 +0100356 // Generate the monolithic hiddenapi-flags.csv file.
357 //
358 // Use annotation flags generated directly from the classes jars as well as annotation flag files
359 // provided by prebuilts.
360 allAnnotationFlagFiles := android.Paths{annotationFlags}
361 allAnnotationFlagFiles = append(allAnnotationFlagFiles, monolithicInfo.AnnotationFlagsPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100362 allFlags := hiddenAPISingletonPaths(ctx).flags
Paul Duffin67b9d612021-07-21 17:38:47 +0100363 buildRuleToGenerateHiddenApiFlags(ctx, "hiddenAPIFlagsFile", "monolithic hidden API flags", allFlags, stubFlags, allAnnotationFlagFiles, monolithicInfo.FlagsFilesByCategory, monolithicInfo.FlagSubsets, android.OptionalPath{})
Paul Duffin537ea3d2021-05-14 10:38:00 +0100364
365 // Generate an intermediate monolithic hiddenapi-metadata.csv file directly from the annotations
366 // in the source code.
Paul Duffind061d402021-06-07 21:36:01 +0100367 intermediateMetadataCSV := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "metadata-from-classes.csv")
368 buildRuleToGenerateMetadata(ctx, "intermediate hidden API metadata", classesJars, stubFlags, intermediateMetadataCSV)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100369
Paul Duffind061d402021-06-07 21:36:01 +0100370 // Generate the monolithic hiddenapi-metadata.csv file.
371 //
372 // Use metadata files generated directly from the classes jars as well as metadata files provided
373 // by prebuilts.
374 //
375 // This has the side effect of ensuring that the output file uses | quotes just in case that is
376 // important for the tools that consume the metadata file.
377 allMetadataFlagFiles := android.Paths{intermediateMetadataCSV}
378 allMetadataFlagFiles = append(allMetadataFlagFiles, monolithicInfo.MetadataPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100379 metadataCSV := hiddenAPISingletonPaths(ctx).metadata
Paul Duffind061d402021-06-07 21:36:01 +0100380 b.buildRuleMergeCSV(ctx, "monolithic hidden API metadata", allMetadataFlagFiles, metadataCSV)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100381
Paul Duffind061d402021-06-07 21:36:01 +0100382 // Generate an intermediate monolithic hiddenapi-index.csv file directly from the CSV files in the
383 // classes jars.
384 intermediateIndexCSV := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "index-from-classes.csv")
385 buildRuleToGenerateIndex(ctx, "intermediate hidden API index", classesJars, intermediateIndexCSV)
386
387 // Generate the monolithic hiddenapi-index.csv file.
388 //
389 // Use index files generated directly from the classes jars as well as index files provided
390 // by prebuilts.
391 allIndexFlagFiles := android.Paths{intermediateIndexCSV}
392 allIndexFlagFiles = append(allIndexFlagFiles, monolithicInfo.IndexPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100393 indexCSV := hiddenAPISingletonPaths(ctx).index
Paul Duffind061d402021-06-07 21:36:01 +0100394 b.buildRuleMergeCSV(ctx, "monolithic hidden API index", allIndexFlagFiles, indexCSV)
Paul Duffinc8ead412021-06-07 19:28:15 +0100395
396 return bootDexJarByModule
Paul Duffin74431d52021-04-21 14:10:42 +0100397}
398
Paul Duffin438eb572021-05-21 16:58:23 +0100399// createAndProvideMonolithicHiddenAPIInfo creates a MonolithicHiddenAPIInfo and provides it for
400// testing.
Paul Duffin89f570a2021-06-16 01:42:33 +0100401func (b *platformBootclasspathModule) createAndProvideMonolithicHiddenAPIInfo(ctx android.ModuleContext, classpathElements ClasspathElements) MonolithicHiddenAPIInfo {
Paul Duffin1352f7c2021-05-21 22:18:49 +0100402 // Create a temporary input structure in which to collate information provided directly by this
403 // module, either through properties or direct dependencies.
404 temporaryInput := newHiddenAPIFlagInput()
405
406 // Create paths to the flag files specified in the properties.
Paul Duffin9b61abb2022-07-27 16:16:54 +0000407 temporaryInput.extractFlagFilesFromProperties(ctx, &b.properties.HiddenAPIFlagFileProperties)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100408
409 // Create the monolithic info, by starting with the flag files specified on this and then merging
410 // in information from all the fragment dependencies of this.
Paul Duffin89f570a2021-06-16 01:42:33 +0100411 monolithicInfo := newMonolithicHiddenAPIInfo(ctx, temporaryInput.FlagFilesByCategory, classpathElements)
Paul Duffin438eb572021-05-21 16:58:23 +0100412
413 // Store the information for testing.
Colin Cross40213022023-12-13 15:19:49 -0800414 android.SetProvider(ctx, MonolithicHiddenAPIInfoProvider, monolithicInfo)
Paul Duffin438eb572021-05-21 16:58:23 +0100415 return monolithicInfo
416}
417
Paul Duffin537ea3d2021-05-14 10:38:00 +0100418func (b *platformBootclasspathModule) buildRuleMergeCSV(ctx android.ModuleContext, desc string, inputPaths android.Paths, outputPath android.WritablePath) {
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100419 rule := android.NewRuleBuilder(pctx, ctx)
420 rule.Command().
421 BuiltTool("merge_csv").
422 Flag("--key_field signature").
Paul Duffin85dee5d2021-04-13 00:14:38 +0100423 FlagWithOutput("--output=", outputPath).
Paul Duffin537ea3d2021-05-14 10:38:00 +0100424 Inputs(inputPaths)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100425
Paul Duffin537ea3d2021-05-14 10:38:00 +0100426 rule.Build(desc, desc)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100427}
Paul Duffinad19d382021-04-26 16:44:00 +0100428
Paul Duffin12d29b72021-04-29 13:50:01 +0100429// generateHiddenApiMakeVars generates make variables needed by hidden API related make rules, e.g.
430// veridex and run-appcompat.
431func (b *platformBootclasspathModule) generateHiddenApiMakeVars(ctx android.MakeVarsContext) {
432 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
433 return
434 }
435 // INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
436 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", b.hiddenAPIFlagsCSV.String())
437}