From 47faaad2544ad90f8faf4f7d999c4e85fd8c1c1b Mon Sep 17 00:00:00 2001 From: Vinh Tran Date: Mon, 25 Sep 2023 14:47:19 -0400 Subject: [PATCH] Add bp2build for rust_protobuf module We only supported bp2build for rust_protobuf_host because the non-host toolchain for rust is not checked in yet. aosp/2759750 changed a converted module from rust_protobuf_host to rust_protobuf and broke CI. This CL adds bp2build support for rust_protobuf but makes the target incompatible with android os for now untilnon-host rust toolchain is checked in. Bug: 301956497 Test: b build //build/make/tools/aconfig:all --config=android Change-Id: I739896c79f32674000c2603e394f16860a6fc57d --- bp2build/rust_protobuf_conversion_test.go | 31 ++++++++++++++++++++++- rust/protobuf.go | 10 ++++++-- rust/rust.go | 2 +- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/bp2build/rust_protobuf_conversion_test.go b/bp2build/rust_protobuf_conversion_test.go index cf256aadc..bd4f54b41 100644 --- a/bp2build/rust_protobuf_conversion_test.go +++ b/bp2build/rust_protobuf_conversion_test.go @@ -27,7 +27,7 @@ func runRustProtobufTestCase(t *testing.T, tc Bp2buildTestCase) { func registerRustProtobufModuleTypes(ctx android.RegistrationContext) { ctx.RegisterModuleType("rust_protobuf_host", rust.RustProtobufHostFactory) - + ctx.RegisterModuleType("rust_protobuf", rust.RustProtobufHostFactory) } func TestRustProtobufHostTestCase(t *testing.T) { @@ -58,3 +58,32 @@ rust_protobuf_host { }, ) } + +func TestRustProtobufTestCase(t *testing.T) { + runRustProtobufTestCase(t, Bp2buildTestCase{ + Dir: "external/rust/crates/foo", + Blueprint: "", + Filesystem: map[string]string{ + "external/rust/crates/foo/src/lib.rs": "", + "external/rust/crates/foo/src/helper.rs": "", + "external/rust/crates/foo/Android.bp": ` +rust_protobuf { + name: "libfoo", + crate_name: "foo", + protos: ["src/foo.proto"], + bazel_module: { bp2build_available: true }, +} +`, + }, + ExpectedBazelTargets: []string{ + makeBazelTargetHostOrDevice("proto_library", "libfoo_proto", AttrNameToString{ + "srcs": `["src/foo.proto"]`, + }, android.HostSupported), + makeBazelTargetHostOrDevice("rust_proto_library", "libfoo", AttrNameToString{ + "crate_name": `"foo"`, + "deps": `[":libfoo_proto"]`, + }, android.HostSupported), + }, + }, + ) +} diff --git a/rust/protobuf.go b/rust/protobuf.go index c8d2bdaeb..c3aa8dc64 100644 --- a/rust/protobuf.go +++ b/rust/protobuf.go @@ -305,7 +305,11 @@ func protoLibraryBp2build(ctx android.Bp2buildMutatorContext, m *Module) { }, } - ctx.CreateBazelTargetModule( + // TODO(b/295918553): Remove androidRestriction after rust toolchain for android is checked in. + var androidRestriction bazel.BoolAttribute + androidRestriction.SetSelectValue(bazel.OsConfigurationAxis, "android", proptools.BoolPtr(false)) + + ctx.CreateBazelTargetModuleWithRestrictions( bazel.BazelTargetModuleProperties{ Rule_class: "proto_library", }, @@ -317,9 +321,10 @@ func protoLibraryBp2build(ctx android.Bp2buildMutatorContext, m *Module) { android.BazelLabelForModuleSrc(ctx, protoFiles), ), }, + androidRestriction, ) - ctx.CreateBazelTargetModule( + ctx.CreateBazelTargetModuleWithRestrictions( bazel.BazelTargetModuleProperties{ Rule_class: "rust_proto_library", Bzl_load_location: "@rules_rust//proto/protobuf:defs.bzl", @@ -333,5 +338,6 @@ func protoLibraryBp2build(ctx android.Bp2buildMutatorContext, m *Module) { }, Deps: protoDeps, }, + androidRestriction, ) } diff --git a/rust/rust.go b/rust/rust.go index 3b2448493..250e39857 100644 --- a/rust/rust.go +++ b/rust/rust.go @@ -1853,7 +1853,7 @@ func (m *Module) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { procMacroBp2build(ctx, m) } else if ctx.ModuleType() == "rust_binary_host" { binaryBp2build(ctx, m) - } else if ctx.ModuleType() == "rust_protobuf_host" { + } else if ctx.ModuleType() == "rust_protobuf_host" || ctx.ModuleType() == "rust_protobuf" { protoLibraryBp2build(ctx, m) } else { ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")