Skip to content

Commit 6173319

Browse files
committed
[build] Rely on CMake for compiler introspection
Amend 97465f7 with an approach that also works for externals using `@rules_cc//cc/compiler`, instead of a custom Drake flag, by writing a symlink in the build directory to the underlying compiler with a filename expected by Bazel.
1 parent dceb50d commit 6173319

File tree

5 files changed

+53
-73
lines changed

5 files changed

+53
-73
lines changed

CMakeLists.txt

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -291,23 +291,39 @@ endif()
291291
set(BAZEL_REPO_ENV)
292292

293293
if(NOT APPLE)
294+
# Determine the real underlying compiler, so that compiler- and version-
295+
# specific warning logic is enforced throughout the build (even for
296+
# externals). Since rules_cc only identifies the compiler based on the
297+
# filename (rather than actually running it, as CMake does), create a local
298+
# symlink to the compiler with the expected name.
299+
# TODO(tyler-yankee): Ideally, rules_cc would perform proper compiler
300+
# introspection so that this workaround is not needed, but that doesn't seem
301+
# to exist yet.
302+
303+
# Base case; use whatever we were given. (If the filenames already look like
304+
# gcc or clang, or if we fail to parse, we'll fall back to this.)
305+
set(DRAKE_C_COMPILER "${CMAKE_C_COMPILER}")
306+
set(DRAKE_CXX_COMPILER "${CMAKE_CXX_COMPILER}")
307+
308+
cmake_path(GET CMAKE_C_COMPILER FILENAME C_COMPILER_BASENAME)
309+
if (NOT C_COMPILER_BASENAME MATCHES "(gcc|clang)(-\\d+)?")
310+
if(CMAKE_C_COMPILER_ID STREQUAL Clang)
311+
set(DRAKE_C_COMPILER "${CMAKE_BINARY_DIR}/clang")
312+
set(DRAKE_CXX_COMPILER "${CMAKE_BINARY_DIR}/clang++")
313+
file(CREATE_LINK "${CMAKE_C_COMPILER}" "${DRAKE_C_COMPILER}" SYMBOLIC)
314+
file(CREATE_LINK "${CMAKE_CXX_COMPILER}" "${DRAKE_CXX_COMPILER}" SYMBOLIC)
315+
elseif(CMAKE_C_COMPILER_ID STREQUAL GNU)
316+
set(DRAKE_C_COMPILER "${CMAKE_BINARY_DIR}/gcc")
317+
set(DRAKE_CXX_COMPILER "${CMAKE_BINARY_DIR}/g++")
318+
file(CREATE_LINK "${CMAKE_C_COMPILER}" "${DRAKE_C_COMPILER}" SYMBOLIC)
319+
file(CREATE_LINK "${CMAKE_CXX_COMPILER}" "${DRAKE_CXX_COMPILER}" SYMBOLIC)
320+
endif()
321+
endif()
322+
294323
string(APPEND BAZEL_REPO_ENV
295-
" --repo_env=CC=${CMAKE_C_COMPILER}"
296-
" --repo_env=CXX=${CMAKE_CXX_COMPILER}"
324+
" --repo_env=CC=${DRAKE_C_COMPILER}"
325+
" --repo_env=CXX=${DRAKE_CXX_COMPILER}"
297326
)
298-
# Pass a Bazel flag which enforces the underlying compiler so that
299-
# compiler- and version-specific logic (e.g., warnings) is enforced in the
300-
# build, since rules_cc's support for compiler identification is not as
301-
# robust as using CMake's CMAKE_<LANG>_COMPILER_ID.
302-
if(CMAKE_C_COMPILER_ID STREQUAL Clang)
303-
string(APPEND BAZEL_REPO_ENV
304-
" --@drake//tools/cc_toolchain:compiler=clang"
305-
)
306-
elseif(CMAKE_C_COMPILER_ID STREQUAL GNU)
307-
string(APPEND BAZEL_REPO_ENV
308-
" --@drake//tools/cc_toolchain:compiler=gcc"
309-
)
310-
endif()
311327
endif()
312328

313329
get_filename_component(PROJECT_BINARY_DIR_REALPATH

tools/cc_toolchain/BUILD.bazel

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,16 @@ string_flag(
2626
],
2727
)
2828

