Record the actual APEXes that a module is part of.

Consider this case:

apex {
    name: "com.android.foo",
    native_libs: ["foo"],
}

override_apex {
    name: "com.mycompany.android.foo",
    base: "com.android.foo",
}

cc_library {
    name: "foo",
}

There are two APEXes defined: "com.android.foo" and
"com.mycompany.android.foo" which is a copy of "com.android.foo" with
some properties overridden (e.g. signing keys).

The module "foo" is mutated into two variants by the apex mutator: the
platform variant and the apex variant. The former has the variation name
"" and the later has "apex<min_api_ver>" which usually is "apex10000".

Internally, the apex variant has an alias "com.android.foo".

ApexInfo.InApexVariants() returns only "com.android.foo" when called for
the module "foo".

We can see that the information that "foo" is also part of
"com.mycompany.android.foo" is completely lost. This is causing problem
when we compare the apex membership by their "soong module name", not
the "apex name". In the example above, the two modules have different
soone module names, but have the same apex name: "com.android.foo".

To fix that, this CL introduces a new field `InApexes` to the `ApexInfo`
struct. It has the actual name of the APEXes that the module is part of.
With the example above, `InApexes` is ["com.android.foo",
"com.mycompany.android.foo"].

Bug: 180325915
Test: m nothing
Test: m nothing on non-AOSP targets with ag/13740887 applied.

Change-Id: I4e7a7ac5495d2e622ba92a4358ed967e066c6c2e
This commit is contained in:
Jiyong Park 2021-05-12 17:13:56 +09:00
parent ab50b0795a
commit 78349b56a0
5 changed files with 62 additions and 25 deletions

View file

