fork/rules_rust

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

UebelAndreMigrate all settings labels from aliases to using settings package (#3032)c3d71edd

main
7.6 KiB251 linesraw
1"""End to end tests for rust toolchains."""
2
3load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4load("@bazel_skylib//rules:write_file.bzl", "write_file")
5load("//rust:defs.bzl", "rust_library", "rust_shared_library")
6load("//rust:toolchain.bzl", "rust_stdlib_filegroup", "rust_toolchain")
7
8EXEC_TOOLCHAIN_FLAG = "missing"
9TOOLCHAIN_FLAG = "before"
10CONFIG_FLAG = "after"
11CRATE_FLAGS = {"cdylib": ["cdylib_flag"], "rlib": ["rlib_flag"]}
12TARGET_FLAG = "-Ccodegen-units=1"
13
14def _toolchain_adds_rustc_flags_impl(ctx, crate_type):
15    """ Tests adding extra_rustc_flags on the toolchain, asserts that:
16
17    - extra_rustc_flags added by the toolchain are applied BEFORE flags added by a config on the commandline
18    - The exec flags from the toolchain don't go on the commandline for a non-exec target
19    - crate type rustc flags are added
20    - target specific rustc flags are added AFTER the crate type rustc flags
21    """
22    env = analysistest.begin(ctx)
23    target = analysistest.target_under_test(env)
24    action = target[DepActionsInfo].actions[0]
25
26    asserts.equals(env, "Rustc", action.mnemonic)
27
28    asserts.true(
29        env,
30        action.argv[-3:] == [TOOLCHAIN_FLAG, CONFIG_FLAG, TARGET_FLAG],
31        "Unexpected rustc flags: {}\nShould have ended with: {}".format(
32            action.argv,
33            [TOOLCHAIN_FLAG, CONFIG_FLAG, TARGET_FLAG],
34        ),
35    )
36
37    asserts.true(
38        env,
39        action.argv[-4] == CRATE_FLAGS[crate_type][0],
40        "Unexpected rustc flags: {}\nShould have contained: {}".format(
41            action.argv,
42            CRATE_FLAGS["rlib"],
43        ),
44    )
45
46    for type in CRATE_FLAGS.keys():
47        if type == crate_type:
48            continue
49        asserts.false(
50            env,
51            CRATE_FLAGS[type][0] in action.argv,
52            "Unexpected rustc flags: {}\nShould not contain: {}".format(
53                action.argv,
54                CRATE_FLAGS[type],
55            ),
56        )
57
58    asserts.true(
59        env,
60        EXEC_TOOLCHAIN_FLAG not in action.argv,
61        "Found exec toolchain flag ({}) in rustc flags: {}".format(EXEC_TOOLCHAIN_FLAG, action.argv),
62    )
63
64    found_sysroot = False
65    for arg in action.argv:
66        if arg.startswith("--sysroot") and arg.endswith("test/toolchain/rust_extra_flags_toolchain"):
67            found_sysroot = True
68    asserts.true(
69        env,
70        found_sysroot,
71        "Missing --sysroot flag or --sysroot does not point to correct sysroot directory",
72    )
73
74    return analysistest.end(env)
75
76def _toolchain_adds_rustc_flags_lib_impl(ctx):
77    return _toolchain_adds_rustc_flags_impl(ctx, "rlib")
78
79def _toolchain_adds_rustc_flags_shared_lib_impl(ctx):
80    return _toolchain_adds_rustc_flags_impl(ctx, "cdylib")
81
82toolchain_adds_rustc_flags_lib_test = analysistest.make(
83    _toolchain_adds_rustc_flags_lib_impl,
84    config_settings = {
85        str(Label("//rust/settings:extra_rustc_flags")): [CONFIG_FLAG],
86        str(Label("//rust/settings:toolchain_generated_sysroot")): True,
87    },
88)
89
90toolchain_adds_rustc_flags_shared_lib_test = analysistest.make(
91    _toolchain_adds_rustc_flags_shared_lib_impl,
92    config_settings = {
93        str(Label("//rust/settings:extra_rustc_flags")): [CONFIG_FLAG],
94        str(Label("//rust/settings:toolchain_generated_sysroot")): True,
95    },
96)
97
98def _extra_toolchain_transition_impl(settings, _attr):
99    return {"//command_line_option:extra_toolchains": [
100        "@rules_rust//test/toolchain:extra_flags_toolchain",
101    ] + settings["//command_line_option:extra_toolchains"]}
102
103_extra_toolchain_transition = transition(
104    implementation = _extra_toolchain_transition_impl,
105    inputs = ["//command_line_option:extra_toolchains"],
106    outputs = ["//command_line_option:extra_toolchains"],
107)
108
109DepActionsInfo = provider(
110    "Contains information about dependencies actions.",
111    fields = {"actions": "List[Action]"},
112)
113
114def _collect_dep_actions_aspect_impl(target, ctx):
115    actions = []
116    actions.extend(target.actions)
117    for dep in ctx.rule.attr.deps:
118        actions.extend(dep[DepActionsInfo].actions)
119    return [DepActionsInfo(actions = actions)]
120
121collect_dep_actions_aspect = aspect(
122    implementation = _collect_dep_actions_aspect_impl,
123    attr_aspects = ["deps"],
124)
125
126def _extra_toolchain_wrapper_impl(ctx):
127    return [ctx.attr.dep[DepActionsInfo]]
128
129extra_toolchain_wrapper = rule(
130    implementation = _extra_toolchain_wrapper_impl,
131    attrs = {
132        "dep": attr.label(aspects = [collect_dep_actions_aspect]),
133        "_allowlist_function_transition": attr.label(
134            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
135        ),
136    },
137    cfg = _extra_toolchain_transition,
138)
139
140def _define_targets():
141    rust_library(
142        name = "lib",
143        srcs = ["lib.rs"],
144        edition = "2021",
145        rustc_flags = [TARGET_FLAG],
146    )
147
148    rust_shared_library(
149        name = "shared_lib",
150        srcs = ["lib.rs"],
151        edition = "2021",
152        rustc_flags = [TARGET_FLAG],
153    )
154
155    native.filegroup(
156        name = "stdlib_srcs",
157        srcs = ["config.txt"],
158    )
159    rust_stdlib_filegroup(
160        name = "std_libs",
161        srcs = [":stdlib_srcs"],
162    )
163    write_file(
164        name = "mock_rustc",
165        out = "mock_rustc.exe",
166        content = [],
167        is_executable = True,
168    )
169    write_file(
170        name = "mock_rustdoc",
171        out = "mock_rustdoc.exe",
172        content = [],
173        is_executable = True,
174    )
175
176    rust_toolchain(
177        name = "rust_extra_flags_toolchain",
178        binary_ext = "",
179        dylib_ext = ".so",
180        exec_triple = "x86_64-unknown-none",
181        target_triple = "x86_64-unknown-none",
182        rust_doc = ":mock_rustdoc",
183        rust_std = ":std_libs",
184        rustc = ":mock_rustc",
185        staticlib_ext = ".a",
186        stdlib_linkflags = [],
187        extra_rustc_flags = [TOOLCHAIN_FLAG],
188        extra_exec_rustc_flags = [EXEC_TOOLCHAIN_FLAG],
189        extra_rustc_flags_for_crate_types = CRATE_FLAGS,
190        visibility = ["//visibility:public"],
191    )
192
193    native.toolchain(
194        name = "extra_flags_toolchain",
195        toolchain = ":rust_extra_flags_toolchain",
196        toolchain_type = "@rules_rust//rust:toolchain",
197    )
198
199    extra_toolchain_wrapper(
200        name = "lib_with_extra_toolchain",
201        dep = ":lib",
202    )
203
204    extra_toolchain_wrapper(
205        name = "shared_lib_with_extra_toolchain",
206        dep = ":shared_lib",
207    )
208
209def _rust_stdlib_filegroup_provides_runfiles_test_impl(ctx):
210    env = analysistest.begin(ctx)
211    target = analysistest.target_under_test(env)
212    runfiles = target[DefaultInfo].default_runfiles
213    asserts.true(env, len(runfiles.files.to_list()) > 0)
214
215    return analysistest.end(env)
216
217rust_stdlib_filegroup_provides_runfiles_test = analysistest.make(
218    _rust_stdlib_filegroup_provides_runfiles_test_impl,
219)
220
221def toolchain_test_suite(name):
222    """ Instantiates tests for rust toolchains.
223
224    Args:
225        name: a name for the test suite
226    """
227    _define_targets()
228
229    toolchain_adds_rustc_flags_lib_test(
230        name = "toolchain_adds_rustc_flags_lib_test",
231        target_under_test = ":lib_with_extra_toolchain",
232    )
233
234    toolchain_adds_rustc_flags_shared_lib_test(
235        name = "toolchain_adds_rustc_flags_shared_lib_test",
236        target_under_test = ":shared_lib_with_extra_toolchain",
237    )
238
239    rust_stdlib_filegroup_provides_runfiles_test(
240        name = "rust_stdlib_filegroup_provides_runfiles_test",
241        target_under_test = ":std_libs",
242    )
243
244    native.test_suite(
245        name = name,
246        tests = [
247            ":toolchain_adds_rustc_flags_lib_test",
248            ":toolchain_adds_rustc_flags_shared_lib_test",
249            ":rust_stdlib_filegroup_provides_runfiles_test",
250        ],
251    )