Skip to content

Commit 02061f2

Browse files
committed
Add test
1 parent e249725 commit 02061f2

File tree

2 files changed

+183
-1
lines changed

2 files changed

+183
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
out=""
4+
dep=""
5+
while [[ $# -gt 0 ]]; do
6+
if [[ "$1" == "-o" ]]; then
7+
out="$2"
8+
shift
9+
elif [[ "$1" == "-MF" ]]; then
10+
dep="$2"
11+
shift
12+
fi
13+
shift
14+
done
15+
if [[ -n "$dep" ]]; then
16+
mkdir -p "$(dirname "$dep")"
17+
: > "$dep"
18+
fi
19+
if [[ -n "$out" ]]; then
20+
mkdir -p "$(dirname "$out")"
21+
: > "$out"
22+
fi

test/integration/cc_common_link/unit/cc_common_link_test.bzl

Lines changed: 161 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"""Unittests for the --experimental_use_cc_common_link build setting."""
22

33
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
45
load("@bazel_skylib//rules:write_file.bzl", "write_file")
5-
load("@rules_cc//cc:defs.bzl", "cc_library")
6+
load("@rules_cc//cc:cc_toolchain_config_lib.bzl", "artifact_name_pattern", "tool_path")
7+
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_toolchain")
8+
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
69
load(
710
"@rules_rust//rust:defs.bzl",
811
"rust_binary",
@@ -16,6 +19,54 @@ DepActionsInfo = provider(
1619
fields = {"actions": "List[Action]"},
1720
)
1821

22+
def _artifact_name_patterns_cc_config_impl(ctx):
23+
tool_exec = ctx.actions.declare_file("artifact_name_patterns_fake_cc.sh")
24+
ctx.actions.symlink(
25+
output = tool_exec,
26+
target_file = ctx.file.tool,
27+
is_executable = True,
28+
)
29+
30+
return cc_common.create_cc_toolchain_config_info(
31+
ctx = ctx,
32+
toolchain_identifier = "artifact-name-pattern-cc-toolchain",
33+
host_system_name = "unknown",
34+
target_system_name = "unknown",
35+
target_cpu = "unknown",
36+
target_libc = "unknown",
37+
compiler = "unknown",
38+
abi_version = "unknown",
39+
abi_libc_version = "unknown",
40+
cxx_builtin_include_directories = [],
41+
artifact_name_patterns = [
42+
artifact_name_pattern(
43+
category_name = "executable",
44+
prefix = "",
45+
extension = ".exe",
46+
),
47+
],
48+
tool_paths = [tool_path(name = name, path = tool_exec.path) for name in [
49+
"gcc",
50+
"ld",
51+
"ar",
52+
"cpp",
53+
"gcov",
54+
"nm",
55+
"objcopy",
56+
"objdump",
57+
"strip",
58+
]],
59+
)
60+
61+
artifact_name_patterns_cc_config = rule(
62+
implementation = _artifact_name_patterns_cc_config_impl,
63+
attrs = {
64+
"tool": attr.label(
65+
allow_single_file = True,
66+
),
67+
},
68+
)
69+
1970
def _collect_dep_actions_aspect_impl(target, _ctx):
2071
return [DepActionsInfo(actions = target.actions)]
2172

@@ -61,6 +112,38 @@ with_collect_dep_actions = rule(
61112
},
62113
)
63114

115+
def _artifact_name_patterns_transition_impl(_settings, attr):
116+
return {
117+
"@rules_rust//rust/settings:experimental_use_cc_common_link": True,
118+
"//command_line_option:extra_toolchains": attr.extra_toolchains,
119+
}
120+
121+
artifact_name_patterns_transition = transition(
122+
inputs = [],
123+
outputs = [
124+
"@rules_rust//rust/settings:experimental_use_cc_common_link",
125+
"//command_line_option:extra_toolchains",
126+
],
127+
implementation = _artifact_name_patterns_transition_impl,
128+
)
129+
130+
def _use_cc_common_link_with_toolchains_impl(ctx):
131+
return [ctx.attr.target[0][DepActionsInfo], ctx.attr.target[0][OutputGroupInfo]]
132+
133+
use_cc_common_link_with_toolchains = rule(
134+
implementation = _use_cc_common_link_with_toolchains_impl,
135+
attrs = {
136+
"target": attr.label(
137+
cfg = artifact_name_patterns_transition,
138+
aspects = [collect_dep_actions_aspect],
139+
),
140+
"extra_toolchains": attr.string_list(),
141+
"_allowlist_function_transition": attr.label(
142+
default = Label("@bazel_tools//tools/allowlists/function_transition_allowlist"),
143+
),
144+
},
145+
)
146+
64147
def _with_exec_cfg_impl(ctx):
65148
return [ctx.attr.target[DepActionsInfo]]
66149

@@ -103,6 +186,33 @@ def _use_cc_common_link_test(ctx):
103186

104187
use_cc_common_link_test = analysistest.make(_use_cc_common_link_test, attrs = {"expect_pdb": attr.bool()})
105188

189+
def _artifact_name_patterns_link_output_test(ctx):
190+
env = analysistest.begin(ctx)
191+
tut = analysistest.target_under_test(env)
192+
link_actions = [action for action in tut[DepActionsInfo].actions if action.mnemonic == "CppLink"]
193+
asserts.equals(env, 1, len(link_actions))
194+
195+
link_outputs = [output.basename for output in link_actions[0].outputs.to_list()]
196+
asserts.true(
197+
env,
198+
ctx.attr.expected_output in link_outputs,
199+
"Expected '{}' in link outputs: {}".format(ctx.attr.expected_output, link_outputs),
200+
)
201+
asserts.false(
202+
env,
203+
any([name.endswith(".exe.exe") for name in link_outputs]),
204+
"Unexpected double-extension in link outputs: {}".format(link_outputs),
205+
)
206+
207+
return analysistest.end(env)
208+
209+
artifact_name_patterns_link_output_test = analysistest.make(
210+
_artifact_name_patterns_link_output_test,
211+
attrs = {
212+
"expected_output": attr.string(),
213+
},
214+
)
215+
106216
def _custom_malloc_test(ctx):
107217
env = analysistest.begin(ctx)
108218
tut = analysistest.target_under_test(env)
@@ -214,6 +324,55 @@ def _cc_common_link_test_targets():
214324
target_under_test = ":cdylib_with_cc_common_link",
215325
)
216326

327+
# Toolchains configured with custom artifact_name_patterns to ensure we honor the declared main output.
328+
native.filegroup(
329+
name = "artifact_name_patterns_empty",
330+
srcs = [],
331+
)
332+
native.filegroup(
333+
name = "artifact_name_patterns_tool",
334+
srcs = ["artifact_name_patterns_fake_cc.sh"],
335+
)
336+
337+
artifact_name_patterns_cc_config(
338+
name = "artifact_name_patterns_cc_config_target",
339+
tool = ":artifact_name_patterns_fake_cc.sh",
340+
)
341+
342+
cc_toolchain(
343+
name = "artifact_name_patterns_cc_toolchain_impl",
344+
all_files = ":artifact_name_patterns_tool",
345+
compiler_files = ":artifact_name_patterns_tool",
346+
dwp_files = ":artifact_name_patterns_empty",
347+
linker_files = ":artifact_name_patterns_tool",
348+
objcopy_files = ":artifact_name_patterns_tool",
349+
strip_files = ":artifact_name_patterns_tool",
350+
supports_param_files = 0,
351+
toolchain_config = ":artifact_name_patterns_cc_config_target",
352+
toolchain_identifier = "artifact-name-pattern-cc-toolchain",
353+
)
354+
355+
rust_binary(
356+
name = "artifact_name_patterns_bin",
357+
binary_name = "artifact_name_patterns_bin.exe",
358+
srcs = ["bin.rs"],
359+
edition = "2018",
360+
)
361+
362+
use_cc_common_link_with_toolchains(
363+
name = "artifact_name_patterns_bin_with_cc_common_link",
364+
target = ":artifact_name_patterns_bin",
365+
extra_toolchains = [
366+
"//unit:artifact_name_patterns_cc_toolchain_impl",
367+
],
368+
)
369+
370+
artifact_name_patterns_link_output_test(
371+
name = "artifact_name_patterns_link_output_test",
372+
target_under_test = ":artifact_name_patterns_bin_with_cc_common_link",
373+
expected_output = "artifact_name_patterns_bin.exe",
374+
)
375+
217376
custom_malloc_test(
218377
name = "custom_malloc_on_binary_test",
219378
target_under_test = ":bin_with_cc_common_link",
@@ -225,6 +384,7 @@ def _cc_common_link_test_targets():
225384
"use_cc_common_link_on_test",
226385
"use_cc_common_link_on_crate_test",
227386
"use_cc_common_link_on_cdylib",
387+
"artifact_name_patterns_link_output_test",
228388
"custom_malloc_on_binary_test",
229389
]
230390

0 commit comments

Comments
 (0)