Skip to content

Commit

Permalink
simplify cmake compiler version detection
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardmgruber committed Mar 21, 2022
1 parent cba85ad commit 9879ab4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 112 deletions.
44 changes: 6 additions & 38 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.13)

cmake_policy(SET CMP0028 NEW) # Double colon in target name means ALIAS or IMPORTED target.
cmake_policy(SET CMP0048 NEW) # The ``project()`` command manages VERSION variables.
Expand Down Expand Up @@ -46,42 +46,13 @@ if(USE_CCACHE)
endif()
endif()

# TODO: check that 'decltype' compiles
# TODO: check that 'constexpr' compiles
if(NOT Vc_COMPILER_IS_MSVC) # MSVC doesn't provide a switch to turn C++11 on/off AFAIK
AddCompilerFlag("-std=c++14" CXX_RESULT _ok CXX_FLAGS CMAKE_CXX_FLAGS)
if(NOT _ok)
AddCompilerFlag("-std=c++1y" CXX_RESULT _ok CXX_FLAGS CMAKE_CXX_FLAGS)
if(NOT _ok)
AddCompilerFlag("-std=c++11" CXX_RESULT _ok CXX_FLAGS CMAKE_CXX_FLAGS)
if(NOT _ok)
AddCompilerFlag("-std=c++0x" CXX_RESULT _ok CXX_FLAGS CMAKE_CXX_FLAGS)
if(NOT _ok)
message(FATAL_ERROR "Vc 1.x requires C++11, better even C++14. It seems this is not available. If this was incorrectly determined please notify [email protected]")
endif()
endif()
endif()
endif()
elseif(MSVC_VERSION LESS 1900)
message(FATAL_ERROR "Vc 1.x requires at least Visual Studio 2015.")
endif()

if(Vc_COMPILER_IS_GCC)
if(Vc_GCC_VERSION VERSION_GREATER "5.0.0" AND Vc_GCC_VERSION VERSION_LESS "6.0.0")
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "5.0.0" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0.0")
UserWarning("GCC 5 goes into an endless loop comiling example_scaling_scalar. Therefore, this target is disabled.")
list(APPEND disabled_targets
example_scaling_scalar
)
list(APPEND disabled_targets example_scaling_scalar)
endif()
elseif(Vc_COMPILER_IS_MSVC)
if(MSVC_VERSION LESS 1700)
# MSVC before 2012 has a broken std::vector::resize implementation. STL + Vc code will probably not compile.
# UserWarning in VcMacros.cmake
list(APPEND disabled_targets
stlcontainer_sse
stlcontainer_avx
)
endif()
# Disable warning "C++ exception specification ignored except to indicate a function is not __declspec(nothrow)"
# MSVC emits the warning for the _UnitTest_Compare desctructor which needs the throw declaration so that it doesn't std::terminate
AddCompilerFlag("/wd4290")
Expand All @@ -97,10 +68,6 @@ if(Vc_COMPILER_IS_INTEL)
AddCompilerFlag("-fp-model source")
endif()

if(CMAKE_BUILD_TYPE STREQUAL "" AND NOT CMAKE_CXX_FLAGS MATCHES "-O[123]")
message(STATUS "WARNING! It seems you are compiling without optimization. Please set CMAKE_BUILD_TYPE.")
endif(CMAKE_BUILD_TYPE STREQUAL "" AND NOT CMAKE_CXX_FLAGS MATCHES "-O[123]")

include_directories(${CMAKE_CURRENT_SOURCE_DIR}) # ${CMAKE_CURRENT_SOURCE_DIR}/include)

add_custom_target(other VERBATIM)
Expand Down Expand Up @@ -129,6 +96,7 @@ else()
list(APPEND _srcs src/support_dummy.cpp)
endif()
add_library(Vc STATIC ${_srcs})
target_compile_features(Vc PUBLIC cxx_std_11)
set_property(TARGET Vc APPEND PROPERTY COMPILE_OPTIONS ${libvc_compile_flags})
add_target_property(Vc LABELS "other")
if(XCODE)
Expand Down Expand Up @@ -245,12 +213,12 @@ configure_file(${PROJECT_SOURCE_DIR}/CTestCustom.cmake ${PROJECT_BINARY_DIR}/CTe
if(BUILD_TESTING)
add_custom_target(build_tests ALL VERBATIM)
add_subdirectory(tests)
endif(BUILD_TESTING)
endif()

set(BUILD_EXAMPLES FALSE CACHE BOOL "Build examples.")
if(BUILD_EXAMPLES)
add_subdirectory(examples)
endif(BUILD_EXAMPLES)
endif()

# Hide Vc_IMPL as it is only meant for users of Vc
mark_as_advanced(Vc_IMPL)
Expand Down
4 changes: 2 additions & 2 deletions cmake/AddTargetProperty.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ macro(add_target_property _target _prop _value)
get_target_property(_oldprop "${_target}" ${_prop})
if(NOT _oldprop)
set_target_properties("${_target}" PROPERTIES ${_prop} "${_value}")
else(NOT _oldprop)
else()
set_target_properties("${_target}" PROPERTIES ${_prop} "${_oldprop} ${_value}")
endif(NOT _oldprop)
endif()
endmacro(add_target_property)
2 changes: 1 addition & 1 deletion cmake/CheckCXXCompilerFlag.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ MACRO (CHECK_CXX_COMPILER_FLAG _FLAG _RESULT)
FAIL_REGEX " #10353: " # ICC: option '-mfma' ignored, suggest using '-march=core-avx2'
)
SET (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}")
ENDMACRO (CHECK_CXX_COMPILER_FLAG)
ENDMACRO ()

