xref: /aosp_15_r20/build/soong/android/apex.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2018 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 android
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"fmt"
19*333d2b36SAndroid Build Coastguard Worker	"slices"
20*333d2b36SAndroid Build Coastguard Worker	"sort"
21*333d2b36SAndroid Build Coastguard Worker	"strconv"
22*333d2b36SAndroid Build Coastguard Worker	"strings"
23*333d2b36SAndroid Build Coastguard Worker	"sync"
24*333d2b36SAndroid Build Coastguard Worker
25*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint"
26*333d2b36SAndroid Build Coastguard Worker)
27*333d2b36SAndroid Build Coastguard Worker
28*333d2b36SAndroid Build Coastguard Workervar (
29*333d2b36SAndroid Build Coastguard Worker	// This is the sdk version when APEX was first introduced
30*333d2b36SAndroid Build Coastguard Worker	SdkVersion_Android10 = uncheckedFinalApiLevel(29)
31*333d2b36SAndroid Build Coastguard Worker)
32*333d2b36SAndroid Build Coastguard Worker
33*333d2b36SAndroid Build Coastguard Worker// ApexInfo describes the metadata about one or more apexBundles that an apex variant of a module is
34*333d2b36SAndroid Build Coastguard Worker// part of.  When an apex variant is created, the variant is associated with one apexBundle. But
35*333d2b36SAndroid Build Coastguard Worker// when multiple apex variants are merged for deduping (see mergeApexVariations), this holds the
36*333d2b36SAndroid Build Coastguard Worker// information about the apexBundles that are merged together.
37*333d2b36SAndroid Build Coastguard Worker// Accessible via `ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)`
38*333d2b36SAndroid Build Coastguard Workertype ApexInfo struct {
39*333d2b36SAndroid Build Coastguard Worker	// Name of the apex variation that this module (i.e. the apex variant of the module) is
40*333d2b36SAndroid Build Coastguard Worker	// mutated into, or "" for a platform (i.e. non-APEX) variant.
41*333d2b36SAndroid Build Coastguard Worker	//
42*333d2b36SAndroid Build Coastguard Worker	// Also note that a module can be included in multiple APEXes, in which case, the module is
43*333d2b36SAndroid Build Coastguard Worker	// mutated into one or more variants, each of which is for an APEX. The variants then can
44*333d2b36SAndroid Build Coastguard Worker	// later be deduped if they don't need to be compiled differently. This is an optimization
45*333d2b36SAndroid Build Coastguard Worker	// done in mergeApexVariations.
46*333d2b36SAndroid Build Coastguard Worker	ApexVariationName string
47*333d2b36SAndroid Build Coastguard Worker
48*333d2b36SAndroid Build Coastguard Worker	// ApiLevel that this module has to support at minimum.
49*333d2b36SAndroid Build Coastguard Worker	MinSdkVersion ApiLevel
50*333d2b36SAndroid Build Coastguard Worker
51*333d2b36SAndroid Build Coastguard Worker	// True if this module comes from an updatable apexBundle.
52*333d2b36SAndroid Build Coastguard Worker	Updatable bool
53*333d2b36SAndroid Build Coastguard Worker
54*333d2b36SAndroid Build Coastguard Worker	// True if this module can use private platform APIs. Only non-updatable APEX can set this
55*333d2b36SAndroid Build Coastguard Worker	// to true.
56*333d2b36SAndroid Build Coastguard Worker	UsePlatformApis bool
57*333d2b36SAndroid Build Coastguard Worker
58*333d2b36SAndroid Build Coastguard Worker	// List of Apex variant names that this module is associated with. This initially is the
59*333d2b36SAndroid Build Coastguard Worker	// same as the `ApexVariationName` field.  Then when multiple apex variants are merged in
60*333d2b36SAndroid Build Coastguard Worker	// mergeApexVariations, ApexInfo struct of the merged variant holds the list of apexBundles
61*333d2b36SAndroid Build Coastguard Worker	// that are merged together.
62*333d2b36SAndroid Build Coastguard Worker	InApexVariants []string
63*333d2b36SAndroid Build Coastguard Worker
64*333d2b36SAndroid Build Coastguard Worker	// True if this is for a prebuilt_apex.
65*333d2b36SAndroid Build Coastguard Worker	//
66*333d2b36SAndroid Build Coastguard Worker	// If true then this will customize the apex processing to make it suitable for handling
67*333d2b36SAndroid Build Coastguard Worker	// prebuilt_apex, e.g. it will prevent ApexInfos from being merged together.
68*333d2b36SAndroid Build Coastguard Worker	//
69*333d2b36SAndroid Build Coastguard Worker	// See Prebuilt.ApexInfoMutator for more information.
70*333d2b36SAndroid Build Coastguard Worker	ForPrebuiltApex bool
71*333d2b36SAndroid Build Coastguard Worker
72*333d2b36SAndroid Build Coastguard Worker	// Returns the name of the test apexes that this module is included in.
73*333d2b36SAndroid Build Coastguard Worker	TestApexes []string
74*333d2b36SAndroid Build Coastguard Worker
75*333d2b36SAndroid Build Coastguard Worker	// Returns the name of the overridden apex (com.android.foo)
76*333d2b36SAndroid Build Coastguard Worker	BaseApexName string
77*333d2b36SAndroid Build Coastguard Worker
78*333d2b36SAndroid Build Coastguard Worker	// Returns the value of `apex_available_name`
79*333d2b36SAndroid Build Coastguard Worker	ApexAvailableName string
80*333d2b36SAndroid Build Coastguard Worker}
81*333d2b36SAndroid Build Coastguard Worker
82*333d2b36SAndroid Build Coastguard Worker// AllApexInfo holds the ApexInfo of all apexes that include this module.
83*333d2b36SAndroid Build Coastguard Workertype AllApexInfo struct {
84*333d2b36SAndroid Build Coastguard Worker	ApexInfos []ApexInfo
85*333d2b36SAndroid Build Coastguard Worker}
86*333d2b36SAndroid Build Coastguard Worker
87*333d2b36SAndroid Build Coastguard Workervar ApexInfoProvider = blueprint.NewMutatorProvider[ApexInfo]("apex_mutate")
88*333d2b36SAndroid Build Coastguard Workervar AllApexInfoProvider = blueprint.NewMutatorProvider[*AllApexInfo]("apex_info")
89*333d2b36SAndroid Build Coastguard Worker
90*333d2b36SAndroid Build Coastguard Workerfunc (i ApexInfo) AddJSONData(d *map[string]interface{}) {
91*333d2b36SAndroid Build Coastguard Worker	(*d)["Apex"] = map[string]interface{}{
92*333d2b36SAndroid Build Coastguard Worker		"ApexVariationName": i.ApexVariationName,
93*333d2b36SAndroid Build Coastguard Worker		"MinSdkVersion":     i.MinSdkVersion,
94*333d2b36SAndroid Build Coastguard Worker		"InApexVariants":    i.InApexVariants,
95*333d2b36SAndroid Build Coastguard Worker		"ForPrebuiltApex":   i.ForPrebuiltApex,
96*333d2b36SAndroid Build Coastguard Worker	}
97*333d2b36SAndroid Build Coastguard Worker}
98*333d2b36SAndroid Build Coastguard Worker
99*333d2b36SAndroid Build Coastguard Worker// mergedName gives the name of the alias variation that will be used when multiple apex variations
100*333d2b36SAndroid Build Coastguard Worker// of a module can be deduped into one variation. For example, if libfoo is included in both apex.a
101*333d2b36SAndroid Build Coastguard Worker// and apex.b, and if the two APEXes have the same min_sdk_version (say 29), then libfoo doesn't
102*333d2b36SAndroid Build Coastguard Worker// have to be built twice, but only once. In that case, the two apex variations apex.a and apex.b
103*333d2b36SAndroid Build Coastguard Worker// are configured to have the same alias variation named apex29. Whether platform APIs is allowed
104*333d2b36SAndroid Build Coastguard Worker// or not also matters; if two APEXes don't have the same allowance, they get different names and
105*333d2b36SAndroid Build Coastguard Worker// thus wouldn't be merged.
106*333d2b36SAndroid Build Coastguard Workerfunc (i ApexInfo) mergedName() string {
107*333d2b36SAndroid Build Coastguard Worker	name := "apex" + strconv.Itoa(i.MinSdkVersion.FinalOrFutureInt())
108*333d2b36SAndroid Build Coastguard Worker	return name
109*333d2b36SAndroid Build Coastguard Worker}
110*333d2b36SAndroid Build Coastguard Worker
111*333d2b36SAndroid Build Coastguard Worker// IsForPlatform tells whether this module is for the platform or not. If false is returned, it
112*333d2b36SAndroid Build Coastguard Worker// means that this apex variant of the module is built for an APEX.
113*333d2b36SAndroid Build Coastguard Workerfunc (i ApexInfo) IsForPlatform() bool {
114*333d2b36SAndroid Build Coastguard Worker	return i.ApexVariationName == ""
115*333d2b36SAndroid Build Coastguard Worker}
116*333d2b36SAndroid Build Coastguard Worker
117*333d2b36SAndroid Build Coastguard Worker// InApexVariant tells whether this apex variant of the module is part of the given apexVariant or
118*333d2b36SAndroid Build Coastguard Worker// not.
119*333d2b36SAndroid Build Coastguard Workerfunc (i ApexInfo) InApexVariant(apexVariant string) bool {
120*333d2b36SAndroid Build Coastguard Worker	for _, a := range i.InApexVariants {
121*333d2b36SAndroid Build Coastguard Worker		if a == apexVariant {
122*333d2b36SAndroid Build Coastguard Worker			return true
123*333d2b36SAndroid Build Coastguard Worker		}
124*333d2b36SAndroid Build Coastguard Worker	}
125*333d2b36SAndroid Build Coastguard Worker	return false
126*333d2b36SAndroid Build Coastguard Worker}
127*333d2b36SAndroid Build Coastguard Worker
128*333d2b36SAndroid Build Coastguard Worker// To satisfy the comparable interface
129*333d2b36SAndroid Build Coastguard Workerfunc (i ApexInfo) Equal(other any) bool {
130*333d2b36SAndroid Build Coastguard Worker	otherApexInfo, ok := other.(ApexInfo)
131*333d2b36SAndroid Build Coastguard Worker	return ok && i.ApexVariationName == otherApexInfo.ApexVariationName &&
132*333d2b36SAndroid Build Coastguard Worker		i.MinSdkVersion == otherApexInfo.MinSdkVersion &&
133*333d2b36SAndroid Build Coastguard Worker		i.Updatable == otherApexInfo.Updatable &&
134*333d2b36SAndroid Build Coastguard Worker		i.UsePlatformApis == otherApexInfo.UsePlatformApis &&
135*333d2b36SAndroid Build Coastguard Worker		slices.Equal(i.InApexVariants, otherApexInfo.InApexVariants)
136*333d2b36SAndroid Build Coastguard Worker}
137*333d2b36SAndroid Build Coastguard Worker
138*333d2b36SAndroid Build Coastguard Worker// ApexBundleInfo contains information about the dependencies of an apex
139*333d2b36SAndroid Build Coastguard Workertype ApexBundleInfo struct {
140*333d2b36SAndroid Build Coastguard Worker}
141*333d2b36SAndroid Build Coastguard Worker
142*333d2b36SAndroid Build Coastguard Workervar ApexBundleInfoProvider = blueprint.NewMutatorProvider[ApexBundleInfo]("apex_info")
143*333d2b36SAndroid Build Coastguard Worker
144*333d2b36SAndroid Build Coastguard Worker// DepIsInSameApex defines an interface that should be used to determine whether a given dependency
145*333d2b36SAndroid Build Coastguard Worker// should be considered as part of the same APEX as the current module or not. Note: this was
146*333d2b36SAndroid Build Coastguard Worker// extracted from ApexModule to make it easier to define custom subsets of the ApexModule interface
147*333d2b36SAndroid Build Coastguard Worker// and improve code navigation within the IDE.
148*333d2b36SAndroid Build Coastguard Workertype DepIsInSameApex interface {
149*333d2b36SAndroid Build Coastguard Worker	// DepIsInSameApex tests if the other module 'dep' is considered as part of the same APEX as
150*333d2b36SAndroid Build Coastguard Worker	// this module. For example, a static lib dependency usually returns true here, while a
151*333d2b36SAndroid Build Coastguard Worker	// shared lib dependency to a stub library returns false.
152*333d2b36SAndroid Build Coastguard Worker	//
153*333d2b36SAndroid Build Coastguard Worker	// This method must not be called directly without first ignoring dependencies whose tags
154*333d2b36SAndroid Build Coastguard Worker	// implement ExcludeFromApexContentsTag. Calls from within the func passed to WalkPayloadDeps()
155*333d2b36SAndroid Build Coastguard Worker	// are fine as WalkPayloadDeps() will ignore those dependencies automatically. Otherwise, use
156*333d2b36SAndroid Build Coastguard Worker	// IsDepInSameApex instead.
157*333d2b36SAndroid Build Coastguard Worker	DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
158*333d2b36SAndroid Build Coastguard Worker}
159*333d2b36SAndroid Build Coastguard Worker
160*333d2b36SAndroid Build Coastguard Workerfunc IsDepInSameApex(ctx BaseModuleContext, module, dep Module) bool {
161*333d2b36SAndroid Build Coastguard Worker	depTag := ctx.OtherModuleDependencyTag(dep)
162*333d2b36SAndroid Build Coastguard Worker	if _, ok := depTag.(ExcludeFromApexContentsTag); ok {
163*333d2b36SAndroid Build Coastguard Worker		// The tag defines a dependency that never requires the child module to be part of the same
164*333d2b36SAndroid Build Coastguard Worker		// apex as the parent.
165*333d2b36SAndroid Build Coastguard Worker		return false
166*333d2b36SAndroid Build Coastguard Worker	}
167*333d2b36SAndroid Build Coastguard Worker	return module.(DepIsInSameApex).DepIsInSameApex(ctx, dep)
168*333d2b36SAndroid Build Coastguard Worker}
169*333d2b36SAndroid Build Coastguard Worker
170*333d2b36SAndroid Build Coastguard Worker// ApexModule is the interface that a module type is expected to implement if the module has to be
171*333d2b36SAndroid Build Coastguard Worker// built differently depending on whether the module is destined for an APEX or not (i.e., installed
172*333d2b36SAndroid Build Coastguard Worker// to one of the regular partitions).
173*333d2b36SAndroid Build Coastguard Worker//
174*333d2b36SAndroid Build Coastguard Worker// Native shared libraries are one such module type; when it is built for an APEX, it should depend
175*333d2b36SAndroid Build Coastguard Worker// only on stable interfaces such as NDK, stable AIDL, or C APIs from other APEXes.
176*333d2b36SAndroid Build Coastguard Worker//
177*333d2b36SAndroid Build Coastguard Worker// A module implementing this interface will be mutated into multiple variations by apex.apexMutator
178*333d2b36SAndroid Build Coastguard Worker// if it is directly or indirectly included in one or more APEXes. Specifically, if a module is
179*333d2b36SAndroid Build Coastguard Worker// included in apex.foo and apex.bar then three apex variants are created: platform, apex.foo and
180*333d2b36SAndroid Build Coastguard Worker// apex.bar. The platform variant is for the regular partitions (e.g., /system or /vendor, etc.)
181*333d2b36SAndroid Build Coastguard Worker// while the other two are for the APEXs, respectively. The latter two variations can be merged (see
182*333d2b36SAndroid Build Coastguard Worker// mergedName) when the two APEXes have the same min_sdk_version requirement.
183*333d2b36SAndroid Build Coastguard Workertype ApexModule interface {
184*333d2b36SAndroid Build Coastguard Worker	Module
185*333d2b36SAndroid Build Coastguard Worker	DepIsInSameApex
186*333d2b36SAndroid Build Coastguard Worker
187*333d2b36SAndroid Build Coastguard Worker	apexModuleBase() *ApexModuleBase
188*333d2b36SAndroid Build Coastguard Worker
189*333d2b36SAndroid Build Coastguard Worker	// Marks that this module should be built for the specified APEX. Call this BEFORE
190*333d2b36SAndroid Build Coastguard Worker	// apex.apexMutator is run.
191*333d2b36SAndroid Build Coastguard Worker	BuildForApex(apex ApexInfo)
192*333d2b36SAndroid Build Coastguard Worker
193*333d2b36SAndroid Build Coastguard Worker	// Returns true if this module is present in any APEX either directly or indirectly. Call
194*333d2b36SAndroid Build Coastguard Worker	// this after apex.apexMutator is run.
195*333d2b36SAndroid Build Coastguard Worker	InAnyApex() bool
196*333d2b36SAndroid Build Coastguard Worker
197*333d2b36SAndroid Build Coastguard Worker	// NotInPlatform returns true if the module is not available to the platform due to
198*333d2b36SAndroid Build Coastguard Worker	// apex_available being set and not containing "//apex_available:platform".
199*333d2b36SAndroid Build Coastguard Worker	NotInPlatform() bool
200*333d2b36SAndroid Build Coastguard Worker
201*333d2b36SAndroid Build Coastguard Worker	// Tests if this module could have APEX variants. Even when a module type implements
202*333d2b36SAndroid Build Coastguard Worker	// ApexModule interface, APEX variants are created only for the module instances that return
203*333d2b36SAndroid Build Coastguard Worker	// true here. This is useful for not creating APEX variants for certain types of shared
204*333d2b36SAndroid Build Coastguard Worker	// libraries such as NDK stubs.
205*333d2b36SAndroid Build Coastguard Worker	CanHaveApexVariants() bool
206*333d2b36SAndroid Build Coastguard Worker
207*333d2b36SAndroid Build Coastguard Worker	// Tests if this module can be installed to APEX as a file. For example, this would return
208*333d2b36SAndroid Build Coastguard Worker	// true for shared libs while return false for static libs because static libs are not
209*333d2b36SAndroid Build Coastguard Worker	// installable module (but it can still be mutated for APEX)
210*333d2b36SAndroid Build Coastguard Worker	IsInstallableToApex() bool
211*333d2b36SAndroid Build Coastguard Worker
212*333d2b36SAndroid Build Coastguard Worker	// Tests if this module is available for the specified APEX or ":platform". This is from the
213*333d2b36SAndroid Build Coastguard Worker	// apex_available property of the module.
214*333d2b36SAndroid Build Coastguard Worker	AvailableFor(what string) bool
215*333d2b36SAndroid Build Coastguard Worker
216*333d2b36SAndroid Build Coastguard Worker	// AlwaysRequiresPlatformApexVariant allows the implementing module to determine whether an
217*333d2b36SAndroid Build Coastguard Worker	// APEX mutator should always be created for it.
218*333d2b36SAndroid Build Coastguard Worker	//
219*333d2b36SAndroid Build Coastguard Worker	// Returns false by default.
220*333d2b36SAndroid Build Coastguard Worker	AlwaysRequiresPlatformApexVariant() bool
221*333d2b36SAndroid Build Coastguard Worker
222*333d2b36SAndroid Build Coastguard Worker	// Returns true if this module is not available to platform (i.e. apex_available property
223*333d2b36SAndroid Build Coastguard Worker	// doesn't have "//apex_available:platform"), or shouldn't be available to platform, which
224*333d2b36SAndroid Build Coastguard Worker	// is the case when this module depends on other module that isn't available to platform.
225*333d2b36SAndroid Build Coastguard Worker	NotAvailableForPlatform() bool
226*333d2b36SAndroid Build Coastguard Worker
227*333d2b36SAndroid Build Coastguard Worker	// Marks that this module is not available to platform. Set by the
228*333d2b36SAndroid Build Coastguard Worker	// check-platform-availability mutator in the apex package.
229*333d2b36SAndroid Build Coastguard Worker	SetNotAvailableForPlatform()
230*333d2b36SAndroid Build Coastguard Worker
231*333d2b36SAndroid Build Coastguard Worker	// Returns nil (success) if this module should support the given sdk version. Returns an
232*333d2b36SAndroid Build Coastguard Worker	// error if not. No default implementation is provided for this method. A module type
233*333d2b36SAndroid Build Coastguard Worker	// implementing this interface should provide an implementation. A module supports an sdk
234*333d2b36SAndroid Build Coastguard Worker	// version when the module's min_sdk_version is equal to or less than the given sdk version.
235*333d2b36SAndroid Build Coastguard Worker	ShouldSupportSdkVersion(ctx BaseModuleContext, sdkVersion ApiLevel) error
236*333d2b36SAndroid Build Coastguard Worker
237*333d2b36SAndroid Build Coastguard Worker	// Returns true if this module needs a unique variation per apex, effectively disabling the
238*333d2b36SAndroid Build Coastguard Worker	// deduping. This is turned on when, for example if use_apex_name_macro is set so that each
239*333d2b36SAndroid Build Coastguard Worker	// apex variant should be built with different macro definitions.
240*333d2b36SAndroid Build Coastguard Worker	UniqueApexVariations() bool
241*333d2b36SAndroid Build Coastguard Worker}
242*333d2b36SAndroid Build Coastguard Worker
243*333d2b36SAndroid Build Coastguard Worker// Properties that are common to all module types implementing ApexModule interface.
244*333d2b36SAndroid Build Coastguard Workertype ApexProperties struct {
245*333d2b36SAndroid Build Coastguard Worker	// Availability of this module in APEXes. Only the listed APEXes can contain this module. If
246*333d2b36SAndroid Build Coastguard Worker	// the module has stubs then other APEXes and the platform may access it through them
247*333d2b36SAndroid Build Coastguard Worker	// (subject to visibility).
248*333d2b36SAndroid Build Coastguard Worker	//
249*333d2b36SAndroid Build Coastguard Worker	// "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
250*333d2b36SAndroid Build Coastguard Worker	// "//apex_available:platform" refers to non-APEX partitions like "system.img".
251*333d2b36SAndroid Build Coastguard Worker	// Prefix pattern (com.foo.*) can be used to match with any APEX name with the prefix(com.foo.).
252*333d2b36SAndroid Build Coastguard Worker	// Default is ["//apex_available:platform"].
253*333d2b36SAndroid Build Coastguard Worker	Apex_available []string
254*333d2b36SAndroid Build Coastguard Worker
255*333d2b36SAndroid Build Coastguard Worker	// See ApexModule.NotAvailableForPlatform()
256*333d2b36SAndroid Build Coastguard Worker	NotAvailableForPlatform bool `blueprint:"mutated"`
257*333d2b36SAndroid Build Coastguard Worker
258*333d2b36SAndroid Build Coastguard Worker	// See ApexModule.UniqueApexVariants()
259*333d2b36SAndroid Build Coastguard Worker	UniqueApexVariationsForDeps bool `blueprint:"mutated"`
260*333d2b36SAndroid Build Coastguard Worker
261*333d2b36SAndroid Build Coastguard Worker	// The test apexes that includes this apex variant
262*333d2b36SAndroid Build Coastguard Worker	TestApexes []string `blueprint:"mutated"`
263*333d2b36SAndroid Build Coastguard Worker}
264*333d2b36SAndroid Build Coastguard Worker
265*333d2b36SAndroid Build Coastguard Worker// Marker interface that identifies dependencies that are excluded from APEX contents.
266*333d2b36SAndroid Build Coastguard Worker//
267*333d2b36SAndroid Build Coastguard Worker// Unless the tag also implements the AlwaysRequireApexVariantTag this will prevent an apex variant
268*333d2b36SAndroid Build Coastguard Worker// from being created for the module.
269*333d2b36SAndroid Build Coastguard Worker//
270*333d2b36SAndroid Build Coastguard Worker// At the moment the sdk.sdkRequirementsMutator relies on the fact that the existing tags which
271*333d2b36SAndroid Build Coastguard Worker// implement this interface do not define dependencies onto members of an sdk_snapshot. If that
272*333d2b36SAndroid Build Coastguard Worker// changes then sdk.sdkRequirementsMutator will need fixing.
273*333d2b36SAndroid Build Coastguard Workertype ExcludeFromApexContentsTag interface {
274*333d2b36SAndroid Build Coastguard Worker	blueprint.DependencyTag
275*333d2b36SAndroid Build Coastguard Worker
276*333d2b36SAndroid Build Coastguard Worker	// Method that differentiates this interface from others.
277*333d2b36SAndroid Build Coastguard Worker	ExcludeFromApexContents()
278*333d2b36SAndroid Build Coastguard Worker}
279*333d2b36SAndroid Build Coastguard Worker
280*333d2b36SAndroid Build Coastguard Worker// Marker interface that identifies dependencies that always requires an APEX variant to be created.
281*333d2b36SAndroid Build Coastguard Worker//
282*333d2b36SAndroid Build Coastguard Worker// It is possible for a dependency to require an apex variant but exclude the module from the APEX
283*333d2b36SAndroid Build Coastguard Worker// contents. See sdk.sdkMemberDependencyTag.
284*333d2b36SAndroid Build Coastguard Workertype AlwaysRequireApexVariantTag interface {
285*333d2b36SAndroid Build Coastguard Worker	blueprint.DependencyTag
286*333d2b36SAndroid Build Coastguard Worker
287*333d2b36SAndroid Build Coastguard Worker	// Return true if this tag requires that the target dependency has an apex variant.
288*333d2b36SAndroid Build Coastguard Worker	AlwaysRequireApexVariant() bool
289*333d2b36SAndroid Build Coastguard Worker}
290*333d2b36SAndroid Build Coastguard Worker
291*333d2b36SAndroid Build Coastguard Worker// Interface that identifies dependencies to skip Apex dependency check
292*333d2b36SAndroid Build Coastguard Workertype SkipApexAllowedDependenciesCheck interface {
293*333d2b36SAndroid Build Coastguard Worker	// Returns true to skip the Apex dependency check, which limits the allowed dependency in build.
294*333d2b36SAndroid Build Coastguard Worker	SkipApexAllowedDependenciesCheck() bool
295*333d2b36SAndroid Build Coastguard Worker}
296*333d2b36SAndroid Build Coastguard Worker
297*333d2b36SAndroid Build Coastguard Worker// ApexModuleBase provides the default implementation for the ApexModule interface. APEX-aware
298*333d2b36SAndroid Build Coastguard Worker// modules are expected to include this struct and call InitApexModule().
299*333d2b36SAndroid Build Coastguard Workertype ApexModuleBase struct {
300*333d2b36SAndroid Build Coastguard Worker	ApexProperties     ApexProperties
301*333d2b36SAndroid Build Coastguard Worker	apexPropertiesLock sync.Mutex // protects ApexProperties during parallel apexDirectlyInAnyMutator
302*333d2b36SAndroid Build Coastguard Worker
303*333d2b36SAndroid Build Coastguard Worker	canHaveApexVariants bool
304*333d2b36SAndroid Build Coastguard Worker
305*333d2b36SAndroid Build Coastguard Worker	apexInfos     []ApexInfo
306*333d2b36SAndroid Build Coastguard Worker	apexInfosLock sync.Mutex // protects apexInfos during parallel apexInfoMutator
307*333d2b36SAndroid Build Coastguard Worker}
308*333d2b36SAndroid Build Coastguard Worker
309*333d2b36SAndroid Build Coastguard Worker// Initializes ApexModuleBase struct. Not calling this (even when inheriting from ApexModuleBase)
310*333d2b36SAndroid Build Coastguard Worker// prevents the module from being mutated for apexBundle.
311*333d2b36SAndroid Build Coastguard Workerfunc InitApexModule(m ApexModule) {
312*333d2b36SAndroid Build Coastguard Worker	base := m.apexModuleBase()
313*333d2b36SAndroid Build Coastguard Worker	base.canHaveApexVariants = true
314*333d2b36SAndroid Build Coastguard Worker
315*333d2b36SAndroid Build Coastguard Worker	m.AddProperties(&base.ApexProperties)
316*333d2b36SAndroid Build Coastguard Worker}
317*333d2b36SAndroid Build Coastguard Worker
318*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
319*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
320*333d2b36SAndroid Build Coastguard Worker	return m
321*333d2b36SAndroid Build Coastguard Worker}
322*333d2b36SAndroid Build Coastguard Worker
323*333d2b36SAndroid Build Coastguard Workervar (
324*333d2b36SAndroid Build Coastguard Worker	availableToPlatformList = []string{AvailableToPlatform}
325*333d2b36SAndroid Build Coastguard Worker)
326*333d2b36SAndroid Build Coastguard Worker
327*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
328*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) ApexAvailable() []string {
329*333d2b36SAndroid Build Coastguard Worker	aa := m.ApexProperties.Apex_available
330*333d2b36SAndroid Build Coastguard Worker	if len(aa) > 0 {
331*333d2b36SAndroid Build Coastguard Worker		return aa
332*333d2b36SAndroid Build Coastguard Worker	}
333*333d2b36SAndroid Build Coastguard Worker	// Default is availability to platform
334*333d2b36SAndroid Build Coastguard Worker	return CopyOf(availableToPlatformList)
335*333d2b36SAndroid Build Coastguard Worker}
336*333d2b36SAndroid Build Coastguard Worker
337*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
338*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) BuildForApex(apex ApexInfo) {
339*333d2b36SAndroid Build Coastguard Worker	m.apexInfosLock.Lock()
340*333d2b36SAndroid Build Coastguard Worker	defer m.apexInfosLock.Unlock()
341*333d2b36SAndroid Build Coastguard Worker	if slices.ContainsFunc(m.apexInfos, func(existing ApexInfo) bool {
342*333d2b36SAndroid Build Coastguard Worker		return existing.ApexVariationName == apex.ApexVariationName
343*333d2b36SAndroid Build Coastguard Worker	}) {
344*333d2b36SAndroid Build Coastguard Worker		return
345*333d2b36SAndroid Build Coastguard Worker	}
346*333d2b36SAndroid Build Coastguard Worker	m.apexInfos = append(m.apexInfos, apex)
347*333d2b36SAndroid Build Coastguard Worker}
348*333d2b36SAndroid Build Coastguard Worker
349*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
350*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) InAnyApex() bool {
351*333d2b36SAndroid Build Coastguard Worker	for _, apex_name := range m.ApexProperties.Apex_available {
352*333d2b36SAndroid Build Coastguard Worker		if apex_name != AvailableToPlatform {
353*333d2b36SAndroid Build Coastguard Worker			return true
354*333d2b36SAndroid Build Coastguard Worker		}
355*333d2b36SAndroid Build Coastguard Worker	}
356*333d2b36SAndroid Build Coastguard Worker	return false
357*333d2b36SAndroid Build Coastguard Worker}
358*333d2b36SAndroid Build Coastguard Worker
359*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
360*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) NotInPlatform() bool {
361*333d2b36SAndroid Build Coastguard Worker	return !m.AvailableFor(AvailableToPlatform)
362*333d2b36SAndroid Build Coastguard Worker}
363*333d2b36SAndroid Build Coastguard Worker
364*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
365*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) CanHaveApexVariants() bool {
366*333d2b36SAndroid Build Coastguard Worker	return m.canHaveApexVariants
367*333d2b36SAndroid Build Coastguard Worker}
368*333d2b36SAndroid Build Coastguard Worker
369*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
370*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) IsInstallableToApex() bool {
371*333d2b36SAndroid Build Coastguard Worker	// If needed, this will bel overridden by concrete types inheriting
372*333d2b36SAndroid Build Coastguard Worker	// ApexModuleBase
373*333d2b36SAndroid Build Coastguard Worker	return false
374*333d2b36SAndroid Build Coastguard Worker}
375*333d2b36SAndroid Build Coastguard Worker
376*333d2b36SAndroid Build Coastguard Worker// Returns the test apexes that this module is included in.
377*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) TestApexes() []string {
378*333d2b36SAndroid Build Coastguard Worker	return m.ApexProperties.TestApexes
379*333d2b36SAndroid Build Coastguard Worker}
380*333d2b36SAndroid Build Coastguard Worker
381*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
382*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) UniqueApexVariations() bool {
383*333d2b36SAndroid Build Coastguard Worker	// If needed, this will bel overridden by concrete types inheriting
384*333d2b36SAndroid Build Coastguard Worker	// ApexModuleBase
385*333d2b36SAndroid Build Coastguard Worker	return false
386*333d2b36SAndroid Build Coastguard Worker}
387*333d2b36SAndroid Build Coastguard Worker
388*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
389*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
390*333d2b36SAndroid Build Coastguard Worker	// By default, if there is a dependency from A to B, we try to include both in the same
391*333d2b36SAndroid Build Coastguard Worker	// APEX, unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning
392*333d2b36SAndroid Build Coastguard Worker	// true. This is overridden by some module types like apex.ApexBundle, cc.Module,
393*333d2b36SAndroid Build Coastguard Worker	// java.Module, etc.
394*333d2b36SAndroid Build Coastguard Worker	return true
395*333d2b36SAndroid Build Coastguard Worker}
396*333d2b36SAndroid Build Coastguard Worker
397*333d2b36SAndroid Build Coastguard Workerconst (
398*333d2b36SAndroid Build Coastguard Worker	AvailableToPlatform = "//apex_available:platform"
399*333d2b36SAndroid Build Coastguard Worker	AvailableToAnyApex  = "//apex_available:anyapex"
400*333d2b36SAndroid Build Coastguard Worker)
401*333d2b36SAndroid Build Coastguard Worker
402*333d2b36SAndroid Build Coastguard Worker// CheckAvailableForApex provides the default algorithm for checking the apex availability. When the
403*333d2b36SAndroid Build Coastguard Worker// availability is empty, it defaults to ["//apex_available:platform"] which means "available to the
404*333d2b36SAndroid Build Coastguard Worker// platform but not available to any APEX". When the list is not empty, `what` is matched against
405*333d2b36SAndroid Build Coastguard Worker// the list. If there is any matching element in the list, thus function returns true. The special
406*333d2b36SAndroid Build Coastguard Worker// availability "//apex_available:anyapex" matches with anything except for
407*333d2b36SAndroid Build Coastguard Worker// "//apex_available:platform".
408*333d2b36SAndroid Build Coastguard Workerfunc CheckAvailableForApex(what string, apex_available []string) bool {
409*333d2b36SAndroid Build Coastguard Worker	if len(apex_available) == 0 {
410*333d2b36SAndroid Build Coastguard Worker		return what == AvailableToPlatform
411*333d2b36SAndroid Build Coastguard Worker	}
412*333d2b36SAndroid Build Coastguard Worker
413*333d2b36SAndroid Build Coastguard Worker	// TODO b/248601389
414*333d2b36SAndroid Build Coastguard Worker	if what == "com.google.mainline.primary.libs" || what == "com.google.mainline.go.primary.libs" {
415*333d2b36SAndroid Build Coastguard Worker		return true
416*333d2b36SAndroid Build Coastguard Worker	}
417*333d2b36SAndroid Build Coastguard Worker
418*333d2b36SAndroid Build Coastguard Worker	for _, apex_name := range apex_available {
419*333d2b36SAndroid Build Coastguard Worker		// exact match.
420*333d2b36SAndroid Build Coastguard Worker		if apex_name == what {
421*333d2b36SAndroid Build Coastguard Worker			return true
422*333d2b36SAndroid Build Coastguard Worker		}
423*333d2b36SAndroid Build Coastguard Worker		// //apex_available:anyapex matches with any apex name, but not //apex_available:platform
424*333d2b36SAndroid Build Coastguard Worker		if apex_name == AvailableToAnyApex && what != AvailableToPlatform {
425*333d2b36SAndroid Build Coastguard Worker			return true
426*333d2b36SAndroid Build Coastguard Worker		}
427*333d2b36SAndroid Build Coastguard Worker		// prefix match.
428*333d2b36SAndroid Build Coastguard Worker		if strings.HasSuffix(apex_name, ".*") && strings.HasPrefix(what, strings.TrimSuffix(apex_name, "*")) {
429*333d2b36SAndroid Build Coastguard Worker			return true
430*333d2b36SAndroid Build Coastguard Worker		}
431*333d2b36SAndroid Build Coastguard Worker	}
432*333d2b36SAndroid Build Coastguard Worker	return false
433*333d2b36SAndroid Build Coastguard Worker}
434*333d2b36SAndroid Build Coastguard Worker
435*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
436*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) AvailableFor(what string) bool {
437*333d2b36SAndroid Build Coastguard Worker	return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
438*333d2b36SAndroid Build Coastguard Worker}
439*333d2b36SAndroid Build Coastguard Worker
440*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
441*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) AlwaysRequiresPlatformApexVariant() bool {
442*333d2b36SAndroid Build Coastguard Worker	return false
443*333d2b36SAndroid Build Coastguard Worker}
444*333d2b36SAndroid Build Coastguard Worker
445*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
446*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) NotAvailableForPlatform() bool {
447*333d2b36SAndroid Build Coastguard Worker	return m.ApexProperties.NotAvailableForPlatform
448*333d2b36SAndroid Build Coastguard Worker}
449*333d2b36SAndroid Build Coastguard Worker
450*333d2b36SAndroid Build Coastguard Worker// Implements ApexModule
451*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) SetNotAvailableForPlatform() {
452*333d2b36SAndroid Build Coastguard Worker	m.ApexProperties.NotAvailableForPlatform = true
453*333d2b36SAndroid Build Coastguard Worker}
454*333d2b36SAndroid Build Coastguard Worker
455*333d2b36SAndroid Build Coastguard Worker// This function makes sure that the apex_available property is valid
456*333d2b36SAndroid Build Coastguard Workerfunc (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
457*333d2b36SAndroid Build Coastguard Worker	for _, n := range m.ApexProperties.Apex_available {
458*333d2b36SAndroid Build Coastguard Worker		if n == AvailableToPlatform || n == AvailableToAnyApex {
459*333d2b36SAndroid Build Coastguard Worker			continue
460*333d2b36SAndroid Build Coastguard Worker		}
461*333d2b36SAndroid Build Coastguard Worker		// Prefix pattern should end with .* and has at least two components.
462*333d2b36SAndroid Build Coastguard Worker		if strings.Contains(n, "*") {
463*333d2b36SAndroid Build Coastguard Worker			if !strings.HasSuffix(n, ".*") {
464*333d2b36SAndroid Build Coastguard Worker				mctx.PropertyErrorf("apex_available", "Wildcard should end with .* like com.foo.*")
465*333d2b36SAndroid Build Coastguard Worker			}
466*333d2b36SAndroid Build Coastguard Worker			if strings.Count(n, ".") < 2 {
467*333d2b36SAndroid Build Coastguard Worker				mctx.PropertyErrorf("apex_available", "Wildcard requires two or more components like com.foo.*")
468*333d2b36SAndroid Build Coastguard Worker			}
469*333d2b36SAndroid Build Coastguard Worker			if strings.Count(n, "*") != 1 {
470*333d2b36SAndroid Build Coastguard Worker				mctx.PropertyErrorf("apex_available", "Wildcard is not allowed in the middle.")
471*333d2b36SAndroid Build Coastguard Worker			}
472*333d2b36SAndroid Build Coastguard Worker			continue
473*333d2b36SAndroid Build Coastguard Worker		}
474*333d2b36SAndroid Build Coastguard Worker		if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
475*333d2b36SAndroid Build Coastguard Worker			mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
476*333d2b36SAndroid Build Coastguard Worker		}
477*333d2b36SAndroid Build Coastguard Worker	}
478*333d2b36SAndroid Build Coastguard Worker}
479*333d2b36SAndroid Build Coastguard Worker
480*333d2b36SAndroid Build Coastguard Worker// AvailableToSameApexes returns true if the two modules are apex_available to
481*333d2b36SAndroid Build Coastguard Worker// exactly the same set of APEXes (and platform), i.e. if their apex_available
482*333d2b36SAndroid Build Coastguard Worker// properties have the same elements.
483*333d2b36SAndroid Build Coastguard Workerfunc AvailableToSameApexes(mod1, mod2 ApexModule) bool {
484*333d2b36SAndroid Build Coastguard Worker	mod1ApexAvail := SortedUniqueStrings(mod1.apexModuleBase().ApexProperties.Apex_available)
485*333d2b36SAndroid Build Coastguard Worker	mod2ApexAvail := SortedUniqueStrings(mod2.apexModuleBase().ApexProperties.Apex_available)
486*333d2b36SAndroid Build Coastguard Worker	if len(mod1ApexAvail) != len(mod2ApexAvail) {
487*333d2b36SAndroid Build Coastguard Worker		return false
488*333d2b36SAndroid Build Coastguard Worker	}
489*333d2b36SAndroid Build Coastguard Worker	for i, v := range mod1ApexAvail {
490*333d2b36SAndroid Build Coastguard Worker		if v != mod2ApexAvail[i] {
491*333d2b36SAndroid Build Coastguard Worker			return false
492*333d2b36SAndroid Build Coastguard Worker		}
493*333d2b36SAndroid Build Coastguard Worker	}
494*333d2b36SAndroid Build Coastguard Worker	return true
495*333d2b36SAndroid Build Coastguard Worker}
496*333d2b36SAndroid Build Coastguard Worker
497*333d2b36SAndroid Build Coastguard Worker// mergeApexVariations deduplicates apex variations that would build identically into a common
498*333d2b36SAndroid Build Coastguard Worker// variation. It returns the reduced list of variations and a list of aliases from the original
499*333d2b36SAndroid Build Coastguard Worker// variation names to the new variation names.
500*333d2b36SAndroid Build Coastguard Workerfunc mergeApexVariations(apexInfos []ApexInfo) (merged []ApexInfo, aliases [][2]string) {
501*333d2b36SAndroid Build Coastguard Worker	seen := make(map[string]int)
502*333d2b36SAndroid Build Coastguard Worker	for _, apexInfo := range apexInfos {
503*333d2b36SAndroid Build Coastguard Worker		// If this is for a prebuilt apex then use the actual name of the apex variation to prevent this
504*333d2b36SAndroid Build Coastguard Worker		// from being merged with other ApexInfo. See Prebuilt.ApexInfoMutator for more information.
505*333d2b36SAndroid Build Coastguard Worker		if apexInfo.ForPrebuiltApex {
506*333d2b36SAndroid Build Coastguard Worker			merged = append(merged, apexInfo)
507*333d2b36SAndroid Build Coastguard Worker			continue
508*333d2b36SAndroid Build Coastguard Worker		}
509*333d2b36SAndroid Build Coastguard Worker
510*333d2b36SAndroid Build Coastguard Worker		// Merge the ApexInfo together. If a compatible ApexInfo exists then merge the information from
511*333d2b36SAndroid Build Coastguard Worker		// this one into it, otherwise create a new merged ApexInfo from this one and save it away so
512*333d2b36SAndroid Build Coastguard Worker		// other ApexInfo instances can be merged into it.
513*333d2b36SAndroid Build Coastguard Worker		variantName := apexInfo.ApexVariationName
514*333d2b36SAndroid Build Coastguard Worker		mergedName := apexInfo.mergedName()
515*333d2b36SAndroid Build Coastguard Worker		if index, exists := seen[mergedName]; exists {
516*333d2b36SAndroid Build Coastguard Worker			// Variants having the same mergedName are deduped
517*333d2b36SAndroid Build Coastguard Worker			merged[index].InApexVariants = append(merged[index].InApexVariants, variantName)
518*333d2b36SAndroid Build Coastguard Worker			merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable
519*333d2b36SAndroid Build Coastguard Worker			// Platform APIs is allowed for this module only when all APEXes containing
520*333d2b36SAndroid Build Coastguard Worker			// the module are with `use_platform_apis: true`.
521*333d2b36SAndroid Build Coastguard Worker			merged[index].UsePlatformApis = merged[index].UsePlatformApis && apexInfo.UsePlatformApis
522*333d2b36SAndroid Build Coastguard Worker			merged[index].TestApexes = append(merged[index].TestApexes, apexInfo.TestApexes...)
523*333d2b36SAndroid Build Coastguard Worker		} else {
524*333d2b36SAndroid Build Coastguard Worker			seen[mergedName] = len(merged)
525*333d2b36SAndroid Build Coastguard Worker			apexInfo.ApexVariationName = mergedName
526*333d2b36SAndroid Build Coastguard Worker			apexInfo.InApexVariants = CopyOf(apexInfo.InApexVariants)
527*333d2b36SAndroid Build Coastguard Worker			apexInfo.TestApexes = CopyOf(apexInfo.TestApexes)
528*333d2b36SAndroid Build Coastguard Worker			merged = append(merged, apexInfo)
529*333d2b36SAndroid Build Coastguard Worker		}
530*333d2b36SAndroid Build Coastguard Worker		aliases = append(aliases, [2]string{variantName, mergedName})
531*333d2b36SAndroid Build Coastguard Worker	}
532*333d2b36SAndroid Build Coastguard Worker	return merged, aliases
533*333d2b36SAndroid Build Coastguard Worker}
534*333d2b36SAndroid Build Coastguard Worker
535*333d2b36SAndroid Build Coastguard Worker// IncomingApexTransition is called by apexTransitionMutator.IncomingTransition on modules that can be in apexes.
536*333d2b36SAndroid Build Coastguard Worker// The incomingVariation can be either the name of an apex if the dependency is coming directly from an apex
537*333d2b36SAndroid Build Coastguard Worker// module, or it can be the name of an apex variation (e.g. apex10000) if it is coming from another module that
538*333d2b36SAndroid Build Coastguard Worker// is in the apex.
539*333d2b36SAndroid Build Coastguard Workerfunc IncomingApexTransition(ctx IncomingTransitionContext, incomingVariation string) string {
540*333d2b36SAndroid Build Coastguard Worker	module := ctx.Module().(ApexModule)
541*333d2b36SAndroid Build Coastguard Worker	base := module.apexModuleBase()
542*333d2b36SAndroid Build Coastguard Worker
543*333d2b36SAndroid Build Coastguard Worker	var apexInfos []ApexInfo
544*333d2b36SAndroid Build Coastguard Worker	if allApexInfos, ok := ModuleProvider(ctx, AllApexInfoProvider); ok {
545*333d2b36SAndroid Build Coastguard Worker		apexInfos = allApexInfos.ApexInfos
546*333d2b36SAndroid Build Coastguard Worker	}
547*333d2b36SAndroid Build Coastguard Worker
548*333d2b36SAndroid Build Coastguard Worker	// Dependencies from platform variations go to the platform variation.
549*333d2b36SAndroid Build Coastguard Worker	if incomingVariation == "" {
550*333d2b36SAndroid Build Coastguard Worker		return ""
551*333d2b36SAndroid Build Coastguard Worker	}
552*333d2b36SAndroid Build Coastguard Worker
553*333d2b36SAndroid Build Coastguard Worker	if len(apexInfos) == 0 {
554*333d2b36SAndroid Build Coastguard Worker		if ctx.IsAddingDependency() {
555*333d2b36SAndroid Build Coastguard Worker			// If this module has no apex variations we can't do any mapping on the incoming variation, just return it
556*333d2b36SAndroid Build Coastguard Worker			// and let the caller get a "missing variant" error.
557*333d2b36SAndroid Build Coastguard Worker			return incomingVariation
558*333d2b36SAndroid Build Coastguard Worker		} else {
559*333d2b36SAndroid Build Coastguard Worker			// If this module has no apex variations the use the platform variation.
560*333d2b36SAndroid Build Coastguard Worker			return ""
561*333d2b36SAndroid Build Coastguard Worker		}
562*333d2b36SAndroid Build Coastguard Worker	}
563*333d2b36SAndroid Build Coastguard Worker
564*333d2b36SAndroid Build Coastguard Worker	// Convert the list of apex infos into from the AllApexInfoProvider into the merged list
565*333d2b36SAndroid Build Coastguard Worker	// of apex variations and the aliases from apex names to apex variations.
566*333d2b36SAndroid Build Coastguard Worker	var aliases [][2]string
567*333d2b36SAndroid Build Coastguard Worker	if !module.UniqueApexVariations() && !base.ApexProperties.UniqueApexVariationsForDeps {
568*333d2b36SAndroid Build Coastguard Worker		apexInfos, aliases = mergeApexVariations(apexInfos)
569*333d2b36SAndroid Build Coastguard Worker	}
570*333d2b36SAndroid Build Coastguard Worker
571*333d2b36SAndroid Build Coastguard Worker	// Check if the incoming variation matches an apex name, and if so use the corresponding
572*333d2b36SAndroid Build Coastguard Worker	// apex variation.
573*333d2b36SAndroid Build Coastguard Worker	aliasIndex := slices.IndexFunc(aliases, func(alias [2]string) bool {
574*333d2b36SAndroid Build Coastguard Worker		return alias[0] == incomingVariation
575*333d2b36SAndroid Build Coastguard Worker	})
576*333d2b36SAndroid Build Coastguard Worker	if aliasIndex >= 0 {
577*333d2b36SAndroid Build Coastguard Worker		return aliases[aliasIndex][1]
578*333d2b36SAndroid Build Coastguard Worker	}
579*333d2b36SAndroid Build Coastguard Worker
580*333d2b36SAndroid Build Coastguard Worker	// Check if the incoming variation matches an apex variation.
581*333d2b36SAndroid Build Coastguard Worker	apexIndex := slices.IndexFunc(apexInfos, func(info ApexInfo) bool {
582*333d2b36SAndroid Build Coastguard Worker		return info.ApexVariationName == incomingVariation
583*333d2b36SAndroid Build Coastguard Worker	})
584*333d2b36SAndroid Build Coastguard Worker	if apexIndex >= 0 {
585*333d2b36SAndroid Build Coastguard Worker		return incomingVariation
586*333d2b36SAndroid Build Coastguard Worker	}
587*333d2b36SAndroid Build Coastguard Worker
588*333d2b36SAndroid Build Coastguard Worker	return ""
589*333d2b36SAndroid Build Coastguard Worker}
590*333d2b36SAndroid Build Coastguard Worker
591*333d2b36SAndroid Build Coastguard Workerfunc MutateApexTransition(ctx BaseModuleContext, variation string) {
592*333d2b36SAndroid Build Coastguard Worker	module := ctx.Module().(ApexModule)
593*333d2b36SAndroid Build Coastguard Worker	base := module.apexModuleBase()
594*333d2b36SAndroid Build Coastguard Worker	platformVariation := variation == ""
595*333d2b36SAndroid Build Coastguard Worker
596*333d2b36SAndroid Build Coastguard Worker	var apexInfos []ApexInfo
597*333d2b36SAndroid Build Coastguard Worker	if allApexInfos, ok := ModuleProvider(ctx, AllApexInfoProvider); ok {
598*333d2b36SAndroid Build Coastguard Worker		apexInfos = allApexInfos.ApexInfos
599*333d2b36SAndroid Build Coastguard Worker	}
600*333d2b36SAndroid Build Coastguard Worker
601*333d2b36SAndroid Build Coastguard Worker	// Shortcut
602*333d2b36SAndroid Build Coastguard Worker	if len(apexInfos) == 0 {
603*333d2b36SAndroid Build Coastguard Worker		return
604*333d2b36SAndroid Build Coastguard Worker	}
605*333d2b36SAndroid Build Coastguard Worker
606*333d2b36SAndroid Build Coastguard Worker	// Do some validity checks.
607*333d2b36SAndroid Build Coastguard Worker	// TODO(jiyong): is this the right place?
608*333d2b36SAndroid Build Coastguard Worker	base.checkApexAvailableProperty(ctx)
609*333d2b36SAndroid Build Coastguard Worker
610*333d2b36SAndroid Build Coastguard Worker	if !module.UniqueApexVariations() && !base.ApexProperties.UniqueApexVariationsForDeps {
611*333d2b36SAndroid Build Coastguard Worker		apexInfos, _ = mergeApexVariations(apexInfos)
612*333d2b36SAndroid Build Coastguard Worker	}
613*333d2b36SAndroid Build Coastguard Worker
614*333d2b36SAndroid Build Coastguard Worker	if platformVariation && !ctx.Host() && !module.AvailableFor(AvailableToPlatform) && module.NotAvailableForPlatform() {
615*333d2b36SAndroid Build Coastguard Worker		// Do not install the module for platform, but still allow it to output
616*333d2b36SAndroid Build Coastguard Worker		// uninstallable AndroidMk entries in certain cases when they have side
617*333d2b36SAndroid Build Coastguard Worker		// effects.  TODO(jiyong): move this routine to somewhere else
618*333d2b36SAndroid Build Coastguard Worker		module.MakeUninstallable()
619*333d2b36SAndroid Build Coastguard Worker	}
620*333d2b36SAndroid Build Coastguard Worker	if !platformVariation {
621*333d2b36SAndroid Build Coastguard Worker		var thisApexInfo ApexInfo
622*333d2b36SAndroid Build Coastguard Worker
623*333d2b36SAndroid Build Coastguard Worker		apexIndex := slices.IndexFunc(apexInfos, func(info ApexInfo) bool {
624*333d2b36SAndroid Build Coastguard Worker			return info.ApexVariationName == variation
625*333d2b36SAndroid Build Coastguard Worker		})
626*333d2b36SAndroid Build Coastguard Worker		if apexIndex >= 0 {
627*333d2b36SAndroid Build Coastguard Worker			thisApexInfo = apexInfos[apexIndex]
628*333d2b36SAndroid Build Coastguard Worker		} else {
629*333d2b36SAndroid Build Coastguard Worker			panic(fmt.Errorf("failed to find apexInfo for incoming variation %q", variation))
630*333d2b36SAndroid Build Coastguard Worker		}
631*333d2b36SAndroid Build Coastguard Worker
632*333d2b36SAndroid Build Coastguard Worker		SetProvider(ctx, ApexInfoProvider, thisApexInfo)
633*333d2b36SAndroid Build Coastguard Worker	}
634*333d2b36SAndroid Build Coastguard Worker
635*333d2b36SAndroid Build Coastguard Worker	// Set the value of TestApexes in every single apex variant.
636*333d2b36SAndroid Build Coastguard Worker	// This allows each apex variant to be aware of the test apexes in the user provided apex_available.
637*333d2b36SAndroid Build Coastguard Worker	var testApexes []string
638*333d2b36SAndroid Build Coastguard Worker	for _, a := range apexInfos {
639*333d2b36SAndroid Build Coastguard Worker		testApexes = append(testApexes, a.TestApexes...)
640*333d2b36SAndroid Build Coastguard Worker	}
641*333d2b36SAndroid Build Coastguard Worker	base.ApexProperties.TestApexes = testApexes
642*333d2b36SAndroid Build Coastguard Worker
643*333d2b36SAndroid Build Coastguard Worker}
644*333d2b36SAndroid Build Coastguard Worker
645*333d2b36SAndroid Build Coastguard Workerfunc ApexInfoMutator(ctx TopDownMutatorContext, module ApexModule) {
646*333d2b36SAndroid Build Coastguard Worker	base := module.apexModuleBase()
647*333d2b36SAndroid Build Coastguard Worker	if len(base.apexInfos) > 0 {
648*333d2b36SAndroid Build Coastguard Worker		apexInfos := slices.Clone(base.apexInfos)
649*333d2b36SAndroid Build Coastguard Worker		slices.SortFunc(apexInfos, func(a, b ApexInfo) int {
650*333d2b36SAndroid Build Coastguard Worker			return strings.Compare(a.ApexVariationName, b.ApexVariationName)
651*333d2b36SAndroid Build Coastguard Worker		})
652*333d2b36SAndroid Build Coastguard Worker		SetProvider(ctx, AllApexInfoProvider, &AllApexInfo{apexInfos})
653*333d2b36SAndroid Build Coastguard Worker		// base.apexInfos is only needed to propagate the list of apexes from the apex module to its
654*333d2b36SAndroid Build Coastguard Worker		// contents within apexInfoMutator. Clear it so it doesn't accidentally get used later.
655*333d2b36SAndroid Build Coastguard Worker		base.apexInfos = nil
656*333d2b36SAndroid Build Coastguard Worker	}
657*333d2b36SAndroid Build Coastguard Worker}
658*333d2b36SAndroid Build Coastguard Worker
659*333d2b36SAndroid Build Coastguard Worker// UpdateUniqueApexVariationsForDeps sets UniqueApexVariationsForDeps if any dependencies that are
660*333d2b36SAndroid Build Coastguard Worker// in the same APEX have unique APEX variations so that the module can link against the right
661*333d2b36SAndroid Build Coastguard Worker// variant.
662*333d2b36SAndroid Build Coastguard Workerfunc UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext, am ApexModule) {
663*333d2b36SAndroid Build Coastguard Worker	// anyInSameApex returns true if the two ApexInfo lists contain any values in an
664*333d2b36SAndroid Build Coastguard Worker	// InApexVariants list in common. It is used instead of DepIsInSameApex because it needs to
665*333d2b36SAndroid Build Coastguard Worker	// determine if the dep is in the same APEX due to being directly included, not only if it
666*333d2b36SAndroid Build Coastguard Worker	// is included _because_ it is a dependency.
667*333d2b36SAndroid Build Coastguard Worker	anyInSameApex := func(a, b ApexModule) bool {
668*333d2b36SAndroid Build Coastguard Worker		collectApexes := func(m ApexModule) []string {
669*333d2b36SAndroid Build Coastguard Worker			if allApexInfo, ok := OtherModuleProvider(mctx, m, AllApexInfoProvider); ok {
670*333d2b36SAndroid Build Coastguard Worker				var ret []string
671*333d2b36SAndroid Build Coastguard Worker				for _, info := range allApexInfo.ApexInfos {
672*333d2b36SAndroid Build Coastguard Worker					ret = append(ret, info.InApexVariants...)
673*333d2b36SAndroid Build Coastguard Worker				}
674*333d2b36SAndroid Build Coastguard Worker				return ret
675*333d2b36SAndroid Build Coastguard Worker			}
676*333d2b36SAndroid Build Coastguard Worker			return nil
677*333d2b36SAndroid Build Coastguard Worker		}
678*333d2b36SAndroid Build Coastguard Worker
679*333d2b36SAndroid Build Coastguard Worker		aApexes := collectApexes(a)
680*333d2b36SAndroid Build Coastguard Worker		bApexes := collectApexes(b)
681*333d2b36SAndroid Build Coastguard Worker		sort.Strings(bApexes)
682*333d2b36SAndroid Build Coastguard Worker		for _, aApex := range aApexes {
683*333d2b36SAndroid Build Coastguard Worker			index := sort.SearchStrings(bApexes, aApex)
684*333d2b36SAndroid Build Coastguard Worker			if index < len(bApexes) && bApexes[index] == aApex {
685*333d2b36SAndroid Build Coastguard Worker				return true
686*333d2b36SAndroid Build Coastguard Worker			}
687*333d2b36SAndroid Build Coastguard Worker		}
688*333d2b36SAndroid Build Coastguard Worker		return false
689*333d2b36SAndroid Build Coastguard Worker	}
690*333d2b36SAndroid Build Coastguard Worker
691*333d2b36SAndroid Build Coastguard Worker	// If any of the dependencies requires unique apex variations, so does this module.
692*333d2b36SAndroid Build Coastguard Worker	mctx.VisitDirectDeps(func(dep Module) {
693*333d2b36SAndroid Build Coastguard Worker		if depApexModule, ok := dep.(ApexModule); ok {
694*333d2b36SAndroid Build Coastguard Worker			if anyInSameApex(depApexModule, am) &&
695*333d2b36SAndroid Build Coastguard Worker				(depApexModule.UniqueApexVariations() ||
696*333d2b36SAndroid Build Coastguard Worker					depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) {
697*333d2b36SAndroid Build Coastguard Worker				am.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps = true
698*333d2b36SAndroid Build Coastguard Worker			}
699*333d2b36SAndroid Build Coastguard Worker		}
700*333d2b36SAndroid Build Coastguard Worker	})
701*333d2b36SAndroid Build Coastguard Worker}
702*333d2b36SAndroid Build Coastguard Worker
703*333d2b36SAndroid Build Coastguard Worker////////////////////////////////////////////////////////////////////////////////////////////////////
704*333d2b36SAndroid Build Coastguard Worker//Below are routines for extra safety checks.
705*333d2b36SAndroid Build Coastguard Worker//
706*333d2b36SAndroid Build Coastguard Worker// BuildDepsInfoLists is to flatten the dependency graph for an apexBundle into a text file
707*333d2b36SAndroid Build Coastguard Worker// (actually two in slightly different formats). The files are mostly for debugging, for example to
708*333d2b36SAndroid Build Coastguard Worker// see why a certain module is included in an APEX via which dependency path.
709*333d2b36SAndroid Build Coastguard Worker//
710*333d2b36SAndroid Build Coastguard Worker// CheckMinSdkVersion is to make sure that all modules in an apexBundle satisfy the min_sdk_version
711*333d2b36SAndroid Build Coastguard Worker// requirement of the apexBundle.
712*333d2b36SAndroid Build Coastguard Worker
713*333d2b36SAndroid Build Coastguard Worker// A dependency info for a single ApexModule, either direct or transitive.
714*333d2b36SAndroid Build Coastguard Workertype ApexModuleDepInfo struct {
715*333d2b36SAndroid Build Coastguard Worker	// Name of the dependency
716*333d2b36SAndroid Build Coastguard Worker	To string
717*333d2b36SAndroid Build Coastguard Worker	// List of dependencies To belongs to. Includes APEX itself, if a direct dependency.
718*333d2b36SAndroid Build Coastguard Worker	From []string
719*333d2b36SAndroid Build Coastguard Worker	// Whether the dependency belongs to the final compiled APEX.
720*333d2b36SAndroid Build Coastguard Worker	IsExternal bool
721*333d2b36SAndroid Build Coastguard Worker	// min_sdk_version of the ApexModule
722*333d2b36SAndroid Build Coastguard Worker	MinSdkVersion string
723*333d2b36SAndroid Build Coastguard Worker}
724*333d2b36SAndroid Build Coastguard Worker
725*333d2b36SAndroid Build Coastguard Worker// A map of a dependency name to its ApexModuleDepInfo
726*333d2b36SAndroid Build Coastguard Workertype DepNameToDepInfoMap map[string]ApexModuleDepInfo
727*333d2b36SAndroid Build Coastguard Worker
728*333d2b36SAndroid Build Coastguard Workertype ApexBundleDepsInfo struct {
729*333d2b36SAndroid Build Coastguard Worker	flatListPath Path
730*333d2b36SAndroid Build Coastguard Worker	fullListPath Path
731*333d2b36SAndroid Build Coastguard Worker}
732*333d2b36SAndroid Build Coastguard Worker
733*333d2b36SAndroid Build Coastguard Workertype ApexBundleDepsInfoIntf interface {
734*333d2b36SAndroid Build Coastguard Worker	Updatable() bool
735*333d2b36SAndroid Build Coastguard Worker	FlatListPath() Path
736*333d2b36SAndroid Build Coastguard Worker	FullListPath() Path
737*333d2b36SAndroid Build Coastguard Worker}
738*333d2b36SAndroid Build Coastguard Worker
739*333d2b36SAndroid Build Coastguard Workerfunc (d *ApexBundleDepsInfo) FlatListPath() Path {
740*333d2b36SAndroid Build Coastguard Worker	return d.flatListPath
741*333d2b36SAndroid Build Coastguard Worker}
742*333d2b36SAndroid Build Coastguard Worker
743*333d2b36SAndroid Build Coastguard Workerfunc (d *ApexBundleDepsInfo) FullListPath() Path {
744*333d2b36SAndroid Build Coastguard Worker	return d.fullListPath
745*333d2b36SAndroid Build Coastguard Worker}
746*333d2b36SAndroid Build Coastguard Worker
747*333d2b36SAndroid Build Coastguard Worker// Generate two module out files:
748*333d2b36SAndroid Build Coastguard Worker// 1. FullList with transitive deps and their parents in the dep graph
749*333d2b36SAndroid Build Coastguard Worker// 2. FlatList with a flat list of transitive deps
750*333d2b36SAndroid Build Coastguard Worker// In both cases transitive deps of external deps are not included. Neither are deps that are only
751*333d2b36SAndroid Build Coastguard Worker// available to APEXes; they are developed with updatability in mind and don't need manual approval.
752*333d2b36SAndroid Build Coastguard Workerfunc (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) {
753*333d2b36SAndroid Build Coastguard Worker	var fullContent strings.Builder
754*333d2b36SAndroid Build Coastguard Worker	var flatContent strings.Builder
755*333d2b36SAndroid Build Coastguard Worker
756*333d2b36SAndroid Build Coastguard Worker	fmt.Fprintf(&fullContent, "%s(minSdkVersion:%s):\n", ctx.ModuleName(), minSdkVersion)
757*333d2b36SAndroid Build Coastguard Worker	for _, key := range FirstUniqueStrings(SortedKeys(depInfos)) {
758*333d2b36SAndroid Build Coastguard Worker		info := depInfos[key]
759*333d2b36SAndroid Build Coastguard Worker		toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
760*333d2b36SAndroid Build Coastguard Worker		if info.IsExternal {
761*333d2b36SAndroid Build Coastguard Worker			toName = toName + " (external)"
762*333d2b36SAndroid Build Coastguard Worker		}
763*333d2b36SAndroid Build Coastguard Worker		fmt.Fprintf(&fullContent, "  %s <- %s\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
764*333d2b36SAndroid Build Coastguard Worker		fmt.Fprintf(&flatContent, "%s\n", toName)
765*333d2b36SAndroid Build Coastguard Worker	}
766*333d2b36SAndroid Build Coastguard Worker
767*333d2b36SAndroid Build Coastguard Worker	fullListPath := PathForModuleOut(ctx, "depsinfo", "fulllist.txt")
768*333d2b36SAndroid Build Coastguard Worker	WriteFileRule(ctx, fullListPath, fullContent.String())
769*333d2b36SAndroid Build Coastguard Worker	d.fullListPath = fullListPath
770*333d2b36SAndroid Build Coastguard Worker
771*333d2b36SAndroid Build Coastguard Worker	flatListPath := PathForModuleOut(ctx, "depsinfo", "flatlist.txt")
772*333d2b36SAndroid Build Coastguard Worker	WriteFileRule(ctx, flatListPath, flatContent.String())
773*333d2b36SAndroid Build Coastguard Worker	d.flatListPath = flatListPath
774*333d2b36SAndroid Build Coastguard Worker
775*333d2b36SAndroid Build Coastguard Worker	ctx.Phony(fmt.Sprintf("%s-depsinfo", ctx.ModuleName()), fullListPath, flatListPath)
776*333d2b36SAndroid Build Coastguard Worker}
777*333d2b36SAndroid Build Coastguard Worker
778*333d2b36SAndroid Build Coastguard Worker// Function called while walking an APEX's payload dependencies.
779*333d2b36SAndroid Build Coastguard Worker//
780*333d2b36SAndroid Build Coastguard Worker// Return true if the `to` module should be visited, false otherwise.
781*333d2b36SAndroid Build Coastguard Workertype PayloadDepsCallback func(ctx BaseModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool
782*333d2b36SAndroid Build Coastguard Workertype WalkPayloadDepsFunc func(ctx BaseModuleContext, do PayloadDepsCallback)
783*333d2b36SAndroid Build Coastguard Worker
784*333d2b36SAndroid Build Coastguard Worker// ModuleWithMinSdkVersionCheck represents a module that implements min_sdk_version checks
785*333d2b36SAndroid Build Coastguard Workertype ModuleWithMinSdkVersionCheck interface {
786*333d2b36SAndroid Build Coastguard Worker	Module
787*333d2b36SAndroid Build Coastguard Worker	MinSdkVersion(ctx EarlyModuleContext) ApiLevel
788*333d2b36SAndroid Build Coastguard Worker	CheckMinSdkVersion(ctx ModuleContext)
789*333d2b36SAndroid Build Coastguard Worker}
790*333d2b36SAndroid Build Coastguard Worker
791*333d2b36SAndroid Build Coastguard Worker// CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version
792*333d2b36SAndroid Build Coastguard Worker// accordingly
793*333d2b36SAndroid Build Coastguard Workerfunc CheckMinSdkVersion(ctx ModuleContext, minSdkVersion ApiLevel, walk WalkPayloadDepsFunc) {
794*333d2b36SAndroid Build Coastguard Worker	// do not enforce min_sdk_version for host
795*333d2b36SAndroid Build Coastguard Worker	if ctx.Host() {
796*333d2b36SAndroid Build Coastguard Worker		return
797*333d2b36SAndroid Build Coastguard Worker	}
798*333d2b36SAndroid Build Coastguard Worker
799*333d2b36SAndroid Build Coastguard Worker	// do not enforce for coverage build
800*333d2b36SAndroid Build Coastguard Worker	if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() {
801*333d2b36SAndroid Build Coastguard Worker		return
802*333d2b36SAndroid Build Coastguard Worker	}
803*333d2b36SAndroid Build Coastguard Worker
804*333d2b36SAndroid Build Coastguard Worker	// do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version
805*333d2b36SAndroid Build Coastguard Worker	if minSdkVersion.IsNone() {
806*333d2b36SAndroid Build Coastguard Worker		return
807*333d2b36SAndroid Build Coastguard Worker	}
808*333d2b36SAndroid Build Coastguard Worker
809*333d2b36SAndroid Build Coastguard Worker	walk(ctx, func(ctx BaseModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool {
810*333d2b36SAndroid Build Coastguard Worker		if externalDep {
811*333d2b36SAndroid Build Coastguard Worker			// external deps are outside the payload boundary, which is "stable"
812*333d2b36SAndroid Build Coastguard Worker			// interface. We don't have to check min_sdk_version for external
813*333d2b36SAndroid Build Coastguard Worker			// dependencies.
814*333d2b36SAndroid Build Coastguard Worker			return false
815*333d2b36SAndroid Build Coastguard Worker		}
816*333d2b36SAndroid Build Coastguard Worker		if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
817*333d2b36SAndroid Build Coastguard Worker			return false
818*333d2b36SAndroid Build Coastguard Worker		}
819*333d2b36SAndroid Build Coastguard Worker		if m, ok := to.(ModuleWithMinSdkVersionCheck); ok {
820*333d2b36SAndroid Build Coastguard Worker			// This dependency performs its own min_sdk_version check, just make sure it sets min_sdk_version
821*333d2b36SAndroid Build Coastguard Worker			// to trigger the check.
822*333d2b36SAndroid Build Coastguard Worker			if !m.MinSdkVersion(ctx).Specified() {
823*333d2b36SAndroid Build Coastguard Worker				ctx.OtherModuleErrorf(m, "must set min_sdk_version")
824*333d2b36SAndroid Build Coastguard Worker			}
825*333d2b36SAndroid Build Coastguard Worker			return false
826*333d2b36SAndroid Build Coastguard Worker		}
827*333d2b36SAndroid Build Coastguard Worker		if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil {
828*333d2b36SAndroid Build Coastguard Worker			toName := ctx.OtherModuleName(to)
829*333d2b36SAndroid Build Coastguard Worker			ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v."+
830*333d2b36SAndroid Build Coastguard Worker				"\n\nDependency path: %s\n\n"+
831*333d2b36SAndroid Build Coastguard Worker				"Consider adding 'min_sdk_version: %q' to %q",
832*333d2b36SAndroid Build Coastguard Worker				minSdkVersion, ctx.ModuleName(), err.Error(),
833*333d2b36SAndroid Build Coastguard Worker				ctx.GetPathString(false),
834*333d2b36SAndroid Build Coastguard Worker				minSdkVersion, toName)
835*333d2b36SAndroid Build Coastguard Worker			return false
836*333d2b36SAndroid Build Coastguard Worker		}
837*333d2b36SAndroid Build Coastguard Worker		return true
838*333d2b36SAndroid Build Coastguard Worker	})
839*333d2b36SAndroid Build Coastguard Worker}
840*333d2b36SAndroid Build Coastguard Worker
841*333d2b36SAndroid Build Coastguard Worker// Construct ApiLevel object from min_sdk_version string value
842*333d2b36SAndroid Build Coastguard Workerfunc MinSdkVersionFromValue(ctx EarlyModuleContext, value string) ApiLevel {
843*333d2b36SAndroid Build Coastguard Worker	if value == "" {
844*333d2b36SAndroid Build Coastguard Worker		return NoneApiLevel
845*333d2b36SAndroid Build Coastguard Worker	}
846*333d2b36SAndroid Build Coastguard Worker	apiLevel, err := ApiLevelFromUser(ctx, value)
847*333d2b36SAndroid Build Coastguard Worker	if err != nil {
848*333d2b36SAndroid Build Coastguard Worker		ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
849*333d2b36SAndroid Build Coastguard Worker		return NoneApiLevel
850*333d2b36SAndroid Build Coastguard Worker	}
851*333d2b36SAndroid Build Coastguard Worker	return apiLevel
852*333d2b36SAndroid Build Coastguard Worker}
853*333d2b36SAndroid Build Coastguard Worker
854*333d2b36SAndroid Build Coastguard Workervar ApexExportsInfoProvider = blueprint.NewProvider[ApexExportsInfo]()
855*333d2b36SAndroid Build Coastguard Worker
856*333d2b36SAndroid Build Coastguard Worker// ApexExportsInfo contains information about the artifacts provided by apexes to dexpreopt and hiddenapi
857*333d2b36SAndroid Build Coastguard Workertype ApexExportsInfo struct {
858*333d2b36SAndroid Build Coastguard Worker	// Canonical name of this APEX. Used to determine the path to the activated APEX on
859*333d2b36SAndroid Build Coastguard Worker	// device (/apex/<apex_name>)
860*333d2b36SAndroid Build Coastguard Worker	ApexName string
861*333d2b36SAndroid Build Coastguard Worker
862*333d2b36SAndroid Build Coastguard Worker	// Path to the image profile file on host (or empty, if profile is not generated).
863*333d2b36SAndroid Build Coastguard Worker	ProfilePathOnHost Path
864*333d2b36SAndroid Build Coastguard Worker
865*333d2b36SAndroid Build Coastguard Worker	// Map from the apex library name (without prebuilt_ prefix) to the dex file path on host
866*333d2b36SAndroid Build Coastguard Worker	LibraryNameToDexJarPathOnHost map[string]Path
867*333d2b36SAndroid Build Coastguard Worker}
868*333d2b36SAndroid Build Coastguard Worker
869*333d2b36SAndroid Build Coastguard Workervar PrebuiltInfoProvider = blueprint.NewProvider[PrebuiltInfo]()
870*333d2b36SAndroid Build Coastguard Worker
871*333d2b36SAndroid Build Coastguard Worker// contents of prebuilt_info.json
872*333d2b36SAndroid Build Coastguard Workertype PrebuiltInfo struct {
873*333d2b36SAndroid Build Coastguard Worker	// Name of the apex, without the prebuilt_ prefix
874*333d2b36SAndroid Build Coastguard Worker	Name string
875*333d2b36SAndroid Build Coastguard Worker
876*333d2b36SAndroid Build Coastguard Worker	Is_prebuilt bool
877*333d2b36SAndroid Build Coastguard Worker
878*333d2b36SAndroid Build Coastguard Worker	// This is relative to root of the workspace.
879*333d2b36SAndroid Build Coastguard Worker	// In case of mainline modules, this file contains the build_id that was used
880*333d2b36SAndroid Build Coastguard Worker	// to generate the mainline module prebuilt.
881*333d2b36SAndroid Build Coastguard Worker	Prebuilt_info_file_path string `json:",omitempty"`
882*333d2b36SAndroid Build Coastguard Worker}
883