From 9b81d79ef6abb98019c9771bfb9fa83ce5956437 Mon Sep 17 00:00:00 2001 From: Trevor Radcliffe Date: Fri, 29 Sep 2023 15:22:52 +0000 Subject: [PATCH] Implement bp2build for Sysprop Java Bug: 297356813 Test: bp2build and inspect BUILD files Test: Conversion Unit Tests Change-Id: Ib70400eb91bca946df1d99d953d7a0e7e63fb7cf --- cc/Android.bp | 2 +- cc/gen.go | 12 +-- sysprop/Android.bp | 1 + sysprop/bp2build/Android.bp | 16 ++++ cc/sysprop.go => sysprop/bp2build/bp2build.go | 81 +++++++++++------ sysprop/sysprop_library.go | 32 +++++-- sysprop/sysprop_library_conversion_test.go | 87 ++++++++++++++++++- 7 files changed, 189 insertions(+), 42 deletions(-) create mode 100644 sysprop/bp2build/Android.bp rename cc/sysprop.go => sysprop/bp2build/bp2build.go (51%) diff --git a/cc/Android.bp b/cc/Android.bp index c32d85490..8fa0fbeb0 100644 --- a/cc/Android.bp +++ b/cc/Android.bp @@ -18,6 +18,7 @@ bootstrap_go_package { "soong-genrule", "soong-multitree", "soong-snapshot", + "soong-sysprop-bp2build", "soong-tradefed", ], srcs: [ @@ -49,7 +50,6 @@ bootstrap_go_package { "snapshot_utils.go", "stl.go", "strip.go", - "sysprop.go", "tidy.go", "util.go", "vendor_snapshot.go", diff --git a/cc/gen.go b/cc/gen.go index b15f16496..151f23d9e 100644 --- a/cc/gen.go +++ b/cc/gen.go @@ -20,6 +20,7 @@ import ( "android/soong/aidl_library" "android/soong/bazel" + "android/soong/sysprop/bp2build" "github.com/google/blueprint" @@ -240,12 +241,13 @@ func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Pa } func bp2buildCcSysprop(ctx android.Bp2buildMutatorContext, moduleName string, minSdkVersion *string, srcs bazel.LabelListAttribute) *bazel.LabelAttribute { - labels := SyspropLibraryLabels{ - SyspropLibraryLabel: moduleName + "_sysprop_library", - StaticLibraryLabel: moduleName + "_cc_sysprop_library_static", + labels := bp2build.SyspropLibraryLabels{ + SyspropLibraryLabel: moduleName + "_sysprop_library", + CcStaticLibraryLabel: moduleName + "_cc_sysprop_library_static", } - Bp2buildSysprop(ctx, labels, srcs, minSdkVersion) - return createLabelAttributeCorrespondingToSrcs(":"+labels.StaticLibraryLabel, srcs) + bp2build.Bp2buildBaseSyspropLibrary(ctx, labels.SyspropLibraryLabel, srcs) + bp2build.Bp2buildSyspropCc(ctx, labels, minSdkVersion) + return createLabelAttributeCorrespondingToSrcs(":"+labels.CcStaticLibraryLabel, srcs) } // Creates a LabelAttribute for a given label where the value is only set for diff --git a/sysprop/Android.bp b/sysprop/Android.bp index e5263fec4..7f5100091 100644 --- a/sysprop/Android.bp +++ b/sysprop/Android.bp @@ -12,6 +12,7 @@ bootstrap_go_package { "soong-bp2build", "soong-cc", "soong-java", + "soong-sysprop-bp2build", ], srcs: [ "sysprop_library.go", diff --git a/sysprop/bp2build/Android.bp b/sysprop/bp2build/Android.bp new file mode 100644 index 000000000..1b9eda83e --- /dev/null +++ b/sysprop/bp2build/Android.bp @@ -0,0 +1,16 @@ +package { + default_applicable_licenses: ["Android-Apache-2.0"], +} + +bootstrap_go_package { + name: "soong-sysprop-bp2build", + pkgPath: "android/soong/sysprop/bp2build", + deps: [ + "soong-android", + "soong-bazel", + ], + srcs: [ + "bp2build.go", + ], + pluginFor: ["soong_build"], +} diff --git a/cc/sysprop.go b/sysprop/bp2build/bp2build.go similarity index 51% rename from cc/sysprop.go rename to sysprop/bp2build/bp2build.go index be030046d..18991de60 100644 --- a/cc/sysprop.go +++ b/sysprop/bp2build/bp2build.go @@ -12,44 +12,50 @@ // See the License for the specific language governing permissions and // limitations under the License. -package cc +package bp2build import ( "android/soong/android" "android/soong/bazel" ) +type SyspropLibraryLabels struct { + SyspropLibraryLabel string + CcSharedLibraryLabel string + CcStaticLibraryLabel string + JavaLibraryLabel string +} + // TODO(b/240463568): Additional properties will be added for API validation type bazelSyspropLibraryAttributes struct { Srcs bazel.LabelListAttribute Tags bazel.StringListAttribute } +func Bp2buildBaseSyspropLibrary(ctx android.Bp2buildMutatorContext, name string, srcs bazel.LabelListAttribute) { + apexAvailableTags := android.ApexAvailableTagsWithoutTestApexes(ctx.(android.Bp2buildMutatorContext), ctx.Module()) + + ctx.CreateBazelTargetModule( + bazel.BazelTargetModuleProperties{ + Rule_class: "sysprop_library", + Bzl_load_location: "//build/bazel/rules/sysprop:sysprop_library.bzl", + }, + android.CommonAttributes{Name: name}, + &bazelSyspropLibraryAttributes{ + Srcs: srcs, + Tags: apexAvailableTags, + }, + ) +} + type bazelCcSyspropLibraryAttributes struct { Dep bazel.LabelAttribute Min_sdk_version *string Tags bazel.StringListAttribute } -type SyspropLibraryLabels struct { - SyspropLibraryLabel string - SharedLibraryLabel string - StaticLibraryLabel string -} - -func Bp2buildSysprop(ctx android.Bp2buildMutatorContext, labels SyspropLibraryLabels, srcs bazel.LabelListAttribute, minSdkVersion *string) { - apexAvailableTags := android.ApexAvailableTagsWithoutTestApexes(ctx, ctx.Module()) - ctx.CreateBazelTargetModule( - bazel.BazelTargetModuleProperties{ - Rule_class: "sysprop_library", - Bzl_load_location: "//build/bazel/rules/sysprop:sysprop_library.bzl", - }, - android.CommonAttributes{Name: labels.SyspropLibraryLabel}, - &bazelSyspropLibraryAttributes{ - Srcs: srcs, - Tags: apexAvailableTags, - }, - ) +func Bp2buildSyspropCc(ctx android.Bp2buildMutatorContext, labels SyspropLibraryLabels, minSdkVersion *string) { + apexAvailableTags := android.ApexAvailableTagsWithoutTestApexes(ctx.(android.Bp2buildMutatorContext), ctx.Module()) attrs := &bazelCcSyspropLibraryAttributes{ Dep: *bazel.MakeLabelAttribute(":" + labels.SyspropLibraryLabel), @@ -57,21 +63,44 @@ func Bp2buildSysprop(ctx android.Bp2buildMutatorContext, labels SyspropLibraryLa Tags: apexAvailableTags, } - if labels.SharedLibraryLabel != "" { + if labels.CcSharedLibraryLabel != "" { ctx.CreateBazelTargetModule( bazel.BazelTargetModuleProperties{ Rule_class: "cc_sysprop_library_shared", Bzl_load_location: "//build/bazel/rules/cc:cc_sysprop_library.bzl", }, - android.CommonAttributes{Name: labels.SharedLibraryLabel}, + android.CommonAttributes{Name: labels.CcSharedLibraryLabel}, attrs) } + if labels.CcStaticLibraryLabel != "" { + ctx.CreateBazelTargetModule( + bazel.BazelTargetModuleProperties{ + Rule_class: "cc_sysprop_library_static", + Bzl_load_location: "//build/bazel/rules/cc:cc_sysprop_library.bzl", + }, + android.CommonAttributes{Name: labels.CcStaticLibraryLabel}, + attrs) + } +} + +type bazelJavaLibraryAttributes struct { + Dep bazel.LabelAttribute + Min_sdk_version *string + Tags bazel.StringListAttribute +} + +func Bp2buildSyspropJava(ctx android.Bp2buildMutatorContext, labels SyspropLibraryLabels, minSdkVersion *string) { + apexAvailableTags := android.ApexAvailableTagsWithoutTestApexes(ctx.(android.Bp2buildMutatorContext), ctx.Module()) ctx.CreateBazelTargetModule( bazel.BazelTargetModuleProperties{ - Rule_class: "cc_sysprop_library_static", - Bzl_load_location: "//build/bazel/rules/cc:cc_sysprop_library.bzl", + Rule_class: "java_sysprop_library", + Bzl_load_location: "//build/bazel/rules/java:java_sysprop_library.bzl", }, - android.CommonAttributes{Name: labels.StaticLibraryLabel}, - attrs) + android.CommonAttributes{Name: labels.JavaLibraryLabel}, + &bazelJavaLibraryAttributes{ + Dep: *bazel.MakeLabelAttribute(":" + labels.SyspropLibraryLabel), + Min_sdk_version: minSdkVersion, + Tags: apexAvailableTags, + }) } diff --git a/sysprop/sysprop_library.go b/sysprop/sysprop_library.go index d16bf32f9..13cf68f7d 100644 --- a/sysprop/sysprop_library.go +++ b/sysprop/sysprop_library.go @@ -24,6 +24,8 @@ import ( "sync" "android/soong/bazel" + "android/soong/sysprop/bp2build" + "android/soong/ui/metrics/bp2build_metrics_proto" "github.com/google/blueprint" "github.com/google/blueprint/proptools" @@ -230,6 +232,10 @@ func (m *syspropLibrary) javaGenPublicStubName() string { return m.BaseModuleName() + "_java_gen_public" } +func (m *syspropLibrary) bp2buildJavaImplementationModuleName() string { + return m.BaseModuleName() + "_java_library" +} + func (m *syspropLibrary) BaseModuleName() string { return m.ModuleBase.Name() } @@ -431,6 +437,7 @@ type javaLibraryProperties struct { Min_sdk_version *string Bazel_module struct { Bp2build_available *bool + Label *string } } @@ -551,8 +558,10 @@ func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) { Min_sdk_version: m.properties.Java.Min_sdk_version, Bazel_module: struct { Bp2build_available *bool + Label *string }{ - Bp2build_available: proptools.BoolPtr(false), + Label: proptools.StringPtr( + fmt.Sprintf("//%s:%s", ctx.ModuleDir(), m.bp2buildJavaImplementationModuleName())), }, }) @@ -573,6 +582,7 @@ func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) { Stem: proptools.StringPtr(m.BaseModuleName()), Bazel_module: struct { Bp2build_available *bool + Label *string }{ Bp2build_available: proptools.BoolPtr(false), }, @@ -592,13 +602,17 @@ func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) { // TODO(b/240463568): Additional properties will be added for API validation func (m *syspropLibrary) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { - labels := cc.SyspropLibraryLabels{ - SyspropLibraryLabel: m.BaseModuleName(), - SharedLibraryLabel: m.CcImplementationModuleName(), - StaticLibraryLabel: cc.BazelLabelNameForStaticModule(m.CcImplementationModuleName()), + if m.Owner() != "Platform" { + ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNSUPPORTED, "Only sysprop libraries owned by platform are supported at this time") + return } - cc.Bp2buildSysprop(ctx, - labels, - bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Srcs)), - m.properties.Cpp.Min_sdk_version) + labels := bp2build.SyspropLibraryLabels{ + SyspropLibraryLabel: m.BaseModuleName(), + CcSharedLibraryLabel: m.CcImplementationModuleName(), + CcStaticLibraryLabel: cc.BazelLabelNameForStaticModule(m.CcImplementationModuleName()), + JavaLibraryLabel: m.bp2buildJavaImplementationModuleName(), + } + bp2build.Bp2buildBaseSyspropLibrary(ctx, labels.SyspropLibraryLabel, bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Srcs))) + bp2build.Bp2buildSyspropCc(ctx, labels, m.properties.Cpp.Min_sdk_version) + bp2build.Bp2buildSyspropJava(ctx, labels, m.properties.Java.Min_sdk_version) } diff --git a/sysprop/sysprop_library_conversion_test.go b/sysprop/sysprop_library_conversion_test.go index 89adf7d28..dabcc927b 100644 --- a/sysprop/sysprop_library_conversion_test.go +++ b/sysprop/sysprop_library_conversion_test.go @@ -58,13 +58,18 @@ sysprop_library { bp2build.AttrNameToString{ "dep": `":sysprop_foo"`, }), + bp2build.MakeBazelTargetNoRestrictions("java_sysprop_library", + "sysprop_foo_java_library", + bp2build.AttrNameToString{ + "dep": `":sysprop_foo"`, + }), }, }) } func TestSyspropLibraryCppMinSdkVersion(t *testing.T) { bp2build.RunBp2BuildTestCaseSimple(t, bp2build.Bp2buildTestCase{ - Description: "sysprop_library with min_sdk_version", + Description: "sysprop_library with cpp min_sdk_version", ModuleTypeUnderTest: "sysprop_library", ModuleTypeUnderTestFactory: syspropLibraryFactory, Filesystem: map[string]string{ @@ -105,6 +110,86 @@ sysprop_library { "dep": `":sysprop_foo"`, "min_sdk_version": `"5"`, }), + bp2build.MakeBazelTargetNoRestrictions("java_sysprop_library", + "sysprop_foo_java_library", + bp2build.AttrNameToString{ + "dep": `":sysprop_foo"`, + }), }, }) } + +func TestSyspropLibraryJavaMinSdkVersion(t *testing.T) { + bp2build.RunBp2BuildTestCaseSimple(t, bp2build.Bp2buildTestCase{ + Description: "sysprop_library with java min_sdk_version", + ModuleTypeUnderTest: "sysprop_library", + ModuleTypeUnderTestFactory: syspropLibraryFactory, + Filesystem: map[string]string{ + "foo.sysprop": "", + "bar.sysprop": "", + }, + Blueprint: ` +sysprop_library { + name: "sysprop_foo", + srcs: [ + "foo.sysprop", + "bar.sysprop", + ], + java: { + min_sdk_version: "5", + }, + property_owner: "Platform", +} +`, + ExpectedBazelTargets: []string{ + bp2build.MakeBazelTargetNoRestrictions("sysprop_library", + "sysprop_foo", + bp2build.AttrNameToString{ + "srcs": `[ + "foo.sysprop", + "bar.sysprop", + ]`, + }), + bp2build.MakeBazelTargetNoRestrictions("cc_sysprop_library_shared", + "libsysprop_foo", + bp2build.AttrNameToString{ + "dep": `":sysprop_foo"`, + }), + bp2build.MakeBazelTargetNoRestrictions("cc_sysprop_library_static", + "libsysprop_foo_bp2build_cc_library_static", + bp2build.AttrNameToString{ + "dep": `":sysprop_foo"`, + }), + bp2build.MakeBazelTargetNoRestrictions("java_sysprop_library", + "sysprop_foo_java_library", + bp2build.AttrNameToString{ + "dep": `":sysprop_foo"`, + "min_sdk_version": `"5"`, + }), + }, + }) +} + +func TestSyspropLibraryOwnerNotPlatformUnconvertible(t *testing.T) { + bp2build.RunBp2BuildTestCaseSimple(t, bp2build.Bp2buildTestCase{ + Description: "sysprop_library simple", + ModuleTypeUnderTest: "sysprop_library", + ModuleTypeUnderTestFactory: syspropLibraryFactory, + Filesystem: map[string]string{ + "foo.sysprop": "", + "bar.sysprop": "", + }, + Blueprint: ` +sysprop_library { + name: "sysprop_foo", + srcs: [ + "foo.sysprop", + "bar.sysprop", + ], + property_owner: "Vendor", + device_specific: true, +} +`, + ExpectedBazelTargets: []string{}, + }) +}