From c61c4fab6f302a079cb588b3a4fe8624aecf56c9 Mon Sep 17 00:00:00 2001 From: Dav999-v Date: Tue, 22 Mar 2022 20:50:49 +0100 Subject: [PATCH] Fix C/C++ standards being unset for VVVVVV target if CMake is >= 3.1.3 So, it turns out we weren't quite done fighting CMake yet... To accommodate #869 (and actually also #272), the C standard was raised from C90 to C99. This turned out to require a bit of a fight with the CentOS CI's CMake version to get it to set the flags we wanted (and to not overwrite them later). Eventually the fix was to move the block that sets the standards to later in the file, which was done in 24353a54bb40665b64a93faebfb2ebd96cd0f6b5. As it apparently turns out, if your CMake is at least 3.1.3 and `CMAKE__STANDARD` is used instead of the workaround, the standard setting now has an effect on the third party libraries, but not on VVVVVV itself. The cause is (probably) the phrase "if it is set when a target is created" in the CMake documentation - the `CMAKE__STANDARD` values have to come before the VVVVVV target is defined. In other words, the compiler's default C/C++ standard will be used, probably something like C17 and C++17. As I can confirm with `__cplusplus` and `__STDC_VERSION__` with my recent-enough CMake. If I force the pre-3.1.3 workaround to be used, everything is compiled with C99/C++98 as expected; and the `-fno-exceptions` `-fno-rtti` flags appear everywhere regardless of version. So my fix is to make the CMakeLists a little less complex by simplifying away the `CMAKE__STANDARD` and `CMAKE__EXTENSIONS`, and always using the workaround regardless of CMake version. There's nothing wrong with the workaround, the same thing is also done for `-fno-exceptions` `-fno-rtti`, and it's good to have a less complicated CMakeLists that doesn't do different and unexpected things for different versions. --- desktop_version/CMakeLists.txt | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/desktop_version/CMakeLists.txt b/desktop_version/CMakeLists.txt index 7b7aec8f..384de9fc 100644 --- a/desktop_version/CMakeLists.txt +++ b/desktop_version/CMakeLists.txt @@ -248,27 +248,9 @@ if(MSVC) target_compile_options(VVVVVV PRIVATE /wd4244) endif() -if(${CMAKE_VERSION} VERSION_LESS "3.1.3") - message(WARNING "Your CMake version is too old; using workaround") - - if(MSVC) - # MSVC doesn't have /std:c99 or /std:c++98 switches! - else() - string(REGEX REPLACE "-std=[a-z0-9]+" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99") - - string(REGEX REPLACE "-std=[a-z0-9+]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98") - endif() -else() - set(CMAKE_C_STANDARD 99) - set(CMAKE_C_EXTENSIONS OFF) - - set(CMAKE_CXX_STANDARD 98) - set(CMAKE_CXX_EXTENSIONS OFF) -endif() - if(MSVC) + # MSVC doesn't have /std:c99 or /std:c++98 switches! + # Disable exceptions string(REGEX REPLACE "/EH[a-z]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") @@ -277,6 +259,12 @@ if(MSVC) string(REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-") else() + string(REGEX REPLACE "-std=[a-z0-9]+" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99") + + string(REGEX REPLACE "-std=[a-z0-9+]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98") + # Disable exceptions string(REPLACE "-fexceptions" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")