xref: /aosp_15_r20/build/soong/java/bootclasspath.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2021 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage java
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
19*333d2b36SAndroid Build Coastguard Worker
20*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint"
21*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint/proptools"
22*333d2b36SAndroid Build Coastguard Worker)
23*333d2b36SAndroid Build Coastguard Worker
24*333d2b36SAndroid Build Coastguard Worker// Contains code that is common to both platform_bootclasspath and bootclasspath_fragment.
25*333d2b36SAndroid Build Coastguard Worker
26*333d2b36SAndroid Build Coastguard Workerfunc init() {
27*333d2b36SAndroid Build Coastguard Worker	registerBootclasspathBuildComponents(android.InitRegistrationContext)
28*333d2b36SAndroid Build Coastguard Worker}
29*333d2b36SAndroid Build Coastguard Worker
30*333d2b36SAndroid Build Coastguard Workerfunc registerBootclasspathBuildComponents(ctx android.RegistrationContext) {
31*333d2b36SAndroid Build Coastguard Worker	ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
32*333d2b36SAndroid Build Coastguard Worker		ctx.BottomUp("bootclasspath_deps", bootclasspathDepsMutator)
33*333d2b36SAndroid Build Coastguard Worker	})
34*333d2b36SAndroid Build Coastguard Worker}
35*333d2b36SAndroid Build Coastguard Worker
36*333d2b36SAndroid Build Coastguard Worker// BootclasspathDepsMutator is the interface that a module must implement if it wants to add
37*333d2b36SAndroid Build Coastguard Worker// dependencies onto APEX specific variants of bootclasspath fragments or bootclasspath contents.
38*333d2b36SAndroid Build Coastguard Workertype BootclasspathDepsMutator interface {
39*333d2b36SAndroid Build Coastguard Worker	// BootclasspathDepsMutator implementations should add dependencies using
40*333d2b36SAndroid Build Coastguard Worker	// addDependencyOntoApexModulePair and addDependencyOntoApexVariants.
41*333d2b36SAndroid Build Coastguard Worker	BootclasspathDepsMutator(ctx android.BottomUpMutatorContext)
42*333d2b36SAndroid Build Coastguard Worker}
43*333d2b36SAndroid Build Coastguard Worker
44*333d2b36SAndroid Build Coastguard Worker// bootclasspathDepsMutator is called during the final deps phase after all APEX variants have
45*333d2b36SAndroid Build Coastguard Worker// been created so can add dependencies onto specific APEX variants of modules.
46*333d2b36SAndroid Build Coastguard Workerfunc bootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
47*333d2b36SAndroid Build Coastguard Worker	m := ctx.Module()
48*333d2b36SAndroid Build Coastguard Worker	if p, ok := m.(BootclasspathDepsMutator); ok {
49*333d2b36SAndroid Build Coastguard Worker		p.BootclasspathDepsMutator(ctx)
50*333d2b36SAndroid Build Coastguard Worker	}
51*333d2b36SAndroid Build Coastguard Worker}
52*333d2b36SAndroid Build Coastguard Worker
53*333d2b36SAndroid Build Coastguard Worker// addDependencyOntoApexVariants adds dependencies onto the appropriate apex specific variants of
54*333d2b36SAndroid Build Coastguard Worker// the module as specified in the ApexVariantReference list.
55*333d2b36SAndroid Build Coastguard Workerfunc addDependencyOntoApexVariants(ctx android.BottomUpMutatorContext, propertyName string, refs []ApexVariantReference, tag blueprint.DependencyTag) {
56*333d2b36SAndroid Build Coastguard Worker	for i, ref := range refs {
57*333d2b36SAndroid Build Coastguard Worker		apex := proptools.StringDefault(ref.Apex, "platform")
58*333d2b36SAndroid Build Coastguard Worker
59*333d2b36SAndroid Build Coastguard Worker		if ref.Module == nil {
60*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf(propertyName, "missing module name at position %d", i)
61*333d2b36SAndroid Build Coastguard Worker			continue
62*333d2b36SAndroid Build Coastguard Worker		}
63*333d2b36SAndroid Build Coastguard Worker		name := proptools.String(ref.Module)
64*333d2b36SAndroid Build Coastguard Worker
65*333d2b36SAndroid Build Coastguard Worker		addDependencyOntoApexModulePair(ctx, apex, name, tag)
66*333d2b36SAndroid Build Coastguard Worker	}
67*333d2b36SAndroid Build Coastguard Worker}
68*333d2b36SAndroid Build Coastguard Worker
69*333d2b36SAndroid Build Coastguard Worker// addDependencyOntoApexModulePair adds a dependency onto the specified APEX specific variant or the
70*333d2b36SAndroid Build Coastguard Worker// specified module.
71*333d2b36SAndroid Build Coastguard Worker//
72*333d2b36SAndroid Build Coastguard Worker// If apex="platform" or "system_ext" then this adds a dependency onto the platform variant of the
73*333d2b36SAndroid Build Coastguard Worker// module. This adds dependencies onto the prebuilt and source modules with the specified name,
74*333d2b36SAndroid Build Coastguard Worker// depending on which ones are available. Visiting must use isActiveModule to select the preferred
75*333d2b36SAndroid Build Coastguard Worker// module when both source and prebuilt modules are available.
76*333d2b36SAndroid Build Coastguard Worker//
77*333d2b36SAndroid Build Coastguard Worker// Use gatherApexModulePairDepsWithTag to retrieve the dependencies.
78*333d2b36SAndroid Build Coastguard Workerfunc addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tag blueprint.DependencyTag) {
79*333d2b36SAndroid Build Coastguard Worker	var variations []blueprint.Variation
80*333d2b36SAndroid Build Coastguard Worker	if !android.IsConfiguredJarForPlatform(apex) {
81*333d2b36SAndroid Build Coastguard Worker		// Pick the correct apex variant.
82*333d2b36SAndroid Build Coastguard Worker		variations = []blueprint.Variation{
83*333d2b36SAndroid Build Coastguard Worker			{Mutator: "apex", Variation: apex},
84*333d2b36SAndroid Build Coastguard Worker		}
85*333d2b36SAndroid Build Coastguard Worker	}
86*333d2b36SAndroid Build Coastguard Worker
87*333d2b36SAndroid Build Coastguard Worker	target := ctx.Module().Target()
88*333d2b36SAndroid Build Coastguard Worker	variations = append(variations, target.Variations()...)
89*333d2b36SAndroid Build Coastguard Worker
90*333d2b36SAndroid Build Coastguard Worker	addedDep := false
91*333d2b36SAndroid Build Coastguard Worker	if ctx.OtherModuleDependencyVariantExists(variations, name) {
92*333d2b36SAndroid Build Coastguard Worker		ctx.AddFarVariationDependencies(variations, tag, name)
93*333d2b36SAndroid Build Coastguard Worker		addedDep = true
94*333d2b36SAndroid Build Coastguard Worker	}
95*333d2b36SAndroid Build Coastguard Worker
96*333d2b36SAndroid Build Coastguard Worker	// Add a dependency on the prebuilt module if it exists.
97*333d2b36SAndroid Build Coastguard Worker	prebuiltName := android.PrebuiltNameFromSource(name)
98*333d2b36SAndroid Build Coastguard Worker	if ctx.OtherModuleDependencyVariantExists(variations, prebuiltName) {
99*333d2b36SAndroid Build Coastguard Worker		ctx.AddVariationDependencies(variations, tag, prebuiltName)
100*333d2b36SAndroid Build Coastguard Worker		addedDep = true
101*333d2b36SAndroid Build Coastguard Worker	}
102*333d2b36SAndroid Build Coastguard Worker
103*333d2b36SAndroid Build Coastguard Worker	// If no appropriate variant existing for this, so no dependency could be added, then it is an
104*333d2b36SAndroid Build Coastguard Worker	// error, unless missing dependencies are allowed. The simplest way to handle that is to add a
105*333d2b36SAndroid Build Coastguard Worker	// dependency that will not be satisfied and the default behavior will handle it.
106*333d2b36SAndroid Build Coastguard Worker	if !addedDep {
107*333d2b36SAndroid Build Coastguard Worker		// Add dependency on the unprefixed (i.e. source or renamed prebuilt) module which we know does
108*333d2b36SAndroid Build Coastguard Worker		// not exist. The resulting error message will contain useful information about the available
109*333d2b36SAndroid Build Coastguard Worker		// variants.
110*333d2b36SAndroid Build Coastguard Worker		reportMissingVariationDependency(ctx, variations, name)
111*333d2b36SAndroid Build Coastguard Worker
112*333d2b36SAndroid Build Coastguard Worker		// Add dependency on the missing prefixed prebuilt variant too if a module with that name exists
113*333d2b36SAndroid Build Coastguard Worker		// so that information about its available variants will be reported too.
114*333d2b36SAndroid Build Coastguard Worker		if ctx.OtherModuleExists(prebuiltName) {
115*333d2b36SAndroid Build Coastguard Worker			reportMissingVariationDependency(ctx, variations, prebuiltName)
116*333d2b36SAndroid Build Coastguard Worker		}
117*333d2b36SAndroid Build Coastguard Worker	}
118*333d2b36SAndroid Build Coastguard Worker}
119*333d2b36SAndroid Build Coastguard Worker
120*333d2b36SAndroid Build Coastguard Worker// reportMissingVariationDependency intentionally adds a dependency on a missing variation in order
121*333d2b36SAndroid Build Coastguard Worker// to generate an appropriate error message with information about the available variations.
122*333d2b36SAndroid Build Coastguard Workerfunc reportMissingVariationDependency(ctx android.BottomUpMutatorContext, variations []blueprint.Variation, name string) {
123*333d2b36SAndroid Build Coastguard Worker	ctx.AddFarVariationDependencies(variations, nil, name)
124*333d2b36SAndroid Build Coastguard Worker}
125*333d2b36SAndroid Build Coastguard Worker
126*333d2b36SAndroid Build Coastguard Worker// gatherApexModulePairDepsWithTag returns the list of dependencies with the supplied tag that was
127*333d2b36SAndroid Build Coastguard Worker// added by addDependencyOntoApexModulePair.
128*333d2b36SAndroid Build Coastguard Workerfunc gatherApexModulePairDepsWithTag(ctx android.BaseModuleContext, tag blueprint.DependencyTag) []android.Module {
129*333d2b36SAndroid Build Coastguard Worker	var modules []android.Module
130*333d2b36SAndroid Build Coastguard Worker	isActiveModulePred := func(module android.Module) bool {
131*333d2b36SAndroid Build Coastguard Worker		return isActiveModule(ctx, module)
132*333d2b36SAndroid Build Coastguard Worker	}
133*333d2b36SAndroid Build Coastguard Worker	ctx.VisitDirectDepsIf(isActiveModulePred, func(module android.Module) {
134*333d2b36SAndroid Build Coastguard Worker		t := ctx.OtherModuleDependencyTag(module)
135*333d2b36SAndroid Build Coastguard Worker		if t == tag {
136*333d2b36SAndroid Build Coastguard Worker			modules = append(modules, module)
137*333d2b36SAndroid Build Coastguard Worker		}
138*333d2b36SAndroid Build Coastguard Worker	})
139*333d2b36SAndroid Build Coastguard Worker	return modules
140*333d2b36SAndroid Build Coastguard Worker}
141*333d2b36SAndroid Build Coastguard Worker
142*333d2b36SAndroid Build Coastguard Worker// ApexVariantReference specifies a particular apex variant of a module.
143*333d2b36SAndroid Build Coastguard Workertype ApexVariantReference struct {
144*333d2b36SAndroid Build Coastguard Worker	android.BpPrintableBase
145*333d2b36SAndroid Build Coastguard Worker
146*333d2b36SAndroid Build Coastguard Worker	// The name of the module apex variant, i.e. the apex containing the module variant.
147*333d2b36SAndroid Build Coastguard Worker	//
148*333d2b36SAndroid Build Coastguard Worker	// If this is not specified then it defaults to "platform" which will cause a dependency to be
149*333d2b36SAndroid Build Coastguard Worker	// added to the module's platform variant.
150*333d2b36SAndroid Build Coastguard Worker	//
151*333d2b36SAndroid Build Coastguard Worker	// A value of system_ext should be used for any module that will be part of the system_ext
152*333d2b36SAndroid Build Coastguard Worker	// partition.
153*333d2b36SAndroid Build Coastguard Worker	Apex *string
154*333d2b36SAndroid Build Coastguard Worker
155*333d2b36SAndroid Build Coastguard Worker	// The name of the module.
156*333d2b36SAndroid Build Coastguard Worker	Module *string
157*333d2b36SAndroid Build Coastguard Worker}
158*333d2b36SAndroid Build Coastguard Worker
159*333d2b36SAndroid Build Coastguard Worker// BootclasspathFragmentsDepsProperties contains properties related to dependencies onto fragments.
160*333d2b36SAndroid Build Coastguard Workertype BootclasspathFragmentsDepsProperties struct {
161*333d2b36SAndroid Build Coastguard Worker	// The names of the bootclasspath_fragment modules that form part of this module.
162*333d2b36SAndroid Build Coastguard Worker	Fragments []ApexVariantReference
163*333d2b36SAndroid Build Coastguard Worker}
164*333d2b36SAndroid Build Coastguard Worker
165*333d2b36SAndroid Build Coastguard Worker// addDependenciesOntoFragments adds dependencies to the fragments specified in this properties
166*333d2b36SAndroid Build Coastguard Worker// structure.
167*333d2b36SAndroid Build Coastguard Workerfunc (p *BootclasspathFragmentsDepsProperties) addDependenciesOntoFragments(ctx android.BottomUpMutatorContext) {
168*333d2b36SAndroid Build Coastguard Worker	addDependencyOntoApexVariants(ctx, "fragments", p.Fragments, bootclasspathFragmentDepTag)
169*333d2b36SAndroid Build Coastguard Worker}
170*333d2b36SAndroid Build Coastguard Worker
171*333d2b36SAndroid Build Coastguard Worker// bootclasspathDependencyTag defines dependencies from/to bootclasspath_fragment,
172*333d2b36SAndroid Build Coastguard Worker// prebuilt_bootclasspath_fragment and platform_bootclasspath onto either source or prebuilt
173*333d2b36SAndroid Build Coastguard Worker// modules.
174*333d2b36SAndroid Build Coastguard Workertype bootclasspathDependencyTag struct {
175*333d2b36SAndroid Build Coastguard Worker	blueprint.BaseDependencyTag
176*333d2b36SAndroid Build Coastguard Worker
177*333d2b36SAndroid Build Coastguard Worker	name string
178*333d2b36SAndroid Build Coastguard Worker}
179*333d2b36SAndroid Build Coastguard Worker
180*333d2b36SAndroid Build Coastguard Workerfunc (t bootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() {
181*333d2b36SAndroid Build Coastguard Worker}
182*333d2b36SAndroid Build Coastguard Worker
183*333d2b36SAndroid Build Coastguard Worker// Dependencies that use the bootclasspathDependencyTag instances are only added after all the
184*333d2b36SAndroid Build Coastguard Worker// visibility checking has been done so this has no functional effect. However, it does make it
185*333d2b36SAndroid Build Coastguard Worker// clear that visibility is not being enforced on these tags.
186*333d2b36SAndroid Build Coastguard Workervar _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathDependencyTag{}
187*333d2b36SAndroid Build Coastguard Worker
188*333d2b36SAndroid Build Coastguard Worker// The tag used for dependencies onto bootclasspath_fragments.
189*333d2b36SAndroid Build Coastguard Workervar bootclasspathFragmentDepTag = bootclasspathDependencyTag{name: "fragment"}
190*333d2b36SAndroid Build Coastguard Worker
191*333d2b36SAndroid Build Coastguard Worker// The tag used for dependencies onto platform_bootclasspath.
192*333d2b36SAndroid Build Coastguard Workervar platformBootclasspathDepTag = bootclasspathDependencyTag{name: "platform"}
193*333d2b36SAndroid Build Coastguard Worker
194*333d2b36SAndroid Build Coastguard Worker// BootclasspathNestedAPIProperties defines properties related to the API provided by parts of the
195*333d2b36SAndroid Build Coastguard Worker// bootclasspath that are nested within the main BootclasspathAPIProperties.
196*333d2b36SAndroid Build Coastguard Workertype BootclasspathNestedAPIProperties struct {
197*333d2b36SAndroid Build Coastguard Worker	// java_library or preferably, java_sdk_library modules providing stub classes that define the
198*333d2b36SAndroid Build Coastguard Worker	// APIs provided by this bootclasspath_fragment.
199*333d2b36SAndroid Build Coastguard Worker	Stub_libs proptools.Configurable[[]string]
200*333d2b36SAndroid Build Coastguard Worker}
201*333d2b36SAndroid Build Coastguard Worker
202*333d2b36SAndroid Build Coastguard Worker// BootclasspathAPIProperties defines properties for defining the API provided by parts of the
203*333d2b36SAndroid Build Coastguard Worker// bootclasspath.
204*333d2b36SAndroid Build Coastguard Workertype BootclasspathAPIProperties struct {
205*333d2b36SAndroid Build Coastguard Worker	// Api properties provide information about the APIs provided by the bootclasspath_fragment.
206*333d2b36SAndroid Build Coastguard Worker	// Properties in this section apply to public, system and test api scopes. They DO NOT apply to
207*333d2b36SAndroid Build Coastguard Worker	// core_platform as that is a special, ART specific scope, that does not follow the pattern and so
208*333d2b36SAndroid Build Coastguard Worker	// has its own section. It is in the process of being deprecated and replaced by the system scope
209*333d2b36SAndroid Build Coastguard Worker	// but this will remain for the foreseeable future to maintain backwards compatibility.
210*333d2b36SAndroid Build Coastguard Worker	//
211*333d2b36SAndroid Build Coastguard Worker	// Every bootclasspath_fragment must specify at least one stubs_lib in this section and must
212*333d2b36SAndroid Build Coastguard Worker	// specify stubs for all the APIs provided by its contents. Failure to do so will lead to those
213*333d2b36SAndroid Build Coastguard Worker	// methods being inaccessible to other parts of Android, including but not limited to
214*333d2b36SAndroid Build Coastguard Worker	// applications.
215*333d2b36SAndroid Build Coastguard Worker	Api BootclasspathNestedAPIProperties
216*333d2b36SAndroid Build Coastguard Worker
217*333d2b36SAndroid Build Coastguard Worker	// Properties related to the core platform API surface.
218*333d2b36SAndroid Build Coastguard Worker	//
219*333d2b36SAndroid Build Coastguard Worker	// This must only be used by the following modules:
220*333d2b36SAndroid Build Coastguard Worker	// * ART
221*333d2b36SAndroid Build Coastguard Worker	// * Conscrypt
222*333d2b36SAndroid Build Coastguard Worker	// * I18N
223*333d2b36SAndroid Build Coastguard Worker	//
224*333d2b36SAndroid Build Coastguard Worker	// The bootclasspath_fragments for each of the above modules must specify at least one stubs_lib
225*333d2b36SAndroid Build Coastguard Worker	// and must specify stubs for all the APIs provided by its contents. Failure to do so will lead to
226*333d2b36SAndroid Build Coastguard Worker	// those methods being inaccessible to the other modules in the list.
227*333d2b36SAndroid Build Coastguard Worker	Core_platform_api BootclasspathNestedAPIProperties
228*333d2b36SAndroid Build Coastguard Worker}
229*333d2b36SAndroid Build Coastguard Worker
230*333d2b36SAndroid Build Coastguard Worker// apiScopeToStubLibs calculates the stub library modules for each relevant *HiddenAPIScope from the
231*333d2b36SAndroid Build Coastguard Worker// Stub_libs properties.
232*333d2b36SAndroid Build Coastguard Workerfunc (p BootclasspathAPIProperties) apiScopeToStubLibs(ctx android.BaseModuleContext) map[*HiddenAPIScope][]string {
233*333d2b36SAndroid Build Coastguard Worker	m := map[*HiddenAPIScope][]string{}
234*333d2b36SAndroid Build Coastguard Worker	for _, apiScope := range hiddenAPISdkLibrarySupportedScopes {
235*333d2b36SAndroid Build Coastguard Worker		m[apiScope] = p.Api.Stub_libs.GetOrDefault(ctx, nil)
236*333d2b36SAndroid Build Coastguard Worker	}
237*333d2b36SAndroid Build Coastguard Worker	m[CorePlatformHiddenAPIScope] = p.Core_platform_api.Stub_libs.GetOrDefault(ctx, nil)
238*333d2b36SAndroid Build Coastguard Worker	return m
239*333d2b36SAndroid Build Coastguard Worker}
240