Skip to content

Commit 6d42ca3

Browse files
mmaslankaprvfmeum
andauthored
Added option to skip linking against libunwind (#346)
It may sometimes be desired not to use `libunwind`. Added a `bool_flag` that allows the user to disable linking against the `libunwind` library. To disable linking against libunwind use the following command line parameter: ``` --@toolchains_llvm//toolchain/config:libunwind=False ``` --------- Signed-off-by: Michał Maślanka <michal@redpanda.com> Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
1 parent 4ab573b commit 6d42ca3

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

toolchain/cc_toolchain_config.bzl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ def cc_toolchain_config(
141141
# Similar to link_flags, but placed later in the command line such that
142142
# unused symbols are not stripped.
143143
link_libs = []
144+
libunwind_link_flags = []
145+
compiler_rt_link_flags = []
144146

145147
# Flags for ar.
146148
archive_flags = []
@@ -205,13 +207,14 @@ def cc_toolchain_config(
205207
link_flags.extend([
206208
"-l:libc++.a",
207209
"-l:libc++abi.a",
210+
])
211+
compiler_rt_link_flags = ["-rtlib=compiler-rt"]
212+
libunwind_link_flags = [
208213
"-l:libunwind.a",
209-
# Compiler runtime features.
210-
"-rtlib=compiler-rt",
211214
# To support libunwind.
212215
"-lpthread",
213216
"-ldl",
214-
])
217+
]
215218
else:
216219
# Several system libraries on macOS dynamically link libc++ and
217220
# libc++abi, so static linking them becomes a problem. We need to
@@ -223,11 +226,13 @@ def cc_toolchain_config(
223226
"-L{}/usr/lib".format(sysroot_path),
224227
"-lc++",
225228
"-lc++abi",
226-
"-Bstatic",
227-
"-lunwind",
228229
"-Bdynamic",
229230
"-L{}lib".format(toolchain_path_prefix),
230231
])
232+
libunwind_link_flags = [
233+
"-Bstatic",
234+
"-lunwind",
235+
]
231236

232237
elif stdlib == "libc++":
233238
cxx_flags = [
@@ -340,7 +345,8 @@ def cc_toolchain_config(
340345
dbg_compile_flags = dbg_compile_flags,
341346
opt_compile_flags = opt_compile_flags,
342347
cxx_flags = cxx_flags,
343-
link_flags = link_flags,
348+
link_flags = link_flags + select({str(Label("@toolchains_llvm//toolchain/config:use_libunwind")): libunwind_link_flags, "//conditions:default": []}) +
349+
select({str(Label("@toolchains_llvm//toolchain/config:use_compiler_rt")): compiler_rt_link_flags, "//conditions:default": []}),
344350
archive_flags = archive_flags,
345351
link_libs = link_libs,
346352
opt_link_flags = opt_link_flags,

toolchain/config/BUILD.bazel

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2021 The Bazel Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
16+
17+
bool_flag(
18+
name = "libunwind",
19+
build_setting_default = True,
20+
visibility = ["//visibility:public"],
21+
)
22+
23+
bool_flag(
24+
name = "compiler-rt",
25+
build_setting_default = True,
26+
visibility = ["//visibility:public"],
27+
)
28+
29+
config_setting(
30+
name = "use_libunwind",
31+
flag_values = {":libunwind": "True"},
32+
visibility = ["//visibility:public"],
33+
)
34+
35+
config_setting(
36+
name = "use_compiler_rt",
37+
flag_values = {":compiler-rt": "True"},
38+
visibility = ["//visibility:public"],
39+
)

0 commit comments

Comments
 (0)