Skip to content

Commit 643f750

Browse files
committed
Add test
1 parent e249725 commit 643f750

File tree

2 files changed

+182
-1
lines changed

2 files changed

+182
-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: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
44
load("@bazel_skylib//rules:write_file.bzl", "write_file")
5-
load("@rules_cc//cc:defs.bzl", "cc_library")
5+
load("@rules_cc//cc:cc_toolchain_config_lib.bzl", "artifact_name_pattern", "tool_path")
6+
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_toolchain")
7+
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
68
load(
79
"@rules_rust//rust:defs.bzl",
810
"rust_binary",
@@ -16,6 +18,54 @@ DepActionsInfo = provider(
1618
fields = {"actions": "List[Action]"},
1719
)
1820

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

@@ -61,6 +111,38 @@ with_collect_dep_actions = rule(
61111
},
62112
)
63113

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

@@ -103,6 +185,33 @@ def _use_cc_common_link_test(ctx):
103185

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

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

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

0 commit comments

Comments
 (0)