Merge changes from topics "build-aconfig", "build-libprotobuf-for-host-only" into main am: 54a5905803
Original change: https://android-review.googlesource.com/c/platform/build/soong/+/2726675 Change-Id: Ib0f42a43a92e2b490c5a8d3ed46210d55235b0c0 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -488,6 +488,55 @@ var (
|
|||||||
}
|
}
|
||||||
|
|
||||||
Bp2buildModuleAlwaysConvertList = []string{
|
Bp2buildModuleAlwaysConvertList = []string{
|
||||||
|
// aconfig
|
||||||
|
"libonce_cell",
|
||||||
|
"libanyhow",
|
||||||
|
"libunicode_segmentation",
|
||||||
|
"libmemchr",
|
||||||
|
"libbitflags-1.3.2",
|
||||||
|
"libryu",
|
||||||
|
"libitoa",
|
||||||
|
"libos_str_bytes",
|
||||||
|
"libheck",
|
||||||
|
"libclap_lex",
|
||||||
|
"libsyn",
|
||||||
|
"libquote",
|
||||||
|
"libunicode_ident",
|
||||||
|
"libproc_macro2",
|
||||||
|
"libthiserror_impl",
|
||||||
|
"libserde_derive",
|
||||||
|
"libclap_derive",
|
||||||
|
"libthiserror",
|
||||||
|
"libserde",
|
||||||
|
"libclap",
|
||||||
|
"libbytes",
|
||||||
|
"libprotobuf_support",
|
||||||
|
"libtinytemplate",
|
||||||
|
"libserde_json",
|
||||||
|
"libprotobuf",
|
||||||
|
|
||||||
|
"protoc-gen-rust",
|
||||||
|
"libprotobuf_codegen",
|
||||||
|
"libprotobuf_parse",
|
||||||
|
"libregex",
|
||||||
|
"libtempfile",
|
||||||
|
"libwhich",
|
||||||
|
"libregex_syntax",
|
||||||
|
"libfastrand",
|
||||||
|
"libeither",
|
||||||
|
"libaho_corasick",
|
||||||
|
"liblibc",
|
||||||
|
"libcfg_if",
|
||||||
|
"liblog_rust",
|
||||||
|
"libgetrandom",
|
||||||
|
"libremove_dir_all",
|
||||||
|
"libahash",
|
||||||
|
"libhashbrown",
|
||||||
|
"libindexmap",
|
||||||
|
"libaconfig_protos",
|
||||||
|
"libpaste",
|
||||||
|
"aconfig",
|
||||||
|
|
||||||
// ext
|
// ext
|
||||||
"tagsoup",
|
"tagsoup",
|
||||||
|
|
||||||
@@ -881,6 +930,20 @@ var (
|
|||||||
// the "prebuilt_" prefix to the name, so that it's differentiable from
|
// the "prebuilt_" prefix to the name, so that it's differentiable from
|
||||||
// the source versions within Soong's module graph.
|
// the source versions within Soong's module graph.
|
||||||
Bp2buildModuleDoNotConvertList = []string{
|
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.
|
// TODO(b/263326760): Failed already.
|
||||||
"minijail_compiler_unittest",
|
"minijail_compiler_unittest",
|
||||||
"minijail_parser_unittest",
|
"minijail_parser_unittest",
|
||||||
|
74
bp2build/rust_binary_conversion_test.go
Normal file
74
bp2build/rust_binary_conversion_test.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package bp2build
|
||||||
|
|
||||||
|
import (
|
||||||
|
"android/soong/android"
|
||||||
|
"android/soong/rust"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func runRustBinaryTestCase(t *testing.T, tc Bp2buildTestCase) {
|
||||||
|
t.Helper()
|
||||||
|
RunBp2BuildTestCase(t, registerRustBinaryModuleTypes, tc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func registerRustBinaryModuleTypes(ctx android.RegistrationContext) {
|
||||||
|
ctx.RegisterModuleType("rust_binary_host", rust.RustBinaryHostFactory)
|
||||||
|
ctx.RegisterModuleType("rust_library_host", rust.RustLibraryHostFactory)
|
||||||
|
ctx.RegisterModuleType("rust_proc_macro", rust.ProcMacroFactory)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRustBinaryHost(t *testing.T) {
|
||||||
|
runRustBinaryTestCase(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_binary_host {
|
||||||
|
name: "libfoo",
|
||||||
|
crate_name: "foo",
|
||||||
|
srcs: ["src/main.rs"],
|
||||||
|
edition: "2021",
|
||||||
|
features: ["bah-enabled"],
|
||||||
|
cfgs: ["baz"],
|
||||||
|
rustlibs: ["libbar"],
|
||||||
|
proc_macros: ["libbah"],
|
||||||
|
bazel_module: { bp2build_available: true },
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
"external/rust/crates/bar/Android.bp": `
|
||||||
|
rust_library_host {
|
||||||
|
name: "libbar",
|
||||||
|
crate_name: "bar",
|
||||||
|
srcs: ["src/lib.rs"],
|
||||||
|
bazel_module: { bp2build_available: true },
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
"external/rust/crates/bah/Android.bp": `
|
||||||
|
rust_proc_macro {
|
||||||
|
name: "libbah",
|
||||||
|
crate_name: "bah",
|
||||||
|
srcs: ["src/lib.rs"],
|
||||||
|
bazel_module: { bp2build_available: true },
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
ExpectedBazelTargets: []string{
|
||||||
|
makeBazelTargetHostOrDevice("rust_binary", "libfoo", AttrNameToString{
|
||||||
|
"crate_name": `"foo"`,
|
||||||
|
"srcs": `[
|
||||||
|
"src/helper.rs",
|
||||||
|
"src/lib.rs",
|
||||||
|
]`,
|
||||||
|
"deps": `["//external/rust/crates/bar:libbar"]`,
|
||||||
|
"proc_macro_deps": `["//external/rust/crates/bah:libbah"]`,
|
||||||
|
"edition": `"2021"`,
|
||||||
|
"crate_features": `["bah-enabled"]`,
|
||||||
|
"rustc_flags": `["--cfg=baz"]`,
|
||||||
|
}, android.HostSupported),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
96
bp2build/rust_library_conversion_test.go
Normal file
96
bp2build/rust_library_conversion_test.go
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
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 TestLibProtobuf(t *testing.T) {
|
||||||
|
runRustLibraryTestCase(t, Bp2buildTestCase{
|
||||||
|
Dir: "external/rust/crates/foo",
|
||||||
|
Blueprint: "",
|
||||||
|
Filesystem: map[string]string{
|
||||||
|
"external/rust/crates/foo/src/lib.rs": "",
|
||||||
|
"external/rust/crates/foo/Android.bp": `
|
||||||
|
rust_library_host {
|
||||||
|
name: "libprotobuf",
|
||||||
|
crate_name: "protobuf",
|
||||||
|
srcs: ["src/lib.rs"],
|
||||||
|
bazel_module: { bp2build_available: true },
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
ExpectedBazelTargets: []string{
|
||||||
|
// TODO(b/290790800): Remove the restriction when rust toolchain for android is implemented
|
||||||
|
makeBazelTargetHostOrDevice("rust_library", "libprotobuf", AttrNameToString{
|
||||||
|
"crate_name": `"protobuf"`,
|
||||||
|
"srcs": `["src/lib.rs"]`,
|
||||||
|
"deps": `[":libprotobuf_build_script"]`,
|
||||||
|
}, android.HostSupported),
|
||||||
|
makeBazelTargetHostOrDevice("cargo_build_script", "libprotobuf_build_script", AttrNameToString{
|
||||||
|
"srcs": `["build.rs"]`,
|
||||||
|
}, android.HostSupported),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
62
bp2build/rust_proc_macro_conversion_test.go
Normal file
62
bp2build/rust_proc_macro_conversion_test.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package bp2build
|
||||||
|
|
||||||
|
import (
|
||||||
|
"android/soong/android"
|
||||||
|
"android/soong/rust"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func rustRustProcMacroTestCase(t *testing.T, tc Bp2buildTestCase) {
|
||||||
|
t.Helper()
|
||||||
|
RunBp2BuildTestCase(t, registerRustProcMacroModuleTypes, tc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func registerRustProcMacroModuleTypes(ctx android.RegistrationContext) {
|
||||||
|
ctx.RegisterModuleType("rust_library_host", rust.RustLibraryHostFactory)
|
||||||
|
ctx.RegisterModuleType("rust_proc_macro", rust.ProcMacroFactory)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRustProcMacroLibrary(t *testing.T) {
|
||||||
|
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_proc_macro {
|
||||||
|
name: "libfoo",
|
||||||
|
crate_name: "foo",
|
||||||
|
srcs: ["src/lib.rs"],
|
||||||
|
edition: "2021",
|
||||||
|
features: ["bah-enabled"],
|
||||||
|
cfgs: ["baz"],
|
||||||
|
rustlibs: ["libbar"],
|
||||||
|
bazel_module: { bp2build_available: true },
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
"external/rust/crates/bar/src/lib.rs": "",
|
||||||
|
"external/rust/crates/bar/Android.bp": `
|
||||||
|
rust_library_host {
|
||||||
|
name: "libbar",
|
||||||
|
crate_name: "bar",
|
||||||
|
srcs: ["src/lib.rs"],
|
||||||
|
bazel_module: { bp2build_available: true },
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
ExpectedBazelTargets: []string{
|
||||||
|
makeBazelTargetHostOrDevice("rust_proc_macro", "libfoo", AttrNameToString{
|
||||||
|
"crate_name": `"foo"`,
|
||||||
|
"srcs": `[
|
||||||
|
"src/helper.rs",
|
||||||
|
"src/lib.rs",
|
||||||
|
]`,
|
||||||
|
"crate_features": `["bah-enabled"]`,
|
||||||
|
"edition": `"2021"`,
|
||||||
|
"rustc_flags": `["--cfg=baz"]`,
|
||||||
|
"deps": `["//external/rust/crates/bar:libbar"]`,
|
||||||
|
}, android.HostSupported),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
46
bp2build/rust_protobuf_conversion_test.go
Normal file
46
bp2build/rust_protobuf_conversion_test.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package bp2build
|
||||||
|
|
||||||
|
import (
|
||||||
|
"android/soong/android"
|
||||||
|
"android/soong/rust"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func runRustProtobufTestCase(t *testing.T, tc Bp2buildTestCase) {
|
||||||
|
t.Helper()
|
||||||
|
RunBp2BuildTestCase(t, registerRustProtobufModuleTypes, tc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func registerRustProtobufModuleTypes(ctx android.RegistrationContext) {
|
||||||
|
ctx.RegisterModuleType("rust_protobuf_host", rust.RustProtobufHostFactory)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRustProtobufHostTestCase(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_host {
|
||||||
|
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),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
@@ -16,6 +16,8 @@ package rust
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"android/soong/android"
|
"android/soong/android"
|
||||||
|
"android/soong/bazel"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -60,6 +62,8 @@ func RustBinaryHostFactory() android.Module {
|
|||||||
func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
|
func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
|
||||||
module := newModule(hod, android.MultilibFirst)
|
module := newModule(hod, android.MultilibFirst)
|
||||||
|
|
||||||
|
android.InitBazelModule(module)
|
||||||
|
|
||||||
binary := &binaryDecorator{
|
binary := &binaryDecorator{
|
||||||
baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
|
baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
|
||||||
}
|
}
|
||||||
@@ -183,3 +187,88 @@ func (binary *binaryDecorator) staticallyLinked() bool {
|
|||||||
func (binary *binaryDecorator) testBinary() bool {
|
func (binary *binaryDecorator) testBinary() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type rustBinaryLibraryAttributes struct {
|
||||||
|
Srcs bazel.LabelListAttribute
|
||||||
|
Compile_data bazel.LabelListAttribute
|
||||||
|
Crate_name bazel.StringAttribute
|
||||||
|
Edition bazel.StringAttribute
|
||||||
|
Crate_features bazel.StringListAttribute
|
||||||
|
Deps bazel.LabelListAttribute
|
||||||
|
Proc_macro_deps bazel.LabelListAttribute
|
||||||
|
Rustc_flags bazel.StringListAttribute
|
||||||
|
}
|
||||||
|
|
||||||
|
func binaryBp2build(ctx android.TopDownMutatorContext, m *Module) {
|
||||||
|
binary := m.compiler.(*binaryDecorator)
|
||||||
|
|
||||||
|
var srcs bazel.LabelList
|
||||||
|
var compileData bazel.LabelList
|
||||||
|
|
||||||
|
if binary.baseCompiler.Properties.Srcs[0] == "src/main.rs" {
|
||||||
|
srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
|
||||||
|
compileData = android.BazelLabelForModuleSrc(
|
||||||
|
ctx,
|
||||||
|
[]string{
|
||||||
|
"src/**/*.proto",
|
||||||
|
"examples/**/*.rs",
|
||||||
|
"**/*.md",
|
||||||
|
"templates/**/*.template",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
srcs = android.BazelLabelForModuleSrc(ctx, binary.baseCompiler.Properties.Srcs)
|
||||||
|
}
|
||||||
|
|
||||||
|
deps := android.BazelLabelForModuleDeps(ctx, append(
|
||||||
|
binary.baseCompiler.Properties.Rustlibs,
|
||||||
|
))
|
||||||
|
|
||||||
|
procMacroDeps := android.BazelLabelForModuleDeps(ctx, binary.baseCompiler.Properties.Proc_macros)
|
||||||
|
|
||||||
|
var rustcFLags []string
|
||||||
|
for _, cfg := range binary.baseCompiler.Properties.Cfgs {
|
||||||
|
rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg))
|
||||||
|
}
|
||||||
|
|
||||||
|
attrs := &rustBinaryLibraryAttributes{
|
||||||
|
Srcs: bazel.MakeLabelListAttribute(
|
||||||
|
srcs,
|
||||||
|
),
|
||||||
|
Compile_data: bazel.MakeLabelListAttribute(
|
||||||
|
compileData,
|
||||||
|
),
|
||||||
|
Crate_name: bazel.StringAttribute{
|
||||||
|
Value: &binary.baseCompiler.Properties.Crate_name,
|
||||||
|
},
|
||||||
|
Edition: bazel.StringAttribute{
|
||||||
|
Value: binary.baseCompiler.Properties.Edition,
|
||||||
|
},
|
||||||
|
Crate_features: bazel.StringListAttribute{
|
||||||
|
Value: binary.baseCompiler.Properties.Features,
|
||||||
|
},
|
||||||
|
Deps: bazel.MakeLabelListAttribute(
|
||||||
|
deps,
|
||||||
|
),
|
||||||
|
Proc_macro_deps: bazel.MakeLabelListAttribute(
|
||||||
|
procMacroDeps,
|
||||||
|
),
|
||||||
|
Rustc_flags: bazel.StringListAttribute{
|
||||||
|
Value: append(
|
||||||
|
rustcFLags,
|
||||||
|
binary.baseCompiler.Properties.Flags...,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.CreateBazelTargetModule(
|
||||||
|
bazel.BazelTargetModuleProperties{
|
||||||
|
Rule_class: "rust_binary",
|
||||||
|
Bzl_load_location: "@rules_rust//rust:defs.bzl",
|
||||||
|
},
|
||||||
|
android.CommonAttributes{
|
||||||
|
Name: m.Name(),
|
||||||
|
},
|
||||||
|
attrs,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
157
rust/library.go
157
rust/library.go
@@ -20,7 +20,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"android/soong/android"
|
"android/soong/android"
|
||||||
|
"android/soong/bazel"
|
||||||
"android/soong/cc"
|
"android/soong/cc"
|
||||||
|
|
||||||
|
"github.com/google/blueprint/proptools"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -398,6 +401,8 @@ func (library *libraryDecorator) BuildOnlyShared() {
|
|||||||
func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
|
func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
|
||||||
module := newModule(hod, android.MultilibBoth)
|
module := newModule(hod, android.MultilibBoth)
|
||||||
|
|
||||||
|
android.InitBazelModule(module)
|
||||||
|
|
||||||
library := &libraryDecorator{
|
library := &libraryDecorator{
|
||||||
MutatedProperties: LibraryMutatedProperties{
|
MutatedProperties: LibraryMutatedProperties{
|
||||||
BuildDylib: false,
|
BuildDylib: false,
|
||||||
@@ -793,3 +798,155 @@ func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext,
|
|||||||
// TODO(185577950): If support for generated headers is added, they need to be collected here as well.
|
// TODO(185577950): If support for generated headers is added, they need to be collected here as well.
|
||||||
l.collectedSnapshotHeaders = ret
|
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
|
||||||
|
Proc_macro_deps bazel.LabelListAttribute
|
||||||
|
}
|
||||||
|
|
||||||
|
func libraryBp2build(ctx android.TopDownMutatorContext, m *Module) {
|
||||||
|
lib := m.compiler.(*libraryDecorator)
|
||||||
|
|
||||||
|
srcs, compileData := srcsAndCompileDataAttrs(ctx, *lib.baseCompiler)
|
||||||
|
|
||||||
|
deps := android.BazelLabelForModuleDeps(ctx, append(
|
||||||
|
lib.baseCompiler.Properties.Rustlibs,
|
||||||
|
lib.baseCompiler.Properties.Rlibs...,
|
||||||
|
))
|
||||||
|
|
||||||
|
cargoBuildScript := cargoBuildScriptBp2build(ctx, m)
|
||||||
|
if cargoBuildScript != nil {
|
||||||
|
deps.Add(&bazel.Label{
|
||||||
|
Label: ":" + *cargoBuildScript,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
procMacroDeps := android.BazelLabelForModuleDeps(ctx, lib.baseCompiler.Properties.Proc_macros)
|
||||||
|
|
||||||
|
var rustcFLags []string
|
||||||
|
for _, cfg := range lib.baseCompiler.Properties.Cfgs {
|
||||||
|
rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg))
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
),
|
||||||
|
Proc_macro_deps: bazel.MakeLabelListAttribute(
|
||||||
|
procMacroDeps,
|
||||||
|
),
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
type cargoBuildScriptAttributes struct {
|
||||||
|
Srcs bazel.LabelListAttribute
|
||||||
|
Edition bazel.StringAttribute
|
||||||
|
Version bazel.StringAttribute
|
||||||
|
}
|
||||||
|
|
||||||
|
func cargoBuildScriptBp2build(ctx android.TopDownMutatorContext, m *Module) *string {
|
||||||
|
// Soong treats some crates like libprotobuf as special in that they have
|
||||||
|
// cargo build script ran to produce an out folder and check it into AOSP
|
||||||
|
// For example, https://cs.android.com/android/platform/superproject/main/+/main:external/rust/crates/protobuf/out/
|
||||||
|
// is produced by cargo build script https://cs.android.com/android/platform/superproject/main/+/main:external/rust/crates/protobuf/build.rs
|
||||||
|
// The out folder is then fed into `rust_library` by a genrule
|
||||||
|
// https://cs.android.com/android/platform/superproject/main/+/main:external/rust/crates/protobuf/Android.bp;l=22
|
||||||
|
// This allows Soong to decouple from cargo completely.
|
||||||
|
|
||||||
|
// Soong decouples from cargo so that it has control over cc compilation.
|
||||||
|
// https://cs.android.com/android/platform/superproject/main/+/main:development/scripts/cargo2android.py;l=1033-1041;drc=8449944a50a0445a5ecaf9b7aed12608c81bf3f1
|
||||||
|
// generates a `cc_library_static` module to have custom cc flags.
|
||||||
|
// Since bp2build will convert the cc modules to cc targets which include the cflags,
|
||||||
|
// Bazel does not need to have this optimization.
|
||||||
|
|
||||||
|
// Performance-wise: rust_library -> cargo_build_script vs rust_library -> genrule (like Soong)
|
||||||
|
// don't have any major difference in build time in Bazel. So using cargo_build_script does not slow
|
||||||
|
// down the build.
|
||||||
|
|
||||||
|
// The benefit of using `cargo_build_script` here is that it would take care of setting correct
|
||||||
|
// `OUT_DIR` for us - similar to what Soong does here
|
||||||
|
// https://cs.android.com/android/platform/superproject/main/+/main:build/soong/rust/builder.go;l=202-218;drc=f29ca58e88c5846bbe8955e5192135e5ab4f14a1
|
||||||
|
|
||||||
|
// TODO(b/297364081): cargo2android.py has logic for when generate/not cc_library_static and out directory
|
||||||
|
// bp2build might be able use the same logic for when to use `cargo_build_script`.
|
||||||
|
// For now, we're building libprotobuf_build_script as a one-off until we have a more principled solution
|
||||||
|
if m.Name() != "libprotobuf" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
lib := m.compiler.(*libraryDecorator)
|
||||||
|
|
||||||
|
name := m.Name() + "_build_script"
|
||||||
|
attrs := &cargoBuildScriptAttributes{
|
||||||
|
Srcs: bazel.MakeLabelListAttribute(
|
||||||
|
android.BazelLabelForModuleSrc(ctx, []string{"build.rs"}),
|
||||||
|
),
|
||||||
|
Edition: bazel.StringAttribute{
|
||||||
|
Value: lib.baseCompiler.Properties.Edition,
|
||||||
|
},
|
||||||
|
Version: bazel.StringAttribute{
|
||||||
|
Value: lib.baseCompiler.Properties.Cargo_pkg_version,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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: "cargo_build_script",
|
||||||
|
Bzl_load_location: "@rules_rust//cargo:cargo_build_script.bzl",
|
||||||
|
},
|
||||||
|
android.CommonAttributes{
|
||||||
|
Name: name,
|
||||||
|
},
|
||||||
|
attrs,
|
||||||
|
restriction,
|
||||||
|
)
|
||||||
|
|
||||||
|
return &name
|
||||||
|
}
|
||||||
|
@@ -16,6 +16,8 @@ package rust
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"android/soong/android"
|
"android/soong/android"
|
||||||
|
"android/soong/bazel"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -47,6 +49,8 @@ func ProcMacroFactory() android.Module {
|
|||||||
func NewProcMacro(hod android.HostOrDeviceSupported) (*Module, *procMacroDecorator) {
|
func NewProcMacro(hod android.HostOrDeviceSupported) (*Module, *procMacroDecorator) {
|
||||||
module := newModule(hod, android.MultilibFirst)
|
module := newModule(hod, android.MultilibFirst)
|
||||||
|
|
||||||
|
android.InitBazelModule(module)
|
||||||
|
|
||||||
procMacro := &procMacroDecorator{
|
procMacro := &procMacroDecorator{
|
||||||
baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
|
baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
|
||||||
flagExporter: NewFlagExporter(),
|
flagExporter: NewFlagExporter(),
|
||||||
@@ -99,3 +103,65 @@ func (procMacro *procMacroDecorator) everInstallable() bool {
|
|||||||
// Proc_macros are never installed
|
// Proc_macros are never installed
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type procMacroAttributes 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 procMacroBp2build(ctx android.TopDownMutatorContext, m *Module) {
|
||||||
|
procMacro := m.compiler.(*procMacroDecorator)
|
||||||
|
srcs, compileData := srcsAndCompileDataAttrs(ctx, *procMacro.baseCompiler)
|
||||||
|
deps := android.BazelLabelForModuleDeps(ctx, append(
|
||||||
|
procMacro.baseCompiler.Properties.Rustlibs,
|
||||||
|
procMacro.baseCompiler.Properties.Rlibs...,
|
||||||
|
))
|
||||||
|
|
||||||
|
var rustcFLags []string
|
||||||
|
for _, cfg := range procMacro.baseCompiler.Properties.Cfgs {
|
||||||
|
rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg))
|
||||||
|
}
|
||||||
|
|
||||||
|
attrs := &procMacroAttributes{
|
||||||
|
Srcs: bazel.MakeLabelListAttribute(
|
||||||
|
srcs,
|
||||||
|
),
|
||||||
|
Compile_data: bazel.MakeLabelListAttribute(
|
||||||
|
compileData,
|
||||||
|
),
|
||||||
|
Crate_name: bazel.StringAttribute{
|
||||||
|
Value: &procMacro.baseCompiler.Properties.Crate_name,
|
||||||
|
},
|
||||||
|
Edition: bazel.StringAttribute{
|
||||||
|
Value: procMacro.baseCompiler.Properties.Edition,
|
||||||
|
},
|
||||||
|
Crate_features: bazel.StringListAttribute{
|
||||||
|
Value: procMacro.baseCompiler.Properties.Features,
|
||||||
|
},
|
||||||
|
Deps: bazel.MakeLabelListAttribute(
|
||||||
|
deps,
|
||||||
|
),
|
||||||
|
Rustc_flags: bazel.StringListAttribute{
|
||||||
|
Value: append(
|
||||||
|
rustcFLags,
|
||||||
|
procMacro.baseCompiler.Properties.Flags...,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
// m.IsConvertedByBp2build()
|
||||||
|
ctx.CreateBazelTargetModule(
|
||||||
|
bazel.BazelTargetModuleProperties{
|
||||||
|
Rule_class: "rust_proc_macro",
|
||||||
|
Bzl_load_location: "@rules_rust//rust:defs.bzl",
|
||||||
|
},
|
||||||
|
android.CommonAttributes{
|
||||||
|
Name: m.Name(),
|
||||||
|
},
|
||||||
|
attrs,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
@@ -19,6 +19,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"android/soong/android"
|
"android/soong/android"
|
||||||
|
"android/soong/bazel"
|
||||||
|
|
||||||
|
"github.com/google/blueprint/proptools"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -264,5 +267,71 @@ func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecor
|
|||||||
|
|
||||||
module := NewSourceProviderModule(hod, protobuf, false, false)
|
module := NewSourceProviderModule(hod, protobuf, false, false)
|
||||||
|
|
||||||
|
android.InitBazelModule(module)
|
||||||
|
|
||||||
return module, protobuf
|
return module, protobuf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type rustProtoAttributes struct {
|
||||||
|
Srcs bazel.LabelListAttribute
|
||||||
|
Crate_name bazel.StringAttribute
|
||||||
|
Deps bazel.LabelListAttribute
|
||||||
|
}
|
||||||
|
|
||||||
|
type protoLibraryAttributes struct {
|
||||||
|
Srcs bazel.LabelListAttribute
|
||||||
|
}
|
||||||
|
|
||||||
|
func protoLibraryBp2build(ctx android.TopDownMutatorContext, m *Module) {
|
||||||
|
var protoFiles []string
|
||||||
|
|
||||||
|
for _, propsInterface := range m.sourceProvider.SourceProviderProps() {
|
||||||
|
if possibleProps, ok := propsInterface.(*ProtobufProperties); ok {
|
||||||
|
protoFiles = possibleProps.Protos
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protoLibraryName := m.Name() + "_proto"
|
||||||
|
|
||||||
|
protoDeps := bazel.LabelListAttribute{
|
||||||
|
Value: bazel.LabelList{
|
||||||
|
Includes: []bazel.Label{
|
||||||
|
{
|
||||||
|
Label: ":" + protoLibraryName,
|
||||||
|
OriginalModuleName: m.Name(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.CreateBazelTargetModule(
|
||||||
|
bazel.BazelTargetModuleProperties{
|
||||||
|
Rule_class: "proto_library",
|
||||||
|
},
|
||||||
|
android.CommonAttributes{
|
||||||
|
Name: protoLibraryName,
|
||||||
|
},
|
||||||
|
&protoLibraryAttributes{
|
||||||
|
Srcs: bazel.MakeLabelListAttribute(
|
||||||
|
android.BazelLabelForModuleSrc(ctx, protoFiles),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
ctx.CreateBazelTargetModule(
|
||||||
|
bazel.BazelTargetModuleProperties{
|
||||||
|
Rule_class: "rust_proto_library",
|
||||||
|
Bzl_load_location: "@rules_rust//proto/protobuf:defs.bzl",
|
||||||
|
},
|
||||||
|
android.CommonAttributes{
|
||||||
|
Name: m.Name(),
|
||||||
|
},
|
||||||
|
&rustProtoAttributes{
|
||||||
|
Crate_name: bazel.StringAttribute{
|
||||||
|
Value: proptools.StringPtr(m.CrateName()),
|
||||||
|
},
|
||||||
|
Deps: protoDeps,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
44
rust/rust.go
44
rust/rust.go
@@ -15,7 +15,9 @@
|
|||||||
package rust
|
package rust
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"android/soong/bazel"
|
||||||
"android/soong/bloaty"
|
"android/soong/bloaty"
|
||||||
|
"android/soong/ui/metrics/bp2build_metrics_proto"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -169,6 +171,8 @@ type Module struct {
|
|||||||
apexSdkVersion android.ApiLevel
|
apexSdkVersion android.ApiLevel
|
||||||
|
|
||||||
transitiveAndroidMkSharedLibs *android.DepSet[string]
|
transitiveAndroidMkSharedLibs *android.DepSet[string]
|
||||||
|
|
||||||
|
android.BazelModuleBase
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mod *Module) Header() bool {
|
func (mod *Module) Header() bool {
|
||||||
@@ -1841,6 +1845,46 @@ func (c *Module) Partition() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
|
||||||
|
if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
|
||||||
|
libraryBp2build(ctx, m)
|
||||||
|
} else if ctx.ModuleType() == "rust_proc_macro" {
|
||||||
|
procMacroBp2build(ctx, m)
|
||||||
|
} else if ctx.ModuleType() == "rust_binary_host" {
|
||||||
|
binaryBp2build(ctx, m)
|
||||||
|
} else if ctx.ModuleType() == "rust_protobuf_host" {
|
||||||
|
protoLibraryBp2build(ctx, m)
|
||||||
|
} else {
|
||||||
|
ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is a workaround by assuming the conventions that rust crate repos are structured
|
||||||
|
// while waiting for the sandboxing work to complete.
|
||||||
|
// TODO(b/297344471): 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.
|
||||||
|
func srcsAndCompileDataAttrs(ctx android.TopDownMutatorContext, c baseCompiler) (bazel.LabelList, bazel.LabelList) {
|
||||||
|
var srcs bazel.LabelList
|
||||||
|
var compileData bazel.LabelList
|
||||||
|
|
||||||
|
if c.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, c.Properties.Srcs)
|
||||||
|
}
|
||||||
|
|
||||||
|
return srcs, compileData
|
||||||
|
}
|
||||||
|
|
||||||
var Bool = proptools.Bool
|
var Bool = proptools.Bool
|
||||||
var BoolDefault = proptools.BoolDefault
|
var BoolDefault = proptools.BoolDefault
|
||||||
var String = proptools.String
|
var String = proptools.String
|
||||||
|
Reference in New Issue
Block a user