Skip to content

Commit f37aa76

Browse files
Add tests
1 parent e0e5ddc commit f37aa76

File tree

2 files changed

+176
-9
lines changed

2 files changed

+176
-9
lines changed

test/cargo_build_script/cc_args_and_env/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
load(
22
"cc_args_and_env_test.bzl",
3+
"ar_flags_test",
34
"bindir_absolute_test",
45
"bindir_relative_test",
56
"fsanitize_ignorelist_absolute_test",
67
"fsanitize_ignorelist_relative_test",
78
"isystem_absolute_test",
89
"isystem_relative_test",
10+
"legacy_cc_toolchain_test",
911
"sysroot_absolute_test",
1012
"sysroot_next_absolute_test",
1113
"sysroot_relative_test",
@@ -28,3 +30,7 @@ bindir_absolute_test(name = "bindir_absolute_test")
2830
fsanitize_ignorelist_absolute_test(name = "fsanitize_ignorelist_absolute_test")
2931

3032
fsanitize_ignorelist_relative_test(name = "fsanitize_ignorelist_relative_test")
33+
34+
ar_flags_test(name = "ar_flags_test")
35+
36+
legacy_cc_toolchain_test(name = "legacy_cc_toolchain_test")

test/cargo_build_script/cc_args_and_env/cc_args_and_env_test.bzl

Lines changed: 170 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ To verify the processed cargo cc_args, we use cc_args_and_env_analysis_test().
88
"""
99

1010
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
11-
load("@rules_cc//cc:action_names.bzl", "ACTION_NAME_GROUPS")
12-
load("@rules_cc//cc:cc_toolchain_config_lib.bzl", "feature", "flag_group", "flag_set")
11+
load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES", "ACTION_NAME_GROUPS")
12+
load("@rules_cc//cc:cc_toolchain_config_lib.bzl", "action_config", "feature", "flag_group", "flag_set", "tool", "tool_path")
1313
load("@rules_cc//cc:defs.bzl", "cc_toolchain")
1414
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
1515
load("//cargo:defs.bzl", "cargo_build_script")
1616

17+
_EXPECTED_CC_TOOLCHAIN_TOOLS = {
18+
"CC": "/usr/fake/gcc",
19+
"CXX": "/usr/fake/g++",
20+
"AR": "/usr/fake/ar",
21+
}
22+
1723
def _test_cc_config_impl(ctx):
1824
features = [
1925
feature(
@@ -30,9 +36,96 @@ def _test_cc_config_impl(ctx):
3036
),
3137
],
3238
),
39+
feature(
40+
name = "default_cpp_flags",
41+
enabled = True,
42+
flag_sets = [
43+
flag_set(
44+
actions = ACTION_NAME_GROUPS.all_cpp_compile_actions,
45+
flag_groups = ([
46+
flag_group(
47+
flags = ctx.attr.extra_cxx_compile_flags,
48+
),
49+
]),
50+
),
51+
],
52+
),
53+
feature(
54+
name = "default_ar_flags",
55+
enabled = True,
56+
flag_sets = [
57+
flag_set(
58+
actions = [ACTION_NAMES.cpp_link_static_library],
59+
flag_groups = ([
60+
flag_group(
61+
flags = ctx.attr.extra_ar_flags,
62+
),
63+
]),
64+
),
65+
],
66+
),
3367
]
68+
69+
tool_paths = []
70+
action_configs = []
71+
if ctx.attr.legacy_cc_toolchain:
72+
tool_paths = [
73+
tool_path(
74+
name = "gcc",
75+
path = _EXPECTED_CC_TOOLCHAIN_TOOLS["CC"],
76+
),
77+
tool_path(
78+
name = "cpp",
79+
path = _EXPECTED_CC_TOOLCHAIN_TOOLS["CXX"],
80+
),
81+
tool_path(
82+
name = "ar",
83+
path = _EXPECTED_CC_TOOLCHAIN_TOOLS["AR"],
84+
),
85+
# These need to be set to something to create a toolchain, but
86+
# is not tested here.
87+
tool_path(
88+
name = "ld",
89+
path = "/usr/ignored/false",
90+
),
91+
tool_path(
92+
name = "nm",
93+
path = "/usr/ignored/false",
94+
),
95+
tool_path(
96+
name = "objdump",
97+
path = "/usr/ignored/false",
98+
),
99+
tool_path(
100+
name = "strip",
101+
path = "/usr/ignored/false",
102+
),
103+
]
104+
else:
105+
action_configs = (
106+
action_config(
107+
action_name = ACTION_NAMES.c_compile,
108+
tools = [
109+
tool(path = _EXPECTED_CC_TOOLCHAIN_TOOLS["CC"]),
110+
],
111+
),
112+
action_config(
113+
action_name = ACTION_NAMES.cpp_compile,
114+
tools = [
115+
tool(path = _EXPECTED_CC_TOOLCHAIN_TOOLS["CXX"]),
116+
],
117+
),
118+
action_config(
119+
action_name = ACTION_NAMES.cpp_link_static_library,
120+
tools = [
121+
tool(path = _EXPECTED_CC_TOOLCHAIN_TOOLS["AR"]),
122+
],
123+
),
124+
)
125+
34126
config_info = cc_common.create_cc_toolchain_config_info(
35127
ctx = ctx,
128+
action_configs = action_configs,
36129
toolchain_identifier = "test-cc-toolchain",
37130
host_system_name = "unknown",
38131
target_system_name = "unknown",
@@ -42,13 +135,17 @@ def _test_cc_config_impl(ctx):
42135
abi_version = "unknown",
43136
abi_libc_version = "unknown",
44137
features = features,
138+
tool_paths = tool_paths,
45139
)
46140
return config_info
47141

48142
test_cc_config = rule(
49143
implementation = _test_cc_config_impl,
50144
attrs = {
51145
"extra_cc_compile_flags": attr.string_list(),
146+
"extra_cxx_compile_flags": attr.string_list(),
147+
"extra_ar_flags": attr.string_list(),
148+
"legacy_cc_toolchain": attr.bool(default = False),
52149
},
53150
provides = [CcToolchainConfigInfo],
54151
)
@@ -84,27 +181,66 @@ def _cc_args_and_env_analysis_test_impl(ctx):
84181
env = analysistest.begin(ctx)
85182
tut = analysistest.target_under_test(env)
86183
cargo_action = tut[DepActionsInfo].actions[0]
87-
cflags = cargo_action.env["CFLAGS"].split(" ")
88-
for flag in ctx.attr.expected_cflags:
89-
asserts.true(
184+
185+
for env_var, expected_path in _EXPECTED_CC_TOOLCHAIN_TOOLS.items():
186+
if ctx.attr.legacy_cc_toolchain and env_var == "CXX":
187+
# When using the legacy tool_path toolchain configuration approach,
188+
# the CXX tool is forced to be the same as the the CC tool.
189+
# See: https://github.com/bazelbuild/bazel/blob/14840856986f21b54330e352b83d687825648889/src/main/starlark/builtins_bzl/common/cc/toolchain_config/legacy_features.bzl#L1296-L1304
190+
expected_path = _EXPECTED_CC_TOOLCHAIN_TOOLS["CC"]
191+
192+
actual_path = cargo_action.env.get(env_var)
193+
asserts.false(
194+
env,
195+
actual_path == None,
196+
"error: '{}' tool unset".format(env_var),
197+
)
198+
asserts.equals(
90199
env,
91-
flag in cflags,
92-
"error: expected '{}' to be in cargo CFLAGS: '{}'".format(flag, cflags),
200+
expected_path,
201+
actual_path,
202+
"error: '{}' tool path '{}' does not match expected '{}'".format(
203+
env_var,
204+
actual_path,
205+
expected_path,
206+
),
93207
)
94208

209+
_ENV_VAR_TO_EXPECTED_ARGS = {
210+
"CFLAGS": ctx.attr.expected_cflags,
211+
"CXXFLAGS": ctx.attr.expected_cxxflags,
212+
"ARFLAGS": ctx.attr.expected_arflags,
213+
}
214+
215+
for env_var, expected_flags in _ENV_VAR_TO_EXPECTED_ARGS.items():
216+
actual_flags = cargo_action.env[env_var].split(" ")
217+
for flag in expected_flags:
218+
asserts.true(
219+
env,
220+
flag in actual_flags,
221+
"error: expected '{}' to be in cargo {}: '{}'".format(flag, env_var, actual_flags),
222+
)
223+
95224
return analysistest.end(env)
96225

97226
cc_args_and_env_analysis_test = analysistest.make(
98227
impl = _cc_args_and_env_analysis_test_impl,
99228
doc = """An analysistest to examine the custom cargo flags of an cargo_build_script target.""",
100229
attrs = {
101-
"expected_cflags": attr.string_list(),
230+
"expected_cflags": attr.string_list(default = ["-Wall"]),
231+
"expected_cxxflags": attr.string_list(default = ["-fno-rtti"]),
232+
"expected_arflags": attr.string_list(default = ["-x"]),
233+
"legacy_cc_toolchain": attr.bool(default = False),
102234
},
103235
)
104236

105237
def cargo_build_script_with_extra_cc_compile_flags(
238+
*,
106239
name,
107-
extra_cc_compile_flags):
240+
extra_cc_compile_flags = ["-Wall"],
241+
extra_cxx_compile_flags = ["-fno-rtti"],
242+
extra_ar_flags = ["-x"],
243+
legacy_cc_toolchain = False):
108244
"""Produces a test cargo_build_script target that's set up to use a custom cc_toolchain with the extra_cc_compile_flags.
109245
110246
This is achieved by creating a cascade of targets:
@@ -121,6 +257,9 @@ def cargo_build_script_with_extra_cc_compile_flags(
121257
test_cc_config(
122258
name = "%s/cc_toolchain_config" % name,
123259
extra_cc_compile_flags = extra_cc_compile_flags,
260+
extra_cxx_compile_flags = extra_cxx_compile_flags,
261+
extra_ar_flags = extra_ar_flags,
262+
legacy_cc_toolchain = legacy_cc_toolchain,
124263
)
125264
cc_toolchain(
126265
name = "%s/test_cc_toolchain_impl" % name,
@@ -250,3 +389,25 @@ def fsanitize_ignorelist_absolute_test(name):
250389
target_under_test = "%s/cargo_build_script" % name,
251390
expected_cflags = ["-fsanitize-ignorelist=/test/absolute/path"],
252391
)
392+
393+
def ar_flags_test(name):
394+
cargo_build_script_with_extra_cc_compile_flags(
395+
name = "%s/cargo_build_script" % name,
396+
extra_ar_flags = ["-static"],
397+
)
398+
cc_args_and_env_analysis_test(
399+
name = name,
400+
target_under_test = "%s/cargo_build_script" % name,
401+
expected_arflags = ["-static"],
402+
)
403+
404+
def legacy_cc_toolchain_test(name):
405+
cargo_build_script_with_extra_cc_compile_flags(
406+
name = "%s/cargo_build_script" % name,
407+
legacy_cc_toolchain = True,
408+
)
409+
cc_args_and_env_analysis_test(
410+
name = name,
411+
target_under_test = "%s/cargo_build_script" % name,
412+
legacy_cc_toolchain = True,
413+
)

0 commit comments

Comments
 (0)