82 changes: 14 additions & 68 deletions cmake/VcMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@

cmake_minimum_required(VERSION 2.8.3...3.13)

get_filename_component(_currentDir "${CMAKE_CURRENT_LIST_FILE}" PATH)
include ("${_currentDir}/UserWarning.cmake")
include ("${_currentDir}/AddCompilerFlag.cmake")
include ("${_currentDir}/OptimizeForArchitecture.cmake")
include ("${CMAKE_CURRENT_LIST_DIR}/UserWarning.cmake")
include ("${CMAKE_CURRENT_LIST_DIR}/AddCompilerFlag.cmake")
include ("${CMAKE_CURRENT_LIST_DIR}/OptimizeForArchitecture.cmake")

macro(vc_determine_compiler)
if(NOT DEFINED Vc_COMPILER_IS_INTEL)
Expand All @@ -47,74 +46,30 @@ macro(vc_determine_compiler)
set(Vc_COMPILER_IS_CLANG false)
set(Vc_COMPILER_IS_MSVC false)
set(Vc_COMPILER_IS_GCC false)
if(CMAKE_CXX_COMPILER MATCHES "/(icpc|icc)$")
if(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
set(Vc_COMPILER_IS_INTEL true)
exec_program(${CMAKE_CXX_COMPILER} ARGS -dumpversion OUTPUT_VARIABLE Vc_ICC_VERSION)
message(STATUS "Detected Compiler: Intel ${Vc_ICC_VERSION}")

# break build with too old clang as early as possible.
if(Vc_ICC_VERSION VERSION_LESS 18.0.0)
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 18.0.0)
message(FATAL_ERROR "Vc 1.4 requires least ICC 18")
endif()
elseif(CMAKE_CXX_COMPILER MATCHES "(opencc|openCC)$")
set(Vc_COMPILER_IS_OPEN64 true)
message(STATUS "Detected Compiler: Open64")
elseif(CMAKE_CXX_COMPILER MATCHES "clang\\+\\+$" OR "${_cxx_compiler_version}" MATCHES "clang")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(Vc_COMPILER_IS_CLANG true)
exec_program(${CMAKE_CXX_COMPILER} ARGS --version OUTPUT_VARIABLE Vc_CLANG_VERSION)
string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" Vc_CLANG_VERSION "${Vc_CLANG_VERSION}")
message(STATUS "Detected Compiler: Clang ${Vc_CLANG_VERSION}")

# break build with too old clang as early as possible.
if(Vc_CLANG_VERSION VERSION_LESS 3.4)
message(FATAL_ERROR "Vc 1.x requires C++11 support. This requires at least clang 3.4")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.7.0)
message(FATAL_ERROR "Clang 3.6 has serious issues with AVX code generation\nPlease update to a more recent clang version.\n")
endif()
elseif(MSVC)
set(Vc_COMPILER_IS_MSVC true)
message(STATUS "Detected Compiler: MSVC ${MSVC_VERSION}")
if(MSVC_VERSION LESS 1900)
message(FATAL_ERROR "Vc 1.x requires at least Visual Studio 2015.")
endif()
elseif(CMAKE_COMPILER_IS_GNUCXX)
set(Vc_COMPILER_IS_GCC true)
exec_program(${CMAKE_CXX_COMPILER} ARGS -dumpversion OUTPUT_VARIABLE Vc_GCC_VERSION)
message(STATUS "Detected Compiler: GCC ${Vc_GCC_VERSION}")

# some distributions patch their GCC to return nothing or only major and minor version on -dumpversion.
# In that case we must extract the version number from --version.
if(NOT Vc_GCC_VERSION OR Vc_GCC_VERSION MATCHES "^[0-9]\\.[0-9]+$")
exec_program(${CMAKE_CXX_COMPILER} ARGS --version OUTPUT_VARIABLE Vc_GCC_VERSION)
string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" Vc_GCC_VERSION "${Vc_GCC_VERSION}")
message(STATUS "GCC Version from --version: ${Vc_GCC_VERSION}")
endif()

