mixed builds: Make apex's GetBazelLabel return the label of the override_apex, if applicable. am: 889f2f2844

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/2363859

Change-Id: I05fe6a528087a2b60bac7c500641f41fb078032f
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Jingwen Chen
2022-12-20 11:10:52 +00:00
committed by Automerger Merge Worker
6 changed files with 308 additions and 101 deletions

View File

@@ -1364,7 +1364,10 @@ var (
// Bazel prod-mode allowlist. Modules in this list are built by Bazel // Bazel prod-mode allowlist. Modules in this list are built by Bazel
// in either prod mode or staging mode. // in either prod mode or staging mode.
ProdMixedBuildsEnabledList = []string{"com.android.tzdata"} ProdMixedBuildsEnabledList = []string{
"com.android.tzdata",
"test1_com.android.tzdata",
}
// Staging-mode allowlist. Modules in this list are only built // Staging-mode allowlist. Modules in this list are only built
// by Bazel with --bazel-mode-staging. This list should contain modules // by Bazel with --bazel-mode-staging. This list should contain modules

View File

@@ -455,6 +455,9 @@ func samePackage(label1, label2 string) bool {
func bp2buildModuleLabel(ctx BazelConversionContext, module blueprint.Module) string { func bp2buildModuleLabel(ctx BazelConversionContext, module blueprint.Module) string {
moduleName := ctx.OtherModuleName(module) moduleName := ctx.OtherModuleName(module)
moduleDir := ctx.OtherModuleDir(module) moduleDir := ctx.OtherModuleDir(module)
if moduleDir == Bp2BuildTopLevel {
moduleDir = ""
}
return fmt.Sprintf("//%s:%s", moduleDir, moduleName) return fmt.Sprintf("//%s:%s", moduleDir, moduleName)
} }

View File

@@ -28,6 +28,7 @@ package android
// module based on it. // module based on it.
import ( import (
"fmt"
"sort" "sort"
"sync" "sync"
@@ -48,6 +49,10 @@ type OverrideModule interface {
// i.e. cases where an overriding module, too, is overridden by a prebuilt module. // i.e. cases where an overriding module, too, is overridden by a prebuilt module.
setOverriddenByPrebuilt(overridden bool) setOverriddenByPrebuilt(overridden bool)
getOverriddenByPrebuilt() bool getOverriddenByPrebuilt() bool
// Directory containing the Blueprint definition of the overriding module
setModuleDir(string)
ModuleDir() string
} }
// Base module struct for override module types // Base module struct for override module types
@@ -57,6 +62,8 @@ type OverrideModuleBase struct {
overridingProperties []interface{} overridingProperties []interface{}
overriddenByPrebuilt bool overriddenByPrebuilt bool
moduleDir string
} }
type OverrideModuleProperties struct { type OverrideModuleProperties struct {
@@ -66,6 +73,14 @@ type OverrideModuleProperties struct {
// TODO(jungjw): Add an optional override_name bool flag. // TODO(jungjw): Add an optional override_name bool flag.
} }
func (o *OverrideModuleBase) setModuleDir(d string) {
o.moduleDir = d
}
func (o *OverrideModuleBase) ModuleDir() string {
return o.moduleDir
}
func (o *OverrideModuleBase) getOverridingProperties() []interface{} { func (o *OverrideModuleBase) getOverridingProperties() []interface{} {
return o.overridingProperties return o.overridingProperties
} }
@@ -108,6 +123,7 @@ type OverridableModule interface {
override(ctx BaseModuleContext, o OverrideModule) override(ctx BaseModuleContext, o OverrideModule)
GetOverriddenBy() string GetOverriddenBy() string
GetOverriddenByModuleDir() string
setOverridesProperty(overridesProperties *[]string) setOverridesProperty(overridesProperties *[]string)
@@ -117,7 +133,8 @@ type OverridableModule interface {
} }
type overridableModuleProperties struct { type overridableModuleProperties struct {
OverriddenBy string `blueprint:"mutated"` OverriddenBy string `blueprint:"mutated"`
OverriddenByModuleDir string `blueprint:"mutated"`
} }
// Base module struct for overridable module types // Base module struct for overridable module types
@@ -196,6 +213,7 @@ func (b *OverridableModuleBase) override(ctx BaseModuleContext, o OverrideModule
*b.overridesProperty = append(*b.overridesProperty, ctx.ModuleName()) *b.overridesProperty = append(*b.overridesProperty, ctx.ModuleName())
} }
b.overridableModuleProperties.OverriddenBy = o.Name() b.overridableModuleProperties.OverriddenBy = o.Name()
b.overridableModuleProperties.OverriddenByModuleDir = o.ModuleDir()
} }
// GetOverriddenBy returns the name of the override module that has overridden this module. // GetOverriddenBy returns the name of the override module that has overridden this module.
@@ -206,6 +224,10 @@ func (b *OverridableModuleBase) GetOverriddenBy() string {
return b.overridableModuleProperties.OverriddenBy return b.overridableModuleProperties.OverriddenBy
} }
func (b *OverridableModuleBase) GetOverriddenByModuleDir() string {
return b.overridableModuleProperties.OverriddenByModuleDir
}
func (b *OverridableModuleBase) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) { func (b *OverridableModuleBase) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) {
} }
@@ -254,7 +276,9 @@ func overrideModuleDepsMutator(ctx BottomUpMutatorContext) {
}) })
baseModule := ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base)[0] baseModule := ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base)[0]
if o, ok := baseModule.(OverridableModule); ok { if o, ok := baseModule.(OverridableModule); ok {
o.addOverride(ctx.Module().(OverrideModule)) overrideModule := ctx.Module().(OverrideModule)
overrideModule.setModuleDir(ctx.ModuleDir())
o.addOverride(overrideModule)
} }
} }
} }
@@ -314,11 +338,35 @@ func replaceDepsOnOverridingModuleMutator(ctx BottomUpMutatorContext) {
// ModuleNameWithPossibleOverride returns the name of the OverrideModule that overrides the current // ModuleNameWithPossibleOverride returns the name of the OverrideModule that overrides the current
// variant of this OverridableModule, or ctx.ModuleName() if this module is not an OverridableModule // variant of this OverridableModule, or ctx.ModuleName() if this module is not an OverridableModule
// or if this variant is not overridden. // or if this variant is not overridden.
func ModuleNameWithPossibleOverride(ctx ModuleContext) string { func ModuleNameWithPossibleOverride(ctx BazelConversionContext) string {
if overridable, ok := ctx.Module().(OverridableModule); ok { if overridable, ok := ctx.Module().(OverridableModule); ok {
if o := overridable.GetOverriddenBy(); o != "" { if o := overridable.GetOverriddenBy(); o != "" {
return o return o
} }
} }
return ctx.ModuleName() return ctx.OtherModuleName(ctx.Module())
}
// ModuleDirWithPossibleOverride returns the dir of the OverrideModule that overrides the current
// variant of this OverridableModule, or ctx.ModuleName() if this module is not an OverridableModule
// or if this variant is not overridden.
func moduleDirWithPossibleOverride(ctx BazelConversionContext) string {
if overridable, ok := ctx.Module().(OverridableModule); ok {
if o := overridable.GetOverriddenByModuleDir(); o != "" {
return o
}
}
return ctx.OtherModuleDir(ctx.Module())
}
// MaybeBp2buildLabelOfOverridingModule returns the bazel label of the
// overriding module of an OverridableModule (e.g. override_apex label of a base
// apex), or the module's label itself if not overridden.
func MaybeBp2buildLabelOfOverridingModule(ctx BazelConversionContext) string {
moduleName := ModuleNameWithPossibleOverride(ctx)
moduleDir := moduleDirWithPossibleOverride(ctx)
if moduleDir == Bp2BuildTopLevel {
moduleDir = ""
}
return fmt.Sprintf("//%s:%s", moduleDir, moduleName)
} }

View File

@@ -1875,6 +1875,17 @@ func (a *apexBundle) QueueBazelCall(ctx android.BaseModuleContext) {
bazelCtx.QueueBazelRequest(a.GetBazelLabel(ctx, a), cquery.GetApexInfo, android.GetConfigKey(ctx)) bazelCtx.QueueBazelRequest(a.GetBazelLabel(ctx, a), cquery.GetApexInfo, android.GetConfigKey(ctx))
} }
// GetBazelLabel returns the bazel label of this apexBundle, or the label of the
// override_apex module overriding this apexBundle. An apexBundle can be
// overridden by different override_apex modules (e.g. Google or Go variants),
// which is handled by the overrides mutators.
func (a *apexBundle) GetBazelLabel(ctx android.BazelConversionPathContext, module blueprint.Module) string {
if _, ok := ctx.Module().(android.OverridableModule); ok {
return android.MaybeBp2buildLabelOfOverridingModule(ctx)
}
return a.BazelModuleBase.GetBazelLabel(ctx, a)
}
func (a *apexBundle) ProcessBazelQueryResponse(ctx android.ModuleContext) { func (a *apexBundle) ProcessBazelQueryResponse(ctx android.ModuleContext) {
if !a.commonBuildActions(ctx) { if !a.commonBuildActions(ctx) {
return return

View File

@@ -29,7 +29,6 @@ import (
"github.com/google/blueprint/proptools" "github.com/google/blueprint/proptools"
"android/soong/android" "android/soong/android"
"android/soong/bazel/cquery"
"android/soong/bpf" "android/soong/bpf"
"android/soong/cc" "android/soong/cc"
"android/soong/dexpreopt" "android/soong/dexpreopt"
@@ -9804,98 +9803,3 @@ func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
libcCoreVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared").Module() libcCoreVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared").Module()
android.AssertBoolEquals(t, "core variant should link against source libc", true, hasDep(libfooCoreVariant, libcCoreVariant)) android.AssertBoolEquals(t, "core variant should link against source libc", true, hasDep(libfooCoreVariant, libcCoreVariant))
} }
func TestApexImageInMixedBuilds(t *testing.T) {
bp := `
apex_key{
name: "foo_key",
}
apex {
name: "foo",
key: "foo_key",
updatable: true,
min_sdk_version: "31",
file_contexts: ":myapex-file_contexts",
bazel_module: { label: "//:foo" },
}`
outputBaseDir := "out/bazel"
result := android.GroupFixturePreparers(
prepareForApexTest,
android.FixtureModifyConfig(func(config android.Config) {
config.BazelContext = android.MockBazelContext{
OutputBaseDir: outputBaseDir,
LabelToApexInfo: map[string]cquery.ApexInfo{
"//:foo": cquery.ApexInfo{
SignedOutput: "signed_out.apex",
UnsignedOutput: "unsigned_out.apex",
BundleKeyInfo: []string{"public_key", "private_key"},
ContainerKeyInfo: []string{"container_cert", "container_private"},
SymbolsUsedByApex: "foo_using.txt",
JavaSymbolsUsedByApex: "foo_using.xml",
BundleFile: "apex_bundle.zip",
InstalledFiles: "installed-files.txt",
RequiresLibs: []string{"//path/c:c", "//path/d:d"},
// unused
PackageName: "pkg_name",
ProvidesLibs: []string{"a", "b"},
},
},
}
}),
).RunTestWithBp(t, bp)
m := result.ModuleForTests("foo", "android_common_foo_image").Module()
ab, ok := m.(*apexBundle)
if !ok {
t.Fatalf("Expected module to be an apexBundle, was not")
}
if w, g := "out/bazel/execroot/__main__/public_key", ab.publicKeyFile.String(); w != g {
t.Errorf("Expected public key %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/private_key", ab.privateKeyFile.String(); w != g {
t.Errorf("Expected private key %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/container_cert", ab.containerCertificateFile.String(); w != g {
t.Errorf("Expected public container key %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/container_private", ab.containerPrivateKeyFile.String(); w != g {
t.Errorf("Expected private container key %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/signed_out.apex", ab.outputFile.String(); w != g {
t.Errorf("Expected output file %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/foo_using.txt", ab.nativeApisUsedByModuleFile.String(); w != g {
t.Errorf("Expected output file %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/foo_using.xml", ab.javaApisUsedByModuleFile.String(); w != g {
t.Errorf("Expected output file %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/installed-files.txt", ab.installedFilesFile.String(); w != g {
t.Errorf("Expected installed-files.txt %q, got %q", w, g)
}
mkData := android.AndroidMkDataForTest(t, result.TestContext, m)
var builder strings.Builder
mkData.Custom(&builder, "foo", "BAZEL_TARGET_", "", mkData)
data := builder.String()
if w := "ALL_MODULES.$(my_register_name).BUNDLE := out/bazel/execroot/__main__/apex_bundle.zip"; !strings.Contains(data, w) {
t.Errorf("Expected %q in androidmk data, but did not find %q", w, data)
}
if w := "$(call dist-for-goals,checkbuild,out/bazel/execroot/__main__/installed-files.txt:foo-installed-files.txt)"; !strings.Contains(data, w) {
t.Errorf("Expected %q in androidmk data, but did not find %q", w, data)
}
if w := "LOCAL_REQUIRED_MODULES := c d"; !strings.Contains(data, w) {
t.Errorf("Expected %q in androidmk data, but did not find it in %q", w, data)
}
}

238
apex/bp2build_test.go Normal file
View File

@@ -0,0 +1,238 @@
// Copyright 2022 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apex
import (
"android/soong/android"
"android/soong/bazel/cquery"
"strings"
"testing"
)
func TestApexImageInMixedBuilds(t *testing.T) {
bp := `
apex_key{
name: "foo_key",
}
apex {
name: "foo",
key: "foo_key",
updatable: true,
min_sdk_version: "31",
file_contexts: ":myapex-file_contexts",
bazel_module: { label: "//:foo" },
}`
outputBaseDir := "out/bazel"
result := android.GroupFixturePreparers(
prepareForApexTest,
android.FixtureModifyConfig(func(config android.Config) {
config.BazelContext = android.MockBazelContext{
OutputBaseDir: outputBaseDir,
LabelToApexInfo: map[string]cquery.ApexInfo{
"//:foo": cquery.ApexInfo{
SignedOutput: "signed_out.apex",
UnsignedOutput: "unsigned_out.apex",
BundleKeyInfo: []string{"public_key", "private_key"},
ContainerKeyInfo: []string{"container_cert", "container_private"},
SymbolsUsedByApex: "foo_using.txt",
JavaSymbolsUsedByApex: "foo_using.xml",
BundleFile: "apex_bundle.zip",
InstalledFiles: "installed-files.txt",
RequiresLibs: []string{"//path/c:c", "//path/d:d"},
// unused
PackageName: "pkg_name",
ProvidesLibs: []string{"a", "b"},
},
},
}
}),
).RunTestWithBp(t, bp)
m := result.ModuleForTests("foo", "android_common_foo_image").Module()
ab, ok := m.(*apexBundle)
if !ok {
t.Fatalf("Expected module to be an apexBundle, was not")
}
if w, g := "out/bazel/execroot/__main__/public_key", ab.publicKeyFile.String(); w != g {
t.Errorf("Expected public key %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/private_key", ab.privateKeyFile.String(); w != g {
t.Errorf("Expected private key %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/container_cert", ab.containerCertificateFile.String(); w != g {
t.Errorf("Expected public container key %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/container_private", ab.containerPrivateKeyFile.String(); w != g {
t.Errorf("Expected private container key %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/signed_out.apex", ab.outputFile.String(); w != g {
t.Errorf("Expected output file %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/foo_using.txt", ab.nativeApisUsedByModuleFile.String(); w != g {
t.Errorf("Expected output file %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/foo_using.xml", ab.javaApisUsedByModuleFile.String(); w != g {
t.Errorf("Expected output file %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/installed-files.txt", ab.installedFilesFile.String(); w != g {
t.Errorf("Expected installed-files.txt %q, got %q", w, g)
}
mkData := android.AndroidMkDataForTest(t, result.TestContext, m)
var builder strings.Builder
mkData.Custom(&builder, "foo", "BAZEL_TARGET_", "", mkData)
data := builder.String()
if w := "ALL_MODULES.$(my_register_name).BUNDLE := out/bazel/execroot/__main__/apex_bundle.zip"; !strings.Contains(data, w) {
t.Errorf("Expected %q in androidmk data, but did not find %q", w, data)
}
if w := "$(call dist-for-goals,checkbuild,out/bazel/execroot/__main__/installed-files.txt:foo-installed-files.txt)"; !strings.Contains(data, w) {
t.Errorf("Expected %q in androidmk data, but did not find %q", w, data)
}
if w := "LOCAL_REQUIRED_MODULES := c d"; !strings.Contains(data, w) {
t.Errorf("Expected %q in androidmk data, but did not find it in %q", w, data)
}
}
func TestOverrideApexImageInMixedBuilds(t *testing.T) {
bp := `
apex_key{
name: "foo_key",
}
apex_key{
name: "override_foo_key",
}
apex {
name: "foo",
key: "foo_key",
updatable: true,
min_sdk_version: "31",
package_name: "pkg_name",
file_contexts: ":myapex-file_contexts",
bazel_module: { label: "//:foo" },
}
override_apex {
name: "override_foo",
key: "override_foo_key",
package_name: "override_pkg_name",
base: "foo",
bazel_module: { label: "//:override_foo" },
}
`
outputBaseDir := "out/bazel"
result := android.GroupFixturePreparers(
prepareForApexTest,
android.FixtureModifyConfig(func(config android.Config) {
config.BazelContext = android.MockBazelContext{
OutputBaseDir: outputBaseDir,
LabelToApexInfo: map[string]cquery.ApexInfo{
"//:foo": cquery.ApexInfo{
SignedOutput: "signed_out.apex",
UnsignedOutput: "unsigned_out.apex",
BundleKeyInfo: []string{"public_key", "private_key"},
ContainerKeyInfo: []string{"container_cert", "container_private"},
SymbolsUsedByApex: "foo_using.txt",
JavaSymbolsUsedByApex: "foo_using.xml",
BundleFile: "apex_bundle.zip",
InstalledFiles: "installed-files.txt",
RequiresLibs: []string{"//path/c:c", "//path/d:d"},
// unused
PackageName: "pkg_name",
ProvidesLibs: []string{"a", "b"},
},
"//:override_foo": cquery.ApexInfo{
SignedOutput: "override_signed_out.apex",
UnsignedOutput: "override_unsigned_out.apex",
BundleKeyInfo: []string{"override_public_key", "override_private_key"},
ContainerKeyInfo: []string{"override_container_cert", "override_container_private"},
SymbolsUsedByApex: "override_foo_using.txt",
JavaSymbolsUsedByApex: "override_foo_using.xml",
BundleFile: "override_apex_bundle.zip",
InstalledFiles: "override_installed-files.txt",
RequiresLibs: []string{"//path/c:c", "//path/d:d"},
// unused
PackageName: "override_pkg_name",
ProvidesLibs: []string{"a", "b"},
},
},
}
}),
).RunTestWithBp(t, bp)
m := result.ModuleForTests("foo", "android_common_override_foo_foo_image").Module()
ab, ok := m.(*apexBundle)
if !ok {
t.Fatalf("Expected module to be an apexBundle, was not")
}
if w, g := "out/bazel/execroot/__main__/override_public_key", ab.publicKeyFile.String(); w != g {
t.Errorf("Expected public key %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/override_private_key", ab.privateKeyFile.String(); w != g {
t.Errorf("Expected private key %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/override_container_cert", ab.containerCertificateFile.String(); w != g {
t.Errorf("Expected public container key %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/override_container_private", ab.containerPrivateKeyFile.String(); w != g {
t.Errorf("Expected private container key %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/override_signed_out.apex", ab.outputFile.String(); w != g {
t.Errorf("Expected output file %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/override_foo_using.txt", ab.nativeApisUsedByModuleFile.String(); w != g {
t.Errorf("Expected output file %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/override_foo_using.xml", ab.javaApisUsedByModuleFile.String(); w != g {
t.Errorf("Expected output file %q, got %q", w, g)
}
if w, g := "out/bazel/execroot/__main__/override_installed-files.txt", ab.installedFilesFile.String(); w != g {
t.Errorf("Expected installed-files.txt %q, got %q", w, g)
}
mkData := android.AndroidMkDataForTest(t, result.TestContext, m)
var builder strings.Builder
mkData.Custom(&builder, "override_foo", "BAZEL_TARGET_", "", mkData)
data := builder.String()
if w := "ALL_MODULES.$(my_register_name).BUNDLE := out/bazel/execroot/__main__/override_apex_bundle.zip"; !strings.Contains(data, w) {
t.Errorf("Expected %q in androidmk data, but did not find %q", w, data)
}
if w := "$(call dist-for-goals,checkbuild,out/bazel/execroot/__main__/override_installed-files.txt:override_foo-installed-files.txt)"; !strings.Contains(data, w) {
t.Errorf("Expected %q in androidmk data, but did not find %q", w, data)
}
if w := "LOCAL_REQUIRED_MODULES := c d"; !strings.Contains(data, w) {
t.Errorf("Expected %q in androidmk data, but did not find it in %q", w, data)
}
}