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