blob: 86062d489fa09a8fdc0c7eaa1fce9d94047281f6 [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 Crossc5f4de52025-01-09 16:04:41 -080018 "github.com/google/blueprint"
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) {
Cole Faust4bc45d82024-11-04 15:56:34 -080029 ctx.RegisterModuleType("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
Colin Crossc5f4de52025-01-09 16:04:41 -080040type platformBootclasspathImplLibDepTagType struct {
41 blueprint.BaseDependencyTag
42}
43
44func (p platformBootclasspathImplLibDepTagType) ExcludeFromVisibilityEnforcement() {}
45
46var platformBootclasspathImplLibDepTag platformBootclasspathImplLibDepTagType
47
48var _ android.ExcludeFromVisibilityEnforcementTag = platformBootclasspathImplLibDepTag
49
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010050type platformBootclasspathModule struct {
Cole Faust4bc45d82024-11-04 15:56:34 -080051 android.ModuleBase
Artur Satayev97259dc2021-04-07 15:17:14 +010052 ClasspathFragmentBase
Paul Duffinb432df92021-03-22 22:09:42 +000053
Paul Duffin62d8c3b2021-04-07 20:35:11 +010054 properties platformBootclasspathProperties
55
Paul Duffinb432df92021-03-22 22:09:42 +000056 // The apex:module pairs obtained from the configured modules.
Paul Duffinb432df92021-03-22 22:09:42 +000057 configuredModules []android.Module
Paul Duffin62d8c3b2021-04-07 20:35:11 +010058
59 // The apex:module pairs obtained from the fragments.
Paul Duffin62d8c3b2021-04-07 20:35:11 +010060 fragments []android.Module
Paul Duffin6a766452021-04-12 14:15:22 +010061
62 // Path to the monolithic hiddenapi-flags.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010063 hiddenAPIFlagsCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010064
65 // Path to the monolithic hiddenapi-index.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010066 hiddenAPIIndexCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010067
68 // Path to the monolithic hiddenapi-unsupported.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010069 hiddenAPIMetadataCSV android.OutputPath
Paul Duffin62d8c3b2021-04-07 20:35:11 +010070}
71
Paul Duffin62d8c3b2021-04-07 20:35:11 +010072type platformBootclasspathProperties struct {
Paul Duffinb67d8782021-04-22 11:49:41 +010073 BootclasspathFragmentsDepsProperties
Paul Duffin702210b2021-04-08 20:12:41 +010074
Paul Duffin9b61abb2022-07-27 16:16:54 +000075 HiddenAPIFlagFileProperties
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010076}
77
Cole Faust4bc45d82024-11-04 15:56:34 -080078func platformBootclasspathFactory() android.Module {
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010079 m := &platformBootclasspathModule{}
Paul Duffin62d8c3b2021-04-07 20:35:11 +010080 m.AddProperties(&m.properties)
satayev95e9c5b2021-04-29 11:50:26 +010081 initClasspathFragment(m, BOOTCLASSPATH)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010082 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
83 return m
84}
85
Artur Satayev97259dc2021-04-07 15:17:14 +010086func (b *platformBootclasspathModule) AndroidMkEntries() (entries []android.AndroidMkEntries) {
87 entries = append(entries, android.AndroidMkEntries{
88 Class: "FAKE",
89 // Need at least one output file in order for this to take effect.
90 OutputFile: android.OptionalPathForPath(b.hiddenAPIFlagsCSV),
91 Include: "$(BUILD_PHONY_PACKAGE)",
92 })
satayev128ce2f2021-05-06 13:21:15 +010093 entries = append(entries, b.classpathFragmentBase().androidMkEntries()...)
Artur Satayev97259dc2021-04-07 15:17:14 +010094 return
Paul Duffin6a766452021-04-12 14:15:22 +010095}
96
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010097func (b *platformBootclasspathModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Spandan Das64c9e0c2023-12-20 20:13:34 +000098 // Create a dependency on all_apex_contributions to determine the selected mainline module
99 ctx.AddDependency(ctx.Module(), apexContributionsMetadataDepTag, "all_apex_contributions")
100
Paul Duffin74431d52021-04-21 14:10:42 +0100101 b.hiddenAPIDepsMutator(ctx)
102
Jiakai Zhangbc698cd2023-05-08 16:28:38 +0000103 if !dexpreopt.IsDex2oatNeeded(ctx) {
Qiao Yang8d8c6602023-05-05 15:03:24 +0000104 return
105 }
106
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100107 // 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)
110}
111
Paul Duffin74431d52021-04-21 14:10:42 +0100112func (b *platformBootclasspathModule) hiddenAPIDepsMutator(ctx android.BottomUpMutatorContext) {
Pratyushfaec4db2023-07-20 11:19:04 +0000113 if ctx.Config().DisableHiddenApiChecks() {
Paul Duffin74431d52021-04-21 14:10:42 +0100114 return
115 }
116
117 // Add dependencies onto the stub lib modules.
Paul Duffin31fad802021-06-18 18:14:25 +0100118 apiLevelToStubLibModules := hiddenAPIComputeMonolithicStubLibModules(ctx.Config())
119 hiddenAPIAddStubLibDependencies(ctx, apiLevelToStubLibModules)
Paul Duffin74431d52021-04-21 14:10:42 +0100120}
121
Paul Duffin4994d262021-04-22 12:08:59 +0100122func (b *platformBootclasspathModule) BootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100123 // Add dependencies on all the ART jars.
124 global := dexpreopt.GetGlobalConfig(ctx)
Spandan Das64c9e0c2023-12-20 20:13:34 +0000125 addDependenciesOntoSelectedBootImageApexes(ctx, "com.android.art")
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000126
127 var bootImageModuleNames []string
128
Spandan Das64c9e0c2023-12-20 20:13:34 +0000129 // TODO: b/308174306 - Remove the mechanism of depending on the java_sdk_library(_import) directly
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100130 addDependenciesOntoBootImageModules(ctx, global.ArtApexJars, platformBootclasspathArtBootJarDepTag)
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000131 bootImageModuleNames = append(bootImageModuleNames, global.ArtApexJars.CopyOfJars()...)
Paul Duffinb432df92021-03-22 22:09:42 +0000132
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100133 // Add dependencies on all the non-updatable jars, which are on the platform or in non-updatable
134 // APEXes.
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000135 platformJars := b.platformJars(ctx)
136 addDependenciesOntoBootImageModules(ctx, platformJars, platformBootclasspathBootJarDepTag)
137 bootImageModuleNames = append(bootImageModuleNames, platformJars.CopyOfJars()...)
Paul Duffinb432df92021-03-22 22:09:42 +0000138
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100139 // Add dependencies on all the updatable jars, except the ART jars.
satayevd604b212021-07-21 14:23:52 +0100140 apexJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
Spandan Das64c9e0c2023-12-20 20:13:34 +0000141 apexes := []string{}
142 for i := 0; i < apexJars.Len(); i++ {
143 apexes = append(apexes, apexJars.Apex(i))
144 }
145 addDependenciesOntoSelectedBootImageApexes(ctx, android.FirstUniqueStrings(apexes)...)
146 // TODO: b/308174306 - Remove the mechanism of depending on the java_sdk_library(_import) directly
satayevd604b212021-07-21 14:23:52 +0100147 addDependenciesOntoBootImageModules(ctx, apexJars, platformBootclasspathApexBootJarDepTag)
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000148 bootImageModuleNames = append(bootImageModuleNames, apexJars.CopyOfJars()...)
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100149
Paul Duffin4994d262021-04-22 12:08:59 +0100150 // Add dependencies on all the fragments.
151 b.properties.BootclasspathFragmentsDepsProperties.addDependenciesOntoFragments(ctx)
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000152
153 for _, bootImageModuleName := range bootImageModuleNames {
154 implLibName := implLibraryModuleName(bootImageModuleName)
155 if ctx.OtherModuleExists(implLibName) {
156 ctx.AddFarVariationDependencies(nil, platformBootclasspathImplLibDepTag, implLibName)
157 }
158 }
Paul Duffinb432df92021-03-22 22:09:42 +0000159}
160
Paul Duffin01b463b2021-04-26 20:05:39 +0100161func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList, tag bootclasspathDependencyTag) {
Paul Duffinb432df92021-03-22 22:09:42 +0000162 for i := 0; i < modules.Len(); i++ {
163 apex := modules.Apex(i)
164 name := modules.Jar(i)
165
Paul Duffin01b463b2021-04-26 20:05:39 +0100166 addDependencyOntoApexModulePair(ctx, apex, name, tag)
Paul Duffinb432df92021-03-22 22:09:42 +0000167 }
168}
169
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100170func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayevd604b212021-07-21 14:23:52 +0100171 // Gather all the dependencies from the art, platform, and apex boot jars.
Paul Duffin01b463b2021-04-26 20:05:39 +0100172 artModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathArtBootJarDepTag)
satayevd604b212021-07-21 14:23:52 +0100173 platformModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathBootJarDepTag)
174 apexModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathApexBootJarDepTag)
Paul Duffin01b463b2021-04-26 20:05:39 +0100175
176 // Concatenate them all, in order as they would appear on the bootclasspath.
177 var allModules []android.Module
178 allModules = append(allModules, artModules...)
satayevd604b212021-07-21 14:23:52 +0100179 allModules = append(allModules, platformModules...)
180 allModules = append(allModules, apexModules...)
Paul Duffin01b463b2021-04-26 20:05:39 +0100181 b.configuredModules = allModules
182
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000183 // Do not add implLibModule to allModules as the impl lib is only used to collect the
184 // transitive source files
185 var implLibModule []android.Module
Jihoon Kang2b133702025-01-09 07:54:41 +0000186 ctx.VisitDirectDepsWithTag(platformBootclasspathImplLibDepTag, func(m android.Module) {
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000187 implLibModule = append(implLibModule, m)
188 })
189
Anton Hansson57162c52023-09-20 13:41:30 +0000190 var transitiveSrcFiles android.Paths
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000191 for _, module := range append(allModules, implLibModule...) {
Colin Cross7727c7f2024-07-18 15:36:32 -0700192 if depInfo, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossa14fb6a2024-10-23 16:57:06 -0700193 transitiveSrcFiles = append(transitiveSrcFiles, depInfo.TransitiveSrcFiles.ToList()...)
Anton Hansson57162c52023-09-20 13:41:30 +0000194 }
195 }
196 jarArgs := resourcePathsToJarArgs(transitiveSrcFiles)
197 jarArgs = append(jarArgs, "-srcjar") // Move srcfiles to the right package
Cole Faust4e9f5922024-11-13 16:09:23 -0800198 srcjar := android.PathForModuleOut(ctx, ctx.ModuleName()+"-transitive.srcjar")
mrziwang444762b2024-06-27 09:58:58 -0700199 TransformResourcesToJar(ctx, srcjar, jarArgs, transitiveSrcFiles)
Anton Hansson57162c52023-09-20 13:41:30 +0000200
Paul Duffin01b463b2021-04-26 20:05:39 +0100201 // Gather all the fragments dependencies.
Paul Duffin9bacf562021-04-28 21:16:02 +0100202 b.fragments = gatherApexModulePairDepsWithTag(ctx, bootclasspathFragmentDepTag)
Paul Duffinb432df92021-03-22 22:09:42 +0000203
Paul Duffinf23bc472021-04-27 12:42:20 +0100204 // Check the configuration of the boot modules.
205 // ART modules are checked by the art-bootclasspath-fragment.
satayevd604b212021-07-21 14:23:52 +0100206 b.checkPlatformModules(ctx, platformModules)
207 b.checkApexModules(ctx, apexModules)
Paul Duffinf23bc472021-04-27 12:42:20 +0100208
satayev013485b2021-05-06 23:38:10 +0100209 b.generateClasspathProtoBuildActions(ctx)
210
Paul Duffinc8ead412021-06-07 19:28:15 +0100211 bootDexJarByModule := b.generateHiddenAPIBuildActions(ctx, b.configuredModules, b.fragments)
212 buildRuleForBootJarsPackageCheck(ctx, bootDexJarByModule)
mrziwang444762b2024-06-27 09:58:58 -0700213
214 ctx.SetOutputFiles(android.Paths{b.hiddenAPIFlagsCSV}, "hiddenapi-flags.csv")
215 ctx.SetOutputFiles(android.Paths{b.hiddenAPIIndexCSV}, "hiddenapi-index.csv")
216 ctx.SetOutputFiles(android.Paths{b.hiddenAPIMetadataCSV}, "hiddenapi-metadata.csv")
217 ctx.SetOutputFiles(android.Paths{srcjar}, ".srcjar")
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100218}
219
satayev013485b2021-05-06 23:38:10 +0100220// Generate classpaths.proto config
221func (b *platformBootclasspathModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
satayevb3090502021-06-15 17:49:10 +0100222 configuredJars := b.configuredJars(ctx)
satayev013485b2021-05-06 23:38:10 +0100223 // ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
satayevb3090502021-06-15 17:49:10 +0100224 classpathJars := configuredJarListToClasspathJars(ctx, configuredJars, BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
225 b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, configuredJars, classpathJars)
Inseob Kimdd532492024-04-30 17:22:58 +0900226 b.classpathFragmentBase().installClasspathProto(ctx)
satayev013485b2021-05-06 23:38:10 +0100227}
228
satayev142ed272021-06-15 16:21:17 +0100229func (b *platformBootclasspathModule) configuredJars(ctx android.ModuleContext) android.ConfiguredJarList {
satayevb3090502021-06-15 17:49:10 +0100230 // Include all non APEX jars
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100231 jars := b.platformJars(ctx)
satayevb3090502021-06-15 17:49:10 +0100232
233 // Include jars from APEXes that don't populate their classpath proto config.
satayevd604b212021-07-21 14:23:52 +0100234 remainingJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
satayevb3090502021-06-15 17:49:10 +0100235 for _, fragment := range b.fragments {
Colin Cross313aa542023-12-13 13:47:44 -0800236 info, _ := android.OtherModuleProvider(ctx, fragment, ClasspathFragmentProtoContentInfoProvider)
satayevb3090502021-06-15 17:49:10 +0100237 if info.ClasspathFragmentProtoGenerated {
238 remainingJars = remainingJars.RemoveList(info.ClasspathFragmentProtoContents)
239 }
240 }
241 for i := 0; i < remainingJars.Len(); i++ {
242 jars = jars.Append(remainingJars.Apex(i), remainingJars.Jar(i))
243 }
244
245 return jars
satayev013485b2021-05-06 23:38:10 +0100246}
247
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100248func (b *platformBootclasspathModule) platformJars(ctx android.PathContext) android.ConfiguredJarList {
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100249 global := dexpreopt.GetGlobalConfig(ctx)
250 return global.BootJars.RemoveList(global.ArtApexJars)
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100251}
252
satayevd604b212021-07-21 14:23:52 +0100253// checkPlatformModules ensures that the non-updatable modules supplied are not part of an
254// apex module.
255func (b *platformBootclasspathModule) checkPlatformModules(ctx android.ModuleContext, modules []android.Module) {
256 // TODO(satayev): change this check to only allow core-icu4j, all apex jars should not be here.
Paul Duffinf23bc472021-04-27 12:42:20 +0100257 for _, m := range modules {
Colin Cross313aa542023-12-13 13:47:44 -0800258 apexInfo, _ := android.OtherModuleProvider(ctx, m, android.ApexInfoProvider)
Paul Duffinf23bc472021-04-27 12:42:20 +0100259 fromUpdatableApex := apexInfo.Updatable
260 if fromUpdatableApex {
261 // error: this jar is part of an updatable apex
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100262 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 +0100263 } else {
264 // ok: this jar is part of the platform or a non-updatable apex
265 }
266 }
267}
268
satayevd604b212021-07-21 14:23:52 +0100269// checkApexModules ensures that the apex modules supplied are not from the platform.
270func (b *platformBootclasspathModule) checkApexModules(ctx android.ModuleContext, modules []android.Module) {
Paul Duffinf23bc472021-04-27 12:42:20 +0100271 for _, m := range modules {
Colin Cross313aa542023-12-13 13:47:44 -0800272 apexInfo, _ := android.OtherModuleProvider(ctx, m, android.ApexInfoProvider)
Paul Duffinf23bc472021-04-27 12:42:20 +0100273 fromUpdatableApex := apexInfo.Updatable
274 if fromUpdatableApex {
275 // ok: this jar is part of an updatable apex
276 } else {
277 name := ctx.OtherModuleName(m)
278 if apexInfo.IsForPlatform() {
Paul Duffin7487a7a2021-05-19 09:36:09 +0100279 // If AlwaysUsePrebuiltSdks() returns true then it is possible that the updatable list will
280 // include platform variants of a prebuilt module due to workarounds elsewhere. In that case
281 // do not treat this as an error.
282 // TODO(b/179354495): Always treat this as an error when migration to bootclasspath_fragment
283 // modules is complete.
284 if !ctx.Config().AlwaysUsePrebuiltSdks() {
285 // error: this jar is part of the platform
satayevd604b212021-07-21 14:23:52 +0100286 ctx.ModuleErrorf("module %q from platform is not allowed in the apex boot jars list", name)
Paul Duffin7487a7a2021-05-19 09:36:09 +0100287 }
Paul Duffinf23bc472021-04-27 12:42:20 +0100288 } else {
289 // TODO(b/177892522): Treat this as an error.
290 // Cannot do that at the moment because framework-wifi and framework-tethering are in the
satayevd604b212021-07-21 14:23:52 +0100291 // PRODUCT_APEX_BOOT_JARS but not marked as updatable in AOSP.
Paul Duffinf23bc472021-04-27 12:42:20 +0100292 }
293 }
294 }
295}
296
Paul Duffin702210b2021-04-08 20:12:41 +0100297// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffinc8ead412021-06-07 19:28:15 +0100298func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module, fragments []android.Module) bootDexJarByModule {
Spandan Dasa90db962024-05-20 18:37:17 +0000299 createEmptyHiddenApiFiles := func() {
300 paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV}
301 for _, path := range paths {
302 ctx.Build(pctx, android.BuildParams{
303 Rule: android.Touch,
304 Output: path,
305 })
306 }
307 }
Paul Duffin702210b2021-04-08 20:12:41 +0100308
Paul Duffin90b8ad32021-04-13 12:25:01 +0100309 // Save the paths to the monolithic files for retrieval via OutputFiles().
310 b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
311 b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
312 b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
Paul Duffin6a766452021-04-12 14:15:22 +0100313
Adrian Roose95a15e2021-06-21 16:03:11 +0200314 bootDexJarByModule := extractBootDexJarsFromModules(ctx, modules)
315
Pratyushfaec4db2023-07-20 11:19:04 +0000316 // Don't run any hiddenapi rules if hidden api checks are disabled. This is a performance
Paul Duffin0b659862021-04-13 13:02:29 +0100317 // optimization that can be used to reduce the incremental build time but as its name suggests it
318 // can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath.
Pratyushfaec4db2023-07-20 11:19:04 +0000319 if ctx.Config().DisableHiddenApiChecks() {
Spandan Dasa90db962024-05-20 18:37:17 +0000320 createEmptyHiddenApiFiles()
Adrian Roose95a15e2021-06-21 16:03:11 +0200321 return bootDexJarByModule
Paul Duffin0b659862021-04-13 13:02:29 +0100322 }
323
Paul Duffin89f570a2021-06-16 01:42:33 +0100324 // Construct a list of ClasspathElement objects from the modules and fragments.
325 classpathElements := CreateClasspathElements(ctx, modules, fragments)
326
327 monolithicInfo := b.createAndProvideMonolithicHiddenAPIInfo(ctx, classpathElements)
328
329 // Extract the classes jars only from those libraries that do not have corresponding fragments as
330 // the fragments will have already provided the flags that are needed.
331 classesJars := monolithicInfo.ClassesJars
332
Spandan Das81fe4d12024-05-15 18:43:47 +0000333 if len(classesJars) == 0 {
334 // This product does not include any monolithic jars. Monolithic hiddenapi flag generation is not required.
Spandan Dasa90db962024-05-20 18:37:17 +0000335 // However, generate an empty file so that the dist tags in f/b/boot/Android.bp can be resolved, and `m dist` works.
336 createEmptyHiddenApiFiles()
Spandan Das81fe4d12024-05-15 18:43:47 +0000337 return bootDexJarByModule
338 }
339
Paul Duffin4539a372021-06-23 23:20:43 +0100340 // Create the input to pass to buildRuleToGenerateHiddenAPIStubFlagsFile
Paul Duffin1352f7c2021-05-21 22:18:49 +0100341 input := newHiddenAPIFlagInput()
342
343 // Gather stub library information from the dependencies on modules provided by
344 // hiddenAPIComputeMonolithicStubLibModules.
345 input.gatherStubLibInfo(ctx, nil)
346
347 // Use the flag files from this module and all the fragments.
348 input.FlagFilesByCategory = monolithicInfo.FlagsFilesByCategory
Paul Duffin74431d52021-04-21 14:10:42 +0100349
Paul Duffin537ea3d2021-05-14 10:38:00 +0100350 // Generate the monolithic stub-flags.csv file.
Paul Duffin537ea3d2021-05-14 10:38:00 +0100351 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
Paul Duffin67b9d612021-07-21 17:38:47 +0100352 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 +0100353
Paul Duffin537ea3d2021-05-14 10:38:00 +0100354 // Generate the annotation-flags.csv file from all the module annotations.
Paul Duffind061d402021-06-07 21:36:01 +0100355 annotationFlags := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "annotation-flags-from-classes.csv")
356 buildRuleToGenerateAnnotationFlags(ctx, "intermediate hidden API flags", classesJars, stubFlags, annotationFlags)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100357
Paul Duffind061d402021-06-07 21:36:01 +0100358 // Generate the monolithic hiddenapi-flags.csv file.
359 //
360 // Use annotation flags generated directly from the classes jars as well as annotation flag files
361 // provided by prebuilts.
362 allAnnotationFlagFiles := android.Paths{annotationFlags}
363 allAnnotationFlagFiles = append(allAnnotationFlagFiles, monolithicInfo.AnnotationFlagsPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100364 allFlags := hiddenAPISingletonPaths(ctx).flags
Paul Duffin67b9d612021-07-21 17:38:47 +0100365 buildRuleToGenerateHiddenApiFlags(ctx, "hiddenAPIFlagsFile", "monolithic hidden API flags", allFlags, stubFlags, allAnnotationFlagFiles, monolithicInfo.FlagsFilesByCategory, monolithicInfo.FlagSubsets, android.OptionalPath{})
Paul Duffin537ea3d2021-05-14 10:38:00 +0100366
367 // Generate an intermediate monolithic hiddenapi-metadata.csv file directly from the annotations
368 // in the source code.
Paul Duffind061d402021-06-07 21:36:01 +0100369 intermediateMetadataCSV := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "metadata-from-classes.csv")
370 buildRuleToGenerateMetadata(ctx, "intermediate hidden API metadata", classesJars, stubFlags, intermediateMetadataCSV)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100371
Paul Duffind061d402021-06-07 21:36:01 +0100372 // Generate the monolithic hiddenapi-metadata.csv file.
373 //
374 // Use metadata files generated directly from the classes jars as well as metadata files provided
375 // by prebuilts.
376 //
377 // This has the side effect of ensuring that the output file uses | quotes just in case that is
378 // important for the tools that consume the metadata file.
379 allMetadataFlagFiles := android.Paths{intermediateMetadataCSV}
380 allMetadataFlagFiles = append(allMetadataFlagFiles, monolithicInfo.MetadataPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100381 metadataCSV := hiddenAPISingletonPaths(ctx).metadata
Paul Duffind061d402021-06-07 21:36:01 +0100382 b.buildRuleMergeCSV(ctx, "monolithic hidden API metadata", allMetadataFlagFiles, metadataCSV)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100383
Paul Duffind061d402021-06-07 21:36:01 +0100384 // Generate an intermediate monolithic hiddenapi-index.csv file directly from the CSV files in the
385 // classes jars.
386 intermediateIndexCSV := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "index-from-classes.csv")
387 buildRuleToGenerateIndex(ctx, "intermediate hidden API index", classesJars, intermediateIndexCSV)
388
389 // Generate the monolithic hiddenapi-index.csv file.
390 //
391 // Use index files generated directly from the classes jars as well as index files provided
392 // by prebuilts.
393 allIndexFlagFiles := android.Paths{intermediateIndexCSV}
394 allIndexFlagFiles = append(allIndexFlagFiles, monolithicInfo.IndexPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100395 indexCSV := hiddenAPISingletonPaths(ctx).index
Paul Duffind061d402021-06-07 21:36:01 +0100396 b.buildRuleMergeCSV(ctx, "monolithic hidden API index", allIndexFlagFiles, indexCSV)
Paul Duffinc8ead412021-06-07 19:28:15 +0100397
398 return bootDexJarByModule
Paul Duffin74431d52021-04-21 14:10:42 +0100399}
400
Paul Duffin438eb572021-05-21 16:58:23 +0100401// createAndProvideMonolithicHiddenAPIInfo creates a MonolithicHiddenAPIInfo and provides it for
402// testing.
Paul Duffin89f570a2021-06-16 01:42:33 +0100403func (b *platformBootclasspathModule) createAndProvideMonolithicHiddenAPIInfo(ctx android.ModuleContext, classpathElements ClasspathElements) MonolithicHiddenAPIInfo {
Paul Duffin1352f7c2021-05-21 22:18:49 +0100404 // Create a temporary input structure in which to collate information provided directly by this
405 // module, either through properties or direct dependencies.
406 temporaryInput := newHiddenAPIFlagInput()
407
408 // Create paths to the flag files specified in the properties.
Paul Duffin9b61abb2022-07-27 16:16:54 +0000409 temporaryInput.extractFlagFilesFromProperties(ctx, &b.properties.HiddenAPIFlagFileProperties)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100410
411 // Create the monolithic info, by starting with the flag files specified on this and then merging
412 // in information from all the fragment dependencies of this.
Paul Duffin89f570a2021-06-16 01:42:33 +0100413 monolithicInfo := newMonolithicHiddenAPIInfo(ctx, temporaryInput.FlagFilesByCategory, classpathElements)
Paul Duffin438eb572021-05-21 16:58:23 +0100414
415 // Store the information for testing.
Colin Cross40213022023-12-13 15:19:49 -0800416 android.SetProvider(ctx, MonolithicHiddenAPIInfoProvider, monolithicInfo)
Paul Duffin438eb572021-05-21 16:58:23 +0100417 return monolithicInfo
418}
419
Paul Duffin537ea3d2021-05-14 10:38:00 +0100420func (b *platformBootclasspathModule) buildRuleMergeCSV(ctx android.ModuleContext, desc string, inputPaths android.Paths, outputPath android.WritablePath) {
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100421 rule := android.NewRuleBuilder(pctx, ctx)
422 rule.Command().
423 BuiltTool("merge_csv").
424 Flag("--key_field signature").
Paul Duffin85dee5d2021-04-13 00:14:38 +0100425 FlagWithOutput("--output=", outputPath).
Paul Duffin537ea3d2021-05-14 10:38:00 +0100426 Inputs(inputPaths)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100427
Paul Duffin537ea3d2021-05-14 10:38:00 +0100428 rule.Build(desc, desc)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100429}