Skip to content

Commit 57c23ee

Browse files
committed
Merge branch 'main' into linker
2 parents 43b8276 + da4de3b commit 57c23ee

File tree

6 files changed

+169
-21
lines changed

6 files changed

+169
-21
lines changed

rust/private/rustc.bzl

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -794,10 +794,18 @@ def collect_inputs(
794794
if linker_script:
795795
nolinkstamp_compile_direct_inputs.append(linker_script)
796796

797+
if crate_info.type in ["dylib", "cdylib"]:
798+
# For shared libraries we want to link C++ runtime library dynamically
799+
# (for example libstdc++.so or libc++.so).
800+
runtime_libs = cc_toolchain.dynamic_runtime_lib(feature_configuration = feature_configuration)
801+
else:
802+
runtime_libs = cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration)
803+
797804
nolinkstamp_compile_inputs = depset(
798805
nolinkstamp_compile_direct_inputs +
799806
additional_transitive_inputs,
800807
transitive = [
808+
runtime_libs,
801809
linker_depset,
802810
crate_info.srcs,
803811
transitive_crate_outputs,
@@ -2425,37 +2433,23 @@ def _add_native_link_flags(
24252433

24262434
args.add_all(make_link_flags_args, map_each = make_link_flags)
24272435

2428-
args.add_all(linkstamp_outs, before_each = "-C", format_each = "link-args=%s")
2436+
args.add_all(linkstamp_outs, format_each = "-Clink-args=%s")
24292437

24302438
if cc_toolchain:
24312439
if crate_type in ["dylib", "cdylib"]:
24322440
# For shared libraries we want to link C++ runtime library dynamically
24332441
# (for example libstdc++.so or libc++.so).
2434-
args.add_all(
2435-
cc_toolchain.dynamic_runtime_lib(feature_configuration = feature_configuration),
2436-
map_each = _get_dirname,
2437-
format_each = "-Lnative=%s",
2438-
)
2442+
runtime_libs = cc_toolchain.dynamic_runtime_lib(feature_configuration = feature_configuration)
2443+
args.add_all(runtime_libs, map_each = _get_dirname, format_each = "-Lnative=%s")
24392444
if include_link_flags:
2440-
args.add_all(
2441-
cc_toolchain.dynamic_runtime_lib(feature_configuration = feature_configuration),
2442-
map_each = get_lib_name,
2443-
format_each = "-ldylib=%s",
2444-
)
2445+
args.add_all(runtime_libs, map_each = get_lib_name, format_each = "-ldylib=%s")
24452446
else:
24462447
# For all other crate types we want to link C++ runtime library statically
24472448
# (for example libstdc++.a or libc++.a).
2448-
args.add_all(
2449-
cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration),
2450-
map_each = _get_dirname,
2451-
format_each = "-Lnative=%s",
2452-
)
2449+
runtime_libs = cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration)
2450+
args.add_all(runtime_libs, map_each = _get_dirname, format_each = "-Lnative=%s")
24532451
if include_link_flags:
2454-
args.add_all(
2455-
cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration),
2456-
map_each = get_lib_name,
2457-
format_each = "-lstatic=%s",
2458-
)
2452+
args.add_all(runtime_libs, map_each = get_lib_name, format_each = "-lstatic=%s")
24592453

24602454
def _get_dirname(file):
24612455
"""A helper function for `_add_native_link_flags`.
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: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
asserts.equals(env, action.mnemonic, "Rustc")
65+
inputs = action.inputs.to_list()
66+
for expected in ctx.attr.expected_inputs:
67+
asserts.true(
68+
env,
69+
any([input.path.endswith("/" + expected) for input in inputs]),
70+
"error: expected '{}' to be in inputs: '{}'".format(expected, inputs),
71+
)
72+
73+
return analysistest.end(env)
74+
75+
inputs_analysis_test = analysistest.make(
76+
impl = _inputs_analysis_test_impl,
77+
doc = """An analysistest to examine the inputs of a library target.""",
78+
attrs = {
79+
"expected_inputs": attr.string_list(),
80+
},
81+
)
82+
83+
def runtime_libs_test(name):
84+
"""Produces test shared and static library targets that are set up to use a custom cc_toolchain with custom runtime libs.
85+
86+
Args:
87+
name: The name of the test target.
88+
"""
89+
90+
test_cc_config(
91+
name = "%s/cc_toolchain_config" % name,
92+
)
93+
cc_toolchain(
94+
name = "%s/test_cc_toolchain_impl" % name,
95+
all_files = ":empty",
96+
compiler_files = ":empty",
97+
dwp_files = ":empty",
98+
linker_files = ":empty",
99+
objcopy_files = ":empty",
100+
strip_files = ":empty",
101+
supports_param_files = 0,
102+
toolchain_config = ":%s/cc_toolchain_config" % name,
103+
toolchain_identifier = "dummy_wasm32_cc",
104+
static_runtime_lib = ":dummy.a",
105+
dynamic_runtime_lib = ":dummy.so",
106+
)
107+
native.toolchain(
108+
name = "%s/test_cc_toolchain" % name,
109+
toolchain = ":%s/test_cc_toolchain_impl" % name,
110+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
111+
)
112+
113+
rust_shared_library(
114+
name = "%s/__shared_library" % name,
115+
edition = "2018",
116+
srcs = ["lib.rs"],
117+
tags = ["manual", "nobuild"],
118+
)
119+
120+
with_extra_toolchain(
121+
name = "%s/_shared_library" % name,
122+
extra_toolchain = ":%s/test_cc_toolchain" % name,
123+
target = "%s/__shared_library" % name,
124+
tags = ["manual"],
125+
)
126+
127+
inputs_analysis_test(
128+
name = "%s/shared_library" % name,
129+
target_under_test = "%s/_shared_library" % name,
130+
expected_inputs = ["dummy.so"],
131+
)
132+
133+
rust_static_library(
134+
name = "%s/__static_library" % name,
135+
edition = "2018",
136+
srcs = ["lib.rs"],
137+
tags = ["manual", "nobuild"],
138+
)
139+
140+
with_extra_toolchain(
141+
name = "%s/_static_library" % name,
142+
extra_toolchain = ":%s/test_cc_toolchain" % name,
143+
target = "%s/__static_library" % name,
144+
tags = ["manual"],
145+
)
146+
147+
inputs_analysis_test(
148+
name = "%s/static_library" % name,
149+
target_under_test = "%s/_static_library" % name,
150+
expected_inputs = ["dummy.a"],
151+
)

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)