Implement bp2build converter for rust_library
Test: go test
Bug: 297294749
Change-Id: I5400fe2c0fe2097b7a5810c736fbd1de4f35c6f7
diff --git a/android/allowlists/allowlists.go b/android/allowlists/allowlists.go
index d7fbb62..4f571d2 100644
--- a/android/allowlists/allowlists.go
+++ b/android/allowlists/allowlists.go
@@ -881,6 +881,20 @@
// the "prebuilt_" prefix to the name, so that it's differentiable from
// the source versions within Soong's module graph.
Bp2buildModuleDoNotConvertList = []string{
+ // rust modules that have cc deps
+ "liblogger",
+ "libbssl_ffi",
+ "libbssl_ffi_nostd",
+ "pull_rust",
+ "libstatslog_rust",
+ "libstatslog_rust_header",
+ "libflatbuffers",
+ "liblog_event_list",
+ "libminijail_rust",
+ "libminijail_sys",
+ "libfsverity_rs",
+ "libtombstoned_client_rust",
+
// TODO(b/263326760): Failed already.
"minijail_compiler_unittest",
"minijail_parser_unittest",
diff --git a/bp2build/rust_library_conversion_test.go b/bp2build/rust_library_conversion_test.go
new file mode 100644
index 0000000..296f57e
--- /dev/null
+++ b/bp2build/rust_library_conversion_test.go
@@ -0,0 +1,66 @@
+package bp2build
+
+import (
+ "android/soong/android"
+ "android/soong/rust"
+ "testing"
+)
+
+func runRustLibraryTestCase(t *testing.T, tc Bp2buildTestCase) {
+ t.Helper()
+ RunBp2BuildTestCase(t, registerRustLibraryModuleTypes, tc)
+}
+
+func registerRustLibraryModuleTypes(ctx android.RegistrationContext) {
+ ctx.RegisterModuleType("rust_library", rust.RustLibraryFactory)
+ ctx.RegisterModuleType("rust_library_host", rust.RustLibraryHostFactory)
+}
+
+func TestRustLibrary(t *testing.T) {
+ expectedAttrs := AttrNameToString{
+ "crate_name": `"foo"`,
+ "srcs": `[
+ "src/helper.rs",
+ "src/lib.rs",
+ ]`,
+ "crate_features": `["bah-enabled"]`,
+ "edition": `"2021"`,
+ "rustc_flags": `["--cfg=baz"]`,
+ }
+
+ runRustLibraryTestCase(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_library {
+ name: "libfoo",
+ crate_name: "foo",
+ host_supported: true,
+ srcs: ["src/lib.rs"],
+ edition: "2021",
+ features: ["bah-enabled"],
+ cfgs: ["baz"],
+ bazel_module: { bp2build_available: true },
+}
+rust_library_host {
+ name: "libfoo_host",
+ crate_name: "foo",
+ srcs: ["src/lib.rs"],
+ edition: "2021",
+ features: ["bah-enabled"],
+ cfgs: ["baz"],
+ bazel_module: { bp2build_available: true },
+}
+`,
+ },
+ ExpectedBazelTargets: []string{
+ // TODO(b/290790800): Remove the restriction when rust toolchain for android is implemented
+ makeBazelTargetHostOrDevice("rust_library", "libfoo", expectedAttrs, android.HostSupported),
+ makeBazelTargetHostOrDevice("rust_library", "libfoo_host", expectedAttrs, android.HostSupported),
+ },
+ },
+ )
+}
diff --git a/rust/library.go b/rust/library.go
index 419fcfc..da386b3 100644
--- a/rust/library.go
+++ b/rust/library.go
@@ -20,7 +20,10 @@
"strings"
"android/soong/android"
+ "android/soong/bazel"
"android/soong/cc"
+
+ "github.com/google/blueprint/proptools"
)
var (
@@ -398,6 +401,8 @@
func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
module := newModule(hod, android.MultilibBoth)
+ android.InitBazelModule(module)
+
library := &libraryDecorator{
MutatedProperties: LibraryMutatedProperties{
BuildDylib: false,
@@ -793,3 +798,92 @@
// TODO(185577950): If support for generated headers is added, they need to be collected here as well.
l.collectedSnapshotHeaders = ret
}
+
+type rustLibraryAttributes struct {
+ Srcs bazel.LabelListAttribute
+ Compile_data bazel.LabelListAttribute
+ Crate_name bazel.StringAttribute
+ Edition bazel.StringAttribute
+ Crate_features bazel.StringListAttribute
+ Deps bazel.LabelListAttribute
+ Rustc_flags bazel.StringListAttribute
+}
+
+func libraryBp2build(ctx android.TopDownMutatorContext, m *Module) {
+ lib := m.compiler.(*libraryDecorator)
+
+ var srcs bazel.LabelList
+ var compileData bazel.LabelList
+ var rustcFLags []string
+
+ // This is a workaround by assuming the conventions that rust crate repos are structured
+ // while waiting for the sandboxing work to complete.
+ // TODO: When crate_root prop is set which enforces inputs sandboxing,
+ // always use `srcs` and `compile_data` props to generate `srcs` and `compile_data` attributes
+ // instead of using globs.
+ if lib.baseCompiler.Properties.Srcs[0] == "src/lib.rs" {
+ srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
+ compileData = android.BazelLabelForModuleSrc(
+ ctx,
+ []string{
+ "src/**/*.proto",
+ "examples/**/*.rs",
+ "**/*.md",
+ },
+ )
+ } else {
+ srcs = android.BazelLabelForModuleSrc(ctx, lib.baseCompiler.Properties.Srcs)
+ }
+
+ for _, cfg := range lib.baseCompiler.Properties.Cfgs {
+ rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg))
+ }
+
+ deps := android.BazelLabelForModuleDeps(ctx, append(
+ lib.baseCompiler.Properties.Rustlibs,
+ lib.baseCompiler.Properties.Rlibs...,
+ ))
+
+ attrs := &rustLibraryAttributes{
+ Srcs: bazel.MakeLabelListAttribute(
+ srcs,
+ ),
+ Compile_data: bazel.MakeLabelListAttribute(
+ compileData,
+ ),
+ Crate_name: bazel.StringAttribute{
+ Value: &lib.baseCompiler.Properties.Crate_name,
+ },
+ Edition: bazel.StringAttribute{
+ Value: lib.baseCompiler.Properties.Edition,
+ },
+ Crate_features: bazel.StringListAttribute{
+ Value: lib.baseCompiler.Properties.Features,
+ },
+ Deps: bazel.MakeLabelListAttribute(
+ deps,
+ ),
+ Rustc_flags: bazel.StringListAttribute{
+ Value: append(
+ rustcFLags,
+ lib.baseCompiler.Properties.Flags...,
+ ),
+ },
+ }
+
+ // TODO(b/290790800): Remove the restriction when rust toolchain for android is implemented
+ var restriction bazel.BoolAttribute
+ restriction.SetSelectValue(bazel.OsConfigurationAxis, "android", proptools.BoolPtr(false))
+
+ ctx.CreateBazelTargetModuleWithRestrictions(
+ bazel.BazelTargetModuleProperties{
+ Rule_class: "rust_library",
+ Bzl_load_location: "@rules_rust//rust:defs.bzl",
+ },
+ android.CommonAttributes{
+ Name: m.Name(),
+ },
+ attrs,
+ restriction,
+ )
+}
diff --git a/rust/rust.go b/rust/rust.go
index 689ff38..edd04ac 100644
--- a/rust/rust.go
+++ b/rust/rust.go
@@ -16,6 +16,7 @@
import (
"android/soong/bloaty"
+ "android/soong/ui/metrics/bp2build_metrics_proto"
"fmt"
"strings"
@@ -169,6 +170,8 @@
apexSdkVersion android.ApiLevel
transitiveAndroidMkSharedLibs *android.DepSet[string]
+
+ android.BazelModuleBase
}
func (mod *Module) Header() bool {
@@ -1841,6 +1844,14 @@
return ""
}
+func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
+ if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
+ libraryBp2build(ctx, m)
+ } else {
+ ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
+ }
+}
+
var Bool = proptools.Bool
var BoolDefault = proptools.BoolDefault
var String = proptools.String