2021-11-26 23:24:09 +00:00
# Compiler
2019-10-30 07:01:53 +00:00
if ( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
set ( COMPILER_GCC 1 )
2020-05-04 15:11:56 +00:00
elseif ( CMAKE_CXX_COMPILER_ID MATCHES "AppleClang" )
2020-05-06 16:47:13 +00:00
set ( COMPILER_CLANG 1 ) # Safe to treat AppleClang as a regular Clang, in general.
2020-05-04 15:11:56 +00:00
elseif ( CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
2019-10-30 07:01:53 +00:00
set ( COMPILER_CLANG 1 )
2022-05-15 09:28:29 +00:00
else ( )
message ( FATAL_ERROR "Compiler ${CMAKE_CXX_COMPILER_ID} is not supported" )
2019-10-30 07:01:53 +00:00
endif ( )
2022-05-15 09:28:29 +00:00
# Print details to output
2022-05-15 10:09:15 +00:00
execute_process ( COMMAND ${ CMAKE_CXX_COMPILER } --version OUTPUT_VARIABLE COMPILER_SELF_IDENTIFICATION OUTPUT_STRIP_TRAILING_WHITESPACE )
message ( STATUS "Using compiler:\n${COMPILER_SELF_IDENTIFICATION}" )
2021-11-27 00:33:34 +00:00
2022-05-15 09:42:42 +00:00
# Require minimum compiler versions
2022-05-15 09:30:59 +00:00
set ( CLANG_MINIMUM_VERSION 12 )
2022-05-15 09:35:16 +00:00
set ( XCODE_MINIMUM_VERSION 12.0 )
set ( APPLE_CLANG_MINIMUM_VERSION 12.0.0 )
2022-05-15 09:30:59 +00:00
set ( GCC_MINIMUM_VERSION 11 )
2019-10-30 07:01:53 +00:00
if ( COMPILER_GCC )
2022-04-23 17:53:25 +00:00
if ( CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${ GCC_MINIMUM_VERSION } )
2022-05-15 09:42:42 +00:00
message ( FATAL_ERROR "Compilation with GCC version ${CMAKE_CXX_COMPILER_VERSION} is unsupported, the minimum required version is ${GCC_MINIMUM_VERSION}." )
2019-10-30 07:01:53 +00:00
endif ( )
2021-04-24 22:51:40 +00:00
2022-05-15 09:42:42 +00:00
message ( WARNING "Compilation with GCC is unsupported. Please use Clang instead." )
2021-04-24 22:51:40 +00:00
2019-10-30 07:01:53 +00:00
elseif ( COMPILER_CLANG )
2020-05-06 16:47:13 +00:00
if ( CMAKE_CXX_COMPILER_ID MATCHES "AppleClang" )
2021-11-25 23:03:31 +00:00
# (Experimental!) Specify "-DALLOW_APPLECLANG=ON" when running CMake configuration step, if you want to experiment with using it.
if ( NOT ALLOW_APPLECLANG AND NOT DEFINED ENV{ALLOW_APPLECLANG} )
2022-05-15 09:42:42 +00:00
message ( FATAL_ERROR "Compilation with AppleClang is unsupported. Please use vanilla Clang, e.g. from Homebrew." )
2021-11-25 23:03:31 +00:00
endif ( )
2021-10-23 19:59:12 +00:00
2022-05-15 09:42:42 +00:00
# For a mapping between XCode / AppleClang / vanilla Clang versions, see https://en.wikipedia.org/wiki/Xcode
2020-05-06 16:47:13 +00:00
if ( CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${ APPLE_CLANG_MINIMUM_VERSION } )
2022-05-15 09:42:42 +00:00
message ( FATAL_ERROR "Compilation with AppleClang version ${CMAKE_CXX_COMPILER_VERSION} is unsupported, the minimum required version is ${APPLE_CLANG_MINIMUM_VERSION} (Xcode ${XCODE_MINIMUM_VERSION})." )
2020-05-06 16:47:13 +00:00
endif ( )
else ( )
if ( CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${ CLANG_MINIMUM_VERSION } )
2022-05-15 09:42:42 +00:00
message ( FATAL_ERROR "Compilation with Clang version ${CMAKE_CXX_COMPILER_VERSION} is unsupported, the minimum required version is ${CLANG_MINIMUM_VERSION}." )
2020-05-06 16:47:13 +00:00
endif ( )
2019-10-30 07:01:53 +00:00
endif ( )
endif ( )
2022-05-15 09:42:42 +00:00
# Linker
2021-11-28 01:37:55 +00:00
string ( REGEX MATCHALL "[0-9]+" COMPILER_VERSION_LIST ${ CMAKE_CXX_COMPILER_VERSION } )
list ( GET COMPILER_VERSION_LIST 0 COMPILER_VERSION_MAJOR )
2019-10-30 07:01:53 +00:00
2020-09-19 16:42:36 +00:00
# Example values: `lld-10`, `gold`.
2019-12-09 09:29:59 +00:00
option ( LINKER_NAME "Linker name or full path" )
2020-09-17 15:37:23 +00:00
2022-05-15 12:09:22 +00:00
if ( NOT LINKER_NAME )
if ( COMPILER_GCC )
find_program ( LLD_PATH NAMES "ld.lld" )
find_program ( GOLD_PATH NAMES "ld.gold" )
2022-11-08 10:32:53 +00:00
elseif ( COMPILER_CLANG )
# llvm lld is a generic driver.
# Invoke ld.lld (Unix), ld64.lld (macOS), lld-link (Windows), wasm-ld (WebAssembly) instead
if ( OS_LINUX )
find_program ( LLD_PATH NAMES "ld.lld-${COMPILER_VERSION_MAJOR}" "ld.lld" )
elseif ( OS_DARWIN )
find_program ( LLD_PATH NAMES "ld64.lld-${COMPILER_VERSION_MAJOR}" "ld64.lld" )
endif ( )
2022-10-29 10:58:11 +00:00
find_program ( GOLD_PATH NAMES "ld.gold" "gold" )
2022-05-15 12:09:22 +00:00
endif ( )
endif ( )
2019-10-30 07:01:53 +00:00
2022-10-29 10:58:11 +00:00
if ( ( OS_LINUX OR OS_DARWIN ) AND NOT LINKER_NAME )
# prefer lld linker over gold or ld on linux and macos
2020-08-14 15:44:04 +00:00
if ( LLD_PATH )
if ( COMPILER_GCC )
# GCC driver requires one of supported linker names like "lld".
set ( LINKER_NAME "lld" )
else ( )
# Clang driver simply allows full linker path.
set ( LINKER_NAME ${ LLD_PATH } )
2019-12-10 13:59:54 +00:00
endif ( )
endif ( )
2020-03-13 15:20:25 +00:00
if ( NOT LINKER_NAME )
if ( GOLD_PATH )
2022-06-13 11:31:08 +00:00
message ( WARNING "Linking with gold is not recommended. Please use lld." )
2020-03-13 15:20:25 +00:00
if ( COMPILER_GCC )
set ( LINKER_NAME "gold" )
else ( )
set ( LINKER_NAME ${ GOLD_PATH } )
endif ( )
2019-12-10 13:59:54 +00:00
endif ( )
2019-10-30 07:01:53 +00:00
endif ( )
endif ( )
2022-05-15 12:09:22 +00:00
# TODO: allow different linker on != OS_LINUX
2019-10-30 07:01:53 +00:00
if ( LINKER_NAME )
2022-05-15 11:32:37 +00:00
if ( COMPILER_CLANG )
2021-05-21 14:42:04 +00:00
find_program ( LLD_PATH NAMES ${ LINKER_NAME } )
2021-12-09 07:17:24 +00:00
if ( NOT LLD_PATH )
message ( FATAL_ERROR "Using linker ${LINKER_NAME} but can't find its path." )
endif ( )
2022-08-31 17:34:35 +00:00
# This a temporary quirk to emit .debug_aranges with ThinLTO
set ( LLD_WRAPPER "${CMAKE_CURRENT_BINARY_DIR}/ld.lld" )
configure_file ( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/ld.lld.in" "${LLD_WRAPPER}" @ONLY )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --ld-path=${LLD_WRAPPER}" )
set ( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --ld-path=${LLD_WRAPPER}" )
2021-03-11 07:42:58 +00:00
else ( )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=${LINKER_NAME}" )
set ( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=${LINKER_NAME}" )
2021-03-10 15:07:56 +00:00
endif ( )
2019-10-30 07:01:53 +00:00
endif ( )
2021-11-26 23:24:09 +00:00
2022-05-15 12:09:22 +00:00
if ( LINKER_NAME )
message ( STATUS "Using linker: ${LINKER_NAME}" )
else ( )
message ( STATUS "Using linker: <default>" )
endif ( )
2021-11-26 23:24:09 +00:00
# Archiver
if ( COMPILER_GCC )
2022-10-18 11:19:16 +00:00
find_program ( LLVM_AR_PATH NAMES "llvm-ar" "llvm-ar-15" "llvm-ar-14" "llvm-ar-13" "llvm-ar-12" )
2021-11-26 23:24:09 +00:00
else ( )
find_program ( LLVM_AR_PATH NAMES "llvm-ar-${COMPILER_VERSION_MAJOR}" "llvm-ar" )
endif ( )
if ( LLVM_AR_PATH )
set ( CMAKE_AR "${LLVM_AR_PATH}" )
endif ( )
2022-05-15 10:09:15 +00:00
message ( STATUS "Using archiver: ${CMAKE_AR}" )
2021-11-26 23:24:09 +00:00
# Ranlib
if ( COMPILER_GCC )
2022-10-18 11:19:16 +00:00
find_program ( LLVM_RANLIB_PATH NAMES "llvm-ranlib" "llvm-ranlib-15" "llvm-ranlib-14" "llvm-ranlib-13" "llvm-ranlib-12" )
2021-11-26 23:24:09 +00:00
else ( )
find_program ( LLVM_RANLIB_PATH NAMES "llvm-ranlib-${COMPILER_VERSION_MAJOR}" "llvm-ranlib" )
endif ( )
if ( LLVM_RANLIB_PATH )
set ( CMAKE_RANLIB "${LLVM_RANLIB_PATH}" )
endif ( )
2022-05-15 10:09:15 +00:00
message ( STATUS "Using ranlib: ${CMAKE_RANLIB}" )
2021-11-26 23:24:09 +00:00
# Install Name Tool
if ( COMPILER_GCC )
2022-10-18 11:19:16 +00:00
find_program ( LLVM_INSTALL_NAME_TOOL_PATH NAMES "llvm-install-name-tool" "llvm-install-name-tool-15" "llvm-install-name-tool-14" "llvm-install-name-tool-13" "llvm-install-name-tool-12" )
2021-11-26 23:24:09 +00:00
else ( )
find_program ( LLVM_INSTALL_NAME_TOOL_PATH NAMES "llvm-install-name-tool-${COMPILER_VERSION_MAJOR}" "llvm-install-name-tool" )
endif ( )
if ( LLVM_INSTALL_NAME_TOOL_PATH )
set ( CMAKE_INSTALL_NAME_TOOL "${LLVM_INSTALL_NAME_TOOL_PATH}" )
endif ( )
2022-05-15 10:09:15 +00:00
message ( STATUS "Using install-name-tool: ${CMAKE_INSTALL_NAME_TOOL}" )
2021-11-26 23:24:09 +00:00
# Objcopy
if ( COMPILER_GCC )
2022-10-18 11:19:16 +00:00
find_program ( OBJCOPY_PATH NAMES "llvm-objcopy" "llvm-objcopy-15" "llvm-objcopy-14" "llvm-objcopy-13" "llvm-objcopy-12" "objcopy" )
2021-11-26 23:24:09 +00:00
else ( )
find_program ( OBJCOPY_PATH NAMES "llvm-objcopy-${COMPILER_VERSION_MAJOR}" "llvm-objcopy" "objcopy" )
endif ( )
if ( OBJCOPY_PATH )
message ( STATUS "Using objcopy: ${OBJCOPY_PATH}" )
else ( )
message ( FATAL_ERROR "Cannot find objcopy." )
endif ( )
2022-03-10 21:23:28 +00:00
2022-05-15 10:19:02 +00:00
# Strip
2022-03-10 21:29:18 +00:00
2022-03-10 21:23:28 +00:00
if ( COMPILER_GCC )
2022-10-18 11:19:16 +00:00
find_program ( STRIP_PATH NAMES "llvm-strip" "llvm-strip-15" "llvm-strip-14" "llvm-strip-13" "llvm-strip-12" "strip" )
2022-03-10 21:23:28 +00:00
else ( )
2022-03-23 17:44:09 +00:00
find_program ( STRIP_PATH NAMES "llvm-strip-${COMPILER_VERSION_MAJOR}" "llvm-strip" "strip" )
2022-03-10 21:23:28 +00:00
endif ( )
2022-03-23 17:44:09 +00:00
if ( STRIP_PATH )
message ( STATUS "Using strip: ${STRIP_PATH}" )
2022-03-10 21:23:28 +00:00
else ( )
2022-03-23 17:44:09 +00:00
message ( FATAL_ERROR "Cannot find strip." )
2022-03-10 21:23:28 +00:00
endif ( )
2022-06-21 21:46:38 +00:00
2022-06-22 18:07:26 +00:00
if ( OS_DARWIN AND NOT CMAKE_TOOLCHAIN_FILE )
# utils/list-licenses/list-licenses.sh (which generates system table system.licenses) needs the GNU versions of find and grep. These are
# not available out-of-the-box on Mac. As a special case, Darwin builds in CI are cross-compiled from x86 Linux where the GNU userland is
# available.
2022-06-21 21:46:38 +00:00
find_program ( GFIND_PATH NAMES "gfind" )
if ( NOT GFIND_PATH )
2022-06-22 14:59:03 +00:00
message ( FATAL_ERROR "GNU find not found. You can install it with 'brew install findutils'." )
2022-06-21 21:46:38 +00:00
endif ( )
find_program ( GGREP_PATH NAMES "ggrep" )
if ( NOT GGREP_PATH )
2022-06-22 14:59:03 +00:00
message ( FATAL_ERROR "GNU grep not found. You can install it with 'brew install grep'." )
2022-06-21 21:46:38 +00:00
endif ( )
endif ( )