Skip to content

Commit 53f81e1

Browse files
committed
Reapply "[tools] Use mold linker on Linux Noble debug builds (RobotLocomotion#22951)" (RobotLocomotion#23239)
This reverts commit 218b815.
1 parent f6e0b00 commit 53f81e1

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

setup/ubuntu/source_distribution/packages-noble-test-only.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
curl
22
libstdc++6-10-dbg
3+
mold
34
python3-dateutil
45
python3-dbg
56
python3-flask

tools/cc_toolchain/BUILD.bazel

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
load("@bazel_skylib//lib:selects.bzl", "selects")
2-
load("@bazel_skylib//rules:common_settings.bzl", "int_flag", "string_flag")
2+
load(
3+
"@bazel_skylib//rules:common_settings.bzl",
4+
"bool_flag",
5+
"int_flag",
6+
"string_flag",
7+
)
38
load("//tools/lint:lint.bzl", "add_lint_tests")
49
load("//tools/skylark:sh.bzl", "sh_binary")
510

@@ -151,6 +156,33 @@ config_setting(
151156
values = {"compilation_mode": "dbg"},
152157
)
153158

159+
# (Internal use only.) When this is set to True, some Drake builds may use the
160+
# mold linker, primarily for its improved handling of DWARF debug
161+
# information. It should be False (the default) for all builds initiated via
162+
# Drake's CMake wrappers. See the other "mold_linker" settings below and issue
163+
# #21836.
164+
bool_flag(
165+
name = "allow_mold_linker",
166+
build_setting_default = False,
167+
)
168+
169+
config_setting(
170+
name = "mold_linker_allowed",
171+
flag_values = {":allow_mold_linker": "True"},
172+
)
173+
174+
selects.config_setting_group(
175+
name = "use_mold_linker",
176+
match_all = [
177+
# TODO(rpoyner-tri): consider removing the "noble" requirement when
178+
# Drake's support of "jammy" ends. Jammy is excluded because its
179+
# version of the mold linker is too old.
180+
"//tools:ubuntu_noble",
181+
":debug",
182+
":mold_linker_allowed",
183+
],
184+
)
185+
154186
config_setting(
155187
name = "compiler_major_13",
156188
flag_values = {":compiler_major": "13"},

tools/cc_toolchain/bazel.rc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ build --action_env=CCACHE_DISABLE=1
99
# command line, or promote *any* warnings even from externals to errors via
1010
# --copt=-Werror.
1111
#
12-
# When compiilng Drake as an external package, this rcfile is not loaded and we
12+
# When compiling Drake as an external package, this rcfile is not loaded and we
1313
# won't promote warnings to errors by default.
1414
build --@drake//tools/cc_toolchain:error_severity=error
15+
16+
# Similarly, only allow using the mold linker when Drake is the main
17+
# module. Other conditions will be checked as well; see
18+
# //tools/cc_toolchain:use_mold_linker for more details.
19+
build --@drake//tools/cc_toolchain:allow_mold_linker=true

tools/skylark/drake_cc.bzl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ def _platform_copts(rule_copts, rule_gcc_copts, rule_clang_copts, cc_test = 0):
129129
"//conditions:default": [],
130130
})
131131

132+
# The BASE_LINKOPTS are used for all drake_cc_{binary,library,test} rules.
133+
BASE_LINKOPTS = select({
134+
"@drake//tools/cc_toolchain:use_mold_linker": [
135+
"-fuse-ld=mold",
136+
"-Wl,--compress-debug-sections=zlib",
137+
"-Wl,--thread-count=2",
138+
],
139+
"//conditions:default": [],
140+
})
141+
132142
def _check_library_deps_blacklist(name, deps):
133143
"""Report an error if a library should not use something from deps."""
134144
if not deps:
@@ -596,6 +606,7 @@ def drake_cc_library(
596606
copts = [],
597607
clang_copts = [],
598608
gcc_copts = [],
609+
linkopts = [],
599610
linkstatic = 1,
600611
internal = False,
601612
compile_once_per_scalar = False,
@@ -649,6 +660,7 @@ def drake_cc_library(
649660
should be surrounded with `#if DRAKE_ONCE_PER_SCALAR_PHASE == 0`.
650661
"""
651662
new_copts = _platform_copts(copts, gcc_copts, clang_copts)
663+
new_linkopts = BASE_LINKOPTS + linkopts
652664
new_tags = kwargs.pop("tags", None) or []
653665
if internal:
654666
if install_hdrs_exclude != []:
@@ -690,6 +702,7 @@ def drake_cc_library(
690702
deps = deps + add_deps,
691703
implementation_deps = implementation_deps,
692704
copts = new_copts,
705+
linkopts = new_linkopts,
693706
linkstatic = linkstatic,
694707
declare_installed_headers = declare_installed_headers,
695708
install_hdrs_exclude = install_hdrs_exclude,
@@ -783,11 +796,13 @@ def drake_cc_binary(
783796
defaults using test_rule_args=["-f", "--bar=42"] or test_rule_size="baz".
784797
"""
785798
new_copts = _platform_copts(copts, gcc_copts, clang_copts)
799+
new_linkopts = BASE_LINKOPTS + linkopts
786800
new_srcs, add_deps = _maybe_add_pruned_private_hdrs_dep(
787801
base_name = name,
788802
srcs = srcs,
789803
deps = deps,
790804
copts = new_copts,
805+
linkopts = new_linkopts,
791806
testonly = testonly,
792807
**kwargs
793808
)
@@ -801,7 +816,7 @@ def drake_cc_binary(
801816
testonly = testonly,
802817
linkshared = linkshared,
803818
linkstatic = linkstatic,
804-
linkopts = linkopts,
819+
linkopts = new_linkopts,
805820
features = [
806821
# We should deduplicate symbols while linking (for a ~6% reduction
807822
# in disk use), to conserve space in CI; see #18545 for details.
@@ -820,6 +835,7 @@ def drake_cc_binary(
820835
data = data + test_rule_data,
821836
deps = deps + add_deps,
822837
copts = copts,
838+
linkopts = new_linkopts,
823839
gcc_copts = gcc_copts,
824840
size = test_rule_size,
825841
timeout = test_rule_timeout,
@@ -839,6 +855,7 @@ def drake_cc_test(
839855
copts = [],
840856
gcc_copts = [],
841857
clang_copts = [],
858+
linkopts = [],
842859
allow_network = None,
843860
display = False,
844861
num_threads = None,
@@ -868,11 +885,13 @@ def drake_cc_test(
868885
kwargs = incorporate_display(kwargs, display = display)
869886
kwargs = incorporate_num_threads(kwargs, num_threads = num_threads)
870887
new_copts = _platform_copts(copts, gcc_copts, clang_copts, cc_test = 1)
888+
new_linkopts = BASE_LINKOPTS + linkopts
871889
new_srcs, add_deps = _maybe_add_pruned_private_hdrs_dep(
872890
base_name = name,
873891
srcs = srcs,
874892
deps = deps,
875893
copts = new_copts,
894+
linkopts = new_linkopts,
876895
**kwargs
877896
)
878897
cc_test(
@@ -882,6 +901,7 @@ def drake_cc_test(
882901
args = args,
883902
deps = deps + add_deps,
884903
copts = new_copts,
904+
linkopts = new_linkopts,
885905
features = [
886906
# We should deduplicate symbols while linking (for a ~6% reduction
887907
# in disk use), to conserve space in CI; see #18545 for details.

0 commit comments

Comments
 (0)