29-
# In our developer builds and CMake installs, we use the following two flags
30-
# as optional hints for configuring compiler warnings. It's not strictly
31-
# necessary to set these, but doing so will tweak the compiler flags and
32-
# reduce warning spam during the build.
29+
# In our developer builds and CMake installs, we use the following flag as
30+
# an optional hint for configuring compiler warnings. It's not strictly
31+
# necessary to set, but doing so will tweak the compiler flags and reduce
32+
# warning spam during the build.
3333
#
34-
# These are intended for internal use only, not for users to configure.
34+
# This is intended for internal use only, not for users to configure.
3535
#
36-
# TODO(jwnimmer-tri) Ideally, rules_cc would provide,
37-
# * for 'compiler', a check for the real underlying compiler by invoking it
38-
# (akin to CMake) instead of just parsing the filename; and
39-
# * for 'compiler_major', some toolchain attribute that we could query.
40-
# Neither of these seem to exist yet.
41-
string_flag(
42-
name = "compiler",
43-
build_setting_default = "",
44-
values = [
45-
"",
46-
"gcc",
47-
"clang",
48-
],
49-
)
50-
36+
# TODO(jwnimmer-tri) Ideally, rules_cc would provide some toolchain attribute
37+
# that we could query for the version of the compiler, but that doesn't seem to
38+
# exist yet.
5139
int_flag(
5240
name = "compiler_major",
5341
build_setting_default = 0,
@@ -73,37 +61,11 @@ config_setting(
7361
flag_values = {":error_severity": "error"},
7462
)
7563

76-
config_setting(
77-
name = "compiler_clang",
78-
flag_values = {":compiler": "clang"},
79-
)
80-
81-
config_setting(
82-
name = "compiler_gcc",
83-
flag_values = {":compiler": "gcc"},
84-
)
85-
86-
selects.config_setting_group(
87-
name = "clang",
88-
match_any = [
89-
"@rules_cc//cc/compiler:clang",
90-
":compiler_clang",
91-
],
92-
)
93-
94-
selects.config_setting_group(
95-
name = "gcc",
96-
match_any = [
97-
"@rules_cc//cc/compiler:gcc",
98-
":compiler_gcc",
99-
],
100-
)
101-
10264
selects.config_setting_group(
10365
name = "apple_clang_with_warnings",
10466
match_all = [
10567
":apple",
106-
":clang",
68+
"@rules_cc//cc/compiler:clang",
10769
":error_severity_warning",
10870
],
10971
)
@@ -112,23 +74,23 @@ selects.config_setting_group(
11274
name = "apple_clang_with_errors",
11375
match_all = [
11476
":apple",
115-
":clang",
77+
"@rules_cc//cc/compiler:clang",
11678
":error_severity_error",
11779
],
11880
)
11981

12082
selects.config_setting_group(
12183
name = "gcc_with_warnings",
12284
match_all = [
123-
":gcc",
85+
"@rules_cc//cc/compiler:gcc",
12486
":error_severity_warning",
12587
],
12688
)
12789

12890
selects.config_setting_group(
12991
name = "gcc_with_errors",
13092
match_all = [
131-
":gcc",
93+
"@rules_cc//cc/compiler:gcc",
13294
":error_severity_error",
13395
],
13496
)
@@ -137,7 +99,7 @@ selects.config_setting_group(
13799
name = "linux_clang_with_warnings",
138100
match_all = [
139101
":linux",
140-
":clang",
102+
"@rules_cc//cc/compiler:clang",
141103
":error_severity_warning",
142104
],
143105
)
@@ -146,7 +108,7 @@ selects.config_setting_group(
146108
name = "linux_clang_with_errors",
147109
match_all = [
148110
":linux",
149-
":clang",
111+
"@rules_cc//cc/compiler:clang",
150112
":error_severity_error",
151113
],
152114
)
@@ -191,7 +153,7 @@ config_setting(
191153
selects.config_setting_group(
192154
name = "gcc_13_with_warnings",
193155
match_all = [
194-
":gcc",
156+
"@rules_cc//cc/compiler:gcc",
195157
":compiler_major_13",
196158
":error_severity_warning",
197159
],
@@ -200,7 +162,7 @@ selects.config_setting_group(
200162
selects.config_setting_group(
201163
name = "gcc_13_with_errors",
202164
match_all = [
203-
":gcc",
165+
"@rules_cc//cc/compiler:gcc",
204166
":compiler_major_13",
205167
":error_severity_error",
206168
],

tools/skylark/drake_cc.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ def _platform_copts(rule_copts, rule_gcc_copts, rule_clang_copts, cc_test = 0):
141141
test_gcc_copts = (CC_TEST_FLAGS + GCC_CC_TEST_FLAGS) if cc_test else []
142142
test_clang_copts = CC_TEST_FLAGS if cc_test else []
143143
return BASE_COPTS + rule_copts + select({
144-
"//tools/cc_toolchain:gcc": rule_gcc_copts + test_gcc_copts,
145-
"//tools/cc_toolchain:clang": rule_clang_copts + test_clang_copts,
144+
"@rules_cc//cc/compiler:gcc": rule_gcc_copts + test_gcc_copts,
145+
"@rules_cc//cc/compiler:clang": rule_clang_copts + test_clang_copts,
146146
"//conditions:default": [],
147147
})
148148

tools/skylark/pybind.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ load("//tools/skylark:drake_py.bzl", "drake_py_library", "drake_py_test")
55
load("//tools/skylark:py.bzl", "py_library")
66

77
EXTRA_PYBIND_COPTS = select({
8-
"@drake//tools/cc_toolchain:clang": [
8+
"@rules_cc//cc/compiler:clang": [
99
# GCC and Clang don't always agree / succeed when inferring storage
1010
# duration (#9600). Workaround it for now.
1111
"-Wno-unused-lambda-capture",

tools/wheel/image/build-drake.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,7 @@ cmake ../drake-src \
5050
-DDRAKE_VERSION_OVERRIDE="${DRAKE_VERSION}" \
5151
-DDRAKE_GIT_SHA_OVERRIDE="${DRAKE_GIT_SHA}" \
5252
-DCMAKE_INSTALL_PREFIX=/tmp/drake-wheel-build/drake-dist \
53-
-DPython_EXECUTABLE=/usr/local/bin/python
53+
-DPython_EXECUTABLE=/usr/local/bin/python \
54+
-DCMAKE_C_COMPILER=/usr/bin/gcc \
55+
-DCMAKE_CXX_COMPILER=/usr/bin/g++
5456
make install

0 commit comments

Comments
 (0)