@ -63,6 +63,14 @@ type ApexInfo struct {
// that are merged together. // that are merged together.
InApexVariants []string InApexVariants []string
// List of APEX Soong module names that this module is part of. Note that the list includes
// different variations of the same APEX. For example, if module `foo` is included in the
// apex `com.android.foo`, and also if there is an override_apex module
// `com.mycompany.android.foo` overriding `com.android.foo`, then this list contains both
// `com.android.foo` and `com.mycompany.android.foo`. If the APEX Soong module is a
// prebuilt, the name here doesn't have the `prebuilt_` prefix.
InApexModules []string
// Pointers to the ApexContents struct each of which is for apexBundle modules that this // Pointers to the ApexContents struct each of which is for apexBundle modules that this
// module is part of. The ApexContents gives information about which modules the apexBundle // module is part of. The ApexContents gives information about which modules the apexBundle
// has and whether a module became part of the apexBundle via a direct dependency or not. // has and whether a module became part of the apexBundle via a direct dependency or not.
@ -122,6 +130,15 @@ func (i ApexInfo) InApexVariantByBaseName(apexVariant string) bool {
return false return false
} }
func (i ApexInfo) InApexModule(apexModuleName string) bool {
for _, a := range i.InApexModules {
if a == apexModuleName {
return true
}
}
return false
}
// ApexTestForInfo stores the contents of APEXes for which this module is a test - although this // ApexTestForInfo stores the contents of APEXes for which this module is a test - although this
// module is not part of the APEX - and thus has access to APEX internals. // module is not part of the APEX - and thus has access to APEX internals.
type ApexTestForInfo struct { type ApexTestForInfo struct {
@ -351,8 +368,21 @@ func (m *ApexModuleBase) ApexAvailable() []string {
func (m *ApexModuleBase) BuildForApex(apex ApexInfo) { func (m *ApexModuleBase) BuildForApex(apex ApexInfo) {
m.apexInfosLock.Lock() m.apexInfosLock.Lock()
defer m.apexInfosLock.Unlock() defer m.apexInfosLock.Unlock()
for _, v := range m.apexInfos { for i, v := range m.apexInfos {
if v.ApexVariationName == apex.ApexVariationName { if v.ApexVariationName == apex.ApexVariationName {
if len(apex.InApexModules) != 1 {
panic(fmt.Errorf("Newly created apexInfo must be for a single APEX"))
}
// Even when the ApexVariantNames are the same, the given ApexInfo might
// actually be for different APEX. This can happen when an APEX is
// overridden via override_apex. For example, there can be two apexes
// `com.android.foo` (from the `apex` module type) and
// `com.mycompany.android.foo` (from the `override_apex` module type), both
// of which has the same ApexVariantName `com.android.foo`. Add the apex
// name to the list so that it's not lost.
if !InList(apex.InApexModules[0], v.InApexModules) {
m.apexInfos[i].InApexModules = append(m.apexInfos[i].InApexModules, apex.InApexModules[0])
}
return return
} }
} }
@ -507,12 +537,14 @@ func mergeApexVariations(ctx PathContext, apexInfos []ApexInfo) (merged []ApexIn
if index, exists := seen[mergedName]; exists { if index, exists := seen[mergedName]; exists {
// Variants having the same mergedName are deduped // Variants having the same mergedName are deduped
merged[index].InApexVariants = append(merged[index].InApexVariants, variantName) merged[index].InApexVariants = append(merged[index].InApexVariants, variantName)
merged[index].InApexModules = append(merged[index].InApexModules, apexInfo.InApexModules...)
merged[index].ApexContents = append(merged[index].ApexContents, apexInfo.ApexContents...) merged[index].ApexContents = append(merged[index].ApexContents, apexInfo.ApexContents...)
merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable
} else { } else {
seen[mergedName] = len(merged) seen[mergedName] = len(merged)
apexInfo.ApexVariationName = mergedName apexInfo.ApexVariationName = mergedName
apexInfo.InApexVariants = CopyOf(apexInfo.InApexVariants) apexInfo.InApexVariants = CopyOf(apexInfo.InApexVariants)
apexInfo.InApexModules = CopyOf(apexInfo.InApexModules)
apexInfo.ApexContents = append([]*ApexContents(nil), apexInfo.ApexContents...) apexInfo.ApexContents = append([]*ApexContents(nil), apexInfo.ApexContents...)
merged = append(merged, apexInfo) merged = append(merged, apexInfo)
} }

View file

@ -33,10 +33,10 @@ func Test_mergeApexVariations(t *testing.T) {
{ {
name: "single", name: "single",
in: []ApexInfo{ in: []ApexInfo{
{"foo", FutureApiLevel, false, nil, []string{"foo"}, nil, NotForPrebuiltApex}, {"foo", FutureApiLevel, false, nil, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex},
}, },
wantMerged: []ApexInfo{ wantMerged: []ApexInfo{
{"apex10000", FutureApiLevel, false, nil, []string{"foo"}, nil, NotForPrebuiltApex}, {"apex10000", FutureApiLevel, false, nil, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex},
}, },
wantAliases: [][2]string{ wantAliases: [][2]string{
{"foo", "apex10000"}, {"foo", "apex10000"},
@ -45,11 +45,11 @@ func Test_mergeApexVariations(t *testing.T) {
{ {
name: "merge", name: "merge",
in: []ApexInfo{ in: []ApexInfo{
{"foo", FutureApiLevel, false, SdkRefs{{"baz", "1"}}, []string{"foo"}, nil, NotForPrebuiltApex}, {"foo", FutureApiLevel, false, SdkRefs{{"baz", "1"}}, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex},
{"bar", FutureApiLevel, false, SdkRefs{{"baz", "1"}}, []string{"bar"}, nil, NotForPrebuiltApex}, {"bar", FutureApiLevel, false, SdkRefs{{"baz", "1"}}, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex},
}, },
wantMerged: []ApexInfo{ wantMerged: []ApexInfo{
{"apex10000_baz_1", FutureApiLevel, false, SdkRefs{{"baz", "1"}}, []string{"bar", "foo"}, nil, false}}, {"apex10000_baz_1", FutureApiLevel, false, SdkRefs{{"baz", "1"}}, []string{"bar", "foo"}, []string{"bar", "foo"}, nil, false}},
wantAliases: [][2]string{ wantAliases: [][2]string{
{"bar", "apex10000_baz_1"}, {"bar", "apex10000_baz_1"},
{"foo", "apex10000_baz_1"}, {"foo", "apex10000_baz_1"},
@ -58,12 +58,12 @@ func Test_mergeApexVariations(t *testing.T) {
{ {
name: "don't merge version", name: "don't merge version",
in: []ApexInfo{ in: []ApexInfo{
{"foo", FutureApiLevel, false, nil, []string{"foo"}, nil, NotForPrebuiltApex}, {"foo", FutureApiLevel, false, nil, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex},
{"bar", uncheckedFinalApiLevel(30), false, nil, []string{"bar"}, nil, NotForPrebuiltApex}, {"bar", uncheckedFinalApiLevel(30), false, nil, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex},
}, },
wantMerged: []ApexInfo{ wantMerged: []ApexInfo{
{"apex30", uncheckedFinalApiLevel(30), false, nil, []string{"bar"}, nil, NotForPrebuiltApex}, {"apex30", uncheckedFinalApiLevel(30), false, nil, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex},
{"apex10000", FutureApiLevel, false, nil, []string{"foo"}, nil, NotForPrebuiltApex}, {"apex10000", FutureApiLevel, false, nil, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex},
}, },
wantAliases: [][2]string{ wantAliases: [][2]string{
{"bar", "apex30"}, {"bar", "apex30"},
@ -73,11 +73,11 @@ func Test_mergeApexVariations(t *testing.T) {
{ {
name: "merge updatable", name: "merge updatable",
in: []ApexInfo{ in: []ApexInfo{
{"foo", FutureApiLevel, false, nil, []string{"foo"}, nil, NotForPrebuiltApex}, {"foo", FutureApiLevel, false, nil, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex},
{"bar", FutureApiLevel, true, nil, []string{"bar"}, nil, NotForPrebuiltApex}, {"bar", FutureApiLevel, true, nil, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex},
}, },
wantMerged: []ApexInfo{ wantMerged: []ApexInfo{
{"apex10000", FutureApiLevel, true, nil, []string{"bar", "foo"}, nil, NotForPrebuiltApex}, {"apex10000", FutureApiLevel, true, nil, []string{"bar", "foo"}, []string{"bar", "foo"}, nil, NotForPrebuiltApex},
}, },
wantAliases: [][2]string{ wantAliases: [][2]string{
{"bar", "apex10000"}, {"bar", "apex10000"},
@ -87,12 +87,12 @@ func Test_mergeApexVariations(t *testing.T) {
{ {
name: "don't merge sdks", name: "don't merge sdks",
in: []ApexInfo{ in: []ApexInfo{
{"foo", FutureApiLevel, false, SdkRefs{{"baz", "1"}}, []string{"foo"}, nil, NotForPrebuiltApex}, {"foo", FutureApiLevel, false, SdkRefs{{"baz", "1"}}, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex},
{"bar", FutureApiLevel, false, SdkRefs{{"baz", "2"}}, []string{"bar"}, nil, NotForPrebuiltApex}, {"bar", FutureApiLevel, false, SdkRefs{{"baz", "2"}}, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex},
}, },
wantMerged: []ApexInfo{ wantMerged: []ApexInfo{
{"apex10000_baz_2", FutureApiLevel, false, SdkRefs{{"baz", "2"}}, []string{"bar"}, nil, NotForPrebuiltApex}, {"apex10000_baz_2", FutureApiLevel, false, SdkRefs{{"baz", "2"}}, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex},
{"apex10000_baz_1", FutureApiLevel, false, SdkRefs{{"baz", "1"}}, []string{"foo"}, nil, NotForPrebuiltApex}, {"apex10000_baz_1", FutureApiLevel, false, SdkRefs{{"baz", "1"}}, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex},
}, },
wantAliases: [][2]string{ wantAliases: [][2]string{
{"bar", "apex10000_baz_2"}, {"bar", "apex10000_baz_2"},
@ -102,15 +102,15 @@ func Test_mergeApexVariations(t *testing.T) {
{ {
name: "don't merge when for prebuilt_apex", name: "don't merge when for prebuilt_apex",
in: []ApexInfo{ in: []ApexInfo{
{"foo", FutureApiLevel, false, nil, []string{"foo"}, nil, NotForPrebuiltApex}, {"foo", FutureApiLevel, false, nil, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex},
{"bar", FutureApiLevel, true, nil, []string{"bar"}, nil, NotForPrebuiltApex}, {"bar", FutureApiLevel, true, nil, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex},
// This one should not be merged in with the others because it is for // This one should not be merged in with the others because it is for
// a prebuilt_apex. // a prebuilt_apex.
{"baz", FutureApiLevel, true, nil, []string{"baz"}, nil, ForPrebuiltApex}, {"baz", FutureApiLevel, true, nil, []string{"baz"}, []string{"baz"}, nil, ForPrebuiltApex},
}, },
wantMerged: []ApexInfo{ wantMerged: []ApexInfo{
{"apex10000", FutureApiLevel, true, nil, []string{"bar", "foo"}, nil, NotForPrebuiltApex}, {"apex10000", FutureApiLevel, true, nil, []string{"bar", "foo"}, []string{"bar", "foo"}, nil, NotForPrebuiltApex},
{"baz", FutureApiLevel, true, nil, []string{"baz"}, nil, ForPrebuiltApex}, {"baz", FutureApiLevel, true, nil, []string{"baz"}, []string{"baz"}, nil, ForPrebuiltApex},
}, },
wantAliases: [][2]string{ wantAliases: [][2]string{
{"bar", "apex10000"}, {"bar", "apex10000"},

View file

@ -906,12 +906,16 @@ func (a *apexBundle) ApexInfoMutator(mctx android.TopDownMutatorContext) {
// This is the main part of this mutator. Mark the collected dependencies that they need to // This is the main part of this mutator. Mark the collected dependencies that they need to
// be built for this apexBundle. // be built for this apexBundle.
// Note that there are many different names.
// ApexVariationName: this is the name of the apex variation
apexInfo := android.ApexInfo{ apexInfo := android.ApexInfo{
ApexVariationName: mctx.ModuleName(), ApexVariationName: mctx.ModuleName(), // could be com.android.foo
MinSdkVersion: minSdkVersion, MinSdkVersion: minSdkVersion,
RequiredSdks: a.RequiredSdks(), RequiredSdks: a.RequiredSdks(),
Updatable: a.Updatable(), Updatable: a.Updatable(),
InApexVariants: []string{mctx.ModuleName()}, InApexVariants: []string{mctx.ModuleName()}, // could be com.android.foo
InApexModules: []string{a.Name()}, // could be com.mycompany.android.foo
ApexContents: []*android.ApexContents{apexContents}, ApexContents: []*android.ApexContents{apexContents},
} }
mctx.WalkDeps(func(child, parent android.Module) bool { mctx.WalkDeps(func(child, parent android.Module) bool {

View file

@ -231,6 +231,7 @@ func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
apexInfo := android.ApexInfo{ apexInfo := android.ApexInfo{
ApexVariationName: android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName()), ApexVariationName: android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName()),
InApexVariants: []string{mctx.ModuleName()}, InApexVariants: []string{mctx.ModuleName()},
InApexModules: []string{mctx.ModuleName()},
ApexContents: []*android.ApexContents{apexContents}, ApexContents: []*android.ApexContents{apexContents},
ForPrebuiltApex: true, ForPrebuiltApex: true,
} }

View file

@ -89,7 +89,7 @@ func (b *bootJarsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
name := android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName(module)) name := android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName(module))
if apex, ok := moduleToApex[name]; ok { if apex, ok := moduleToApex[name]; ok {
apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo) apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
if (apex == "platform" && apexInfo.IsForPlatform()) || apexInfo.InApexVariantByBaseName(apex) { if (apex == "platform" && apexInfo.IsForPlatform()) || apexInfo.InApexModule(apex) {
// The module name/apex variant should be unique in the system but double check // The module name/apex variant should be unique in the system but double check
// just in case something has gone wrong. // just in case something has gone wrong.
if existing, ok := nameToApexVariant[name]; ok { if existing, ok := nameToApexVariant[name]; ok {