Skip to content

Commit b8d155a

Browse files
committed
[tools] Use mold linker on Linux debug builds
1 parent ebb61b9 commit b8d155a

File tree

9 files changed

+55
-2
lines changed

9 files changed

+55
-2
lines changed

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ install_test(
132132
":install",
133133
_INSTALL_TEST_COMMANDS,
134134
],
135+
env = {"COMPILATION_MODE": "$(COMPILATION_MODE)"},
135136
tags = [
136137
# Running acceptance tests under coverage (kcov) probably burns more CI
137138
# time and flakiness compared to any upside.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
curl
22
kcov
33
libstdc++6-10-dbg
4+
mold
45
python3-dateutil
56
python3-dbg
67
python3-flask

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/install/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ drake_py_unittest(
5353
# Runs `install_test_helper` unit tests.
5454
drake_py_unittest(
5555
name = "install_test_helper_test",
56+
env = {"COMPILATION_MODE": "$(COMPILATION_MODE)"},
5657
deps = [":install_test_helper"],
5758
)
5859

tools/install/install_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,12 @@ def main():
9797

9898

9999
if __name__ == '__main__':
100+
# Delete all of this skip logic when jammy support is removed.
101+
is_debug = os.environ.get("COMPILATION_MODE") == "dbg"
102+
is_jammy = subprocess.check_call(["lsb_release", "-sc"]) == "jammy"
103+
is_jammy_debug = is_jammy and is_debug
104+
if is_jammy_debug:
105+
print("Skipping: ld.mold is too old")
106+
sys.exit(0)
107+
100108
main()

tools/install/test/install_test_helper_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
11
import os
2+
import subprocess
23
import unittest
34
import install_test_helper
45

6+
# Delete all of this skip logic when jammy support is removed.
7+
_IS_DEBUG = os.environ.get("COMPILATION_MODE") == "dbg"
8+
_IS_JAMMY = subprocess.check_call(["lsb_release", "-sc"]) == "jammy"
9+
_IS_JAMMY_DEBUG = _IS_JAMMY and _IS_DEBUG
10+
511

612
class TestInstallTestHelperTest(unittest.TestCase):
13+
@unittest.skipIf(_IS_JAMMY_DEBUG, "ld.mold is too old")
714
def test_get_install_dir(self):
815
self.assertIn("TEST_TMPDIR", os.environ)
916
self.assertIn('installation', install_test_helper.get_install_dir())
1017

18+
@unittest.skipIf(_IS_JAMMY_DEBUG, "ld.mold is too old")
1119
def test_create_temporary_dir(self):
1220
subdirectory_name = "tmp"
1321
tmp_dir = install_test_helper.create_temporary_dir(subdirectory_name)
1422
self.assertIn(subdirectory_name, tmp_dir)
1523
self.assertTrue(os.path.isdir(tmp_dir))
1624

25+
@unittest.skipIf(_IS_JAMMY_DEBUG, "ld.mold is too old")
1726
def test_get_python_executable(self):
1827
self.assertIn("python3", install_test_helper.get_python_executable())
1928

29+
@unittest.skipIf(_IS_JAMMY_DEBUG, "ld.mold is too old")
2030
def test_run_and_kill(self):
2131
python = install_test_helper.get_python_executable()
2232
install_test_helper.run_and_kill([python, "-c",
2333
"import time; time.sleep(5)"], 0.5,
2434
from_install_dir=False)
2535

36+
@unittest.skipIf(_IS_JAMMY_DEBUG, "ld.mold is too old")
2637
def test_check_call(self):
2738
python = install_test_helper.get_python_executable()
2839
install_test_helper.check_call([python, "--help"])
2940

41+
@unittest.skipIf(_IS_JAMMY_DEBUG, "ld.mold is too old")
3042
def test_check_output(self):
3143
python = install_test_helper.get_python_executable()
3244
output = install_test_helper.check_output([python, "--help"])
3345
self.assertIn('PYTHONPATH', output)
3446

47+
@unittest.skipIf(_IS_JAMMY_DEBUG, "ld.mold is too old")
3548
def test_read_only(self):
3649
tmp_dir = os.environ['TEST_TMPDIR']
3750
tmp_file = os.path.join(tmp_dir, "test_file")

tools/lint/library_lint_reporter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def main():
6363
# Filter out false positives. All C++ code is OK to depend on these.
6464
item for item in extra_deps
6565
if not (item.startswith("//tools/cc_toolchain:")
66-
or "@bazel_tools//" in item)
66+
or "@bazel_tools//" in item
67+
or item.startswith("//tools/skylark"))
6768
]
6869
if extra_deps:
6970
print(("ERROR: Extra deps in {}'s drake_cc_package_library.").format(

tools/skylark/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
load("@bazel_skylib//lib:selects.bzl", "selects")
12
load("//tools/lint:lint.bzl", "add_lint_tests")
23
load("//tools/skylark:drake_py.bzl", "drake_py_binary", "drake_py_unittest")
34

45
package(default_visibility = ["//visibility:public"])
56

7+
config_setting(
8+
name = "debug",
9+
values = {"compilation_mode": "dbg"},
10+
)
11+
612
config_setting(
713
name = "linux",
814
constraint_values = ["@platforms//os:linux"],
@@ -13,6 +19,14 @@ config_setting(
1319
constraint_values = ["@platforms//os:osx"],
1420
)
1521

22+
selects.config_setting_group(
23+
name = "linux_debug",
24+
match_all = [
25+
":linux",
26+
":debug",
27+
],
28+
)
29+
1630
drake_py_binary(
1731
name = "rewrite_osx_ar_hidden",
1832
srcs = ["rewrite_osx_ar_hidden.py"],

tools/skylark/drake_cc.bzl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ 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/skylark:linux_debug": ["-fuse-ld=mold"],
135+
"//conditions:default": [],
136+
})
137+
132138
def _check_library_deps_blacklist(name, deps):
133139
"""Report an error if a library should not use something from deps."""
134140
if not deps:
@@ -596,6 +602,7 @@ def drake_cc_library(
596602
copts = [],
597603
clang_copts = [],
598604
gcc_copts = [],
605+
linkopts = [],
599606
linkstatic = 1,
600607
internal = False,
601608
compile_once_per_scalar = False,
@@ -649,6 +656,7 @@ def drake_cc_library(
649656
should be surrounded with `#if DRAKE_ONCE_PER_SCALAR_PHASE == 0`.
650657
"""
651658
new_copts = _platform_copts(copts, gcc_copts, clang_copts)
659+
new_linkopts = BASE_LINKOPTS + linkopts
652660
new_tags = kwargs.pop("tags", None) or []
653661
if internal:
654662
if install_hdrs_exclude != []:
@@ -690,6 +698,7 @@ def drake_cc_library(
690698
deps = deps + add_deps,
691699
implementation_deps = implementation_deps,
692700
copts = new_copts,
701+
linkopts = new_linkopts,
693702
linkstatic = linkstatic,
694703
declare_installed_headers = declare_installed_headers,
695704
install_hdrs_exclude = install_hdrs_exclude,
@@ -783,6 +792,7 @@ def drake_cc_binary(
783792
defaults using test_rule_args=["-f", "--bar=42"] or test_rule_size="baz".
784793
"""
785794
new_copts = _platform_copts(copts, gcc_copts, clang_copts)
795+
new_linkopts = BASE_LINKOPTS + linkopts
786796
new_srcs, add_deps = _maybe_add_pruned_private_hdrs_dep(
787797
base_name = name,
788798
srcs = srcs,
@@ -801,7 +811,7 @@ def drake_cc_binary(
801811
testonly = testonly,
802812
linkshared = linkshared,
803813
linkstatic = linkstatic,
804-
linkopts = linkopts,
814+
linkopts = new_linkopts,
805815
features = [
806816
# We should deduplicate symbols while linking (for a ~6% reduction
807817
# in disk use), to conserve space in CI; see #18545 for details.
@@ -839,6 +849,7 @@ def drake_cc_test(
839849
copts = [],
840850
gcc_copts = [],
841851
clang_copts = [],
852+
linkopts = [],
842853
allow_network = None,
843854
display = False,
844855
num_threads = None,
@@ -868,6 +879,7 @@ def drake_cc_test(
868879
kwargs = incorporate_display(kwargs, display = display)
869880
kwargs = incorporate_num_threads(kwargs, num_threads = num_threads)
870881
new_copts = _platform_copts(copts, gcc_copts, clang_copts, cc_test = 1)
882+
new_linkopts = BASE_LINKOPTS + linkopts
871883
new_srcs, add_deps = _maybe_add_pruned_private_hdrs_dep(
872884
base_name = name,
873885
srcs = srcs,
@@ -882,6 +894,7 @@ def drake_cc_test(
882894
args = args,
883895
deps = deps + add_deps,
884896
copts = new_copts,
897+
linkopts = new_linkopts,
885898
features = [
886899
# We should deduplicate symbols while linking (for a ~6% reduction
887900
# in disk use), to conserve space in CI; see #18545 for details.

0 commit comments

Comments
 (0)