[runtimes] Don't try passing --target flags to GCC

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

GitOrigin-RevId: a59165b01778a3b02c510a96951d115d39babd86
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 584bcbd..8c21fc3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -258,17 +258,17 @@
 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()
diff --git a/cmake/Modules/HandleLibcxxabiFlags.cmake b/cmake/Modules/HandleLibcxxabiFlags.cmake
index 19d6e93..512e71a 100644
--- a/cmake/Modules/HandleLibcxxabiFlags.cmake
+++ b/cmake/Modules/HandleLibcxxabiFlags.cmake
@@ -123,6 +123,17 @@
   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)