1
1
"""Analysis test for for rust_bindgen_library rule."""
2
2
3
3
load ("@bazel_skylib//rules:write_file.bzl" , "write_file" )
4
+ load ("@rules_cc//cc:action_names.bzl" , "ALL_CPP_COMPILE_ACTION_NAMES" )
5
+ load ("@rules_cc//cc:cc_toolchain_config_lib.bzl" , "feature" , "flag_group" , "flag_set" )
4
6
load ("@rules_cc//cc:defs.bzl" , "cc_library" , "cc_toolchain" )
5
7
load ("@rules_cc//cc/common:cc_common.bzl" , "cc_common" )
6
8
load ("@rules_rust//rust:defs.bzl" , "rust_binary" )
@@ -9,6 +11,24 @@ load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
9
11
load ("@rules_testing//lib:truth.bzl" , "matching" )
10
12
11
13
def _fake_cc_toolchain_config_impl (ctx ):
14
+ xclang_flags_feature = feature (
15
+ name = "xclang_flags" ,
16
+ enabled = True ,
17
+ flag_sets = [
18
+ flag_set (
19
+ actions = ALL_CPP_COMPILE_ACTION_NAMES ,
20
+ flag_groups = [
21
+ flag_group (flags = [
22
+ "-Xclang" ,
23
+ "-fexperimental-optimized-noescape" ,
24
+ "-Xclang" ,
25
+ "-fcolor-diagnostics" ,
26
+ ]),
27
+ ],
28
+ ),
29
+ ],
30
+ )
31
+
12
32
return cc_common .create_cc_toolchain_config_info (
13
33
ctx = ctx ,
14
34
toolchain_identifier = "fake-toolchain" ,
@@ -19,6 +39,7 @@ def _fake_cc_toolchain_config_impl(ctx):
19
39
compiler = "unknown" ,
20
40
abi_version = "unknown" ,
21
41
abi_libc_version = "unknown" ,
42
+ features = [xclang_flags_feature ],
22
43
)
23
44
24
45
_fake_cc_toolchain_config = rule (
@@ -37,7 +58,7 @@ def _fake_cc_toolchain(name):
37
58
write_file (
38
59
name = stdbool_file ,
39
60
content = [],
40
- out = "my /resource/dir/include/stdbool.h" ,
61
+ out = name + " /resource/dir/include/stdbool.h" ,
41
62
)
42
63
43
64
all_files = name + "_all_files"
@@ -60,6 +81,21 @@ def _fake_cc_toolchain(name):
60
81
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type" ,
61
82
)
62
83
84
+ def _create_simple_rust_bindgen_library (test_name ):
85
+ cc_library (
86
+ name = test_name + "_cc" ,
87
+ hdrs = ["simple.h" ],
88
+ srcs = ["simple.cc" ],
89
+ )
90
+
91
+ rust_bindgen_library (
92
+ name = test_name + "_rust_bindgen" ,
93
+ cc_lib = test_name + "_cc" ,
94
+ header = "simple.h" ,
95
+ tags = ["manual" ],
96
+ edition = "2021" ,
97
+ )
98
+
63
99
def _test_cc_linkopt_impl (env , target ):
64
100
# Assert
65
101
env .expect .that_action (target .actions [0 ]) \
@@ -161,31 +197,47 @@ def _test_resource_dir_impl(env, target):
161
197
env .expect .that_action (target .actions [0 ]).argv ().contains_predicate (
162
198
matching .all (
163
199
matching .str_startswith ("-resource-dir=" ),
164
- matching .str_endswith ("my /resource/dir" ),
200
+ matching .str_endswith ("/resource/dir" ),
165
201
),
166
202
)
167
203
168
204
def _test_resource_dir (name ):
169
205
_fake_cc_toolchain (name + "_toolchain" )
170
206
171
- cc_library (
172
- name = name + "_cc" ,
173
- hdrs = ["simple.h" ],
174
- srcs = ["simple.cc" ],
207
+ _create_simple_rust_bindgen_library (name )
208
+
209
+ analysis_test (
210
+ name = name ,
211
+ target = name + "_rust_bindgen__bindgen" ,
212
+ impl = _test_resource_dir_impl ,
213
+ config_settings = {
214
+ "//command_line_option:extra_toolchains" : [str (native .package_relative_label (name + "_toolchain" ))],
215
+ },
175
216
)
176
217
177
- rust_bindgen_library (
178
- name = name + "_rust_bindgen" ,
179
- cc_lib = name + "_cc" ,
180
- header = "simple.h" ,
181
- tags = ["manual" ],
182
- edition = "2021" ,
218
+ def _test_strip_xclang_impl (env , target ):
219
+ env .expect .that_int (len (target .actions )).is_greater_than (0 )
220
+ env .expect .that_action (target .actions [0 ]).mnemonic ().contains ("RustBindgen" )
221
+ env .expect .that_action (target .actions [0 ]).not_contains_arg (
222
+ "-fexperimental-optimized-noescape" ,
223
+ )
224
+ env .expect .that_action (target .actions [0 ]).contains_at_least_args (
225
+ ["-Xclang" , "-fcolor-diagnostics" ],
183
226
)
184
227
228
+ def _test_strip_xclang (name ):
229
+ # Test that we strip certain `-Xclang` flags defined by forks of Clang
230
+ # that upstream Clang doesn't know about, such as
231
+ # `-fexperimental-optimized-noescape`. (This is added by the toolchain.)
232
+
233
+ _fake_cc_toolchain (name + "_toolchain" )
234
+
235
+ _create_simple_rust_bindgen_library (name )
236
+
185
237
analysis_test (
186
238
name = name ,
187
239
target = name + "_rust_bindgen__bindgen" ,
188
- impl = _test_resource_dir_impl ,
240
+ impl = _test_strip_xclang_impl ,
189
241
config_settings = {
190
242
"//command_line_option:extra_toolchains" : [str (native .package_relative_label (name + "_toolchain" ))],
191
243
},
@@ -199,5 +251,6 @@ def bindgen_test_suite(name):
199
251
_test_cc_lib_object_merging ,
200
252
_test_cc_lib_object_merging_disabled ,
201
253
_test_resource_dir ,
254
+ _test_strip_xclang ,
202
255
],
203
256
)
0 commit comments