Skip to content

[tools] Use mold linker on Linux debug builds #22951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
curl
libstdc++6-10-dbg
mold
python3-dateutil
python3-dbg
python3-flask
Expand Down
34 changes: 33 additions & 1 deletion tools/cc_toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@bazel_skylib//rules:common_settings.bzl", "int_flag", "string_flag")
load(
"@bazel_skylib//rules:common_settings.bzl",
"bool_flag",
"int_flag",
"string_flag",
)
load("//tools/lint:lint.bzl", "add_lint_tests")
load("//tools/skylark:sh.bzl", "sh_binary")

Expand Down Expand Up @@ -98,6 +103,33 @@ config_setting(
values = {"compilation_mode": "dbg"},
)

# (Internal use only.) When this is set to True, some Drake builds may use the
# mold linker, primarily for its improved handling of DWARF debug
# information. It should be False (the default) for all builds initiated via
# Drake's CMake wrappers. See the other "mold_linker" settings below and issue
# #21836.
bool_flag(
name = "allow_mold_linker",
build_setting_default = False,
)

config_setting(
name = "mold_linker_allowed",
flag_values = {":allow_mold_linker": "True"},
)

selects.config_setting_group(
name = "use_mold_linker",
match_all = [
# TODO(rpoyner-tri): consider removing the "noble" requirement when
# Drake's support of "jammy" ends. Jammy is excluded because its
# version of the mold linker is too old.
"//tools:ubuntu_noble",
":debug",
":mold_linker_allowed",
],
)

# In our developer builds and CMake installs, we use this as an optional hint
# for configuring compiler warnings. It's not strictly necessary to set this,
# but doing so might reduce warning spam during the build.
Expand Down
7 changes: 6 additions & 1 deletion tools/cc_toolchain/bazel.rc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ build --action_env=CCACHE_DISABLE=1
# command line, or promote *any* warnings even from externals to errors via
# --copt=-Werror.
#
# When compiilng Drake as an external package, this rcfile is not loaded and we
# When compiling Drake as an external package, this rcfile is not loaded and we
# won't promote warnings to errors by default.
build --@drake//tools/cc_toolchain:error_severity=error

# Similarly, only allow using the mold linker when Drake is the main
# module. Other conditions will be checked as well; see
# //tools/cc_toolchain:use_mold_linker for more details.
build --@drake//tools/cc_toolchain:allow_mold_linker=true
18 changes: 17 additions & 1 deletion tools/skylark/drake_cc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ def _platform_copts(rule_copts, rule_gcc_copts, rule_clang_copts, cc_test = 0):
"//conditions:default": [],
})

# The BASE_LINKOPTS are used for all drake_cc_{binary,library,test} rules.
BASE_LINKOPTS = select({
"@drake//tools/cc_toolchain:use_mold_linker": ["-fuse-ld=mold"],
"//conditions:default": [],
})

def _check_library_deps_blacklist(name, deps):
"""Report an error if a library should not use something from deps."""
if not deps:
Expand Down Expand Up @@ -596,6 +602,7 @@ def drake_cc_library(
copts = [],
clang_copts = [],
gcc_copts = [],
linkopts = [],
linkstatic = 1,
internal = False,
compile_once_per_scalar = False,
Expand Down Expand Up @@ -649,6 +656,7 @@ def drake_cc_library(
should be surrounded with `#if DRAKE_ONCE_PER_SCALAR_PHASE == 0`.
"""
new_copts = _platform_copts(copts, gcc_copts, clang_copts)
new_linkopts = BASE_LINKOPTS + linkopts
new_tags = kwargs.pop("tags", None) or []
if internal:
if install_hdrs_exclude != []:
Expand Down Expand Up @@ -690,6 +698,7 @@ def drake_cc_library(
deps = deps + add_deps,
implementation_deps = implementation_deps,
copts = new_copts,
linkopts = new_linkopts,
linkstatic = linkstatic,
declare_installed_headers = declare_installed_headers,
install_hdrs_exclude = install_hdrs_exclude,
Expand Down Expand Up @@ -783,11 +792,13 @@ def drake_cc_binary(
defaults using test_rule_args=["-f", "--bar=42"] or test_rule_size="baz".
"""
new_copts = _platform_copts(copts, gcc_copts, clang_copts)
new_linkopts = BASE_LINKOPTS + linkopts
new_srcs, add_deps = _maybe_add_pruned_private_hdrs_dep(
base_name = name,
srcs = srcs,
deps = deps,
copts = new_copts,
linkopts = new_linkopts,
testonly = testonly,
**kwargs
)
Expand All @@ -801,7 +812,7 @@ def drake_cc_binary(
testonly = testonly,
linkshared = linkshared,
linkstatic = linkstatic,
linkopts = linkopts,
linkopts = new_linkopts,
features = [
# We should deduplicate symbols while linking (for a ~6% reduction
# in disk use), to conserve space in CI; see #18545 for details.
Expand All @@ -820,6 +831,7 @@ def drake_cc_binary(
data = data + test_rule_data,
deps = deps + add_deps,
copts = copts,
linkopts = new_linkopts,
gcc_copts = gcc_copts,
size = test_rule_size,
timeout = test_rule_timeout,
Expand All @@ -839,6 +851,7 @@ def drake_cc_test(
copts = [],
gcc_copts = [],
clang_copts = [],
linkopts = [],
allow_network = None,
display = False,
num_threads = None,
Expand Down Expand Up @@ -868,11 +881,13 @@ def drake_cc_test(
kwargs = incorporate_display(kwargs, display = display)
kwargs = incorporate_num_threads(kwargs, num_threads = num_threads)
new_copts = _platform_copts(copts, gcc_copts, clang_copts, cc_test = 1)
new_linkopts = BASE_LINKOPTS + linkopts
new_srcs, add_deps = _maybe_add_pruned_private_hdrs_dep(
base_name = name,
srcs = srcs,
deps = deps,
copts = new_copts,
linkopts = new_linkopts,
**kwargs
)
cc_test(
Expand All @@ -882,6 +897,7 @@ def drake_cc_test(
args = args,
deps = deps + add_deps,
copts = new_copts,
linkopts = new_linkopts,
features = [
# We should deduplicate symbols while linking (for a ~6% reduction
# in disk use), to conserve space in CI; see #18545 for details.
Expand Down