Skip to content

Commit

Permalink
[runtimes] Don't try passing --target flags to GCC
Browse files Browse the repository at this point in the history
When a target triple is specified in CMake via XXX_TARGET_TRIPLE, we tried
passing the --target=<...> flag to the compiler. However, not all compilers
support that flag (e.g. GCC, which is not a cross-compiler). As a result,
setting e.g. LIBCXX_TARGET_TRIPLE=<host-triple> would end up trying to
pass --target=<host-triple> to GCC, which breaks everything because the
flag isn't even supported.

This commit only adds `--target=<...>` & friends to the flags if it is
supported by the compiler.

One could argue that it's confusing to pass LIBCXX_TARGET_TRIPLE=<...>
and have it be ignored. That's correct, and one possibility would be
to assert that the requested triple is the same as the host triple when
we know the compiler is unable to cross-compile. However, note that this
is a pre-existing issue (setting the TARGET_TRIPLE variable never had an
influence on the flags passed to the compiler), and also fixing that is
starting to look like reimplementing a lot of CMake logic that is already
handled with CMAKE_CXX_COMPILER_TARGET.

Differential Revision: https://reviews.llvm.org/D106082
  • Loading branch information
ldionne committed Jul 15, 2021
1 parent 6596778 commit a59165b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
6 changes: 3 additions & 3 deletions libcxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -465,17 +465,17 @@ include(HandleLibcxxFlags)
add_target_flags_if(LIBCXX_BUILD_32_BITS "-m32")

if(LIBCXX_TARGET_TRIPLE)
add_target_flags("--target=${LIBCXX_TARGET_TRIPLE}")
add_target_flags_if_supported("--target=${LIBCXX_TARGET_TRIPLE}")
elseif(CMAKE_CXX_COMPILER_TARGET)
set(LIBCXX_TARGET_TRIPLE "${CMAKE_CXX_COMPILER_TARGET}")
endif()
if(LIBCXX_SYSROOT)
add_target_flags("--sysroot=${LIBCXX_SYSROOT}")
add_target_flags_if_supported("--sysroot=${LIBCXX_SYSROOT}")
elseif(CMAKE_SYSROOT)
set(LIBCXX_SYSROOT "${CMAKE_SYSROOT}")
endif()
if(LIBCXX_GCC_TOOLCHAIN)
add_target_flags("--gcc-toolchain=${LIBCXX_GCC_TOOLCHAIN}")
add_target_flags_if_supported("--gcc-toolchain=${LIBCXX_GCC_TOOLCHAIN}")
elseif(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN)
set(LIBCXX_GCC_TOOLCHAIN "${CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}")
endif()
Expand Down
11 changes: 11 additions & 0 deletions libcxx/cmake/Modules/HandleLibcxxFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ macro(add_target_flags_if condition)
endif()
endmacro()

# Add all the flags supported by the compiler to all of
# 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXX_COMPILE_FLAGS'
# and 'LIBCXX_LINK_FLAGS'.
macro(add_target_flags_if_supported)
foreach(flag ${ARGN})
mangle_name("${flag}" flagname)
check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
add_target_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
endforeach()
endmacro()

# Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and
# 'LIBCXX_LINK_FLAGS'.
macro(add_flags)
Expand Down
6 changes: 3 additions & 3 deletions libcxxabi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,17 @@ include(HandleLibcxxabiFlags)
add_target_flags_if(LIBCXXABI_BUILD_32_BITS "-m32")

if(LIBCXXABI_TARGET_TRIPLE)
add_target_flags("--target=${LIBCXXABI_TARGET_TRIPLE}")
add_target_flags_if_supported("--target=${LIBCXXABI_TARGET_TRIPLE}")
elseif(CMAKE_CXX_COMPILER_TARGET)
set(LIBCXXABI_TARGET_TRIPLE "${CMAKE_CXX_COMPILER_TARGET}")
endif()
if(LIBCXX_GCC_TOOLCHAIN)
add_target_flags("--gcc-toolchain=${LIBCXXABI_GCC_TOOLCHAIN}")
add_target_flags_if_supported("--gcc-toolchain=${LIBCXXABI_GCC_TOOLCHAIN}")
elseif(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN)
set(LIBCXXABI_GCC_TOOLCHAIN "${CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}")
endif()
if(LIBCXXABI_SYSROOT)
add_target_flags("--sysroot=${LIBCXXABI_SYSROOT}")
add_target_flags_if_supported("--sysroot=${LIBCXXABI_SYSROOT}")
elseif(CMAKE_SYSROOT)
set(LIBCXXABI_SYSROOT "${CMAKE_SYSROOT}")
endif()
Expand Down
11 changes: 11 additions & 0 deletions libcxxabi/cmake/Modules/HandleLibcxxabiFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ macro(add_target_flags_if condition)
endif()
endmacro()

# Add all the flags supported by the compiler to all of
# 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXXABI_COMPILE_FLAGS'
# and 'LIBCXXABI_LINK_FLAGS'.
macro(add_target_flags_if_supported)
foreach(flag ${ARGN})
mangle_name("${flag}" flagname)
check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
add_target_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag})
endforeach()
endmacro()

# Add a specified list of flags to both 'LIBCXXABI_COMPILE_FLAGS' and
# 'LIBCXXABI_LINK_FLAGS'.
macro(add_flags)
Expand Down
6 changes: 3 additions & 3 deletions libunwind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,17 @@ include(HandleLibunwindFlags)
add_target_flags_if(LIBUNWIND_BUILD_32_BITS "-m32")

if(LIBUNWIND_TARGET_TRIPLE)
add_target_flags("--target=${LIBUNWIND_TARGET_TRIPLE}")
add_target_flags_if_supported("--target=${LIBUNWIND_TARGET_TRIPLE}")
elseif(CMAKE_CXX_COMPILER_TARGET)
set(LIBUNWIND_TARGET_TRIPLE "${CMAKE_CXX_COMPILER_TARGET}")
endif()
if(LIBUNWIND_GCC_TOOLCHAIN)
add_target_flags("--gcc-toolchain=${LIBUNWIND_GCC_TOOLCHAIN}")
add_target_flags_if_supported("--gcc-toolchain=${LIBUNWIND_GCC_TOOLCHAIN}")
elseif(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN)
set(LIBUNWIND_GCC_TOOLCHAIN "${CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}")
endif()
if(LIBUNWIND_SYSROOT)
add_target_flags("--sysroot=${LIBUNWIND_SYSROOT}")
add_target_flags_if_supported("--sysroot=${LIBUNWIND_SYSROOT}")
elseif(CMAKE_SYSROOT)
set(LIBUNWIND_SYSROOT "${CMAKE_SYSROOT}")
endif()
Expand Down
11 changes: 11 additions & 0 deletions libunwind/cmake/Modules/HandleLibunwindFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ macro(add_target_flags_if condition)
endif()
endmacro()

# Add all the flags supported by the compiler to all of
# 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBUNWIND_COMPILE_FLAGS'
# and 'LIBUNWIND_LINK_FLAGS'.
macro(add_target_flags_if_supported)
foreach(flag ${ARGN})
mangle_name("${flag}" flagname)
check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG")
add_target_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${flag})
endforeach()
endmacro()

# Add a specified list of flags to both 'LIBUNWIND_COMPILE_FLAGS' and
# 'LIBUNWIND_LINK_FLAGS'.
macro(add_flags)
Expand Down

0 comments on commit a59165b

Please sign in to comment.