diff --git a/toolchain/cc_toolchain_config.bzl b/toolchain/cc_toolchain_config.bzl index c1b0d922..2e555bef 100644 --- a/toolchain/cc_toolchain_config.bzl +++ b/toolchain/cc_toolchain_config.bzl @@ -141,6 +141,7 @@ def cc_toolchain_config( # Similar to link_flags, but placed later in the command line such that # unused symbols are not stripped. link_libs = [] + libunwind_link_flags = [] # Flags for ar. archive_flags = [] @@ -205,13 +206,15 @@ def cc_toolchain_config( link_flags.extend([ "-l:libc++.a", "-l:libc++abi.a", + ]) + libunwind_link_flags = [ "-l:libunwind.a", # Compiler runtime features. "-rtlib=compiler-rt", # To support libunwind. "-lpthread", "-ldl", - ]) + ] else: # Several system libraries on macOS dynamically link libc++ and # libc++abi, so static linking them becomes a problem. We need to @@ -223,11 +226,13 @@ def cc_toolchain_config( "-L{}/usr/lib".format(sysroot_path), "-lc++", "-lc++abi", - "-Bstatic", - "-lunwind", "-Bdynamic", "-L{}lib".format(toolchain_path_prefix), ]) + libunwind_link_flags = [ + "-Bstatic", + "-lunwind", + ] elif stdlib == "libc++": cxx_flags = [ @@ -340,7 +345,7 @@ def cc_toolchain_config( dbg_compile_flags = dbg_compile_flags, opt_compile_flags = opt_compile_flags, cxx_flags = cxx_flags, - link_flags = link_flags, + link_flags = link_flags + select({"//conditions:default": [], str(Label("@toolchains_llvm//toolchain/config:use_libunwind")): libunwind_link_flags}), archive_flags = archive_flags, link_libs = link_libs, opt_link_flags = opt_link_flags, diff --git a/toolchain/config/BUILD.bazel b/toolchain/config/BUILD.bazel new file mode 100644 index 00000000..e794fe09 --- /dev/null +++ b/toolchain/config/BUILD.bazel @@ -0,0 +1,27 @@ +# Copyright 2021 The Bazel Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") + +bool_flag( + name = "libunwind", + build_setting_default = True, + visibility = ["//visibility:public"], +) + +config_setting( + name = "use_libunwind", + flag_values = {":libunwind": "True"}, + visibility = ["//visibility:public"], +)