1// Copyright 2018 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 android
16
17import (
18	"fmt"
19	"sort"
20	"strconv"
21	"strings"
22	"sync"
23
24	"github.com/google/blueprint"
25)
26
27var (
28	// This is the sdk version when APEX was first introduced
29	SdkVersion_Android10 = uncheckedFinalApiLevel(29)
30)
31
32// ApexInfo describes the metadata about one or more apexBundles that an apex variant of a module is
33// part of.  When an apex variant is created, the variant is associated with one apexBundle. But
34// when multiple apex variants are merged for deduping (see mergeApexVariations), this holds the
35// information about the apexBundles that are merged together.
36// Accessible via `ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)`
37type ApexInfo struct {
38	// Name of the apex variation that this module (i.e. the apex variant of the module) is
39	// mutated into, or "" for a platform (i.e. non-APEX) variant. Note that this name and the
40	// Soong module name of the APEX can be different. That happens when there is
41	// `override_apex` that overrides `apex`. In that case, both Soong modules have the same
42	// apex variation name which usually is `com.android.foo`. This name is also the `name`
43	// in the path `/apex/<name>` where this apex is activated on at runtime.
44	//
45	// Also note that a module can be included in multiple APEXes, in which case, the module is
46	// mutated into one or more variants, each of which is for an APEX. The variants then can
47	// later be deduped if they don't need to be compiled differently. This is an optimization
48	// done in mergeApexVariations.
49	ApexVariationName string
50
51	// ApiLevel that this module has to support at minimum.
52	MinSdkVersion ApiLevel
53
54	// True if this module comes from an updatable apexBundle.
55	Updatable bool
56
57	// The list of SDK modules that the containing apexBundle depends on.
58	RequiredSdks SdkRefs
59
60	// List of Apex variant names that this module is associated with. This initially is the
61	// same as the `ApexVariationName` field.  Then when multiple apex variants are merged in
62	// mergeApexVariations, ApexInfo struct of the merged variant holds the list of apexBundles
63	// that are merged together.
64	InApexVariants []string
65
66	// List of APEX Soong module names that this module is part of. Note that the list includes
67	// different variations of the same APEX. For example, if module `foo` is included in the
68	// apex `com.android.foo`, and also if there is an override_apex module
69	// `com.mycompany.android.foo` overriding `com.android.foo`, then this list contains both
70	// `com.android.foo` and `com.mycompany.android.foo`.  If the APEX Soong module is a
71	// prebuilt, the name here doesn't have the `prebuilt_` prefix.
72	InApexModules []string
73
74	// Pointers to the ApexContents struct each of which is for apexBundle modules that this
75	// module is part of. The ApexContents gives information about which modules the apexBundle
76	// has and whether a module became part of the apexBundle via a direct dependency or not.
77	ApexContents []*ApexContents
78
79	// True if this is for a prebuilt_apex.
80	//
81	// If true then this will customize the apex processing to make it suitable for handling
82	// prebuilt_apex, e.g. it will prevent ApexInfos from being merged together.
83	//
84	// See Prebuilt.ApexInfoMutator for more information.
85	ForPrebuiltApex bool
86}
87
88var ApexInfoProvider = blueprint.NewMutatorProvider(ApexInfo{}, "apex")
89
90// mergedName gives the name of the alias variation that will be used when multiple apex variations
91// of a module can be deduped into one variation. For example, if libfoo is included in both apex.a
92// and apex.b, and if the two APEXes have the same min_sdk_version (say 29), then libfoo doesn't
93// have to be built twice, but only once. In that case, the two apex variations apex.a and apex.b
94// are configured to have the same alias variation named apex29.
95func (i ApexInfo) mergedName(ctx PathContext) string {
96	name := "apex" + strconv.Itoa(i.MinSdkVersion.FinalOrFutureInt())
97	for _, sdk := range i.RequiredSdks {
98		name += "_" + sdk.Name + "_" + sdk.Version
99	}
100	return name
101}
102
103// IsForPlatform tells whether this module is for the platform or not. If false is returned, it
104// means that this apex variant of the module is built for an APEX.
105func (i ApexInfo) IsForPlatform() bool {
106	return i.ApexVariationName == ""
107}
108
109// InApexVariant tells whether this apex variant of the module is part of the given apexVariant or
110// not.
111func (i ApexInfo) InApexVariant(apexVariant string) bool {
112	for _, a := range i.InApexVariants {
113		if a == apexVariant {
114			return true
115		}
116	}
117	return false
118}
119
120func (i ApexInfo) InApexModule(apexModuleName string) bool {
121	for _, a := range i.InApexModules {
122		if a == apexModuleName {
123			return true
124		}
125	}
126	return false
127}
128
129// ApexTestForInfo stores the contents of APEXes for which this module is a test - although this
130// module is not part of the APEX - and thus has access to APEX internals.
131type ApexTestForInfo struct {
132	ApexContents []*ApexContents
133}
134
135var ApexTestForInfoProvider = blueprint.NewMutatorProvider(ApexTestForInfo{}, "apex_test_for")
136
137// DepIsInSameApex defines an interface that should be used to determine whether a given dependency
138// should be considered as part of the same APEX as the current module or not. Note: this was
139// extracted from ApexModule to make it easier to define custom subsets of the ApexModule interface
140// and improve code navigation within the IDE.
141type DepIsInSameApex interface {
142	// DepIsInSameApex tests if the other module 'dep' is considered as part of the same APEX as
143	// this module. For example, a static lib dependency usually returns true here, while a
144	// shared lib dependency to a stub library returns false.
145	//
146	// This method must not be called directly without first ignoring dependencies whose tags
147	// implement ExcludeFromApexContentsTag. Calls from within the func passed to WalkPayloadDeps()
148	// are fine as WalkPayloadDeps() will ignore those dependencies automatically. Otherwise, use
149	// IsDepInSameApex instead.
150	DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
151}
152
153func IsDepInSameApex(ctx BaseModuleContext, module, dep Module) bool {
154	depTag := ctx.OtherModuleDependencyTag(dep)
155	if _, ok := depTag.(ExcludeFromApexContentsTag); ok {
156		// The tag defines a dependency that never requires the child module to be part of the same
157		// apex as the parent.
158		return false
159	}
160	return module.(DepIsInSameApex).DepIsInSameApex(ctx, dep)
161}
162
163// ApexModule is the interface that a module type is expected to implement if the module has to be
164// built differently depending on whether the module is destined for an APEX or not (i.e., installed
165// to one of the regular partitions).
166//
167// Native shared libraries are one such module type; when it is built for an APEX, it should depend
168// only on stable interfaces such as NDK, stable AIDL, or C APIs from other APEXes.
169//
170// A module implementing this interface will be mutated into multiple variations by apex.apexMutator
171// if it is directly or indirectly included in one or more APEXes. Specifically, if a module is
172// included in apex.foo and apex.bar then three apex variants are created: platform, apex.foo and
173// apex.bar. The platform variant is for the regular partitions (e.g., /system or /vendor, etc.)
174// while the other two are for the APEXs, respectively. The latter two variations can be merged (see
175// mergedName) when the two APEXes have the same min_sdk_version requirement.
176type ApexModule interface {
177	Module
178	DepIsInSameApex
179
180	apexModuleBase() *ApexModuleBase
181
182	// Marks that this module should be built for the specified APEX. Call this BEFORE
183	// apex.apexMutator is run.
184	BuildForApex(apex ApexInfo)
185
186	// Returns true if this module is present in any APEX either directly or indirectly. Call
187	// this after apex.apexMutator is run.
188	InAnyApex() bool
189
190	// Returns true if this module is directly in any APEX. Call this AFTER apex.apexMutator is
191	// run.
192	DirectlyInAnyApex() bool
193
194	// NotInPlatform tells whether or not this module is included in an APEX and therefore
195	// shouldn't be exposed to the platform (i.e. outside of the APEX) directly. A module is
196	// considered to be included in an APEX either when there actually is an APEX that
197	// explicitly has the module as its dependency or the module is not available to the
198	// platform, which indicates that the module belongs to at least one or more other APEXes.
199	NotInPlatform() bool
200
201	// Tests if this module could have APEX variants. Even when a module type implements
202	// ApexModule interface, APEX variants are created only for the module instances that return
203	// true here. This is useful for not creating APEX variants for certain types of shared
204	// libraries such as NDK stubs.
205	CanHaveApexVariants() bool
206
207	// Tests if this module can be installed to APEX as a file. For example, this would return
208	// true for shared libs while return false for static libs because static libs are not
209	// installable module (but it can still be mutated for APEX)
210	IsInstallableToApex() bool
211
212	// Tests if this module is available for the specified APEX or ":platform". This is from the
213	// apex_available property of the module.
214	AvailableFor(what string) bool
215
216	// AlwaysRequiresPlatformApexVariant allows the implementing module to determine whether an
217	// APEX mutator should always be created for it.
218	//
219	// Returns false by default.
220	AlwaysRequiresPlatformApexVariant() bool
221
222	// Returns true if this module is not available to platform (i.e. apex_available property
223	// doesn't have "//apex_available:platform"), or shouldn't be available to platform, which
224	// is the case when this module depends on other module that isn't available to platform.
225	NotAvailableForPlatform() bool
226
227	// Marks that this module is not available to platform. Set by the
228	// check-platform-availability mutator in the apex package.
229	SetNotAvailableForPlatform()
230
231	// Returns the list of APEXes that this module is a test for. The module has access to the
232	// private part of the listed APEXes even when it is not included in the APEXes. This by
233	// default returns nil. A module type should override the default implementation. For
234	// example, cc_test module type returns the value of test_for here.
235	TestFor() []string
236
237	// Returns nil (success) if this module should support the given sdk version. Returns an
238	// error if not. No default implementation is provided for this method. A module type
239	// implementing this interface should provide an implementation. A module supports an sdk
240	// version when the module's min_sdk_version is equal to or less than the given sdk version.
241	ShouldSupportSdkVersion(ctx BaseModuleContext, sdkVersion ApiLevel) error
242
243	// Returns true if this module needs a unique variation per apex, effectively disabling the
244	// deduping. This is turned on when, for example if use_apex_name_macro is set so that each
245	// apex variant should be built with different macro definitions.
246	UniqueApexVariations() bool
247}
248
249// Properties that are common to all module types implementing ApexModule interface.
250type ApexProperties struct {
251	// Availability of this module in APEXes. Only the listed APEXes can contain this module. If
252	// the module has stubs then other APEXes and the platform may access it through them
253	// (subject to visibility).
254	//
255	// "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
256	// "//apex_available:platform" refers to non-APEX partitions like "system.img".
257	// "com.android.gki.*" matches any APEX module name with the prefix "com.android.gki.".
258	// Default is ["//apex_available:platform"].
259	Apex_available []string
260
261	// See ApexModule.InAnyApex()
262	InAnyApex bool `blueprint:"mutated"`
263
264	// See ApexModule.DirectlyInAnyApex()
265	DirectlyInAnyApex bool `blueprint:"mutated"`
266
267	// AnyVariantDirectlyInAnyApex is true in the primary variant of a module if _any_ variant
268	// of the module is directly in any apex. This includes host, arch, asan, etc. variants. It
269	// is unused in any variant that is not the primary variant. Ideally this wouldn't be used,
270	// as it incorrectly mixes arch variants if only one arch is in an apex, but a few places
271	// depend on it, for example when an ASAN variant is created before the apexMutator. Call
272	// this after apex.apexMutator is run.
273	AnyVariantDirectlyInAnyApex bool `blueprint:"mutated"`
274
275	// See ApexModule.NotAvailableForPlatform()
276	NotAvailableForPlatform bool `blueprint:"mutated"`
277
278	// See ApexModule.UniqueApexVariants()
279	UniqueApexVariationsForDeps bool `blueprint:"mutated"`
280}
281
282// Marker interface that identifies dependencies that are excluded from APEX contents.
283//
284// Unless the tag also implements the AlwaysRequireApexVariantTag this will prevent an apex variant
285// from being created for the module.
286//
287// At the moment the sdk.sdkRequirementsMutator relies on the fact that the existing tags which
288// implement this interface do not define dependencies onto members of an sdk_snapshot. If that
289// changes then sdk.sdkRequirementsMutator will need fixing.
290type ExcludeFromApexContentsTag interface {
291	blueprint.DependencyTag
292
293	// Method that differentiates this interface from others.
294	ExcludeFromApexContents()
295}
296
297// Marker interface that identifies dependencies that always requires an APEX variant to be created.
298//
299// It is possible for a dependency to require an apex variant but exclude the module from the APEX
300// contents. See sdk.sdkMemberDependencyTag.
301type AlwaysRequireApexVariantTag interface {
302	blueprint.DependencyTag
303
304	// Return true if this tag requires that the target dependency has an apex variant.
305	AlwaysRequireApexVariant() bool
306}
307
308// Marker interface that identifies dependencies that should inherit the DirectlyInAnyApex state
309// from the parent to the child. For example, stubs libraries are marked as DirectlyInAnyApex if
310// their implementation is in an apex.
311type CopyDirectlyInAnyApexTag interface {
312	blueprint.DependencyTag
313
314	// Method that differentiates this interface from others.
315	CopyDirectlyInAnyApex()
316}
317
318// Interface that identifies dependencies to skip Apex dependency check
319type SkipApexAllowedDependenciesCheck interface {
320	// Returns true to skip the Apex dependency check, which limits the allowed dependency in build.
321	SkipApexAllowedDependenciesCheck() bool
322}
323
324// ApexModuleBase provides the default implementation for the ApexModule interface. APEX-aware
325// modules are expected to include this struct and call InitApexModule().
326type ApexModuleBase struct {
327	ApexProperties ApexProperties
328
329	canHaveApexVariants bool
330
331	apexInfos     []ApexInfo
332	apexInfosLock sync.Mutex // protects apexInfos during parallel apexInfoMutator
333}
334
335// Initializes ApexModuleBase struct. Not calling this (even when inheriting from ApexModuleBase)
336// prevents the module from being mutated for apexBundle.
337func InitApexModule(m ApexModule) {
338	base := m.apexModuleBase()
339	base.canHaveApexVariants = true
340
341	m.AddProperties(&base.ApexProperties)
342}
343
344// Implements ApexModule
345func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
346	return m
347}
348
349// Implements ApexModule
350func (m *ApexModuleBase) ApexAvailable() []string {
351	return m.ApexProperties.Apex_available
352}
353
354// Implements ApexModule
355func (m *ApexModuleBase) BuildForApex(apex ApexInfo) {
356	m.apexInfosLock.Lock()
357	defer m.apexInfosLock.Unlock()
358	for i, v := range m.apexInfos {
359		if v.ApexVariationName == apex.ApexVariationName {
360			if len(apex.InApexModules) != 1 {
361				panic(fmt.Errorf("Newly created apexInfo must be for a single APEX"))
362			}
363			// Even when the ApexVariantNames are the same, the given ApexInfo might
364			// actually be for different APEX. This can happen when an APEX is
365			// overridden via override_apex. For example, there can be two apexes
366			// `com.android.foo` (from the `apex` module type) and
367			// `com.mycompany.android.foo` (from the `override_apex` module type), both
368			// of which has the same ApexVariantName `com.android.foo`. Add the apex
369			// name to the list so that it's not lost.
370			if !InList(apex.InApexModules[0], v.InApexModules) {
371				m.apexInfos[i].InApexModules = append(m.apexInfos[i].InApexModules, apex.InApexModules[0])
372			}
373			return
374		}
375	}
376	m.apexInfos = append(m.apexInfos, apex)
377}
378
379// Implements ApexModule
380func (m *ApexModuleBase) InAnyApex() bool {
381	return m.ApexProperties.InAnyApex
382}
383
384// Implements ApexModule
385func (m *ApexModuleBase) DirectlyInAnyApex() bool {
386	return m.ApexProperties.DirectlyInAnyApex
387}
388
389// Implements ApexModule
390func (m *ApexModuleBase) NotInPlatform() bool {
391	return m.ApexProperties.AnyVariantDirectlyInAnyApex || !m.AvailableFor(AvailableToPlatform)
392}
393
394// Implements ApexModule
395func (m *ApexModuleBase) CanHaveApexVariants() bool {
396	return m.canHaveApexVariants
397}
398
399// Implements ApexModule
400func (m *ApexModuleBase) IsInstallableToApex() bool {
401	// If needed, this will bel overridden by concrete types inheriting
402	// ApexModuleBase
403	return false
404}
405
406// Implements ApexModule
407func (m *ApexModuleBase) TestFor() []string {
408	// If needed, this will be overridden by concrete types inheriting
409	// ApexModuleBase
410	return nil
411}
412
413// Implements ApexModule
414func (m *ApexModuleBase) UniqueApexVariations() bool {
415	// If needed, this will bel overridden by concrete types inheriting
416	// ApexModuleBase
417	return false
418}
419
420// Implements ApexModule
421func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
422	// By default, if there is a dependency from A to B, we try to include both in the same
423	// APEX, unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning
424	// true. This is overridden by some module types like apex.ApexBundle, cc.Module,
425	// java.Module, etc.
426	return true
427}
428
429const (
430	AvailableToPlatform = "//apex_available:platform"
431	AvailableToAnyApex  = "//apex_available:anyapex"
432	AvailableToGkiApex  = "com.android.gki.*"
433)
434
435// CheckAvailableForApex provides the default algorithm for checking the apex availability. When the
436// availability is empty, it defaults to ["//apex_available:platform"] which means "available to the
437// platform but not available to any APEX". When the list is not empty, `what` is matched against
438// the list. If there is any matching element in the list, thus function returns true. The special
439// availability "//apex_available:anyapex" matches with anything except for
440// "//apex_available:platform".
441func CheckAvailableForApex(what string, apex_available []string) bool {
442	if len(apex_available) == 0 {
443		return what == AvailableToPlatform
444	}
445	return InList(what, apex_available) ||
446		(what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) ||
447		(strings.HasPrefix(what, "com.android.gki.") && InList(AvailableToGkiApex, apex_available))
448}
449
450// Implements ApexModule
451func (m *ApexModuleBase) AvailableFor(what string) bool {
452	return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
453}
454
455// Implements ApexModule
456func (m *ApexModuleBase) AlwaysRequiresPlatformApexVariant() bool {
457	return false
458}
459
460// Implements ApexModule
461func (m *ApexModuleBase) NotAvailableForPlatform() bool {
462	return m.ApexProperties.NotAvailableForPlatform
463}
464
465// Implements ApexModule
466func (m *ApexModuleBase) SetNotAvailableForPlatform() {
467	m.ApexProperties.NotAvailableForPlatform = true
468}
469
470// This function makes sure that the apex_available property is valid
471func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
472	for _, n := range m.ApexProperties.Apex_available {
473		if n == AvailableToPlatform || n == AvailableToAnyApex || n == AvailableToGkiApex {
474			continue
475		}
476		if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
477			mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
478		}
479	}
480}
481
482// AvailableToSameApexes returns true if the two modules are apex_available to
483// exactly the same set of APEXes (and platform), i.e. if their apex_available
484// properties have the same elements.
485func AvailableToSameApexes(mod1, mod2 ApexModule) bool {
486	mod1ApexAvail := SortedUniqueStrings(mod1.apexModuleBase().ApexProperties.Apex_available)
487	mod2ApexAvail := SortedUniqueStrings(mod2.apexModuleBase().ApexProperties.Apex_available)
488	if len(mod1ApexAvail) != len(mod2ApexAvail) {
489		return false
490	}
491	for i, v := range mod1ApexAvail {
492		if v != mod2ApexAvail[i] {
493			return false
494		}
495	}
496	return true
497}
498
499type byApexName []ApexInfo
500
501func (a byApexName) Len() int           { return len(a) }
502func (a byApexName) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
503func (a byApexName) Less(i, j int) bool { return a[i].ApexVariationName < a[j].ApexVariationName }
504
505// mergeApexVariations deduplicates apex variations that would build identically into a common
506// variation. It returns the reduced list of variations and a list of aliases from the original
507// variation names to the new variation names.
508func mergeApexVariations(ctx PathContext, apexInfos []ApexInfo) (merged []ApexInfo, aliases [][2]string) {
509	sort.Sort(byApexName(apexInfos))
510	seen := make(map[string]int)
511	for _, apexInfo := range apexInfos {
512		// If this is for a prebuilt apex then use the actual name of the apex variation to prevent this
513		// from being merged with other ApexInfo. See Prebuilt.ApexInfoMutator for more information.
514		if apexInfo.ForPrebuiltApex {
515			merged = append(merged, apexInfo)
516			continue
517		}
518
519		// Merge the ApexInfo together. If a compatible ApexInfo exists then merge the information from
520		// this one into it, otherwise create a new merged ApexInfo from this one and save it away so
521		// other ApexInfo instances can be merged into it.
522		variantName := apexInfo.ApexVariationName
523		mergedName := apexInfo.mergedName(ctx)
524		if index, exists := seen[mergedName]; exists {
525			// Variants having the same mergedName are deduped
526			merged[index].InApexVariants = append(merged[index].InApexVariants, variantName)
527			merged[index].InApexModules = append(merged[index].InApexModules, apexInfo.InApexModules...)
528			merged[index].ApexContents = append(merged[index].ApexContents, apexInfo.ApexContents...)
529			merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable
530		} else {
531			seen[mergedName] = len(merged)
532			apexInfo.ApexVariationName = mergedName
533			apexInfo.InApexVariants = CopyOf(apexInfo.InApexVariants)
534			apexInfo.InApexModules = CopyOf(apexInfo.InApexModules)
535			apexInfo.ApexContents = append([]*ApexContents(nil), apexInfo.ApexContents...)
536			merged = append(merged, apexInfo)
537		}
538		aliases = append(aliases, [2]string{variantName, mergedName})
539	}
540	return merged, aliases
541}
542
543// CreateApexVariations mutates a given module into multiple apex variants each of which is for an
544// apexBundle (and/or the platform) where the module is part of.
545func CreateApexVariations(mctx BottomUpMutatorContext, module ApexModule) []Module {
546	base := module.apexModuleBase()
547
548	// Shortcut
549	if len(base.apexInfos) == 0 {
550		return nil
551	}
552
553	// Do some validity checks.
554	// TODO(jiyong): is this the right place?
555	base.checkApexAvailableProperty(mctx)
556
557	var apexInfos []ApexInfo
558	var aliases [][2]string
559	if !mctx.Module().(ApexModule).UniqueApexVariations() && !base.ApexProperties.UniqueApexVariationsForDeps {
560		apexInfos, aliases = mergeApexVariations(mctx, base.apexInfos)
561	} else {
562		apexInfos = base.apexInfos
563	}
564	// base.apexInfos is only needed to propagate the list of apexes from apexInfoMutator to
565	// apexMutator. It is no longer accurate after mergeApexVariations, and won't be copied to
566	// all but the first created variant. Clear it so it doesn't accidentally get used later.
567	base.apexInfos = nil
568	sort.Sort(byApexName(apexInfos))
569
570	var inApex ApexMembership
571	for _, a := range apexInfos {
572		for _, apexContents := range a.ApexContents {
573			inApex = inApex.merge(apexContents.contents[mctx.ModuleName()])
574		}
575	}
576	base.ApexProperties.InAnyApex = true
577	base.ApexProperties.DirectlyInAnyApex = inApex == directlyInApex
578
579	defaultVariation := ""
580	mctx.SetDefaultDependencyVariation(&defaultVariation)
581
582	variations := []string{defaultVariation}
583	for _, a := range apexInfos {
584		variations = append(variations, a.ApexVariationName)
585	}
586	modules := mctx.CreateVariations(variations...)
587	for i, mod := range modules {
588		platformVariation := i == 0
589		if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
590			// Do not install the module for platform, but still allow it to output
591			// uninstallable AndroidMk entries in certain cases when they have side
592			// effects.  TODO(jiyong): move this routine to somewhere else
593			mod.MakeUninstallable()
594		}
595		if !platformVariation {
596			mctx.SetVariationProvider(mod, ApexInfoProvider, apexInfos[i-1])
597		}
598	}
599
600	for _, alias := range aliases {
601		mctx.CreateAliasVariation(alias[0], alias[1])
602	}
603
604	return modules
605}
606
607// UpdateUniqueApexVariationsForDeps sets UniqueApexVariationsForDeps if any dependencies that are
608// in the same APEX have unique APEX variations so that the module can link against the right
609// variant.
610func UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext, am ApexModule) {
611	// anyInSameApex returns true if the two ApexInfo lists contain any values in an
612	// InApexVariants list in common. It is used instead of DepIsInSameApex because it needs to
613	// determine if the dep is in the same APEX due to being directly included, not only if it
614	// is included _because_ it is a dependency.
615	anyInSameApex := func(a, b []ApexInfo) bool {
616		collectApexes := func(infos []ApexInfo) []string {
617			var ret []string
618			for _, info := range infos {
619				ret = append(ret, info.InApexVariants...)
620			}
621			return ret
622		}
623
624		aApexes := collectApexes(a)
625		bApexes := collectApexes(b)
626		sort.Strings(bApexes)
627		for _, aApex := range aApexes {
628			index := sort.SearchStrings(bApexes, aApex)
629			if index < len(bApexes) && bApexes[index] == aApex {
630				return true
631			}
632		}
633		return false
634	}
635
636	// If any of the dependencies requires unique apex variations, so does this module.
637	mctx.VisitDirectDeps(func(dep Module) {
638		if depApexModule, ok := dep.(ApexModule); ok {
639			if anyInSameApex(depApexModule.apexModuleBase().apexInfos, am.apexModuleBase().apexInfos) &&
640				(depApexModule.UniqueApexVariations() ||
641					depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) {
642				am.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps = true
643			}
644		}
645	})
646}
647
648// UpdateDirectlyInAnyApex uses the final module to store if any variant of this module is directly
649// in any APEX, and then copies the final value to all the modules. It also copies the
650// DirectlyInAnyApex value to any direct dependencies with a CopyDirectlyInAnyApexTag dependency
651// tag.
652func UpdateDirectlyInAnyApex(mctx BottomUpMutatorContext, am ApexModule) {
653	base := am.apexModuleBase()
654	// Copy DirectlyInAnyApex and InAnyApex from any direct dependencies with a
655	// CopyDirectlyInAnyApexTag dependency tag.
656	mctx.VisitDirectDeps(func(dep Module) {
657		if _, ok := mctx.OtherModuleDependencyTag(dep).(CopyDirectlyInAnyApexTag); ok {
658			depBase := dep.(ApexModule).apexModuleBase()
659			depBase.ApexProperties.DirectlyInAnyApex = base.ApexProperties.DirectlyInAnyApex
660			depBase.ApexProperties.InAnyApex = base.ApexProperties.InAnyApex
661		}
662	})
663
664	if base.ApexProperties.DirectlyInAnyApex {
665		// Variants of a module are always visited sequentially in order, so it is safe to
666		// write to another variant of this module. For a BottomUpMutator the
667		// PrimaryModule() is visited first and FinalModule() is visited last.
668		mctx.FinalModule().(ApexModule).apexModuleBase().ApexProperties.AnyVariantDirectlyInAnyApex = true
669	}
670
671	// If this is the FinalModule (last visited module) copy
672	// AnyVariantDirectlyInAnyApex to all the other variants
673	if am == mctx.FinalModule().(ApexModule) {
674		mctx.VisitAllModuleVariants(func(variant Module) {
675			variant.(ApexModule).apexModuleBase().ApexProperties.AnyVariantDirectlyInAnyApex =
676				base.ApexProperties.AnyVariantDirectlyInAnyApex
677		})
678	}
679}
680
681// ApexMembership tells how a module became part of an APEX.
682type ApexMembership int
683
684const (
685	notInApex        ApexMembership = 0
686	indirectlyInApex                = iota
687	directlyInApex
688)
689
690// ApexContents gives an information about member modules of an apexBundle.  Each apexBundle has an
691// apexContents, and modules in that apex have a provider containing the apexContents of each
692// apexBundle they are part of.
693type ApexContents struct {
694	// map from a module name to its membership in this apexBundle
695	contents map[string]ApexMembership
696}
697
698// NewApexContents creates and initializes an ApexContents that is suitable
699// for use with an apex module.
700// * contents is a map from a module name to information about its membership within
701//   the apex.
702func NewApexContents(contents map[string]ApexMembership) *ApexContents {
703	return &ApexContents{
704		contents: contents,
705	}
706}
707
708// Updates an existing membership by adding a new direct (or indirect) membership
709func (i ApexMembership) Add(direct bool) ApexMembership {
710	if direct || i == directlyInApex {
711		return directlyInApex
712	}
713	return indirectlyInApex
714}
715
716// Merges two membership into one. Merging is needed because a module can be a part of an apexBundle
717// in many different paths. For example, it could be dependend on by the apexBundle directly, but at
718// the same time, there might be an indirect dependency to the module. In that case, the more
719// specific dependency (the direct one) is chosen.
720func (i ApexMembership) merge(other ApexMembership) ApexMembership {
721	if other == directlyInApex || i == directlyInApex {
722		return directlyInApex
723	}
724
725	if other == indirectlyInApex || i == indirectlyInApex {
726		return indirectlyInApex
727	}
728	return notInApex
729}
730
731// Tests whether a module named moduleName is directly included in the apexBundle where this
732// ApexContents is tagged.
733func (ac *ApexContents) DirectlyInApex(moduleName string) bool {
734	return ac.contents[moduleName] == directlyInApex
735}
736
737// Tests whether a module named moduleName is included in the apexBundle where this ApexContent is
738// tagged.
739func (ac *ApexContents) InApex(moduleName string) bool {
740	return ac.contents[moduleName] != notInApex
741}
742
743// Tests whether a module named moduleName is directly depended on by all APEXes in an ApexInfo.
744func DirectlyInAllApexes(apexInfo ApexInfo, moduleName string) bool {
745	for _, contents := range apexInfo.ApexContents {
746		if !contents.DirectlyInApex(moduleName) {
747			return false
748		}
749	}
750	return true
751}
752
753////////////////////////////////////////////////////////////////////////////////////////////////////
754//Below are routines for extra safety checks.
755//
756// BuildDepsInfoLists is to flatten the dependency graph for an apexBundle into a text file
757// (actually two in slightly different formats). The files are mostly for debugging, for example to
758// see why a certain module is included in an APEX via which dependency path.
759//
760// CheckMinSdkVersion is to make sure that all modules in an apexBundle satisfy the min_sdk_version
761// requirement of the apexBundle.
762
763// A dependency info for a single ApexModule, either direct or transitive.
764type ApexModuleDepInfo struct {
765	// Name of the dependency
766	To string
767	// List of dependencies To belongs to. Includes APEX itself, if a direct dependency.
768	From []string
769	// Whether the dependency belongs to the final compiled APEX.
770	IsExternal bool
771	// min_sdk_version of the ApexModule
772	MinSdkVersion string
773}
774
775// A map of a dependency name to its ApexModuleDepInfo
776type DepNameToDepInfoMap map[string]ApexModuleDepInfo
777
778type ApexBundleDepsInfo struct {
779	flatListPath OutputPath
780	fullListPath OutputPath
781}
782
783type ApexBundleDepsInfoIntf interface {
784	Updatable() bool
785	FlatListPath() Path
786	FullListPath() Path
787}
788
789func (d *ApexBundleDepsInfo) FlatListPath() Path {
790	return d.flatListPath
791}
792
793func (d *ApexBundleDepsInfo) FullListPath() Path {
794	return d.fullListPath
795}
796
797// Generate two module out files:
798// 1. FullList with transitive deps and their parents in the dep graph
799// 2. FlatList with a flat list of transitive deps
800// In both cases transitive deps of external deps are not included. Neither are deps that are only
801// available to APEXes; they are developed with updatability in mind and don't need manual approval.
802func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) {
803	var fullContent strings.Builder
804	var flatContent strings.Builder
805
806	fmt.Fprintf(&fullContent, "%s(minSdkVersion:%s):\n", ctx.ModuleName(), minSdkVersion)
807	for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) {
808		info := depInfos[key]
809		toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
810		if info.IsExternal {
811			toName = toName + " (external)"
812		}
813		fmt.Fprintf(&fullContent, "  %s <- %s\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
814		fmt.Fprintf(&flatContent, "%s\n", toName)
815	}
816
817	d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath
818	WriteFileRule(ctx, d.fullListPath, fullContent.String())
819
820	d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath
821	WriteFileRule(ctx, d.flatListPath, flatContent.String())
822
823	ctx.Phony(fmt.Sprintf("%s-depsinfo", ctx.ModuleName()), d.fullListPath, d.flatListPath)
824}
825
826// TODO(b/158059172): remove minSdkVersion allowlist
827var minSdkVersionAllowlist = func(apiMap map[string]int) map[string]ApiLevel {
828	list := make(map[string]ApiLevel, len(apiMap))
829	for name, finalApiInt := range apiMap {
830		list[name] = uncheckedFinalApiLevel(finalApiInt)
831	}
832	return list
833}(map[string]int{
834	"adbd":                                                     30,
835	"android.net.ipsec.ike":                                    30,
836	"androidx.annotation_annotation-nodeps":                    29,
837	"androidx.arch.core_core-common-nodeps":                    29,
838	"androidx.collection_collection-nodeps":                    29,
839	"androidx.collection_collection-ktx-nodeps":                30,
840	"androidx.concurrent_concurrent-futures-nodeps":            30,
841	"androidx.lifecycle_lifecycle-common-java8-nodeps":         30,
842	"androidx.lifecycle_lifecycle-common-nodeps":               29,
843	"androidx.room_room-common-nodeps":                         30,
844	"androidx-constraintlayout_constraintlayout-solver-nodeps": 29,
845	"apache-commons-compress":                                  29,
846	"bouncycastle_ike_digests":                                 30,
847	"brotli-java":                                              29,
848	"captiveportal-lib":                                        28,
849	"error_prone_annotations":                                  30,
850	"flatbuffer_headers":                                       30,
851	"framework-permission":                                     30,
852	"gemmlowp_headers":                                         30,
853	"guava-listenablefuture-prebuilt-jar":                      30,
854	"ike-internals":                                            30,
855	"kotlinx-coroutines-android":                               28,
856	"kotlinx-coroutines-android-nodeps":                        30,
857	"kotlinx-coroutines-core":                                  28,
858	"kotlinx-coroutines-core-nodeps":                           30,
859	"libadb_crypto":                                            30,
860	"libadb_pairing_auth":                                      30,
861	"libadb_pairing_connection":                                30,
862	"libadb_pairing_server":                                    30,
863	"libadb_protos":                                            30,
864	"libadb_tls_connection":                                    30,
865	"libadbconnection_client":                                  30,
866	"libadbconnection_server":                                  30,
867	"libadbd_core":                                             30,
868	"libadbd_services":                                         30,
869	"libadbd":                                                  30,
870	"libapp_processes_protos_lite":                             30,
871	"libasyncio":                                               30,
872	"libbrotli":                                                30,
873	"libbuildversion":                                          30,
874	"libcrypto_static":                                         30,
875	"libcrypto_utils":                                          30,
876	"libdiagnose_usb":                                          30,
877	"libeigen":                                                 30,
878	"liblz4":                                                   30,
879	"libmdnssd":                                                30,
880	"libneuralnetworks_common":                                 30,
881	"libneuralnetworks_headers":                                30,
882	"libneuralnetworks":                                        30,
883	"libprocpartition":                                         30,
884	"libprotobuf-java-lite":                                    30,
885	"libprotoutil":                                             30,
886	"libsync":                                                  30,
887	"libtextclassifier_hash_headers":                           30,
888	"libtextclassifier_hash_static":                            30,
889	"libtflite_kernel_utils":                                   30,
890	"libwatchdog":                                              29,
891	"libzstd":                                                  30,
892	"metrics-constants-protos":                                 28,
893	"net-utils-framework-common":                               29,
894	"permissioncontroller-statsd":                              28,
895	"philox_random_headers":                                    30,
896	"philox_random":                                            30,
897	"service-permission":                                       30,
898	"tensorflow_headers":                                       30,
899	"xz-java":                                                  29,
900})
901
902// Function called while walking an APEX's payload dependencies.
903//
904// Return true if the `to` module should be visited, false otherwise.
905type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool
906
907// UpdatableModule represents updatable APEX/APK
908type UpdatableModule interface {
909	Module
910	WalkPayloadDeps(ctx ModuleContext, do PayloadDepsCallback)
911}
912
913// CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version
914// accordingly
915func CheckMinSdkVersion(m UpdatableModule, ctx ModuleContext, minSdkVersion ApiLevel) {
916	// do not enforce min_sdk_version for host
917	if ctx.Host() {
918		return
919	}
920
921	// do not enforce for coverage build
922	if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() {
923		return
924	}
925
926	// do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version
927	if minSdkVersion.IsNone() {
928		return
929	}
930
931	m.WalkPayloadDeps(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool {
932		if externalDep {
933			// external deps are outside the payload boundary, which is "stable"
934			// interface. We don't have to check min_sdk_version for external
935			// dependencies.
936			return false
937		}
938		if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
939			return false
940		}
941		if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil {
942			toName := ctx.OtherModuleName(to)
943			if ver, ok := minSdkVersionAllowlist[toName]; !ok || ver.GreaterThan(minSdkVersion) {
944				ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v."+
945					"\n\nDependency path: %s\n\n"+
946					"Consider adding 'min_sdk_version: %q' to %q",
947					minSdkVersion, ctx.ModuleName(), err.Error(),
948					ctx.GetPathString(false),
949					minSdkVersion, toName)
950				return false
951			}
952		}
953		return true
954	})
955}
956