xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/bindgen/bindgen_test.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan"""Analysis test for for rust_bindgen_library rule."""
2*d4726bddSHONG Yifan
3*d4726bddSHONG Yifanload("@rules_cc//cc:defs.bzl", "cc_library")
4*d4726bddSHONG Yifanload("@rules_rust//bindgen:defs.bzl", "rust_bindgen_library")
5*d4726bddSHONG Yifanload("@rules_rust//rust:defs.bzl", "rust_binary")
6*d4726bddSHONG Yifanload("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
7*d4726bddSHONG Yifan
8*d4726bddSHONG Yifandef _test_cc_linkopt_impl(env, target):
9*d4726bddSHONG Yifan    # Assert
10*d4726bddSHONG Yifan    env.expect.that_action(target.actions[0]) \
11*d4726bddSHONG Yifan        .contains_at_least_args(["--codegen=link-arg=-shared"])
12*d4726bddSHONG Yifan
13*d4726bddSHONG Yifandef _test_cc_linkopt(name):
14*d4726bddSHONG Yifan    # Arrange
15*d4726bddSHONG Yifan    cc_library(
16*d4726bddSHONG Yifan        name = name + "_cc",
17*d4726bddSHONG Yifan        srcs = ["simple.cc"],
18*d4726bddSHONG Yifan        hdrs = ["simple.h"],
19*d4726bddSHONG Yifan        linkopts = ["-shared"],
20*d4726bddSHONG Yifan        tags = ["manual"],
21*d4726bddSHONG Yifan    )
22*d4726bddSHONG Yifan    rust_bindgen_library(
23*d4726bddSHONG Yifan        name = name + "_rust_bindgen",
24*d4726bddSHONG Yifan        cc_lib = name + "_cc",
25*d4726bddSHONG Yifan        header = "simple.h",
26*d4726bddSHONG Yifan        tags = ["manual"],
27*d4726bddSHONG Yifan        edition = "2021",
28*d4726bddSHONG Yifan    )
29*d4726bddSHONG Yifan    rust_binary(
30*d4726bddSHONG Yifan        name = name + "_rust_binary",
31*d4726bddSHONG Yifan        srcs = ["main.rs"],
32*d4726bddSHONG Yifan        deps = [name + "_rust_bindgen"],
33*d4726bddSHONG Yifan        tags = ["manual"],
34*d4726bddSHONG Yifan        edition = "2021",
35*d4726bddSHONG Yifan    )
36*d4726bddSHONG Yifan
37*d4726bddSHONG Yifan    # Act
38*d4726bddSHONG Yifan    # TODO: Use targets attr to also verify `rust_bindgen_library` not having
39*d4726bddSHONG Yifan    # the linkopt after https://github.com/bazelbuild/rules_testing/issues/67
40*d4726bddSHONG Yifan    # is released
41*d4726bddSHONG Yifan    analysis_test(
42*d4726bddSHONG Yifan        name = name,
43*d4726bddSHONG Yifan        target = name + "_rust_binary",
44*d4726bddSHONG Yifan        impl = _test_cc_linkopt_impl,
45*d4726bddSHONG Yifan    )
46*d4726bddSHONG Yifan
47*d4726bddSHONG Yifandef bindgen_test_suite(name):
48*d4726bddSHONG Yifan    test_suite(
49*d4726bddSHONG Yifan        name = name,
50*d4726bddSHONG Yifan        tests = [
51*d4726bddSHONG Yifan            _test_cc_linkopt,
52*d4726bddSHONG Yifan        ],
53*d4726bddSHONG Yifan    )
54