blob: b860b767bcda12c5abc4d0098db6dcb1826df658 [file] [log] [blame]
Vinh Tranbcb5f572023-08-24 11:10:01 -04001package bp2build
2
3import (
4 "android/soong/android"
5 "android/soong/rust"
6 "testing"
7)
8
9func runRustLibraryTestCase(t *testing.T, tc Bp2buildTestCase) {
10 t.Helper()
11 RunBp2BuildTestCase(t, registerRustLibraryModuleTypes, tc)
12}
13
14func registerRustLibraryModuleTypes(ctx android.RegistrationContext) {
15 ctx.RegisterModuleType("rust_library", rust.RustLibraryFactory)
16 ctx.RegisterModuleType("rust_library_host", rust.RustLibraryHostFactory)
17}
18
Vinh Tran9b846782023-08-24 12:55:12 -040019func TestLibProtobuf(t *testing.T) {
20 runRustLibraryTestCase(t, Bp2buildTestCase{
21 Dir: "external/rust/crates/foo",
22 Blueprint: "",
23 Filesystem: map[string]string{
24 "external/rust/crates/foo/src/lib.rs": "",
25 "external/rust/crates/foo/Android.bp": `
26rust_library_host {
27 name: "libprotobuf",
28 crate_name: "protobuf",
29 srcs: ["src/lib.rs"],
30 bazel_module: { bp2build_available: true },
31}
32`,
33 },
34 ExpectedBazelTargets: []string{
35 // TODO(b/290790800): Remove the restriction when rust toolchain for android is implemented
36 makeBazelTargetHostOrDevice("rust_library", "libprotobuf", AttrNameToString{
37 "crate_name": `"protobuf"`,
38 "srcs": `["src/lib.rs"]`,
39 "deps": `[":libprotobuf_build_script"]`,
40 }, android.HostSupported),
41 makeBazelTargetHostOrDevice("cargo_build_script", "libprotobuf_build_script", AttrNameToString{
42 "srcs": `["build.rs"]`,
43 }, android.HostSupported),
44 },
45 },
46 )
47}
48
Vinh Tranbcb5f572023-08-24 11:10:01 -040049func TestRustLibrary(t *testing.T) {
50 expectedAttrs := AttrNameToString{
51 "crate_name": `"foo"`,
52 "srcs": `[
53 "src/helper.rs",
54 "src/lib.rs",
55 ]`,
56 "crate_features": `["bah-enabled"]`,
57 "edition": `"2021"`,
58 "rustc_flags": `["--cfg=baz"]`,
59 }
60
61 runRustLibraryTestCase(t, Bp2buildTestCase{
62 Dir: "external/rust/crates/foo",
63 Blueprint: "",
64 Filesystem: map[string]string{
65 "external/rust/crates/foo/src/lib.rs": "",
66 "external/rust/crates/foo/src/helper.rs": "",
67 "external/rust/crates/foo/Android.bp": `
68rust_library {
69 name: "libfoo",
70 crate_name: "foo",
71 host_supported: true,
72 srcs: ["src/lib.rs"],
73 edition: "2021",
74 features: ["bah-enabled"],
75 cfgs: ["baz"],
76 bazel_module: { bp2build_available: true },
77}
78rust_library_host {
79 name: "libfoo_host",
80 crate_name: "foo",
81 srcs: ["src/lib.rs"],
82 edition: "2021",
83 features: ["bah-enabled"],
84 cfgs: ["baz"],
85 bazel_module: { bp2build_available: true },
86}
87`,
88 },
89 ExpectedBazelTargets: []string{
90 // TODO(b/290790800): Remove the restriction when rust toolchain for android is implemented
91 makeBazelTargetHostOrDevice("rust_library", "libfoo", expectedAttrs, android.HostSupported),
92 makeBazelTargetHostOrDevice("rust_library", "libfoo_host", expectedAttrs, android.HostSupported),
93 },
94 },
95 )
96}