fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndreMoved rust extension rules into a separate `rules_rust_ext` workspace. (#3007)2bad96d9

main
3.3 KiB111 linesraw
1"""Analysis test for for rust_bindgen_library rule."""
2
3load("@rules_cc//cc:defs.bzl", "cc_library")
4load("@rules_rust//rust:defs.bzl", "rust_binary")
5load("@rules_rust_bindgen//:defs.bzl", "rust_bindgen_library")
6load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
7
8def _test_cc_linkopt_impl(env, target):
9    # Assert
10    env.expect.that_action(target.actions[0]) \
11        .contains_at_least_args(["--codegen=link-arg=-shared"])
12
13def _test_cc_linkopt(name):
14    # Arrange
15    cc_library(
16        name = name + "_cc",
17        srcs = ["simple.cc"],
18        hdrs = ["simple.h"],
19        linkopts = ["-shared"],
20        tags = ["manual"],
21    )
22    rust_bindgen_library(
23        name = name + "_rust_bindgen",
24        cc_lib = name + "_cc",
25        header = "simple.h",
26        tags = ["manual"],
27        edition = "2021",
28    )
29    rust_binary(
30        name = name + "_rust_binary",
31        srcs = ["main.rs"],
32        deps = [name + "_rust_bindgen"],
33        tags = ["manual"],
34        edition = "2021",
35    )
36
37    # Act
38    # TODO: Use targets attr to also verify `rust_bindgen_library` not having
39    # the linkopt after https://github.com/bazelbuild/rules_testing/issues/67
40    # is released
41    analysis_test(
42        name = name,
43        target = name + "_rust_binary",
44        impl = _test_cc_linkopt_impl,
45    )
46
47def _test_cc_lib_object_merging_impl(env, target):
48    env.expect.that_int(len(target.actions)).is_greater_than(2)
49    env.expect.that_action(target.actions[0]).mnemonic().contains("RustBindgen")
50    env.expect.that_action(target.actions[1]).mnemonic().contains("FileWrite")
51    env.expect.that_action(target.actions[1]).content().contains("-lstatic=test_cc_lib_object_merging_cc")
52    env.expect.that_action(target.actions[2]).mnemonic().contains("FileWrite")
53    env.expect.that_action(target.actions[2]).content().contains("-Lnative=")
54
55def _test_cc_lib_object_merging_disabled_impl(env, target):
56    # no FileWrite actions writing arg files registered
57    env.expect.that_int(len(target.actions)).is_greater_than(0)
58    env.expect.that_action(target.actions[0]).mnemonic().contains("RustBindgen")
59
60def _test_cc_lib_object_merging(name):
61    cc_library(
62        name = name + "_cc",
63        hdrs = ["simple.h"],
64        srcs = ["simple.cc"],
65    )
66
67    rust_bindgen_library(
68        name = name + "_rust_bindgen",
69        cc_lib = name + "_cc",
70        header = "simple.h",
71        tags = ["manual"],
72        edition = "2021",
73    )
74
75    analysis_test(
76        name = name,
77        target = name + "_rust_bindgen__bindgen",
78        impl = _test_cc_lib_object_merging_impl,
79    )
80
81def _test_cc_lib_object_merging_disabled(name):
82    cc_library(
83        name = name + "_cc",
84        hdrs = ["simple.h"],
85        srcs = ["simple.cc"],
86    )
87
88    rust_bindgen_library(
89        name = name + "_rust_bindgen",
90        cc_lib = name + "_cc",
91        header = "simple.h",
92        tags = ["manual"],
93        merge_cc_lib_objects_into_rlib = False,
94        edition = "2021",
95    )
96
97    analysis_test(
98        name = name,
99        target = name + "_rust_bindgen__bindgen",
100        impl = _test_cc_lib_object_merging_disabled_impl,
101    )
102
103def bindgen_test_suite(name):
104    test_suite(
105        name = name,
106        tests = [
107            _test_cc_linkopt,
108            _test_cc_lib_object_merging,
109            _test_cc_lib_object_merging_disabled,
110        ],
111    )