# some distributions patch their GCC to be API incompatible to what the FSF released. In
# those cases we require a macro to identify the distribution version
find_program(Vc_lsb_release lsb_release)
mark_as_advanced(Vc_lsb_release)
if(Vc_lsb_release)
if(NOT Vc_distributor_id)
execute_process(COMMAND ${Vc_lsb_release} -is OUTPUT_VARIABLE Vc_distributor_id OUTPUT_STRIP_TRAILING_WHITESPACE)
string(TOUPPER "${Vc_distributor_id}" Vc_distributor_id)
set(Vc_distributor_id "${Vc_distributor_id}" CACHE STRING "lsb distribution id")
execute_process(COMMAND ${Vc_lsb_release} -rs OUTPUT_VARIABLE Vc_distributor_release OUTPUT_STRIP_TRAILING_WHITESPACE)
set(Vc_distributor_release "${Vc_distributor_release}" CACHE STRING "lsb release id")
endif()
if(Vc_distributor_id STREQUAL "UBUNTU")
execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE _gcc_version)
string(REGEX MATCH "\\(.* ${Vc_GCC_VERSION}-([0-9]+).*\\)" _tmp "${_gcc_version}")
if(_tmp)
set(_patch ${CMAKE_MATCH_1})
string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _tmp "${Vc_distributor_release}")
execute_process(COMMAND printf 0x%x%02x%02x ${CMAKE_MATCH_1} ${CMAKE_MATCH_2} ${_patch} OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE _tmp)
set(Vc_DEFINITIONS "${Vc_DEFINITIONS} -D__GNUC_UBUNTU_VERSION__=${_tmp}")
endif()
endif()
endif()

# break build with too old GCC as early as possible.
if(Vc_GCC_VERSION VERSION_LESS 4.8.1)
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8.1)
message(FATAL_ERROR "Vc 1.x requires C++11 support. This requires at least GCC 4.8.1")
endif()
else()
message(WARNING "Untested/-supported Compiler (${CMAKE_CXX_COMPILER}) for use with Vc.\nPlease fill out the missing parts in the CMake scripts and submit a patch to http://code.compeng.uni-frankfurt.de/projects/vc")
message(WARNING "Untested/-supported Compiler (${CMAKE_CXX_COMPILER}) for use with Vc.\nPlease fill out the missing parts in the CMake scripts and submit a patch to https://github.com/VcDevel/Vc")
endif()
endif()
endmacro()
Expand Down Expand Up @@ -207,7 +162,7 @@ int main() { return 0; }
set(Vc_DEFINITIONS "${Vc_DEFINITIONS} -DVc_HAVE_LIBMVEC=1")
endif()
endif()
endif(Vc_LIB_MVEC)
endif()

set(_add_warning_flags false)
set(_add_buildtype_flags false)
Expand Down Expand Up @@ -337,20 +292,11 @@ int main() { return 0; }
vc_add_compiler_flag(Vc_COMPILE_FLAGS "/Gv") # default to __vectorcall
vc_add_compiler_flag(Vc_COMPILE_FLAGS "/bigobj") # required for building tests with AVX

if(MSVC_VERSION LESS 1900)
UserWarning("MSVC before 2015 does not support enough of C++11")
endif()
elseif(Vc_COMPILER_IS_CLANG)
##################################################################################################
# Clang #
##################################################################################################

if(Vc_CLANG_VERSION VERSION_GREATER "3.5.99" AND Vc_CLANG_VERSION VERSION_LESS 3.7.0)
UserWarning("Clang 3.6 has serious issues with AVX code generation, frequently losing 50% of the data. AVX is therefore disabled.\nPlease update to a more recent clang version.\n")
set(Vc_AVX_INTRINSICS_BROKEN true)
set(Vc_AVX2_INTRINSICS_BROKEN true)
endif()

# disable these warnings because clang shows them for function overloads that were discarded via SFINAE
vc_add_compiler_flag(Vc_COMPILE_FLAGS "-Wno-local-type-template-args")
vc_add_compiler_flag(Vc_COMPILE_FLAGS "-Wno-unnamed-type-template-args")
Expand Down
5 changes: 2 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ vc_set_first_flag_of(_sse4_1_flag "-xSSE4.1" "-msse4.1" "/arch:SSE2")
vc_set_first_flag_of(_avx_flag "-xAVX" "-mavx" "/arch:AVX")
vc_set_first_flag_of(_avx2_flag "-xCORE-AVX2" "-mavx2" "/arch:AVX2")
macro(vc_add_run_target _target)
if("${CMAKE_GENERATOR}" MATCHES "Visual Studio")
# do nothing. This just clutters the solution explorer
else()
# avoid cluttering the solution explorer
if(NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio")
add_custom_target(run_${_target}
${_target} -v
DEPENDS ${_target}
Expand Down

0 comments on commit 9879ab4

Please sign in to comment.