Skip to content

Commit 7e4ed65

Browse files
committed
llvm_toolchain: expose hooks for extra-targets to override some linker flags
1 parent 52170d6 commit 7e4ed65

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

toolchain/cc_toolchain_config.bzl.tpl

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,20 @@ def cc_toolchain_config(name, host_platform, custom_target_triple = None, overri
3939
4040
"extra_compile_flags": list[string],
4141
"extra_linker_flags": list[string],
42-
"omit_hosted_linker_flags": bool,
43-
"omit_cxx_stdlib_flag": bool,
44-
"use_llvm_ar_instead_of_libtool_on_macos": bool,
42+
"custom_linker_tool": {
43+
"darwin": string,
44+
"k8": string,
45+
},
46+
"omit_hosted_linker_flags": bool = False,
47+
48+
"linker_include_build_id_on_linux_hosts": bool = True,
49+
"linker_use_gnu_hash_style_on_linux_hosts": bool = True,
50+
"linker_use_elf_hardening_so_flags_on_linux_hosts": bool = True,
51+
52+
"prefer_static_cxx_libs_on_linux_hosts": bool = True,
53+
"omit_cxx_stdlib_flag": bool = False,
54+
55+
"use_llvm_ar_instead_of_libtool_on_macos": bool = False,
4556
}
4657
```
4758
"""
@@ -118,12 +129,19 @@ def cc_toolchain_config(name, host_platform, custom_target_triple = None, overri
118129
# The linker has no way of knowing if there are C++ objects; so we
119130
# always link C++ libraries.
120131
"-L%{toolchain_path_prefix}/lib",
132+
]
133+
134+
# Unless the target has a compelling reason not to want to do so (i.e.
135+
# has a special linker that doesn't support this syntax), we'd like to
136+
# prefer _statically_ linking the C++ libraries:
137+
prefer_static_cxx_libs_on_linux_hosts =
138+
overrides.get("prefer_static_cxx_libs_on_linux_hosts", True)
139+
linker_flags += [
121140
"-l:libc++.a",
122141
"-l:libc++abi.a",
123-
# Other linker flags.
124-
"-Wl,--build-id=md5",
125-
"-Wl,--hash-style=gnu",
126-
"-Wl,-z,relro,-z,now",
142+
] if prefer_static_cxx_libs_on_linux_hosts else [
143+
"-lc++",
144+
"-lc++abi",
127145
]
128146

129147
if enable_hosted_linker_flags:
@@ -135,6 +153,14 @@ def cc_toolchain_config(name, host_platform, custom_target_triple = None, overri
135153
"-lpthread",
136154
"-ldl",
137155
]
156+
157+
# Other linker flags.
158+
if overrides.get("linker_include_build_id_on_linux_hosts", True):
159+
linker_flags += ["-Wl,--build-id=md5"]
160+
if overrides.get("linker_use_gnu_hash_style_on_linux_hosts", True):
161+
linker_flags += ["-Wl,--hash-style=gnu"]
162+
if overrides.get("linker_use_elf_hardening_so_flags_on_linux_hosts", True):
163+
linker_flags += ["-Wl,-z,relro,-z,now"]
138164
elif host_platform == "darwin":
139165
linker_flags = [
140166
# Difficult to guess options to statically link C++ libraries with

0 commit comments

Comments
 (0)