summaryrefslogtreecommitdiff
path: root/rust/library.go
diff options
context:
space:
mode:
author Vinh Tran <vinhdaitran@google.com> 2023-08-24 11:10:01 -0400
committer Vinh Tran <vinhdaitran@google.com> 2023-08-25 17:42:13 -0400
commitbcb5f57eedf36b1ac20a7c4102085ae38ad807e9 (patch)
tree49f0821bdae8415a26260ec52ea739741b93c3c1 /rust/library.go
parent4e6c42d417c41f91f60333dad0974d7bfb7f5ae2 (diff)
Implement bp2build converter for rust_library
Test: go test Bug: 297294749 Change-Id: I5400fe2c0fe2097b7a5810c736fbd1de4f35c6f7
Diffstat (limited to 'rust/library.go')
-rw-r--r--rust/library.go94
1 files changed, 94 insertions, 0 deletions
diff --git a/rust/library.go b/rust/library.go
index 419fcfca2..da386b352 100644
--- a/rust/library.go
+++ b/rust/library.go
@@ -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,
+ )
+}