fork/rules_rust

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

UebelAndreAdded bzlmod support to core rules_rust (#3034)69ddb09d

main
5.6 KiB173 linesraw
1"""Unittests for rust linkstamp support."""
2
3load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4load("@rules_cc//cc:defs.bzl", "cc_library")
5load("//rust:defs.bzl", "rust_binary", "rust_common", "rust_library", "rust_test")
6load("//test/unit:common.bzl", "assert_action_mnemonic")
7
8def _is_running_on_linux(ctx):
9    return ctx.target_platform_has_constraint(ctx.attr._linux[platform_common.ConstraintValueInfo])
10
11def _get_workspace_prefix(ctx):
12    return "" if ctx.workspace_name in ["rules_rust", "_main"] else "/external/rules_rust"
13
14def _supports_linkstamps_test(ctx):
15    env = analysistest.begin(ctx)
16    tut = analysistest.target_under_test(env)
17    if not _is_running_on_linux(ctx):
18        # Skipping linkstamps tests on an unsupported (non-Linux) platform
19        return analysistest.end(env)
20
21    linkstamp_action = tut.actions[0]
22    assert_action_mnemonic(env, linkstamp_action, "CppLinkstampCompile")
23    linkstamp_out = linkstamp_action.outputs.to_list()[0]
24    asserts.equals(env, linkstamp_out.basename, "linkstamp.o")
25    tut_out = tut.files.to_list()[0]
26    is_test = tut[rust_common.crate_info].is_test
27    workspace_prefix = _get_workspace_prefix(ctx)
28
29    # Rust compilation outputs coming from a test are put in test-{hash} directory
30    # which we need to remove in order to obtain the linkstamp file path.
31    dirname = "/".join(tut_out.dirname.split("/")[:-1]) if is_test else tut_out.dirname
32    expected_linkstamp_path = dirname + "/_objs/" + tut_out.basename + workspace_prefix + "/test/unit/linkstamps/linkstamp.o"
33    asserts.equals(
34        env,
35        linkstamp_out.path,
36        expected_linkstamp_path,
37        "Expected linkstamp output '{actual_path}' to match '{expected_path}'".format(
38            actual_path = linkstamp_out.path,
39            expected_path = expected_linkstamp_path,
40        ),
41    )
42
43    rustc_action = tut.actions[1]
44    assert_action_mnemonic(env, rustc_action, "Rustc")
45    rustc_inputs = rustc_action.inputs.to_list()
46    asserts.true(
47        env,
48        linkstamp_out in rustc_inputs,
49        "Expected linkstamp output '{output}' to be among the binary inputs '{inputs}'".format(
50            output = linkstamp_out,
51            inputs = rustc_inputs,
52        ),
53    )
54    return analysistest.end(env)
55
56supports_linkstamps_test = analysistest.make(
57    _supports_linkstamps_test,
58    attrs = {
59        "_linux": attr.label(
60            default = Label("@platforms//os:linux"),
61        ),
62    },
63)
64
65def _linkstamps_test():
66    # Native linkstamps are only supported by the builtin Linux C++ toolchain. Ideally,
67    # we would be able to inspect the feature_configuration of the target to see if
68    # it has the "linkstamp" feature, but there is no way to get that feature
69    # configuration.
70    cc_library(
71        name = "cc_lib_with_linkstamp",
72        linkstamp = select({
73            "//rust/platform:linux": "linkstamp.cc",
74            "//conditions:default": None,
75        }),
76    )
77
78    rust_binary(
79        name = "some_rust_binary",
80        srcs = ["foo.rs"],
81        edition = "2018",
82        deps = [":cc_lib_with_linkstamp"],
83    )
84
85    supports_linkstamps_test(
86        name = "rust_binary_supports_linkstamps_test",
87        target_under_test = ":some_rust_binary",
88    )
89
90    rust_library(
91        name = "some_rust_library_with_linkstamp_transitively",
92        srcs = ["foo.rs"],
93        edition = "2018",
94        deps = [":cc_lib_with_linkstamp"],
95    )
96
97    rust_binary(
98        name = "some_rust_binary_with_linkstamp_transitively",
99        srcs = ["foo.rs"],
100        edition = "2018",
101        deps = [":some_rust_library_with_linkstamp_transitively"],
102    )
103
104    supports_linkstamps_test(
105        name = "rust_binary_with_linkstamp_transitively",
106        target_under_test = ":some_rust_binary_with_linkstamp_transitively",
107    )
108
109    cc_library(
110        name = "cc_lib_with_linkstamp_transitively",
111        deps = [":cc_lib_with_linkstamp"],
112    )
113
114    rust_binary(
115        name = "some_rust_binary_with_multiple_paths_to_a_linkstamp",
116        srcs = ["foo.rs"],
117        edition = "2018",
118        deps = [":cc_lib_with_linkstamp", ":cc_lib_with_linkstamp_transitively"],
119    )
120
121    supports_linkstamps_test(
122        name = "rust_binary_supports_duplicated_linkstamps",
123        target_under_test = ":some_rust_binary_with_multiple_paths_to_a_linkstamp",
124    )
125
126    rust_test(
127        name = "some_rust_test1",
128        srcs = ["foo.rs"],
129        edition = "2018",
130        deps = [":cc_lib_with_linkstamp"],
131    )
132
133    supports_linkstamps_test(
134        name = "rust_test_supports_linkstamps_test1",
135        target_under_test = ":some_rust_test1",
136    )
137
138    rust_test(
139        name = "some_rust_test2",
140        srcs = ["foo.rs"],
141        edition = "2018",
142        deps = [":cc_lib_with_linkstamp"],
143    )
144
145    supports_linkstamps_test(
146        name = "rust_test_supports_linkstamps_test2",
147        target_under_test = ":some_rust_test2",
148    )
149
150def linkstamps_test_suite(name):
151    """Entry-point macro called from the BUILD file.
152
153    Args:
154      name: Name of the macro.
155    """
156
157    # Older versions of Bazel do not support Starlark linkstamps.
158    if not hasattr(cc_common, "register_linkstamp_compile_action"):
159        # buildifier: disable=print
160        print("Skipping linkstamps tests since this Bazel version does not support Starlark linkstamps.")
161        return
162
163    _linkstamps_test()
164
165    native.test_suite(
166        name = name,
167        tests = [
168            ":rust_binary_supports_linkstamps_test",
169            ":rust_binary_supports_duplicated_linkstamps",
170            ":rust_test_supports_linkstamps_test1",
171            ":rust_test_supports_linkstamps_test2",
172        ],
173    )