Clang: Support the new --gcc-install-dir option
Clang 16 comes with a nice new option --gcc-install-dir
. Long story short, this is the first time I can just rather easily use clang with an gcc installation (without manually adding multiple options for include/lib/tools paths).
The older --gcc-toolchain
switch got deprecated at the same time.
The state of CMake is that it supports the deprecated switch via CMAKE_<LANG>_COMPILER_EXTERNAL_TOOLCHAIN
.
of course some CMAKE_<LANG>_COMPILER_EXTERNAL_INSTALL_DIR
variable would be a start, but I am not sure this would be the most elegant.
gcc
will output the correct path to be used with -print-search-dirs
, so "chaining" a gcc toolchain would be the most practical:
include(GCC_TOOLCHAIN_PATH)
execute_process(COMMAND ${CMAKE_C_COMPILER} -print-search-dirs
OUTPUT_VARIABLE gcc_install_dir
RESULT_VARIABLE _result)
if (NOT _result EQUAL "0")
message(FATAL_ERROR "Failed fetching install path from gcc ${_result}")
endif()
string(REGEX MATCH "^install:[ \t]*([^\r\n]*)" gcc_install_dir "${gcc_install_dir}" )
set(gcc_install_dir "${CMAKE_MATCH_1}")
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(CMAKE_EXE_LINKER_FLAGS_INIT "$CMAKE_EXE_LINKER_FLAGS_INIT --gcc-install-dir=${gcc_install_dir}")
set(CMAKE_MODULE_LINKER_FLAGS_INIT "$CMAKE_MODULE_LINKER_FLAGS_INIT --gcc-install-dir=${gcc_install_dir}")
set(CMAKE_SHARED_LINKER_FLAGS_INIT "$CMAKE_SHARED_LINKER_FLAGS_INIT --gcc-install-dir=${gcc_install_dir}")
set(CMAKE_C_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} --gcc-install-dir=${gcc_install_dir}")
set(CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT} --gcc-install-dir=${gcc_install_dir}")
unset(gcc_install_dir)
Perhaps some module providing this functionality, would be just 2 lines then:
include(GCC_TOOLCHAIN_PATH)
include(ClangUseGccToolchain)
Edited by Brad King