Skip to content

Commit 00e30ca

Browse files
stolyarolehcopybara-github
authored andcommitted
Add support for LLD in unix_cc_configure
Unix cc autoconfiguration would only enable LTO support with the `gold` linker. This commit enables `thin_lto` when using LLD - it's a drop-in replacement for GNU linkers. Closes #12890. PiperOrigin-RevId: 355601031
1 parent 32fc451 commit 00e30ca

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

tools/cpp/unix_cc_configure.bzl

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,16 @@ def _is_linker_option_supported(repository_ctx, cc, option, pattern):
171171
])
172172
return result.stderr.find(pattern) == -1
173173

174-
def _find_gold_linker_path(repository_ctx, cc):
175-
"""Checks if `gold` is supported by the C compiler.
174+
def _find_linker_path(repository_ctx, cc, linker):
175+
"""Checks if a given linker is supported by the C compiler.
176176
177177
Args:
178178
repository_ctx: repository_ctx.
179179
cc: path to the C compiler.
180+
linker: linker to find
180181
181182
Returns:
182-
String to put as value to -fuse-ld= flag, or None if gold couldn't be found.
183+
String to put as value to -fuse-ld= flag, or None if linker couldn't be found.
183184
"""
184185
result = repository_ctx.execute([
185186
cc,
@@ -191,19 +192,19 @@ def _find_gold_linker_path(repository_ctx, cc):
191192
# gold when only a very old (year 2010 and older) is present.
192193
"-Wl,--start-lib",
193194
"-Wl,--end-lib",
194-
"-fuse-ld=gold",
195+
"-fuse-ld=" + linker,
195196
"-v",
196197
])
197198
if result.return_code != 0:
198199
return None
199200

200201
for line in result.stderr.splitlines():
201-
if line.find("gold") == -1:
202+
if line.find(linker) == -1:
202203
continue
203204
for flag in line.split(" "):
204-
if flag.find("gold") == -1:
205+
if flag.find(linker) == -1:
205206
continue
206-
if flag.find("--enable-gold") > -1 or flag.find("--with-plugin-ld") > -1:
207+
if flag.find("--enable-" + linker) > -1 or flag.find("--with-plugin-ld") > -1:
207208
# skip build configuration options of gcc itself
208209
# TODO(hlopko): Add redhat-like worker on the CI (#9392)
209210
continue
@@ -216,8 +217,8 @@ def _find_gold_linker_path(repository_ctx, cc):
216217
flag = flag.replace("-fuse-ld=", "")
217218
return flag
218219
auto_configure_warning(
219-
"CC with -fuse-ld=gold returned 0, but its -v output " +
220-
"didn't contain 'gold', falling back to the default linker.",
220+
"CC with -fuse-ld=" + linker + " returned 0, but its -v output " +
221+
"didn't contain '" + linker + "', falling back to the default linker.",
221222
)
222223
return None
223224

@@ -413,7 +414,10 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
413414
bazel_linklibs,
414415
False,
415416
), ":")
416-
gold_linker_path = _find_gold_linker_path(repository_ctx, cc)
417+
gold_or_lld_linker_path = (
418+
_find_linker_path(repository_ctx, cc, "lld") or
419+
_find_linker_path(repository_ctx, cc, "gold")
420+
)
417421
cc_path = repository_ctx.path(cc)
418422
if not str(cc_path).startswith(str(repository_ctx.path(".")) + "/"):
419423
# cc is outside the repository, set -B
@@ -531,7 +535,7 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
531535
),
532536
"%{cxx_flags}": get_starlark_list(cxx_opts + _escaped_cplus_include_paths(repository_ctx)),
533537
"%{link_flags}": get_starlark_list((
534-
["-fuse-ld=" + gold_linker_path] if gold_linker_path else []
538+
["-fuse-ld=" + gold_or_lld_linker_path] if gold_or_lld_linker_path else []
535539
) + _add_linker_option_if_supported(
536540
repository_ctx,
537541
cc,
@@ -606,6 +610,6 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
606610
"%{dbg_compile_flags}": get_starlark_list(["-g"]),
607611
"%{coverage_compile_flags}": coverage_compile_flags,
608612
"%{coverage_link_flags}": coverage_link_flags,
609-
"%{supports_start_end_lib}": "True" if gold_linker_path else "False",
613+
"%{supports_start_end_lib}": "True" if gold_or_lld_linker_path else "False",
610614
},
611615
)

0 commit comments

Comments
 (0)