blob: 155afc6c2378fb2c329b5c80d29ac64c1e8110a1 [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 (
Colin Cross92b0eb12025-02-06 11:49:52 -080018 "maps"
19 "slices"
20
Colin Crossc5f4de52025-01-09 16:04:41 -080021 "github.com/google/blueprint"
22
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010023 "android/soong/android"
24 "android/soong/dexpreopt"
25)
26
27func init() {
28 registerPlatformBootclasspathBuildComponents(android.InitRegistrationContext)
29}
30
31func registerPlatformBootclasspathBuildComponents(ctx android.RegistrationContext) {
Cole Faust4bc45d82024-11-04 15:56:34 -080032 ctx.RegisterModuleType("platform_bootclasspath", platformBootclasspathFactory)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010033}
34
Paul Duffin01b463b2021-04-26 20:05:39 +010035// The tags used for the dependencies between the platform bootclasspath and any configured boot
36// jars.
Colin Crossc5f4de52025-01-09 16:04:41 -080037type platformBootclasspathImplLibDepTagType struct {
38 blueprint.BaseDependencyTag
39}
40
41func (p platformBootclasspathImplLibDepTagType) ExcludeFromVisibilityEnforcement() {}
42
43var platformBootclasspathImplLibDepTag platformBootclasspathImplLibDepTagType
44
45var _ android.ExcludeFromVisibilityEnforcementTag = platformBootclasspathImplLibDepTag
46
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010047type platformBootclasspathModule struct {
Cole Faust4bc45d82024-11-04 15:56:34 -080048 android.ModuleBase
Artur Satayev97259dc2021-04-07 15:17:14 +010049 ClasspathFragmentBase
Paul Duffinb432df92021-03-22 22:09:42 +000050
Paul Duffin62d8c3b2021-04-07 20:35:11 +010051 properties platformBootclasspathProperties
52
Paul Duffinb432df92021-03-22 22:09:42 +000053 // The apex:module pairs obtained from the configured modules.
Paul Duffinb432df92021-03-22 22:09:42 +000054 configuredModules []android.Module
Paul Duffin62d8c3b2021-04-07 20:35:11 +010055
56 // The apex:module pairs obtained from the fragments.
Paul Duffin62d8c3b2021-04-07 20:35:11 +010057 fragments []android.Module
Paul Duffin6a766452021-04-12 14:15:22 +010058
Colin Cross92b0eb12025-02-06 11:49:52 -080059 // The map of apex to the fragments they contain.
60 apexNameToFragment map[string]android.Module
61
62 // The map of library modules in the bootclasspath to the fragments that contain them.
63 libraryToApex map[android.Module]string
64
Paul Duffin6a766452021-04-12 14:15:22 +010065 // Path to the monolithic hiddenapi-flags.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010066 hiddenAPIFlagsCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010067
68 // Path to the monolithic hiddenapi-index.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010069 hiddenAPIIndexCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010070
71 // Path to the monolithic hiddenapi-unsupported.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010072 hiddenAPIMetadataCSV android.OutputPath
Paul Duffin62d8c3b2021-04-07 20:35:11 +010073}
74
Paul Duffin62d8c3b2021-04-07 20:35:11 +010075type platformBootclasspathProperties struct {
Paul Duffinb67d8782021-04-22 11:49:41 +010076 BootclasspathFragmentsDepsProperties
Paul Duffin702210b2021-04-08 20:12:41 +010077
Paul Duffin9b61abb2022-07-27 16:16:54 +000078 HiddenAPIFlagFileProperties
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010079}
80
Cole Faust4bc45d82024-11-04 15:56:34 -080081func platformBootclasspathFactory() android.Module {
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010082 m := &platformBootclasspathModule{}
Paul Duffin62d8c3b2021-04-07 20:35:11 +010083 m.AddProperties(&m.properties)
satayev95e9c5b2021-04-29 11:50:26 +010084 initClasspathFragment(m, BOOTCLASSPATH)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010085 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
86 return m
87}
88
Artur Satayev97259dc2021-04-07 15:17:14 +010089func (b *platformBootclasspathModule) AndroidMkEntries() (entries []android.AndroidMkEntries) {
90 entries = append(entries, android.AndroidMkEntries{
91 Class: "FAKE",
92 // Need at least one output file in order for this to take effect.
93 OutputFile: android.OptionalPathForPath(b.hiddenAPIFlagsCSV),
94 Include: "$(BUILD_PHONY_PACKAGE)",
95 })
satayev128ce2f2021-05-06 13:21:15 +010096 entries = append(entries, b.classpathFragmentBase().androidMkEntries()...)
Artur Satayev97259dc2021-04-07 15:17:14 +010097 return
Paul Duffin6a766452021-04-12 14:15:22 +010098}
99
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100100func (b *platformBootclasspathModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Spandan Das64c9e0c2023-12-20 20:13:34 +0000101 // Create a dependency on all_apex_contributions to determine the selected mainline module
102 ctx.AddDependency(ctx.Module(), apexContributionsMetadataDepTag, "all_apex_contributions")
103
Paul Duffin74431d52021-04-21 14:10:42 +0100104 b.hiddenAPIDepsMutator(ctx)
105
Colin Crossd8d8b852024-12-20 16:32:37 -0800106 if dexpreopt.IsDex2oatNeeded(ctx) {
107 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
108 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
109 dexpreopt.RegisterToolDeps(ctx)
Qiao Yang8d8c6602023-05-05 15:03:24 +0000110 }
111
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100112 // Add dependencies on all the ART jars.
113 global := dexpreopt.GetGlobalConfig(ctx)
Spandan Das64c9e0c2023-12-20 20:13:34 +0000114 addDependenciesOntoSelectedBootImageApexes(ctx, "com.android.art")
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000115
116 var bootImageModuleNames []string
117
Spandan Das64c9e0c2023-12-20 20:13:34 +0000118 // TODO: b/308174306 - Remove the mechanism of depending on the java_sdk_library(_import) directly
Colin Crossd8d8b852024-12-20 16:32:37 -0800119 addDependenciesOntoBootImageModules(ctx, global.ArtApexJars, artBootJar)
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000120 bootImageModuleNames = append(bootImageModuleNames, global.ArtApexJars.CopyOfJars()...)
Paul Duffinb432df92021-03-22 22:09:42 +0000121
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100122 // Add dependencies on all the non-updatable jars, which are on the platform or in non-updatable
123 // APEXes.
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000124 platformJars := b.platformJars(ctx)
Colin Crossd8d8b852024-12-20 16:32:37 -0800125 addDependenciesOntoBootImageModules(ctx, platformJars, platformBootJar)
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000126 bootImageModuleNames = append(bootImageModuleNames, platformJars.CopyOfJars()...)
Paul Duffinb432df92021-03-22 22:09:42 +0000127
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100128 // Add dependencies on all the updatable jars, except the ART jars.
satayevd604b212021-07-21 14:23:52 +0100129 apexJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
Spandan Das64c9e0c2023-12-20 20:13:34 +0000130 apexes := []string{}
131 for i := 0; i < apexJars.Len(); i++ {
132 apexes = append(apexes, apexJars.Apex(i))
133 }
134 addDependenciesOntoSelectedBootImageApexes(ctx, android.FirstUniqueStrings(apexes)...)
135 // TODO: b/308174306 - Remove the mechanism of depending on the java_sdk_library(_import) directly
Colin Crossd8d8b852024-12-20 16:32:37 -0800136 addDependenciesOntoBootImageModules(ctx, apexJars, apexBootJar)
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000137 bootImageModuleNames = append(bootImageModuleNames, apexJars.CopyOfJars()...)
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)
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000141
142 for _, bootImageModuleName := range bootImageModuleNames {
143 implLibName := implLibraryModuleName(bootImageModuleName)
144 if ctx.OtherModuleExists(implLibName) {
145 ctx.AddFarVariationDependencies(nil, platformBootclasspathImplLibDepTag, implLibName)
146 }
147 }
Paul Duffinb432df92021-03-22 22:09:42 +0000148}
149
Colin Crossd8d8b852024-12-20 16:32:37 -0800150func (b *platformBootclasspathModule) hiddenAPIDepsMutator(ctx android.BottomUpMutatorContext) {
151 if ctx.Config().DisableHiddenApiChecks() {
152 return
153 }
154
155 // Add dependencies onto the stub lib modules.
156 apiLevelToStubLibModules := hiddenAPIComputeMonolithicStubLibModules(ctx.Config())
157 hiddenAPIAddStubLibDependencies(ctx, apiLevelToStubLibModules)
158}
159
160func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList, tagType bootclasspathDependencyTagType) {
Paul Duffinb432df92021-03-22 22:09:42 +0000161 for i := 0; i < modules.Len(); i++ {
162 apex := modules.Apex(i)
163 name := modules.Jar(i)
164
Colin Crossd8d8b852024-12-20 16:32:37 -0800165 addDependencyOntoApexModulePair(ctx, apex, name, tagType)
Paul Duffinb432df92021-03-22 22:09:42 +0000166 }
167}
168
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100169func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayevd604b212021-07-21 14:23:52 +0100170 // Gather all the dependencies from the art, platform, and apex boot jars.
Colin Cross92b0eb12025-02-06 11:49:52 -0800171 artModules, artModulesToApex := gatherApexModulePairDepsWithTag(ctx, artBootJar)
172 platformModules, platformModulesToApex := gatherApexModulePairDepsWithTag(ctx, platformBootJar)
173 apexModules, apexModulesToApex := gatherApexModulePairDepsWithTag(ctx, apexBootJar)
Paul Duffin01b463b2021-04-26 20:05:39 +0100174
175 // Concatenate them all, in order as they would appear on the bootclasspath.
Colin Cross92b0eb12025-02-06 11:49:52 -0800176 allModules := slices.Concat(artModules, platformModules, apexModules)
Paul Duffin01b463b2021-04-26 20:05:39 +0100177 b.configuredModules = allModules
Colin Cross92b0eb12025-02-06 11:49:52 -0800178 b.libraryToApex = maps.Clone(artModulesToApex)
179 maps.Copy(b.libraryToApex, platformModulesToApex)
180 maps.Copy(b.libraryToApex, apexModulesToApex)
Paul Duffin01b463b2021-04-26 20:05:39 +0100181
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000182 // Do not add implLibModule to allModules as the impl lib is only used to collect the
183 // transitive source files
184 var implLibModule []android.Module
Jihoon Kang2b133702025-01-09 07:54:41 +0000185 ctx.VisitDirectDepsWithTag(platformBootclasspathImplLibDepTag, func(m android.Module) {
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000186 implLibModule = append(implLibModule, m)
187 })
188
Anton Hansson57162c52023-09-20 13:41:30 +0000189 var transitiveSrcFiles android.Paths
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000190 for _, module := range append(allModules, implLibModule...) {
Colin Cross7727c7f2024-07-18 15:36:32 -0700191 if depInfo, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossa14fb6a2024-10-23 16:57:06 -0700192 transitiveSrcFiles = append(transitiveSrcFiles, depInfo.TransitiveSrcFiles.ToList()...)
Anton Hansson57162c52023-09-20 13:41:30 +0000193 }
194 }
195 jarArgs := resourcePathsToJarArgs(transitiveSrcFiles)
196 jarArgs = append(jarArgs, "-srcjar") // Move srcfiles to the right package
Cole Faust4e9f5922024-11-13 16:09:23 -0800197 srcjar := android.PathForModuleOut(ctx, ctx.ModuleName()+"-transitive.srcjar")
mrziwang444762b2024-06-27 09:58:58 -0700198 TransformResourcesToJar(ctx, srcjar, jarArgs, transitiveSrcFiles)
Anton Hansson57162c52023-09-20 13:41:30 +0000199
Paul Duffin01b463b2021-04-26 20:05:39 +0100200 // Gather all the fragments dependencies.
Colin Cross92b0eb12025-02-06 11:49:52 -0800201 b.fragments, b.apexNameToFragment = gatherFragments(ctx)
Paul Duffinb432df92021-03-22 22:09:42 +0000202
Paul Duffinf23bc472021-04-27 12:42:20 +0100203 // Check the configuration of the boot modules.
204 // ART modules are checked by the art-bootclasspath-fragment.
satayevd604b212021-07-21 14:23:52 +0100205 b.checkPlatformModules(ctx, platformModules)
206 b.checkApexModules(ctx, apexModules)
Paul Duffinf23bc472021-04-27 12:42:20 +0100207
satayev013485b2021-05-06 23:38:10 +0100208 b.generateClasspathProtoBuildActions(ctx)
209
Colin Cross92b0eb12025-02-06 11:49:52 -0800210 bootDexJarByModule := b.generateHiddenAPIBuildActions(ctx, b.configuredModules, b.fragments, b.libraryToApex, b.apexNameToFragment)
Paul Duffinc8ead412021-06-07 19:28:15 +0100211 buildRuleForBootJarsPackageCheck(ctx, bootDexJarByModule)
mrziwang444762b2024-06-27 09:58:58 -0700212
213 ctx.SetOutputFiles(android.Paths{b.hiddenAPIFlagsCSV}, "hiddenapi-flags.csv")
214 ctx.SetOutputFiles(android.Paths{b.hiddenAPIIndexCSV}, "hiddenapi-index.csv")
215 ctx.SetOutputFiles(android.Paths{b.hiddenAPIMetadataCSV}, "hiddenapi-metadata.csv")
216 ctx.SetOutputFiles(android.Paths{srcjar}, ".srcjar")
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100217}
218
satayev013485b2021-05-06 23:38:10 +0100219// Generate classpaths.proto config
220func (b *platformBootclasspathModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
satayevb3090502021-06-15 17:49:10 +0100221 configuredJars := b.configuredJars(ctx)
satayev013485b2021-05-06 23:38:10 +0100222 // ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
satayevb3090502021-06-15 17:49:10 +0100223 classpathJars := configuredJarListToClasspathJars(ctx, configuredJars, BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
224 b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, configuredJars, classpathJars)
Inseob Kimdd532492024-04-30 17:22:58 +0900225 b.classpathFragmentBase().installClasspathProto(ctx)
satayev013485b2021-05-06 23:38:10 +0100226}
227
satayev142ed272021-06-15 16:21:17 +0100228func (b *platformBootclasspathModule) configuredJars(ctx android.ModuleContext) android.ConfiguredJarList {
satayevb3090502021-06-15 17:49:10 +0100229 // Include all non APEX jars
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100230 jars := b.platformJars(ctx)
satayevb3090502021-06-15 17:49:10 +0100231
232 // Include jars from APEXes that don't populate their classpath proto config.
satayevd604b212021-07-21 14:23:52 +0100233 remainingJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
satayevb3090502021-06-15 17:49:10 +0100234 for _, fragment := range b.fragments {
Colin Cross313aa542023-12-13 13:47:44 -0800235 info, _ := android.OtherModuleProvider(ctx, fragment, ClasspathFragmentProtoContentInfoProvider)
satayevb3090502021-06-15 17:49:10 +0100236 if info.ClasspathFragmentProtoGenerated {
237 remainingJars = remainingJars.RemoveList(info.ClasspathFragmentProtoContents)
238 }
239 }
240 for i := 0; i < remainingJars.Len(); i++ {
241 jars = jars.Append(remainingJars.Apex(i), remainingJars.Jar(i))
242 }
243
244 return jars
satayev013485b2021-05-06 23:38:10 +0100245}
246
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100247func (b *platformBootclasspathModule) platformJars(ctx android.PathContext) android.ConfiguredJarList {
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100248 global := dexpreopt.GetGlobalConfig(ctx)
249 return global.BootJars.RemoveList(global.ArtApexJars)
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100250}
251
satayevd604b212021-07-21 14:23:52 +0100252// checkPlatformModules ensures that the non-updatable modules supplied are not part of an
253// apex module.
254func (b *platformBootclasspathModule) checkPlatformModules(ctx android.ModuleContext, modules []android.Module) {
255 // TODO(satayev): change this check to only allow core-icu4j, all apex jars should not be here.
Paul Duffinf23bc472021-04-27 12:42:20 +0100256 for _, m := range modules {
Colin Cross313aa542023-12-13 13:47:44 -0800257 apexInfo, _ := android.OtherModuleProvider(ctx, m, android.ApexInfoProvider)
Paul Duffinf23bc472021-04-27 12:42:20 +0100258 fromUpdatableApex := apexInfo.Updatable
259 if fromUpdatableApex {
260 // error: this jar is part of an updatable apex
Colin Cross1cea5302024-12-03 16:40:08 -0800261 ctx.ModuleErrorf("module %q from updatable apex %q is not allowed in the platform bootclasspath", ctx.OtherModuleName(m), apexInfo.BaseApexName)
Paul Duffinf23bc472021-04-27 12:42:20 +0100262 } else {
263 // ok: this jar is part of the platform or a non-updatable apex
264 }
265 }
266}
267
satayevd604b212021-07-21 14:23:52 +0100268// checkApexModules ensures that the apex modules supplied are not from the platform.
269func (b *platformBootclasspathModule) checkApexModules(ctx android.ModuleContext, modules []android.Module) {
Paul Duffinf23bc472021-04-27 12:42:20 +0100270 for _, m := range modules {
Colin Cross313aa542023-12-13 13:47:44 -0800271 apexInfo, _ := android.OtherModuleProvider(ctx, m, android.ApexInfoProvider)
Paul Duffinf23bc472021-04-27 12:42:20 +0100272 fromUpdatableApex := apexInfo.Updatable
273 if fromUpdatableApex {
274 // ok: this jar is part of an updatable apex
275 } else {
276 name := ctx.OtherModuleName(m)
277 if apexInfo.IsForPlatform() {
Paul Duffin7487a7a2021-05-19 09:36:09 +0100278 // If AlwaysUsePrebuiltSdks() returns true then it is possible that the updatable list will
279 // include platform variants of a prebuilt module due to workarounds elsewhere. In that case
280 // do not treat this as an error.
281 // TODO(b/179354495): Always treat this as an error when migration to bootclasspath_fragment
282 // modules is complete.
283 if !ctx.Config().AlwaysUsePrebuiltSdks() {
284 // error: this jar is part of the platform
Colin Crossd8d8b852024-12-20 16:32:37 -0800285 if ctx.Config().AllowMissingDependencies() {
286 ctx.AddMissingDependencies([]string{"module_" + name + "_from_platform_is_not_allowed_in_the_apex_boot_jars_list"})
287 } else {
288 ctx.ModuleErrorf("module %q from platform is not allowed in the apex boot jars list", name)
289 }
Paul Duffin7487a7a2021-05-19 09:36:09 +0100290 }
Paul Duffinf23bc472021-04-27 12:42:20 +0100291 } else {
292 // TODO(b/177892522): Treat this as an error.
293 // Cannot do that at the moment because framework-wifi and framework-tethering are in the
satayevd604b212021-07-21 14:23:52 +0100294 // PRODUCT_APEX_BOOT_JARS but not marked as updatable in AOSP.
Paul Duffinf23bc472021-04-27 12:42:20 +0100295 }
296 }
297 }
298}
299
Paul Duffin702210b2021-04-08 20:12:41 +0100300// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Colin Cross92b0eb12025-02-06 11:49:52 -0800301func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module,
302 fragments []android.Module, libraryToApex map[android.Module]string, apexNameToFragment map[string]android.Module) bootDexJarByModule {
Spandan Dasa90db962024-05-20 18:37:17 +0000303 createEmptyHiddenApiFiles := func() {
304 paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV}
305 for _, path := range paths {
306 ctx.Build(pctx, android.BuildParams{
307 Rule: android.Touch,
308 Output: path,
309 })
310 }
311 }
Paul Duffin702210b2021-04-08 20:12:41 +0100312
Paul Duffin90b8ad32021-04-13 12:25:01 +0100313 // Save the paths to the monolithic files for retrieval via OutputFiles().
314 b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
315 b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
316 b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
Paul Duffin6a766452021-04-12 14:15:22 +0100317
Adrian Roose95a15e2021-06-21 16:03:11 +0200318 bootDexJarByModule := extractBootDexJarsFromModules(ctx, modules)
319
Pratyushfaec4db2023-07-20 11:19:04 +0000320 // Don't run any hiddenapi rules if hidden api checks are disabled. This is a performance
Paul Duffin0b659862021-04-13 13:02:29 +0100321 // optimization that can be used to reduce the incremental build time but as its name suggests it
322 // can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath.
Pratyushfaec4db2023-07-20 11:19:04 +0000323 if ctx.Config().DisableHiddenApiChecks() {
Spandan Dasa90db962024-05-20 18:37:17 +0000324 createEmptyHiddenApiFiles()
Adrian Roose95a15e2021-06-21 16:03:11 +0200325 return bootDexJarByModule
Paul Duffin0b659862021-04-13 13:02:29 +0100326 }
327
Paul Duffin89f570a2021-06-16 01:42:33 +0100328 // Construct a list of ClasspathElement objects from the modules and fragments.
Colin Cross92b0eb12025-02-06 11:49:52 -0800329 classpathElements := CreateClasspathElements(ctx, modules, fragments, libraryToApex, apexNameToFragment)
Paul Duffin89f570a2021-06-16 01:42:33 +0100330
331 monolithicInfo := b.createAndProvideMonolithicHiddenAPIInfo(ctx, classpathElements)
332
333 // Extract the classes jars only from those libraries that do not have corresponding fragments as
334 // the fragments will have already provided the flags that are needed.
335 classesJars := monolithicInfo.ClassesJars
336
Spandan Das81fe4d12024-05-15 18:43:47 +0000337 if len(classesJars) == 0 {
338 // This product does not include any monolithic jars. Monolithic hiddenapi flag generation is not required.
Spandan Dasa90db962024-05-20 18:37:17 +0000339 // However, generate an empty file so that the dist tags in f/b/boot/Android.bp can be resolved, and `m dist` works.
340 createEmptyHiddenApiFiles()
Spandan Das81fe4d12024-05-15 18:43:47 +0000341 return bootDexJarByModule
342 }
343
Paul Duffin4539a372021-06-23 23:20:43 +0100344 // Create the input to pass to buildRuleToGenerateHiddenAPIStubFlagsFile
Paul Duffin1352f7c2021-05-21 22:18:49 +0100345 input := newHiddenAPIFlagInput()
346
347 // Gather stub library information from the dependencies on modules provided by
348 // hiddenAPIComputeMonolithicStubLibModules.
349 input.gatherStubLibInfo(ctx, nil)
350
351 // Use the flag files from this module and all the fragments.
352 input.FlagFilesByCategory = monolithicInfo.FlagsFilesByCategory
Paul Duffin74431d52021-04-21 14:10:42 +0100353
Paul Duffin537ea3d2021-05-14 10:38:00 +0100354 // Generate the monolithic stub-flags.csv file.
Paul Duffin537ea3d2021-05-14 10:38:00 +0100355 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
Paul Duffin67b9d612021-07-21 17:38:47 +0100356 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 +0100357
Paul Duffin537ea3d2021-05-14 10:38:00 +0100358 // Generate the annotation-flags.csv file from all the module annotations.
Paul Duffind061d402021-06-07 21:36:01 +0100359 annotationFlags := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "annotation-flags-from-classes.csv")
360 buildRuleToGenerateAnnotationFlags(ctx, "intermediate hidden API flags", classesJars, stubFlags, annotationFlags)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100361
Paul Duffind061d402021-06-07 21:36:01 +0100362 // Generate the monolithic hiddenapi-flags.csv file.
363 //
364 // Use annotation flags generated directly from the classes jars as well as annotation flag files
365 // provided by prebuilts.
366 allAnnotationFlagFiles := android.Paths{annotationFlags}
367 allAnnotationFlagFiles = append(allAnnotationFlagFiles, monolithicInfo.AnnotationFlagsPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100368 allFlags := hiddenAPISingletonPaths(ctx).flags
Paul Duffin67b9d612021-07-21 17:38:47 +0100369 buildRuleToGenerateHiddenApiFlags(ctx, "hiddenAPIFlagsFile", "monolithic hidden API flags", allFlags, stubFlags, allAnnotationFlagFiles, monolithicInfo.FlagsFilesByCategory, monolithicInfo.FlagSubsets, android.OptionalPath{})
Paul Duffin537ea3d2021-05-14 10:38:00 +0100370
371 // Generate an intermediate monolithic hiddenapi-metadata.csv file directly from the annotations
372 // in the source code.
Paul Duffind061d402021-06-07 21:36:01 +0100373 intermediateMetadataCSV := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "metadata-from-classes.csv")
374 buildRuleToGenerateMetadata(ctx, "intermediate hidden API metadata", classesJars, stubFlags, intermediateMetadataCSV)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100375
Paul Duffind061d402021-06-07 21:36:01 +0100376 // Generate the monolithic hiddenapi-metadata.csv file.
377 //
378 // Use metadata files generated directly from the classes jars as well as metadata files provided
379 // by prebuilts.
380 //
381 // This has the side effect of ensuring that the output file uses | quotes just in case that is
382 // important for the tools that consume the metadata file.
383 allMetadataFlagFiles := android.Paths{intermediateMetadataCSV}
384 allMetadataFlagFiles = append(allMetadataFlagFiles, monolithicInfo.MetadataPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100385 metadataCSV := hiddenAPISingletonPaths(ctx).metadata
Paul Duffind061d402021-06-07 21:36:01 +0100386 b.buildRuleMergeCSV(ctx, "monolithic hidden API metadata", allMetadataFlagFiles, metadataCSV)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100387
Paul Duffind061d402021-06-07 21:36:01 +0100388 // Generate an intermediate monolithic hiddenapi-index.csv file directly from the CSV files in the
389 // classes jars.
390 intermediateIndexCSV := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "index-from-classes.csv")
391 buildRuleToGenerateIndex(ctx, "intermediate hidden API index", classesJars, intermediateIndexCSV)
392
393 // Generate the monolithic hiddenapi-index.csv file.
394 //
395 // Use index files generated directly from the classes jars as well as index files provided
396 // by prebuilts.
397 allIndexFlagFiles := android.Paths{intermediateIndexCSV}
398 allIndexFlagFiles = append(allIndexFlagFiles, monolithicInfo.IndexPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100399 indexCSV := hiddenAPISingletonPaths(ctx).index
Paul Duffind061d402021-06-07 21:36:01 +0100400 b.buildRuleMergeCSV(ctx, "monolithic hidden API index", allIndexFlagFiles, indexCSV)
Paul Duffinc8ead412021-06-07 19:28:15 +0100401
402 return bootDexJarByModule
Paul Duffin74431d52021-04-21 14:10:42 +0100403}
404
Paul Duffin438eb572021-05-21 16:58:23 +0100405// createAndProvideMonolithicHiddenAPIInfo creates a MonolithicHiddenAPIInfo and provides it for
406// testing.
Paul Duffin89f570a2021-06-16 01:42:33 +0100407func (b *platformBootclasspathModule) createAndProvideMonolithicHiddenAPIInfo(ctx android.ModuleContext, classpathElements ClasspathElements) MonolithicHiddenAPIInfo {
Paul Duffin1352f7c2021-05-21 22:18:49 +0100408 // Create a temporary input structure in which to collate information provided directly by this
409 // module, either through properties or direct dependencies.
410 temporaryInput := newHiddenAPIFlagInput()
411
412 // Create paths to the flag files specified in the properties.
Paul Duffin9b61abb2022-07-27 16:16:54 +0000413 temporaryInput.extractFlagFilesFromProperties(ctx, &b.properties.HiddenAPIFlagFileProperties)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100414
415 // Create the monolithic info, by starting with the flag files specified on this and then merging
416 // in information from all the fragment dependencies of this.
Paul Duffin89f570a2021-06-16 01:42:33 +0100417 monolithicInfo := newMonolithicHiddenAPIInfo(ctx, temporaryInput.FlagFilesByCategory, classpathElements)
Paul Duffin438eb572021-05-21 16:58:23 +0100418
419 // Store the information for testing.
Colin Cross40213022023-12-13 15:19:49 -0800420 android.SetProvider(ctx, MonolithicHiddenAPIInfoProvider, monolithicInfo)
Paul Duffin438eb572021-05-21 16:58:23 +0100421 return monolithicInfo
422}
423
Paul Duffin537ea3d2021-05-14 10:38:00 +0100424func (b *platformBootclasspathModule) buildRuleMergeCSV(ctx android.ModuleContext, desc string, inputPaths android.Paths, outputPath android.WritablePath) {
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100425 rule := android.NewRuleBuilder(pctx, ctx)
426 rule.Command().
427 BuiltTool("merge_csv").
428 Flag("--key_field signature").
Paul Duffin85dee5d2021-04-13 00:14:38 +0100429 FlagWithOutput("--output=", outputPath).
Paul Duffin537ea3d2021-05-14 10:38:00 +0100430 Inputs(inputPaths)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100431
Paul Duffin537ea3d2021-05-14 10:38:00 +0100432 rule.Build(desc, desc)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100433}