Merge "Change bp2build converter of module "package"."

This commit is contained in:
Treehugger Robot 2023-05-10 17:38:19 +00:00 committed by Gerrit Code Review
commit f47760461c
5 changed files with 94 additions and 7 deletions

View file

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

View file

@ -15,6 +15,8 @@
package android package android
import ( import (
"path/filepath"
"android/soong/bazel" "android/soong/bazel"
"github.com/google/blueprint" "github.com/google/blueprint"
"github.com/google/blueprint/proptools" "github.com/google/blueprint/proptools"
@ -39,8 +41,8 @@ type packageProperties struct {
} }
type bazelPackageAttributes struct { type bazelPackageAttributes struct {
Default_visibility []string Default_visibility []string
Default_applicable_licenses bazel.LabelListAttribute Default_package_metadata bazel.LabelListAttribute
} }
type packageModule struct { type packageModule struct {
@ -53,13 +55,32 @@ type packageModule struct {
var _ Bazelable = &packageModule{} var _ Bazelable = &packageModule{}
func (p *packageModule) ConvertWithBp2build(ctx TopDownMutatorContext) { 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( ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{ bazel.BazelTargetModuleProperties{
Rule_class: "package", Rule_class: "package",
}, },
CommonAttributes{}, CommonAttributes{},
&bazelPackageAttributes{ &bazelPackageAttributes{
Default_applicable_licenses: bazel.MakeLabelListAttribute(BazelLabelForModuleDeps(ctx, p.properties.Default_applicable_licenses)), Default_package_metadata: defaultPackageMetadata,
// FIXME(asmundak): once b/221436821 is resolved // FIXME(asmundak): once b/221436821 is resolved
Default_visibility: []string{"//visibility:public"}, Default_visibility: []string{"//visibility:public"},
}) })

View file

@ -15,9 +15,10 @@
package bp2build package bp2build
import ( import (
"testing"
"android/soong/android" "android/soong/android"
"android/soong/genrule" "android/soong/genrule"
"testing"
) )
func registerDependentModules(ctx android.RegistrationContext) { func registerDependentModules(ctx android.RegistrationContext) {
@ -29,6 +30,7 @@ func TestPackage(t *testing.T) {
tests := []struct { tests := []struct {
description string description string
modules string modules string
fs map[string]string
expected []ExpectedRuleTarget expected []ExpectedRuleTarget
}{ }{
{ {
@ -50,8 +52,8 @@ package {
"package", "package",
"", "",
AttrNameToString{ AttrNameToString{
"default_applicable_licenses": `[":my_license"]`, "default_package_metadata": `[":my_license"]`,
"default_visibility": `["//visibility:public"]`, "default_visibility": `["//visibility:public"]`,
}, },
android.HostAndDeviceDefault, 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 { for _, test := range tests {
expected := make([]string, 0, len(test.expected)) expected := make([]string, 0, len(test.expected))
@ -80,6 +133,7 @@ package {
ModuleTypeUnderTestFactory: android.PackageFactory, ModuleTypeUnderTestFactory: android.PackageFactory,
Blueprint: test.modules, Blueprint: test.modules,
ExpectedBazelTargets: expected, ExpectedBazelTargets: expected,
Filesystem: test.fs,
}) })
} }
} }

View file

@ -87,6 +87,8 @@ func NewSourceFinder(ctx Context, config Config) (f *finder.Finder) {
"TEST_MAPPING", "TEST_MAPPING",
// Bazel top-level file to mark a directory as a Bazel workspace. // Bazel top-level file to mark a directory as a Bazel workspace.
"WORKSPACE", "WORKSPACE",
// METADATA file of packages
"METADATA",
}, },
// Bazel Starlark configuration files and all .mk files for product/board configuration. // Bazel Starlark configuration files and all .mk files for product/board configuration.
IncludeSuffixes: []string{".bzl", ".mk"}, 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) 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. // Recursively look for all TEST_MAPPING files.
testMappings := f.FindNamedAt(".", "TEST_MAPPING") testMappings := f.FindNamedAt(".", "TEST_MAPPING")
err = dumpListToFile(ctx, config, testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list")) 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, pbi.Inputs = append(pbi.Inputs,
config.Bp2BuildFilesMarkerFile(), config.Bp2BuildFilesMarkerFile(),
filepath.Join(config.FileListDir(), "bazel.list")) filepath.Join(config.FileListDir(), "bazel.list"))
case bp2buildFilesTag:
pbi.Inputs = append(pbi.Inputs, filepath.Join(config.FileListDir(), "METADATA.list"))
} }
invocations = append(invocations, pbi) invocations = append(invocations, pbi)
} }