Skip to content

Commit fcc8d35

Browse files
committed
Add test
1 parent ac55bf5 commit fcc8d35

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load(":cc_toolchain_runtime_lib_test.bzl", "runtime_libs_test")
2+
3+
runtime_libs_test(name = "runtime_libs_test")
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
"""
2+
Tests for handling of cc_toolchain's static_runtime_lib/dynamic_runtime_lib.
3+
"""
4+
5+
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
6+
load("@rules_cc//cc:cc_toolchain_config_lib.bzl", "feature")
7+
load("@rules_cc//cc:defs.bzl", "cc_toolchain")
8+
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
9+
load("//rust:defs.bzl", "rust_shared_library", "rust_static_library")
10+
11+
def _test_cc_config_impl(ctx):
12+
config_info = cc_common.create_cc_toolchain_config_info(
13+
ctx = ctx,
14+
toolchain_identifier = "test-cc-toolchain",
15+
host_system_name = "unknown",
16+
target_system_name = "unknown",
17+
target_cpu = "unknown",
18+
target_libc = "unknown",
19+
compiler = "unknown",
20+
abi_version = "unknown",
21+
abi_libc_version = "unknown",
22+
features = [
23+
feature(name = "static_link_cpp_runtimes", enabled = True),
24+
],
25+
)
26+
return config_info
27+
28+
test_cc_config = rule(
29+
implementation = _test_cc_config_impl,
30+
provides = [CcToolchainConfigInfo],
31+
)
32+
33+
def _with_extra_toolchain_transition_impl(_settings, attr):
34+
return {"//command_line_option:extra_toolchains": [attr.extra_toolchain]}
35+
36+
with_extra_toolchain_transition = transition(
37+
implementation = _with_extra_toolchain_transition_impl,
38+
inputs = [],
39+
outputs = ["//command_line_option:extra_toolchains"],
40+
)
41+
42+
DepActionsInfo = provider(
43+
"Contains information about dependencies actions.",
44+
fields = {"actions": "List[Action]"},
45+
)
46+
47+
def _with_extra_toolchain_impl(ctx):
48+
return [
49+
DepActionsInfo(actions = ctx.attr.target[0].actions),
50+
]
51+
52+
with_extra_toolchain = rule(
53+
implementation = _with_extra_toolchain_impl,
54+
attrs = {
55+
"extra_toolchain": attr.label(),
56+
"target": attr.label(cfg = with_extra_toolchain_transition),
57+
},
58+
)
59+
60+
def _inputs_analysis_test_impl(ctx):
61+
env = analysistest.begin(ctx)
62+
tut = analysistest.target_under_test(env)
63+
action = tut[DepActionsInfo].actions[0]
64+
inputs = action.inputs.to_list()
65+
for expected in ctx.attr.expected_inputs:
66+
asserts.true(
67+
env,
68+
any([input.path.endswith("/" + expected) for input in inputs]),
69+
"error: expected '{}' to be in inputs: '{}'".format(expected, inputs),
70+
)
71+
72+
return analysistest.end(env)
73+
74+
inputs_analysis_test = analysistest.make(
75+
impl = _inputs_analysis_test_impl,
76+
doc = """An analysistest to examine the inputs of a library target.""",
77+
attrs = {
78+
"expected_inputs": attr.string_list(),
79+
},
80+
)
81+
82+
def runtime_libs_test(name):
83+
"""Produces test shared and static library targets that are set up to use a custom cc_toolchain with custom runtime libs.
84+
85+
Args:
86+
name: The name of the test target.
87+
"""
88+
89+
test_cc_config(
90+
name = "%s/cc_toolchain_config" % name,
91+
)
92+
cc_toolchain(
93+
name = "%s/test_cc_toolchain_impl" % name,
94+
all_files = ":empty",
95+
compiler_files = ":empty",
96+
dwp_files = ":empty",
97+
linker_files = ":empty",
98+
objcopy_files = ":empty",
99+
strip_files = ":empty",
100+
supports_param_files = 0,
101+
toolchain_config = ":%s/cc_toolchain_config" % name,
102+
toolchain_identifier = "dummy_wasm32_cc",
103+
static_runtime_lib = ":dummy.a",
104+
dynamic_runtime_lib = ":dummy.so",
105+
)
106+
native.toolchain(
107+
name = "%s/test_cc_toolchain" % name,
108+
toolchain = ":%s/test_cc_toolchain_impl" % name,
109+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
110+
)
111+
112+
rust_shared_library(
113+
name = "%s/__shared_library" % name,
114+
edition = "2018",
115+
srcs = ["lib.rs"],
116+
tags = ["manual", "nobuild"],
117+
)
118+
119+
with_extra_toolchain(
120+
name = "%s/_shared_library" % name,
121+
extra_toolchain = ":%s/test_cc_toolchain" % name,
122+
target = "%s/__shared_library" % name,
123+
tags = ["manual"],
124+
)
125+
126+
inputs_analysis_test(
127+
name = "%s/shared_library" % name,
128+
target_under_test = "%s/_shared_library" % name,
129+
expected_inputs = ["dummy.so"],
130+
)
131+
132+
rust_static_library(
133+
name = "%s/__static_library" % name,
134+
edition = "2018",
135+
srcs = ["lib.rs"],
136+
tags = ["manual", "nobuild"],
137+
)
138+
139+
with_extra_toolchain(
140+
name = "%s/_static_library" % name,
141+
extra_toolchain = ":%s/test_cc_toolchain" % name,
142+
target = "%s/__static_library" % name,
143+
tags = ["manual"],
144+
)
145+
146+
inputs_analysis_test(
147+
name = "%s/static_library" % name,
148+
target_under_test = "%s/_static_library" % name,
149+
expected_inputs = ["dummy.a"],
150+
)

test/unit/cc_toolchain_runtime_lib/dummy.a

Whitespace-only changes.

test/unit/cc_toolchain_runtime_lib/dummy.so

Whitespace-only changes.

test/unit/cc_toolchain_runtime_lib/empty

Whitespace-only changes.

0 commit comments

Comments
 (0)