From 9e27096c3701004b97cf673692622349c8a9be92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20D=C3=BCrrenberger?= Date: Mon, 3 May 2021 20:09:34 +0200 Subject: [PATCH] Fix configuration conflicts with Clang - On Windows you can use Clang with both MSVC and MinGW - When using Clang with MSVC all the MSVC conditions should be fulfilled - When using Clang with MinGW all the MSVC steps should not be run --- CMakeLists.txt | 8 +++--- cmake/Config.cmake | 47 ++++++++++++++++++-------------- src/SFML/CMakeLists.txt | 4 +-- src/SFML/Graphics/CMakeLists.txt | 2 +- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 73711753..7c62bf58 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -217,12 +217,12 @@ if(SFML_OS_WINDOWS) sfml_set_option(SFML_USE_STATIC_STD_LIBS FALSE BOOL "TRUE to statically link to the standard libraries, FALSE to use them as DLLs") # the following combination of flags is not valid - if (BUILD_SHARED_LIBS AND SFML_USE_STATIC_STD_LIBS) + if(BUILD_SHARED_LIBS AND SFML_USE_STATIC_STD_LIBS) message(FATAL_ERROR "BUILD_SHARED_LIBS and SFML_USE_STATIC_STD_LIBS cannot be used together") endif() # for VC++, we can apply it globally by modifying the compiler flags - if(SFML_COMPILER_MSVC AND SFML_USE_STATIC_STD_LIBS) + if((SFML_COMPILER_MSVC OR (SFML_COMPILER_CLANG AND NOT MINGW)) AND SFML_USE_STATIC_STD_LIBS) foreach(flag CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) @@ -421,7 +421,7 @@ if(SFML_OS_WINDOWS) install(DIRECTORY extlibs/bin/x86/ DESTINATION ${CMAKE_INSTALL_BINDIR}) if(SFML_COMPILER_MSVC AND SFML_MSVC_VERSION LESS 14) install(DIRECTORY extlibs/libs-msvc/x86/ DESTINATION ${CMAKE_INSTALL_LIBDIR}) - elseif(SFML_COMPILER_MSVC) + elseif(SFML_COMPILER_MSVC OR (SFML_COMPILER_CLANG AND NOT MINGW)) install(DIRECTORY extlibs/libs-msvc-universal/x86/ DESTINATION ${CMAKE_INSTALL_LIBDIR}) else() install(DIRECTORY extlibs/libs-mingw/x86/ DESTINATION ${CMAKE_INSTALL_LIBDIR}) @@ -430,7 +430,7 @@ if(SFML_OS_WINDOWS) install(DIRECTORY extlibs/bin/x64/ DESTINATION ${CMAKE_INSTALL_BINDIR}) if(SFML_COMPILER_MSVC AND SFML_MSVC_VERSION LESS 14) install(DIRECTORY extlibs/libs-msvc/x64/ DESTINATION ${CMAKE_INSTALL_LIBDIR}) - elseif(SFML_COMPILER_MSVC) + elseif(SFML_COMPILER_MSVC OR (SFML_COMPILER_CLANG AND NOT MINGW)) install(DIRECTORY extlibs/libs-msvc-universal/x64/ DESTINATION ${CMAKE_INSTALL_LIBDIR}) else() install(DIRECTORY extlibs/libs-mingw/x64/ DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/cmake/Config.cmake b/cmake/Config.cmake index 116eddda..4bba794d 100644 --- a/cmake/Config.cmake +++ b/cmake/Config.cmake @@ -82,27 +82,12 @@ if(SFML_OS_FREEBSD OR SFML_OS_OPENBSD OR SFML_OS_NETBSD) endif() # detect the compiler and its version -# Note: on some platforms (OS X), CMAKE_COMPILER_IS_GNUCXX is true -# even when CLANG is used, therefore the Clang test is done first -if(CMAKE_CXX_COMPILER MATCHES "clang[+][+]" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") - # CMAKE_CXX_COMPILER_ID is an internal CMake variable subject to change, - # but there is no other way to detect CLang at the moment - set(SFML_COMPILER_CLANG 1) - execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "--version" OUTPUT_VARIABLE CLANG_VERSION_OUTPUT) - string(REGEX REPLACE ".*clang version ([0-9]+\\.[0-9]+).*" "\\1" SFML_CLANG_VERSION "${CLANG_VERSION_OUTPUT}") -elseif(CMAKE_COMPILER_IS_GNUCXX) - set(SFML_COMPILER_GCC 1) - execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "-dumpversion" OUTPUT_VARIABLE GCC_VERSION_OUTPUT) - string(REGEX REPLACE "([0-9]+\\.[0-9]+).*" "\\1" SFML_GCC_VERSION "${GCC_VERSION_OUTPUT}") - execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "--version" OUTPUT_VARIABLE GCC_COMPILER_VERSION) - string(REGEX MATCHALL ".*(tdm[64]*-[1-9]).*" SFML_COMPILER_GCC_TDM "${GCC_COMPILER_VERSION}") - execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "-dumpmachine" OUTPUT_VARIABLE GCC_MACHINE) - string(STRIP "${GCC_MACHINE}" GCC_MACHINE) - if(GCC_MACHINE MATCHES ".*w64.*") - set(SFML_COMPILER_GCC_W64 1) - endif() -elseif(MSVC) +# Note: The detection is order is important because: +# - Visual Studio can both use MSVC and Clang +# - GNUCXX can still be set on macOS when using Clang +if(MSVC) set(SFML_COMPILER_MSVC 1) + if(MSVC_VERSION EQUAL 1400) set(SFML_MSVC_VERSION 8) elseif(MSVC_VERSION EQUAL 1500) @@ -115,6 +100,28 @@ elseif(MSVC) set(SFML_MSVC_VERSION 12) elseif(MSVC_VERSION EQUAL 1900) set(SFML_MSVC_VERSION 14) + elseif(MSVC_VERSION LESS_EQUAL 1919) + set(SFML_MSVC_VERSION 15) + elseif(MSVC_VERSION LESS_EQUAL 1929) + set(SFML_MSVC_VERSION 16) + endif() +elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(SFML_COMPILER_CLANG 1) + + execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "--version" OUTPUT_VARIABLE CLANG_VERSION_OUTPUT) + string(REGEX REPLACE ".*clang version ([0-9]+\\.[0-9]+).*" "\\1" SFML_CLANG_VERSION "${CLANG_VERSION_OUTPUT}") +elseif(CMAKE_COMPILER_IS_GNUCXX) + set(SFML_COMPILER_GCC 1) + + execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "-dumpversion" OUTPUT_VARIABLE GCC_VERSION_OUTPUT) + string(REGEX REPLACE "([0-9]+\\.[0-9]+).*" "\\1" SFML_GCC_VERSION "${GCC_VERSION_OUTPUT}") + execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "--version" OUTPUT_VARIABLE GCC_COMPILER_VERSION) + string(REGEX MATCHALL ".*(tdm[64]*-[1-9]).*" SFML_COMPILER_GCC_TDM "${GCC_COMPILER_VERSION}") + execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "-dumpmachine" OUTPUT_VARIABLE GCC_MACHINE) + string(STRIP "${GCC_MACHINE}" GCC_MACHINE) + + if(GCC_MACHINE MATCHES ".*w64.*") + set(SFML_COMPILER_GCC_W64 1) endif() else() message(FATAL_ERROR "Unsupported compiler") diff --git a/src/SFML/CMakeLists.txt b/src/SFML/CMakeLists.txt index 0014abff..84a77059 100644 --- a/src/SFML/CMakeLists.txt +++ b/src/SFML/CMakeLists.txt @@ -5,7 +5,7 @@ include(${PROJECT_SOURCE_DIR}/cmake/Macros.cmake) # let CMake know about our additional libraries paths if (SFML_OS_WINDOWS) set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${PROJECT_SOURCE_DIR}/extlibs/headers") - if(SFML_COMPILER_GCC) + if(SFML_COMPILER_GCC OR (SFML_COMPILER_CLANG AND MINGW)) if(ARCH_32BITS) set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${PROJECT_SOURCE_DIR}/extlibs/libs-mingw/x86") set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${PROJECT_SOURCE_DIR}/extlibs/bin/x86") @@ -13,7 +13,7 @@ if (SFML_OS_WINDOWS) set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${PROJECT_SOURCE_DIR}/extlibs/libs-mingw/x64") set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${PROJECT_SOURCE_DIR}/extlibs/bin/x64") endif() - elseif(SFML_COMPILER_MSVC OR SFML_COMPILER_CLANG) + elseif(SFML_COMPILER_MSVC OR (SFML_COMPILER_CLANG AND NOT MINGW)) if(SFML_MSVC_VERSION LESS 14) if(ARCH_32BITS) set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${PROJECT_SOURCE_DIR}/extlibs/libs-msvc/x86") diff --git a/src/SFML/Graphics/CMakeLists.txt b/src/SFML/Graphics/CMakeLists.txt index 139703c0..96e28481 100644 --- a/src/SFML/Graphics/CMakeLists.txt +++ b/src/SFML/Graphics/CMakeLists.txt @@ -119,7 +119,7 @@ endif() # starting from Visual Studio 2015, inline versions of some C functions are used; for compatibility link this library # see https://docs.microsoft.com/en-us/cpp/porting/overview-of-potential-upgrade-issues-visual-cpp?view=msvc-160#libraries -if(SFML_COMPILER_MSVC AND MSVC_VERSION GREATER_EQUAL 1900) +if((SFML_COMPILER_MSVC AND SFML_MSVC_VERSION GREATER_EQUAL 14) OR (SFML_COMPILER_CLANG AND SFML_OS_WINDOWS AND NOT MINGW)) target_link_libraries(sfml-graphics PRIVATE legacy_stdio_definitions.lib) endif()