blob: acfc7745ee9a4b3a6915968a57360d4c29de2e6d [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 (
18 "android/soong/android"
19 "android/soong/dexpreopt"
20)
21
22func init() {
23 registerPlatformBootclasspathBuildComponents(android.InitRegistrationContext)
24}
25
26func registerPlatformBootclasspathBuildComponents(ctx android.RegistrationContext) {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000027 ctx.RegisterParallelSingletonModuleType("platform_bootclasspath", platformBootclasspathFactory)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010028}
29
Paul Duffin01b463b2021-04-26 20:05:39 +010030// The tags used for the dependencies between the platform bootclasspath and any configured boot
31// jars.
32var (
satayevd604b212021-07-21 14:23:52 +010033 platformBootclasspathArtBootJarDepTag = bootclasspathDependencyTag{name: "art-boot-jar"}
34 platformBootclasspathBootJarDepTag = bootclasspathDependencyTag{name: "platform-boot-jar"}
35 platformBootclasspathApexBootJarDepTag = bootclasspathDependencyTag{name: "apex-boot-jar"}
Jihoon Kanga6d0aa82024-09-24 00:34:49 +000036 platformBootclasspathImplLibDepTag = dependencyTag{name: "impl-lib-tag"}
Paul Duffin01b463b2021-04-26 20:05:39 +010037)
Paul Duffinb432df92021-03-22 22:09:42 +000038
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010039type platformBootclasspathModule struct {
Paul Duffine3ecce62021-04-29 10:34:11 +010040 android.SingletonModuleBase
Artur Satayev97259dc2021-04-07 15:17:14 +010041 ClasspathFragmentBase
Paul Duffinb432df92021-03-22 22:09:42 +000042
Paul Duffin62d8c3b2021-04-07 20:35:11 +010043 properties platformBootclasspathProperties
44
Paul Duffinb432df92021-03-22 22:09:42 +000045 // The apex:module pairs obtained from the configured modules.
Paul Duffinb432df92021-03-22 22:09:42 +000046 configuredModules []android.Module
Paul Duffin62d8c3b2021-04-07 20:35:11 +010047
48 // The apex:module pairs obtained from the fragments.
Paul Duffin62d8c3b2021-04-07 20:35:11 +010049 fragments []android.Module
Paul Duffin6a766452021-04-12 14:15:22 +010050
51 // Path to the monolithic hiddenapi-flags.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010052 hiddenAPIFlagsCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010053
54 // Path to the monolithic hiddenapi-index.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010055 hiddenAPIIndexCSV android.OutputPath
Paul Duffin6a766452021-04-12 14:15:22 +010056
57 // Path to the monolithic hiddenapi-unsupported.csv file.
Paul Duffin0b659862021-04-13 13:02:29 +010058 hiddenAPIMetadataCSV android.OutputPath
Paul Duffin62d8c3b2021-04-07 20:35:11 +010059}
60
Paul Duffin62d8c3b2021-04-07 20:35:11 +010061type platformBootclasspathProperties struct {
Paul Duffinb67d8782021-04-22 11:49:41 +010062 BootclasspathFragmentsDepsProperties
Paul Duffin702210b2021-04-08 20:12:41 +010063
Paul Duffin9b61abb2022-07-27 16:16:54 +000064 HiddenAPIFlagFileProperties
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010065}
66
Paul Duffine3ecce62021-04-29 10:34:11 +010067func platformBootclasspathFactory() android.SingletonModule {
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010068 m := &platformBootclasspathModule{}
Paul Duffin62d8c3b2021-04-07 20:35:11 +010069 m.AddProperties(&m.properties)
satayev95e9c5b2021-04-29 11:50:26 +010070 initClasspathFragment(m, BOOTCLASSPATH)
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010071 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
72 return m
73}
74
Artur Satayev97259dc2021-04-07 15:17:14 +010075func (b *platformBootclasspathModule) AndroidMkEntries() (entries []android.AndroidMkEntries) {
76 entries = append(entries, android.AndroidMkEntries{
77 Class: "FAKE",
78 // Need at least one output file in order for this to take effect.
79 OutputFile: android.OptionalPathForPath(b.hiddenAPIFlagsCSV),
80 Include: "$(BUILD_PHONY_PACKAGE)",
81 })
satayev128ce2f2021-05-06 13:21:15 +010082 entries = append(entries, b.classpathFragmentBase().androidMkEntries()...)
Artur Satayev97259dc2021-04-07 15:17:14 +010083 return
Paul Duffin6a766452021-04-12 14:15:22 +010084}
85
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010086func (b *platformBootclasspathModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Spandan Das64c9e0c2023-12-20 20:13:34 +000087 // Create a dependency on all_apex_contributions to determine the selected mainline module
88 ctx.AddDependency(ctx.Module(), apexContributionsMetadataDepTag, "all_apex_contributions")
89
Paul Duffin74431d52021-04-21 14:10:42 +010090 b.hiddenAPIDepsMutator(ctx)
91
Jiakai Zhangbc698cd2023-05-08 16:28:38 +000092 if !dexpreopt.IsDex2oatNeeded(ctx) {
Qiao Yang8d8c6602023-05-05 15:03:24 +000093 return
94 }
95
Paul Duffinbb7f1ac2021-03-29 22:18:45 +010096 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
97 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
98 dexpreopt.RegisterToolDeps(ctx)
99}
100
Paul Duffin74431d52021-04-21 14:10:42 +0100101func (b *platformBootclasspathModule) hiddenAPIDepsMutator(ctx android.BottomUpMutatorContext) {
Pratyushfaec4db2023-07-20 11:19:04 +0000102 if ctx.Config().DisableHiddenApiChecks() {
Paul Duffin74431d52021-04-21 14:10:42 +0100103 return
104 }
105
106 // Add dependencies onto the stub lib modules.
Paul Duffin31fad802021-06-18 18:14:25 +0100107 apiLevelToStubLibModules := hiddenAPIComputeMonolithicStubLibModules(ctx.Config())
108 hiddenAPIAddStubLibDependencies(ctx, apiLevelToStubLibModules)
Paul Duffin74431d52021-04-21 14:10:42 +0100109}
110
Paul Duffin4994d262021-04-22 12:08:59 +0100111func (b *platformBootclasspathModule) BootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
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
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100119 addDependenciesOntoBootImageModules(ctx, global.ArtApexJars, platformBootclasspathArtBootJarDepTag)
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)
125 addDependenciesOntoBootImageModules(ctx, platformJars, platformBootclasspathBootJarDepTag)
126 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
satayevd604b212021-07-21 14:23:52 +0100136 addDependenciesOntoBootImageModules(ctx, apexJars, platformBootclasspathApexBootJarDepTag)
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
Paul Duffin01b463b2021-04-26 20:05:39 +0100150func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList, tag bootclasspathDependencyTag) {
Paul Duffinb432df92021-03-22 22:09:42 +0000151 for i := 0; i < modules.Len(); i++ {
152 apex := modules.Apex(i)
153 name := modules.Jar(i)
154
Paul Duffin01b463b2021-04-26 20:05:39 +0100155 addDependencyOntoApexModulePair(ctx, apex, name, tag)
Paul Duffinb432df92021-03-22 22:09:42 +0000156 }
157}
158
Paul Duffine3ecce62021-04-29 10:34:11 +0100159// GenerateSingletonBuildActions does nothing and must never do anything.
160//
161// This module only implements android.SingletonModule so that it can implement
162// android.SingletonMakeVarsProvider.
163func (b *platformBootclasspathModule) GenerateSingletonBuildActions(android.SingletonContext) {
164 // Keep empty
165}
166
167func (d *platformBootclasspathModule) MakeVars(ctx android.MakeVarsContext) {
Paul Duffin12d29b72021-04-29 13:50:01 +0100168 d.generateHiddenApiMakeVars(ctx)
Paul Duffine3ecce62021-04-29 10:34:11 +0100169}
170
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100171func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayevd604b212021-07-21 14:23:52 +0100172 // Gather all the dependencies from the art, platform, and apex boot jars.
Paul Duffin01b463b2021-04-26 20:05:39 +0100173 artModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathArtBootJarDepTag)
satayevd604b212021-07-21 14:23:52 +0100174 platformModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathBootJarDepTag)
175 apexModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathApexBootJarDepTag)
Paul Duffin01b463b2021-04-26 20:05:39 +0100176
177 // Concatenate them all, in order as they would appear on the bootclasspath.
178 var allModules []android.Module
179 allModules = append(allModules, artModules...)
satayevd604b212021-07-21 14:23:52 +0100180 allModules = append(allModules, platformModules...)
181 allModules = append(allModules, apexModules...)
Paul Duffin01b463b2021-04-26 20:05:39 +0100182 b.configuredModules = allModules
183
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000184 // Do not add implLibModule to allModules as the impl lib is only used to collect the
185 // transitive source files
186 var implLibModule []android.Module
187 ctx.VisitDirectDepsWithTag(implLibraryTag, func(m android.Module) {
188 implLibModule = append(implLibModule, m)
189 })
190
Anton Hansson57162c52023-09-20 13:41:30 +0000191 var transitiveSrcFiles android.Paths
Jihoon Kanga6d0aa82024-09-24 00:34:49 +0000192 for _, module := range append(allModules, implLibModule...) {
Colin Cross7727c7f2024-07-18 15:36:32 -0700193 if depInfo, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossa14fb6a2024-10-23 16:57:06 -0700194 transitiveSrcFiles = append(transitiveSrcFiles, depInfo.TransitiveSrcFiles.ToList()...)
Anton Hansson57162c52023-09-20 13:41:30 +0000195 }
196 }
197 jarArgs := resourcePathsToJarArgs(transitiveSrcFiles)
198 jarArgs = append(jarArgs, "-srcjar") // Move srcfiles to the right package
mrziwang444762b2024-06-27 09:58:58 -0700199 srcjar := android.PathForModuleOut(ctx, ctx.ModuleName()+"-transitive.srcjar").OutputPath
200 TransformResourcesToJar(ctx, srcjar, jarArgs, transitiveSrcFiles)
Anton Hansson57162c52023-09-20 13:41:30 +0000201
Paul Duffin01b463b2021-04-26 20:05:39 +0100202 // Gather all the fragments dependencies.
Paul Duffin9bacf562021-04-28 21:16:02 +0100203 b.fragments = gatherApexModulePairDepsWithTag(ctx, bootclasspathFragmentDepTag)
Paul Duffinb432df92021-03-22 22:09:42 +0000204
Paul Duffinf23bc472021-04-27 12:42:20 +0100205 // Check the configuration of the boot modules.
206 // ART modules are checked by the art-bootclasspath-fragment.
satayevd604b212021-07-21 14:23:52 +0100207 b.checkPlatformModules(ctx, platformModules)
208 b.checkApexModules(ctx, apexModules)
Paul Duffinf23bc472021-04-27 12:42:20 +0100209
satayev013485b2021-05-06 23:38:10 +0100210 b.generateClasspathProtoBuildActions(ctx)
211
Paul Duffinc8ead412021-06-07 19:28:15 +0100212 bootDexJarByModule := b.generateHiddenAPIBuildActions(ctx, b.configuredModules, b.fragments)
213 buildRuleForBootJarsPackageCheck(ctx, bootDexJarByModule)
mrziwang444762b2024-06-27 09:58:58 -0700214
215 ctx.SetOutputFiles(android.Paths{b.hiddenAPIFlagsCSV}, "hiddenapi-flags.csv")
216 ctx.SetOutputFiles(android.Paths{b.hiddenAPIIndexCSV}, "hiddenapi-index.csv")
217 ctx.SetOutputFiles(android.Paths{b.hiddenAPIMetadataCSV}, "hiddenapi-metadata.csv")
218 ctx.SetOutputFiles(android.Paths{srcjar}, ".srcjar")
Paul Duffinbb7f1ac2021-03-29 22:18:45 +0100219}
220
satayev013485b2021-05-06 23:38:10 +0100221// Generate classpaths.proto config
222func (b *platformBootclasspathModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
satayevb3090502021-06-15 17:49:10 +0100223 configuredJars := b.configuredJars(ctx)
satayev013485b2021-05-06 23:38:10 +0100224 // ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
satayevb3090502021-06-15 17:49:10 +0100225 classpathJars := configuredJarListToClasspathJars(ctx, configuredJars, BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
226 b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, configuredJars, classpathJars)
Inseob Kimdd532492024-04-30 17:22:58 +0900227 b.classpathFragmentBase().installClasspathProto(ctx)
satayev013485b2021-05-06 23:38:10 +0100228}
229
satayev142ed272021-06-15 16:21:17 +0100230func (b *platformBootclasspathModule) configuredJars(ctx android.ModuleContext) android.ConfiguredJarList {
satayevb3090502021-06-15 17:49:10 +0100231 // Include all non APEX jars
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100232 jars := b.platformJars(ctx)
satayevb3090502021-06-15 17:49:10 +0100233
234 // Include jars from APEXes that don't populate their classpath proto config.
satayevd604b212021-07-21 14:23:52 +0100235 remainingJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
satayevb3090502021-06-15 17:49:10 +0100236 for _, fragment := range b.fragments {
Colin Cross313aa542023-12-13 13:47:44 -0800237 info, _ := android.OtherModuleProvider(ctx, fragment, ClasspathFragmentProtoContentInfoProvider)
satayevb3090502021-06-15 17:49:10 +0100238 if info.ClasspathFragmentProtoGenerated {
239 remainingJars = remainingJars.RemoveList(info.ClasspathFragmentProtoContents)
240 }
241 }
242 for i := 0; i < remainingJars.Len(); i++ {
243 jars = jars.Append(remainingJars.Apex(i), remainingJars.Jar(i))
244 }
245
246 return jars
satayev013485b2021-05-06 23:38:10 +0100247}
248
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100249func (b *platformBootclasspathModule) platformJars(ctx android.PathContext) android.ConfiguredJarList {
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100250 global := dexpreopt.GetGlobalConfig(ctx)
251 return global.BootJars.RemoveList(global.ArtApexJars)
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100252}
253
satayevd604b212021-07-21 14:23:52 +0100254// checkPlatformModules ensures that the non-updatable modules supplied are not part of an
255// apex module.
256func (b *platformBootclasspathModule) checkPlatformModules(ctx android.ModuleContext, modules []android.Module) {
257 // TODO(satayev): change this check to only allow core-icu4j, all apex jars should not be here.
Paul Duffinf23bc472021-04-27 12:42:20 +0100258 for _, m := range modules {
Colin Cross313aa542023-12-13 13:47:44 -0800259 apexInfo, _ := android.OtherModuleProvider(ctx, m, android.ApexInfoProvider)
Paul Duffinf23bc472021-04-27 12:42:20 +0100260 fromUpdatableApex := apexInfo.Updatable
261 if fromUpdatableApex {
262 // error: this jar is part of an updatable apex
Jiakai Zhangc08c1622023-05-10 18:38:34 +0100263 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 +0100264 } else {
265 // ok: this jar is part of the platform or a non-updatable apex
266 }
267 }
268}
269
satayevd604b212021-07-21 14:23:52 +0100270// checkApexModules ensures that the apex modules supplied are not from the platform.
271func (b *platformBootclasspathModule) checkApexModules(ctx android.ModuleContext, modules []android.Module) {
Paul Duffinf23bc472021-04-27 12:42:20 +0100272 for _, m := range modules {
Colin Cross313aa542023-12-13 13:47:44 -0800273 apexInfo, _ := android.OtherModuleProvider(ctx, m, android.ApexInfoProvider)
Paul Duffinf23bc472021-04-27 12:42:20 +0100274 fromUpdatableApex := apexInfo.Updatable
275 if fromUpdatableApex {
276 // ok: this jar is part of an updatable apex
277 } else {
278 name := ctx.OtherModuleName(m)
279 if apexInfo.IsForPlatform() {
Paul Duffin7487a7a2021-05-19 09:36:09 +0100280 // If AlwaysUsePrebuiltSdks() returns true then it is possible that the updatable list will
281 // include platform variants of a prebuilt module due to workarounds elsewhere. In that case
282 // do not treat this as an error.
283 // TODO(b/179354495): Always treat this as an error when migration to bootclasspath_fragment
284 // modules is complete.
285 if !ctx.Config().AlwaysUsePrebuiltSdks() {
286 // error: this jar is part of the platform
satayevd604b212021-07-21 14:23:52 +0100287 ctx.ModuleErrorf("module %q from platform is not allowed in the apex boot jars list", name)
Paul Duffin7487a7a2021-05-19 09:36:09 +0100288 }
Paul Duffinf23bc472021-04-27 12:42:20 +0100289 } else {
290 // TODO(b/177892522): Treat this as an error.
291 // Cannot do that at the moment because framework-wifi and framework-tethering are in the
satayevd604b212021-07-21 14:23:52 +0100292 // PRODUCT_APEX_BOOT_JARS but not marked as updatable in AOSP.
Paul Duffinf23bc472021-04-27 12:42:20 +0100293 }
294 }
295 }
296}
297
Paul Duffin702210b2021-04-08 20:12:41 +0100298// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffinc8ead412021-06-07 19:28:15 +0100299func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module, fragments []android.Module) bootDexJarByModule {
Spandan Dasa90db962024-05-20 18:37:17 +0000300 createEmptyHiddenApiFiles := func() {
301 paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV}
302 for _, path := range paths {
303 ctx.Build(pctx, android.BuildParams{
304 Rule: android.Touch,
305 Output: path,
306 })
307 }
308 }
Paul Duffin702210b2021-04-08 20:12:41 +0100309
Paul Duffin90b8ad32021-04-13 12:25:01 +0100310 // Save the paths to the monolithic files for retrieval via OutputFiles().
311 b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
312 b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
313 b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
Paul Duffin6a766452021-04-12 14:15:22 +0100314
Adrian Roose95a15e2021-06-21 16:03:11 +0200315 bootDexJarByModule := extractBootDexJarsFromModules(ctx, modules)
316
Pratyushfaec4db2023-07-20 11:19:04 +0000317 // Don't run any hiddenapi rules if hidden api checks are disabled. This is a performance
Paul Duffin0b659862021-04-13 13:02:29 +0100318 // optimization that can be used to reduce the incremental build time but as its name suggests it
319 // can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath.
Pratyushfaec4db2023-07-20 11:19:04 +0000320 if ctx.Config().DisableHiddenApiChecks() {
Spandan Dasa90db962024-05-20 18:37:17 +0000321 createEmptyHiddenApiFiles()
Adrian Roose95a15e2021-06-21 16:03:11 +0200322 return bootDexJarByModule
Paul Duffin0b659862021-04-13 13:02:29 +0100323 }
324
Paul Duffin89f570a2021-06-16 01:42:33 +0100325 // Construct a list of ClasspathElement objects from the modules and fragments.
326 classpathElements := CreateClasspathElements(ctx, modules, fragments)
327
328 monolithicInfo := b.createAndProvideMonolithicHiddenAPIInfo(ctx, classpathElements)
329
330 // Extract the classes jars only from those libraries that do not have corresponding fragments as
331 // the fragments will have already provided the flags that are needed.
332 classesJars := monolithicInfo.ClassesJars
333
Spandan Das81fe4d12024-05-15 18:43:47 +0000334 if len(classesJars) == 0 {
335 // This product does not include any monolithic jars. Monolithic hiddenapi flag generation is not required.
Spandan Dasa90db962024-05-20 18:37:17 +0000336 // However, generate an empty file so that the dist tags in f/b/boot/Android.bp can be resolved, and `m dist` works.
337 createEmptyHiddenApiFiles()
Spandan Das81fe4d12024-05-15 18:43:47 +0000338 return bootDexJarByModule
339 }
340
Paul Duffin4539a372021-06-23 23:20:43 +0100341 // Create the input to pass to buildRuleToGenerateHiddenAPIStubFlagsFile
Paul Duffin1352f7c2021-05-21 22:18:49 +0100342 input := newHiddenAPIFlagInput()
343
344 // Gather stub library information from the dependencies on modules provided by
345 // hiddenAPIComputeMonolithicStubLibModules.
346 input.gatherStubLibInfo(ctx, nil)
347
348 // Use the flag files from this module and all the fragments.
349 input.FlagFilesByCategory = monolithicInfo.FlagsFilesByCategory
Paul Duffin74431d52021-04-21 14:10:42 +0100350
Paul Duffin537ea3d2021-05-14 10:38:00 +0100351 // Generate the monolithic stub-flags.csv file.
Paul Duffin537ea3d2021-05-14 10:38:00 +0100352 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
Paul Duffin67b9d612021-07-21 17:38:47 +0100353 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 +0100354
Paul Duffin537ea3d2021-05-14 10:38:00 +0100355 // Generate the annotation-flags.csv file from all the module annotations.
Paul Duffind061d402021-06-07 21:36:01 +0100356 annotationFlags := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "annotation-flags-from-classes.csv")
357 buildRuleToGenerateAnnotationFlags(ctx, "intermediate hidden API flags", classesJars, stubFlags, annotationFlags)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100358
Paul Duffind061d402021-06-07 21:36:01 +0100359 // Generate the monolithic hiddenapi-flags.csv file.
360 //
361 // Use annotation flags generated directly from the classes jars as well as annotation flag files
362 // provided by prebuilts.
363 allAnnotationFlagFiles := android.Paths{annotationFlags}
364 allAnnotationFlagFiles = append(allAnnotationFlagFiles, monolithicInfo.AnnotationFlagsPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100365 allFlags := hiddenAPISingletonPaths(ctx).flags
Paul Duffin67b9d612021-07-21 17:38:47 +0100366 buildRuleToGenerateHiddenApiFlags(ctx, "hiddenAPIFlagsFile", "monolithic hidden API flags", allFlags, stubFlags, allAnnotationFlagFiles, monolithicInfo.FlagsFilesByCategory, monolithicInfo.FlagSubsets, android.OptionalPath{})
Paul Duffin537ea3d2021-05-14 10:38:00 +0100367
368 // Generate an intermediate monolithic hiddenapi-metadata.csv file directly from the annotations
369 // in the source code.
Paul Duffind061d402021-06-07 21:36:01 +0100370 intermediateMetadataCSV := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "metadata-from-classes.csv")
371 buildRuleToGenerateMetadata(ctx, "intermediate hidden API metadata", classesJars, stubFlags, intermediateMetadataCSV)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100372
Paul Duffind061d402021-06-07 21:36:01 +0100373 // Generate the monolithic hiddenapi-metadata.csv file.
374 //
375 // Use metadata files generated directly from the classes jars as well as metadata files provided
376 // by prebuilts.
377 //
378 // This has the side effect of ensuring that the output file uses | quotes just in case that is
379 // important for the tools that consume the metadata file.
380 allMetadataFlagFiles := android.Paths{intermediateMetadataCSV}
381 allMetadataFlagFiles = append(allMetadataFlagFiles, monolithicInfo.MetadataPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100382 metadataCSV := hiddenAPISingletonPaths(ctx).metadata
Paul Duffind061d402021-06-07 21:36:01 +0100383 b.buildRuleMergeCSV(ctx, "monolithic hidden API metadata", allMetadataFlagFiles, metadataCSV)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100384
Paul Duffind061d402021-06-07 21:36:01 +0100385 // Generate an intermediate monolithic hiddenapi-index.csv file directly from the CSV files in the
386 // classes jars.
387 intermediateIndexCSV := android.PathForModuleOut(ctx, "hiddenapi-monolithic", "index-from-classes.csv")
388 buildRuleToGenerateIndex(ctx, "intermediate hidden API index", classesJars, intermediateIndexCSV)
389
390 // Generate the monolithic hiddenapi-index.csv file.
391 //
392 // Use index files generated directly from the classes jars as well as index files provided
393 // by prebuilts.
394 allIndexFlagFiles := android.Paths{intermediateIndexCSV}
395 allIndexFlagFiles = append(allIndexFlagFiles, monolithicInfo.IndexPaths...)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100396 indexCSV := hiddenAPISingletonPaths(ctx).index
Paul Duffind061d402021-06-07 21:36:01 +0100397 b.buildRuleMergeCSV(ctx, "monolithic hidden API index", allIndexFlagFiles, indexCSV)
Paul Duffinc8ead412021-06-07 19:28:15 +0100398
399 return bootDexJarByModule
Paul Duffin74431d52021-04-21 14:10:42 +0100400}
401
Paul Duffin438eb572021-05-21 16:58:23 +0100402// createAndProvideMonolithicHiddenAPIInfo creates a MonolithicHiddenAPIInfo and provides it for
403// testing.
Paul Duffin89f570a2021-06-16 01:42:33 +0100404func (b *platformBootclasspathModule) createAndProvideMonolithicHiddenAPIInfo(ctx android.ModuleContext, classpathElements ClasspathElements) MonolithicHiddenAPIInfo {
Paul Duffin1352f7c2021-05-21 22:18:49 +0100405 // Create a temporary input structure in which to collate information provided directly by this
406 // module, either through properties or direct dependencies.
407 temporaryInput := newHiddenAPIFlagInput()
408
409 // Create paths to the flag files specified in the properties.
Paul Duffin9b61abb2022-07-27 16:16:54 +0000410 temporaryInput.extractFlagFilesFromProperties(ctx, &b.properties.HiddenAPIFlagFileProperties)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100411
412 // Create the monolithic info, by starting with the flag files specified on this and then merging
413 // in information from all the fragment dependencies of this.
Paul Duffin89f570a2021-06-16 01:42:33 +0100414 monolithicInfo := newMonolithicHiddenAPIInfo(ctx, temporaryInput.FlagFilesByCategory, classpathElements)
Paul Duffin438eb572021-05-21 16:58:23 +0100415
416 // Store the information for testing.
Colin Cross40213022023-12-13 15:19:49 -0800417 android.SetProvider(ctx, MonolithicHiddenAPIInfoProvider, monolithicInfo)
Paul Duffin438eb572021-05-21 16:58:23 +0100418 return monolithicInfo
419}
420
Paul Duffin537ea3d2021-05-14 10:38:00 +0100421func (b *platformBootclasspathModule) buildRuleMergeCSV(ctx android.ModuleContext, desc string, inputPaths android.Paths, outputPath android.WritablePath) {
Paul Duffin00b2bfd2021-04-12 17:24:36 +0100422 rule := android.NewRuleBuilder(pctx, ctx)
423 rule.Command().
424 BuiltTool("merge_csv").
425 Flag("--key_field signature").
Paul Duffin85dee5d2021-04-13 00:14:38 +0100426 FlagWithOutput("--output=", outputPath).
Paul Duffin537ea3d2021-05-14 10:38:00 +0100427 Inputs(inputPaths)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100428
Paul Duffin537ea3d2021-05-14 10:38:00 +0100429 rule.Build(desc, desc)
Paul Duffin85dee5d2021-04-13 00:14:38 +0100430}
Paul Duffinad19d382021-04-26 16:44:00 +0100431
Paul Duffin12d29b72021-04-29 13:50:01 +0100432// generateHiddenApiMakeVars generates make variables needed by hidden API related make rules, e.g.
433// veridex and run-appcompat.
434func (b *platformBootclasspathModule) generateHiddenApiMakeVars(ctx android.MakeVarsContext) {
435 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
436 return
437 }
438 // INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
439 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", b.hiddenAPIFlagsCSV.String())
440}