xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/bindgen/bindgen_test.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""Analysis test for for rust_bindgen_library rule."""
2
3load("@rules_cc//cc:defs.bzl", "cc_library")
4load("@rules_rust//bindgen:defs.bzl", "rust_bindgen_library")
5load("@rules_rust//rust:defs.bzl", "rust_binary")
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 bindgen_test_suite(name):
48    test_suite(
49        name = name,
50        tests = [
51            _test_cc_linkopt,
52        ],
53    )
54