fork/rules_rust

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

David TolnayOutside of examples, fill in all `edition` attrs (#1257)03a70796

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