fork/rules_rust

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

UebelAndreUpdated `rust_toolchain.target_json` to take encoded json strings (#1810)92834930

rust-analyzer
4.5 KiB158 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")
6load("//rust:toolchain.bzl", "rust_stdlib_filegroup", "rust_toolchain")
7
8EXEC_TOOLCHAIN_FLAG = "missing"
9TOOLCHAIN_FLAG = "before"
10CONFIG_FLAG = "after"
11
12def _toolchain_adds_rustc_flags_impl(ctx):
13    """ Tests adding extra_rustc_flags on the toolchain, asserts that:
14
15    - extra_rustc_flags added by the toolchain are applied BEFORE flags added by a config on the commandline
16    - The exec flags from the toolchain don't go on the commandline for a non-exec target
17    """
18    env = analysistest.begin(ctx)
19    target = analysistest.target_under_test(env)
20    action = target[DepActionsInfo].actions[0]
21
22    asserts.equals(env, "Rustc", action.mnemonic)
23
24    asserts.true(
25        env,
26        action.argv[-2:] == [TOOLCHAIN_FLAG, CONFIG_FLAG],
27        "Unexpected rustc flags: {}\nShould have ended with: {}".format(
28            action.argv,
29            [TOOLCHAIN_FLAG, CONFIG_FLAG],
30        ),
31    )
32
33    asserts.true(
34        env,
35        EXEC_TOOLCHAIN_FLAG not in action.argv,
36        "Found exec toolchain flag ({}) in rustc flags: {}".format(EXEC_TOOLCHAIN_FLAG, action.argv),
37    )
38
39    return analysistest.end(env)
40
41toolchain_adds_rustc_flags_test = analysistest.make(
42    _toolchain_adds_rustc_flags_impl,
43    config_settings = {
44        "@//:extra_rustc_flags": [CONFIG_FLAG],
45    },
46)
47
48def _extra_toolchain_transition_impl(settings, _attr):
49    return {"//command_line_option:extra_toolchains": [
50        "@rules_rust//test/toolchain:extra_flags_toolchain",
51    ] + settings["//command_line_option:extra_toolchains"]}
52
53_extra_toolchain_transition = transition(
54    implementation = _extra_toolchain_transition_impl,
55    inputs = ["//command_line_option:extra_toolchains"],
56    outputs = ["//command_line_option:extra_toolchains"],
57)
58
59DepActionsInfo = provider(
60    "Contains information about dependencies actions.",
61    fields = {"actions": "List[Action]"},
62)
63
64def _collect_dep_actions_aspect_impl(target, ctx):
65    actions = []
66    actions.extend(target.actions)
67    for dep in ctx.rule.attr.deps:
68        actions.extend(dep[DepActionsInfo].actions)
69    return [DepActionsInfo(actions = actions)]
70
71collect_dep_actions_aspect = aspect(
72    implementation = _collect_dep_actions_aspect_impl,
73    attr_aspects = ["deps"],
74)
75
76def _extra_toolchain_wrapper_impl(ctx):
77    return [ctx.attr.dep[DepActionsInfo]]
78
79extra_toolchain_wrapper = rule(
80    implementation = _extra_toolchain_wrapper_impl,
81    attrs = {
82        "dep": attr.label(aspects = [collect_dep_actions_aspect]),
83        "_allowlist_function_transition": attr.label(
84            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
85        ),
86    },
87    cfg = _extra_toolchain_transition,
88)
89
90def _define_targets():
91    rust_library(
92        name = "lib",
93        srcs = ["lib.rs"],
94        edition = "2021",
95    )
96
97    native.filegroup(
98        name = "stdlib_srcs",
99        srcs = ["config.txt"],
100    )
101    rust_stdlib_filegroup(
102        name = "std_libs",
103        srcs = [":stdlib_srcs"],
104    )
105    write_file(
106        name = "mock_rustc",
107        out = "mock_rustc.exe",
108        content = [],
109        is_executable = True,
110    )
111    write_file(
112        name = "mock_rustdoc",
113        out = "mock_rustdoc.exe",
114        content = [],
115        is_executable = True,
116    )
117
118    rust_toolchain(
119        name = "rust_extra_flags_toolchain",
120        binary_ext = "",
121        dylib_ext = ".so",
122        exec_triple = "x86_64-unknown-none",
123        target_triple = "x86_64-unknown-none",
124        os = "linux",
125        rust_doc = ":mock_rustdoc",
126        rust_std = ":std_libs",
127        rustc = ":mock_rustc",
128        staticlib_ext = ".a",
129        stdlib_linkflags = [],
130        extra_rustc_flags = [TOOLCHAIN_FLAG],
131        extra_exec_rustc_flags = [EXEC_TOOLCHAIN_FLAG],
132    )
133
134    native.toolchain(
135        name = "extra_flags_toolchain",
136        toolchain = ":rust_extra_flags_toolchain",
137        toolchain_type = "@rules_rust//rust:toolchain",
138    )
139
140    extra_toolchain_wrapper(
141        name = "lib_with_extra_toolchain",
142        dep = ":lib",
143    )
144
145def toolchain_test_suite(name):
146    _define_targets()
147
148    toolchain_adds_rustc_flags_test(
149        name = "toolchain_adds_rustc_flags_test",
150        target_under_test = ":lib_with_extra_toolchain",
151    )
152
153    native.test_suite(
154        name = name,
155        tests = [
156            ":toolchain_adds_rustc_flags_test",
157        ],
158    )