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 )
endif ( )
2021-11-27 00:33:34 +00:00
execute_process ( COMMAND ${ CMAKE_CXX_COMPILER } --version )
2019-10-30 07:01:53 +00:00
if ( COMPILER_GCC )
# Require minimum version of gcc
2021-10-23 21:11:01 +00:00
set ( GCC_MINIMUM_VERSION 11 )
2019-10-30 07:01:53 +00:00
if ( CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${ GCC_MINIMUM_VERSION } AND NOT CMAKE_VERSION VERSION_LESS 2.8.9 )
message ( FATAL_ERROR "GCC version must be at least ${GCC_MINIMUM_VERSION}. For example, if GCC ${GCC_MINIMUM_VERSION} is available under gcc-${GCC_MINIMUM_VERSION}, g++-${GCC_MINIMUM_VERSION} names, do the following: export CC=gcc-${GCC_MINIMUM_VERSION} CXX=g++-${GCC_MINIMUM_VERSION}; rm -rf CMakeCache.txt CMakeFiles; and re run cmake or ./release." )
endif ( )
2021-04-24 22:51:40 +00:00
message ( WARNING "GCC compiler is not officially supported for ClickHouse. You should migrate to clang." )
2019-10-30 07:01:53 +00:00
elseif ( COMPILER_CLANG )
2020-05-06 16:47:13 +00:00
# Require minimum version of clang/apple-clang
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} )
message ( FATAL_ERROR "AppleClang is not supported, you should install clang from brew. See the instruction: https://clickhouse.com/docs/en/development/build-osx/" )
endif ( )
2021-10-23 19:59:12 +00:00
2020-05-06 16:47:13 +00:00
# AppleClang 10.0.1 (Xcode 10.2) corresponds to LLVM/Clang upstream version 7.0.0
# AppleClang 11.0.0 (Xcode 11.0) corresponds to LLVM/Clang upstream version 8.0.0
set ( XCODE_MINIMUM_VERSION 10.2 )
set ( APPLE_CLANG_MINIMUM_VERSION 10.0.1 )
if ( CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${ APPLE_CLANG_MINIMUM_VERSION } )
message ( FATAL_ERROR "AppleClang compiler version must be at least ${APPLE_CLANG_MINIMUM_VERSION} (Xcode ${XCODE_MINIMUM_VERSION})." )
elseif ( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 11.0.0 )
2020-05-29 10:00:12 +00:00
# char8_t is available starting (upstream vanilla) Clang 7, but prior to Clang 8,
2020-05-06 16:47:13 +00:00
# it is not enabled by -std=c++20 and can be enabled with an explicit -fchar8_t.
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fchar8_t" )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fchar8_t" )
endif ( )
else ( )
2021-10-24 06:38:47 +00:00
set ( CLANG_MINIMUM_VERSION 12 )
2020-05-06 16:47:13 +00:00
if ( CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${ CLANG_MINIMUM_VERSION } )
message ( FATAL_ERROR "Clang version must be at least ${CLANG_MINIMUM_VERSION}." )
endif ( )
2019-10-30 07:01:53 +00:00
endif ( )
else ( )
2020-04-14 14:32:55 +00:00
message ( WARNING "You are using an unsupported compiler. Compilation has only been tested with Clang and GCC." )
2019-10-30 07:01:53 +00:00
endif ( )
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
2021-11-26 23:24:09 +00:00
# Linker
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
2020-08-14 15:44:04 +00:00
if ( COMPILER_GCC AND NOT LINKER_NAME )
2019-12-11 10:04:51 +00:00
find_program ( LLD_PATH NAMES "ld.lld" )
find_program ( GOLD_PATH NAMES "ld.gold" )
2020-08-14 15:44:04 +00:00
elseif ( NOT LINKER_NAME )
2019-12-11 10:04:51 +00:00
find_program ( LLD_PATH NAMES "ld.lld-${COMPILER_VERSION_MAJOR}" "lld-${COMPILER_VERSION_MAJOR}" "ld.lld" "lld" )
find_program ( GOLD_PATH NAMES "ld.gold" "gold" )
endif ( )
2019-10-30 07:01:53 +00:00
2020-08-14 15:44:04 +00:00
if ( OS_LINUX AND NOT LINKER_NAME )
2020-03-13 15:20:25 +00:00
# We prefer LLD linker over Gold or BFD on Linux.
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 )
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 ( )
if ( LINKER_NAME )
2021-03-11 08:03:03 +00:00
if ( COMPILER_CLANG AND ( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 12.0.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 12.0.0 ) )
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 ( )
2021-05-21 14:42:04 +00:00
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --ld-path=${LLD_PATH}" )
set ( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --ld-path=${LLD_PATH}" )
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
message ( STATUS "Using custom linker by name: ${LINKER_NAME}" )
endif ( )
2021-11-26 23:24:09 +00:00
# Archiver
if ( COMPILER_GCC )
find_program ( LLVM_AR_PATH NAMES "llvm-ar" "llvm-ar-13" "llvm-ar-12" "llvm-ar-11" )
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 ( )
# Ranlib
if ( COMPILER_GCC )
find_program ( LLVM_RANLIB_PATH NAMES "llvm-ranlib" "llvm-ranlib-13" "llvm-ranlib-12" "llvm-ranlib-11" )
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 ( )
# Install Name Tool
if ( COMPILER_GCC )
find_program ( LLVM_INSTALL_NAME_TOOL_PATH NAMES "llvm-install-name-tool" "llvm-install-name-tool-13" "llvm-install-name-tool-12" "llvm-install-name-tool-11" )
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 ( )
# Objcopy
if ( COMPILER_GCC )
find_program ( OBJCOPY_PATH NAMES "llvm-objcopy" "llvm-objcopy-13" "llvm-objcopy-12" "llvm-objcopy-11" "objcopy" )
else ( )
find_program ( OBJCOPY_PATH NAMES "llvm-objcopy-${COMPILER_VERSION_MAJOR}" "llvm-objcopy" "objcopy" )
endif ( )
if ( NOT OBJCOPY_PATH AND OS_DARWIN )
find_program ( BREW_PATH NAMES "brew" )
if ( BREW_PATH )
execute_process ( COMMAND ${ BREW_PATH } --prefix llvm ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE LLVM_PREFIX )
if ( LLVM_PREFIX )
find_program ( OBJCOPY_PATH NAMES "llvm-objcopy" PATHS "${LLVM_PREFIX}/bin" NO_DEFAULT_PATH )
endif ( )
if ( NOT OBJCOPY_PATH )
execute_process ( COMMAND ${ BREW_PATH } --prefix binutils ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE BINUTILS_PREFIX )
if ( BINUTILS_PREFIX )
find_program ( OBJCOPY_PATH NAMES "objcopy" PATHS "${BINUTILS_PREFIX}/bin" NO_DEFAULT_PATH )
endif ( )
endif ( )
endif ( )
endif ( )
if ( OBJCOPY_PATH )
message ( STATUS "Using objcopy: ${OBJCOPY_PATH}" )
else ( )
message ( FATAL_ERROR "Cannot find objcopy." )
endif ( )