Change bp2build converter of module "package".

1) Use attribute name "default_package_metadata" instead of
   "default_applicable_licenses" in packages to better describe its use.
2) Add a filegroup "default_metadata_file" in packages to search for
   METADATA file in each package.
3) Include "default_metadata_file" in each package's
   "default_package_metadata" attribute.

Bug: 275472038
Test: CIs
Change-Id: I645c013c39e3190fd96c4a549d39a331aced16bd
This commit is contained in:
Wei Li 2023-05-05 01:07:15 -07:00
parent e3f0281b88
commit 2c9e8d6128
5 changed files with 94 additions and 7 deletions

View file

@ -76,6 +76,7 @@ func isFilegroupWithPattern(pattern *regexp.Regexp) bazel.LabelMapper {
// https://docs.bazel.build/versions/master/be/general.html#filegroup
type bazelFilegroupAttributes struct {
Srcs bazel.LabelListAttribute
Applicable_licenses bazel.LabelListAttribute
}
type bazelAidlLibraryAttributes struct {

View file

@ -15,6 +15,8 @@
package android
import (
"path/filepath"
"android/soong/bazel"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
@ -40,7 +42,7 @@ type packageProperties struct {
type bazelPackageAttributes struct {
Default_visibility []string
Default_applicable_licenses bazel.LabelListAttribute
Default_package_metadata bazel.LabelListAttribute
}
type packageModule struct {
@ -53,13 +55,32 @@ type packageModule struct {
var _ Bazelable = &packageModule{}
func (p *packageModule) ConvertWithBp2build(ctx TopDownMutatorContext) {
defaultPackageMetadata := bazel.MakeLabelListAttribute(BazelLabelForModuleDeps(ctx, p.properties.Default_applicable_licenses))
// If METADATA file exists in the package, add it to package(default_package_metadata=) using a
// filegroup(name="default_metadata_file") which can be accessed later on each module in Bazel
// using attribute "applicable_licenses".
// Attribute applicable_licenses of filegroup "default_metadata_file" has to be set to [],
// otherwise Bazel reports cyclic reference error.
if existed, _, _ := ctx.Config().fs.Exists(filepath.Join(ctx.ModuleDir(), "METADATA")); existed {
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{
Rule_class: "filegroup",
},
CommonAttributes{Name: "default_metadata_file"},
&bazelFilegroupAttributes{
Srcs: bazel.MakeLabelListAttribute(BazelLabelForModuleSrc(ctx, []string{"METADATA"})),
Applicable_licenses: bazel.LabelListAttribute{Value: bazel.LabelList{Includes: []bazel.Label{}}, EmitEmptyList: true},
})
defaultPackageMetadata.Value.Add(&bazel.Label{Label: ":default_metadata_file"})
}
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{
Rule_class: "package",
},
CommonAttributes{},
&bazelPackageAttributes{
Default_applicable_licenses: bazel.MakeLabelListAttribute(BazelLabelForModuleDeps(ctx, p.properties.Default_applicable_licenses)),
Default_package_metadata: defaultPackageMetadata,
// FIXME(asmundak): once b/221436821 is resolved
Default_visibility: []string{"//visibility:public"},
})

View file

@ -15,9 +15,10 @@
package bp2build
import (
"testing"
"android/soong/android"
"android/soong/genrule"
"testing"
)
func registerDependentModules(ctx android.RegistrationContext) {
@ -29,6 +30,7 @@ func TestPackage(t *testing.T) {
tests := []struct {
description string
modules string
fs map[string]string
expected []ExpectedRuleTarget
}{
{
@ -50,7 +52,7 @@ package {
"package",
"",
AttrNameToString{
"default_applicable_licenses": `[":my_license"]`,
"default_package_metadata": `[":my_license"]`,
"default_visibility": `["//visibility:public"]`,
},
android.HostAndDeviceDefault,
@ -67,6 +69,57 @@ package {
},
},
},
{
description: "package has METADATA file",
fs: map[string]string{
"METADATA": ``,
},
modules: `
license {
name: "my_license",
visibility: [":__subpackages__"],
license_kinds: ["SPDX-license-identifier-Apache-2.0"],
license_text: ["NOTICE"],
}
package {
default_applicable_licenses: ["my_license"],
}
`,
expected: []ExpectedRuleTarget{
{
"package",
"",
AttrNameToString{
"default_package_metadata": `[
":my_license",
":default_metadata_file",
]`,
"default_visibility": `["//visibility:public"]`,
},
android.HostAndDeviceDefault,
},
{
"android_license",
"my_license",
AttrNameToString{
"license_kinds": `["SPDX-license-identifier-Apache-2.0"]`,
"license_text": `"NOTICE"`,
"visibility": `[":__subpackages__"]`,
},
android.HostAndDeviceDefault,
},
{
"filegroup",
"default_metadata_file",
AttrNameToString{
"applicable_licenses": `[]`,
"srcs": `["METADATA"]`,
},
android.HostAndDeviceDefault,
},
},
},
}
for _, test := range tests {
expected := make([]string, 0, len(test.expected))
@ -80,6 +133,7 @@ package {
ModuleTypeUnderTestFactory: android.PackageFactory,
Blueprint: test.modules,
ExpectedBazelTargets: expected,
Filesystem: test.fs,
})
}
}

View file

@ -87,6 +87,8 @@ func NewSourceFinder(ctx Context, config Config) (f *finder.Finder) {
"TEST_MAPPING",
// Bazel top-level file to mark a directory as a Bazel workspace.
"WORKSPACE",
// METADATA file of packages
"METADATA",
},
// Bazel Starlark configuration files and all .mk files for product/board configuration.
IncludeSuffixes: []string{".bzl", ".mk"},
@ -189,6 +191,13 @@ func FindSources(ctx Context, config Config, f *finder.Finder) {
ctx.Fatalf("Could not find OWNERS: %v", err)
}
// Recursively look for all METADATA files.
metadataFiles := f.FindNamedAt(".", "METADATA")
err = dumpListToFile(ctx, config, metadataFiles, filepath.Join(dumpDir, "METADATA.list"))
if err != nil {
ctx.Fatalf("Could not find METADATA: %v", err)
}
// Recursively look for all TEST_MAPPING files.
testMappings := f.FindNamedAt(".", "TEST_MAPPING")
err = dumpListToFile(ctx, config, testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list"))

View file

@ -400,6 +400,8 @@ func bootstrapBlueprint(ctx Context, config Config) {
pbi.Inputs = append(pbi.Inputs,
config.Bp2BuildFilesMarkerFile(),
filepath.Join(config.FileListDir(), "bazel.list"))
case bp2buildFilesTag:
pbi.Inputs = append(pbi.Inputs, filepath.Join(config.FileListDir(), "METADATA.list"))
}
invocations = append(invocations, pbi)
}