15
15
# buildifier: disable=module-docstring
16
16
load ("//rust/private:common.bzl" , "rust_common" )
17
17
load ("//rust/private:utils.bzl" , "find_toolchain" , "get_lib_name" , "get_preferred_artifact" )
18
+ load ("//util/launcher:launcher.bzl" , "create_launcher" )
18
19
19
20
def _rust_doc_test_impl (ctx ):
20
21
"""The implementation for the `rust_doc_test` rule
@@ -41,11 +42,7 @@ def _rust_doc_test_impl(ctx):
41
42
42
43
# Construct rustdoc test command, which will be written to a shell script
43
44
# to be executed to run the test.
44
- flags = _build_rustdoc_flags (dep_info , crate_info )
45
- if toolchain .os != "windows" :
46
- rust_doc_test = _build_rustdoc_test_bash_script (ctx , toolchain , flags , crate_info )
47
- else :
48
- rust_doc_test = _build_rustdoc_test_batch_script (ctx , toolchain , flags , crate_info )
45
+ flags = _build_rustdoc_flags (dep_info , crate_info , toolchain )
49
46
50
47
# The test script compiles the crate and runs it, so it needs both compile and runtime inputs.
51
48
compile_inputs = depset (
@@ -61,13 +58,26 @@ def _rust_doc_test_impl(ctx):
61
58
],
62
59
)
63
60
64
- return [DefaultInfo (
65
- runfiles = ctx .runfiles (
66
- files = compile_inputs .to_list (),
67
- collect_data = True ,
68
- ),
69
- executable = rust_doc_test ,
70
- )]
61
+ rustdoc = ctx .actions .declare_file (ctx .label .name + toolchain .binary_ext )
62
+ ctx .actions .symlink (
63
+ output = rustdoc ,
64
+ target_file = toolchain .rust_doc ,
65
+ is_executable = True ,
66
+ )
67
+
68
+ return create_launcher (
69
+ ctx = ctx ,
70
+ args = [
71
+ "--test" ,
72
+ crate_info .root .path ,
73
+ "--crate-name={}" .format (crate_info .name ),
74
+ ] + flags ,
75
+ toolchain = toolchain ,
76
+ providers = [DefaultInfo (
77
+ runfiles = ctx .runfiles (transitive_files = compile_inputs ),
78
+ )],
79
+ executable = rustdoc ,
80
+ )
71
81
72
82
# TODO: Replace with bazel-skylib's `path.dirname`. This requires addressing some dependency issues or
73
83
# generating docs will break.
@@ -82,12 +92,13 @@ def _dirname(path_str):
82
92
"""
83
93
return "/" .join (path_str .split ("/" )[:- 1 ])
84
94
85
- def _build_rustdoc_flags (dep_info , crate_info ):
95
+ def _build_rustdoc_flags (dep_info , crate_info , toolchain ):
86
96
"""Constructs the rustdoc script used to test `crate`.
87
97
88
98
Args:
89
99
dep_info (DepInfo): The DepInfo provider
90
100
crate_info (CrateInfo): The CrateInfo provider
101
+ toolchain (rust_toolchain): The curret `rust_toolchain`.
91
102
92
103
Returns:
93
104
list: A list of rustdoc flags (str)
@@ -121,80 +132,6 @@ def _build_rustdoc_flags(dep_info, crate_info):
121
132
122
133
return link_search_flags + link_flags + edition_flags
123
134
124
- _rustdoc_test_bash_script = """\
125
- #!/usr/bin/env bash
126
-
127
- set -e;
128
-
129
- {rust_doc} --test \\
130
- {crate_root} \\
131
- --crate-name={crate_name} \\
132
- {flags}
133
- """
134
-
135
- def _build_rustdoc_test_bash_script (ctx , toolchain , flags , crate_info ):
136
- """Generates a helper script for executing a rustdoc test for unix systems
137
-
138
- Args:
139
- ctx (ctx): The `rust_doc_test` rule's context object
140
- toolchain (ToolchainInfo): A rustdoc toolchain
141
- flags (list): A list of rustdoc flags (str)
142
- crate_info (CrateInfo): The CrateInfo provider
143
-
144
- Returns:
145
- File: An executable containing information for a rustdoc test
146
- """
147
- rust_doc_test = ctx .actions .declare_file (
148
- ctx .label .name + ".sh" ,
149
- )
150
- ctx .actions .write (
151
- output = rust_doc_test ,
152
- content = _rustdoc_test_bash_script .format (
153
- rust_doc = toolchain .rust_doc .short_path ,
154
- crate_root = crate_info .root .path ,
155
- crate_name = crate_info .name ,
156
- # TODO: Should be possible to do this with ctx.actions.Args, but can't seem to get them as a str and into the template.
157
- flags = " \\ \n " .join (flags ),
158
- ),
159
- is_executable = True ,
160
- )
161
- return rust_doc_test
162
-
163
- _rustdoc_test_batch_script = """\
164
- {rust_doc} --test ^
165
- {crate_root} ^
166
- --crate-name={crate_name} ^
167
- {flags}
168
- """
169
-
170
- def _build_rustdoc_test_batch_script (ctx , toolchain , flags , crate_info ):
171
- """Generates a helper script for executing a rustdoc test for windows systems
172
-
173
- Args:
174
- ctx (ctx): The `rust_doc_test` rule's context object
175
- toolchain (ToolchainInfo): A rustdoc toolchain
176
- flags (list): A list of rustdoc flags (str)
177
- crate_info (CrateInfo): The CrateInfo provider
178
-
179
- Returns:
180
- File: An executable containing information for a rustdoc test
181
- """
182
- rust_doc_test = ctx .actions .declare_file (
183
- ctx .label .name + ".bat" ,
184
- )
185
- ctx .actions .write (
186
- output = rust_doc_test ,
187
- content = _rustdoc_test_batch_script .format (
188
- rust_doc = toolchain .rust_doc .short_path .replace ("/" , "\\ " ),
189
- crate_root = crate_info .root .path ,
190
- crate_name = crate_info .name ,
191
- # TODO: Should be possible to do this with ctx.actions.Args, but can't seem to get them as a str and into the template.
192
- flags = " ^\n " .join (flags ),
193
- ),
194
- is_executable = True ,
195
- )
196
- return rust_doc_test
197
-
198
135
rust_doc_test = rule (
199
136
implementation = _rust_doc_test_impl ,
200
137
attrs = {
@@ -212,10 +149,20 @@ rust_doc_test = rule(
212
149
doc = "__deprecated__: use `crate`" ,
213
150
providers = [rust_common .crate_info ],
214
151
),
152
+ "_launcher" : attr .label (
153
+ executable = True ,
154
+ default = Label ("//util/launcher:launcher" ),
155
+ cfg = "exec" ,
156
+ doc = (
157
+ "A launcher executable for loading environment and argument files passed in via the " +
158
+ "`env` attribute and ensuring the variables are set for the underlying test executable."
159
+ ),
160
+ ),
215
161
},
216
- executable = True ,
217
162
test = True ,
218
- toolchains = [str (Label ("//rust:toolchain" ))],
163
+ toolchains = [
164
+ str (Label ("//rust:toolchain" )),
165
+ ],
219
166
incompatible_use_toolchain_transition = True ,
220
167
doc = """Runs Rust documentation tests.
221
168
0 commit comments