Implement bp2build converter for rust_library

Test: go test
Bug: 297294749
Change-Id: I5400fe2c0fe2097b7a5810c736fbd1de4f35c6f7
This commit is contained in:
Vinh Tran
2023-08-24 11:10:01 -04:00
parent 4e6c42d417
commit bcb5f57eed
4 changed files with 185 additions and 0 deletions

View File

@@ -881,6 +881,20 @@ var (
// 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",

View File

@@ -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),
},
},
)
}

View File

@@ -20,7 +20,10 @@ import (
"strings"
"android/soong/android"
"android/soong/bazel"
"android/soong/cc"
"github.com/google/blueprint/proptools"
)
var (
@@ -398,6 +401,8 @@ func (library *libraryDecorator) BuildOnlyShared() {
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 @@ func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext,
// 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,
)
}

View File

@@ -16,6 +16,7 @@ package rust
import (
"android/soong/bloaty"
"android/soong/ui/metrics/bp2build_metrics_proto"
"fmt"
"strings"
@@ -169,6 +170,8 @@ type Module struct {
apexSdkVersion android.ApiLevel
transitiveAndroidMkSharedLibs *android.DepSet[string]
android.BazelModuleBase
}
func (mod *Module) Header() bool {
@@ -1841,6 +1844,14 @@ func (c *Module) Partition() string {
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