From 631a49920bea500eb624dacd822ba582e742da7b Mon Sep 17 00:00:00 2001 From: Alexander Neumann <30894796+Neumann-A@users.noreply.github.com> Date: Mon, 10 Jun 2024 14:18:44 +0200 Subject: [PATCH] Move version detection and flags to utilities --- CMakeLists.txt | 118 +++--------------------------------------- cmake/utilities.cmake | 113 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 111 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f23f31bb7..993b6d7876 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.15...3.24) +cmake_minimum_required(VERSION 3.15...3.24 FATAL_ERROR) if(NOT CMAKE_MSVC_RUNTIME_LIBRARY) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") @@ -40,6 +40,10 @@ project(vcpkg LANGUAGES C CXX ) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_STANDARD 17) + list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") include(cmake/utilities.cmake) @@ -48,116 +52,8 @@ include(cmake/utilities.cmake) # ======================== vcpkg_detect_compiler() - -if(MSVC AND NOT COMMAND target_precompile_headers) - message(FATAL_ERROR "CMake 3.16 (target_precompile_headers) is required to build with MSVC") -endif() - -if(VCPKG_EMBED_GIT_SHA) - if(DEFINED VCPKG_VERSION) - message(STATUS "Using supplied version SHA ${VCPKG_VERSION}.") - else() - find_package(Git REQUIRED) - execute_process( - COMMAND "${GIT_EXECUTABLE}" status --porcelain=v1 - WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" - OUTPUT_VARIABLE VCPKG_GIT_STATUS - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - - if(VCPKG_GIT_STATUS STREQUAL "") - execute_process( - COMMAND "${GIT_EXECUTABLE}" rev-parse HEAD - WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" - OUTPUT_VARIABLE VCPKG_VERSION - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - else() - message(WARNING "Skipping embedding SHA due to local changes.") - endif() - endif() -endif() - -if(NOT DEFINED VCPKG_VERSION OR VCPKG_VERSION STREQUAL "") - set(VCPKG_VERSION "unknownhash") -endif() - -if(NOT DEFINED VCPKG_BASE_VERSION OR VCPKG_BASE_VERSION STREQUAL "") - if(VCPKG_OFFICIAL_BUILD) - message(FATAL_ERROR "VCPKG_BASE_VERSION must be set for official builds.") - endif() - - # The first digit is 2 to work with vcpkg_minimum_required in scripts. - set(VCPKG_BASE_VERSION "2999-12-31") -endif() - -set(CMAKE_CXX_EXTENSIONS OFF) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_STANDARD 17) -if(MSVC) - # either MSVC, or clang-cl - string(APPEND CMAKE_C_FLAGS " -FC -permissive- -utf-8 /guard:cf") - string(APPEND CMAKE_CXX_FLAGS " /EHsc -FC -permissive- -utf-8 /guard:cf") - string(APPEND CMAKE_C_FLAGS_RELEASE " /Zi") - string(APPEND CMAKE_CXX_FLAGS_RELEASE " /Zi") - - string(APPEND CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO " /DEBUG /INCREMENTAL:NO /debugtype:cv,fixup /guard:cf") - string(APPEND CMAKE_EXE_LINKER_FLAGS_RELEASE " /DEBUG /INCREMENTAL:NO /debugtype:cv,fixup /guard:cf") - if (MSVC_CXX_ARCHITECTURE_ID STREQUAL "x64") - string(APPEND CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO " /CETCOMPAT") - string(APPEND CMAKE_EXE_LINKER_FLAGS_RELEASE " /CETCOMPAT") - endif() - - # Avoid CMake's default taking of the pretty names - string(REPLACE "/DAMD64" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") - string(REPLACE "/DAMD64" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") - string(REPLACE "/DARM64EC" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") - string(REPLACE "/DARM64EC" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") - - if(VCPKG_DEVELOPMENT_WARNINGS) - string(APPEND CMAKE_C_FLAGS " /W4 /sdl") - string(APPEND CMAKE_CXX_FLAGS " /W4 /sdl") - if(VCPKG_COMPILER STREQUAL "clang") - string(APPEND CMAKE_C_FLAGS " -Wmissing-prototypes -Wno-missing-field-initializers") - string(APPEND CMAKE_CXX_FLAGS " -Wmissing-prototypes -Wno-missing-field-initializers") - elseif(VCPKG_MSVC_ANALYZE) - # -wd6553 is to workaround a violation in the Windows SDK - # c:\program files (x86)\windows kits\10\include\10.0.22000.0\um\winreg.h(780) : warning C6553: The annotation for function 'RegOpenKeyExW' on _Param_(3) does not apply to a value type. - string(APPEND CMAKE_C_FLAGS " -analyze -analyze:stacksize 39000 -wd6553") - string(APPEND CMAKE_CXX_FLAGS " -analyze -analyze:stacksize 39000 -wd6553") - endif() - endif() - - if(VCPKG_WARNINGS_AS_ERRORS) - string(APPEND CMAKE_C_FLAGS " /WX") - string(APPEND CMAKE_CXX_FLAGS " /WX") - endif() -else() - # Neither MSVC nor clang-cl - if(VCPKG_DEVELOPMENT_WARNINGS) - # GCC and clang have different names for the same warning - if(VCPKG_COMPILER STREQUAL "gcc") - set(DECL_WARNING "-Wmissing-declarations") - elseif(VCPKG_COMPILER STREQUAL "clang") - set(DECL_WARNING "-Wmissing-prototypes -Wno-range-loop-analysis") - endif() - - string(APPEND CMAKE_C_FLAGS " -Wall -Wextra -Wpedantic -Wno-unknown-pragmas -Wno-missing-field-initializers ${DECL_WARNING}") - string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wpedantic -Wno-unknown-pragmas -Wno-missing-field-initializers -Wno-redundant-move ${DECL_WARNING}") - endif() - - if(VCPKG_WARNINGS_AS_ERRORS) - string(APPEND CMAKE_C_FLAGS " -Werror") - string(APPEND CMAKE_CXX_FLAGS " -Werror") - endif() -endif() - -if(APPLE) - set(CMAKE_C_ARCHIVE_CREATE " Scr ") - set(CMAKE_CXX_ARCHIVE_CREATE " Scr ") - set(CMAKE_C_ARCHIVE_FINISH " -no_warning_for_no_symbols -c ") - set(CMAKE_CXX_ARCHIVE_FINISH " -no_warning_for_no_symbols -c ") -endif() +vcpkg_define_version() +vcpkg_setup_compiler_flags() # ======================= # === Paths detection === diff --git a/cmake/utilities.cmake b/cmake/utilities.cmake index a975cc4e85..0f3b3183bc 100644 --- a/cmake/utilities.cmake +++ b/cmake/utilities.cmake @@ -1,3 +1,7 @@ +if(MSVC AND NOT COMMAND target_precompile_headers) + message(FATAL_ERROR "CMake 3.16 (target_precompile_headers) is required to build with MSVC") +endif() + # Outputs to Cache: VCPKG_COMPILER function(vcpkg_detect_compiler) if(NOT DEFINED CACHE{VCPKG_COMPILER}) @@ -63,3 +67,112 @@ function(vcpkg_target_add_sourcelink target) target_link_options("${target}" PRIVATE "/SOURCELINK:${native_json_filename}") endif() endfunction() + +function(vcpkg_define_version) + if(VCPKG_EMBED_GIT_SHA) + if(DEFINED VCPKG_VERSION) + message(STATUS "Using supplied version SHA ${VCPKG_VERSION}.") + else() + find_package(Git REQUIRED) + execute_process( + COMMAND "${GIT_EXECUTABLE}" status --porcelain=v1 + WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" + OUTPUT_VARIABLE VCPKG_GIT_STATUS + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + if(VCPKG_GIT_STATUS STREQUAL "") + execute_process( + COMMAND "${GIT_EXECUTABLE}" rev-parse HEAD + WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" + OUTPUT_VARIABLE VCPKG_VERSION + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + else() + message(WARNING "Skipping embedding SHA due to local changes.") + endif() + endif() + endif() + if(NOT DEFINED VCPKG_VERSION OR VCPKG_VERSION STREQUAL "") + set(VCPKG_VERSION "unknownhash") + endif() + + if(NOT DEFINED VCPKG_BASE_VERSION OR VCPKG_BASE_VERSION STREQUAL "") + if(VCPKG_OFFICIAL_BUILD) + message(FATAL_ERROR "VCPKG_BASE_VERSION must be set for official builds.") + endif() + + # The first digit is 2 to work with vcpkg_minimum_required in scripts. + set(VCPKG_BASE_VERSION "2999-12-31") + endif() + + set(VCPKG_BASE_VERSION "${VCPKG_BASE_VERSION}" PARENT_SCOPE) + set(VCPKG_VERSION "${VCPKG_VERSION}" PARENT_SCOPE) +endfunction() + +macro(vcpkg_setup_compiler_flags) + if(MSVC) + # either MSVC, or clang-cl + string(APPEND CMAKE_C_FLAGS " -FC -permissive- -utf-8 /guard:cf") + string(APPEND CMAKE_CXX_FLAGS " /EHsc -FC -permissive- -utf-8 /guard:cf") + string(APPEND CMAKE_C_FLAGS_RELEASE " /Zi") + string(APPEND CMAKE_CXX_FLAGS_RELEASE " /Zi") + + string(APPEND CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO " /DEBUG /INCREMENTAL:NO /debugtype:cv,fixup /guard:cf") + string(APPEND CMAKE_EXE_LINKER_FLAGS_RELEASE " /DEBUG /INCREMENTAL:NO /debugtype:cv,fixup /guard:cf") + if (MSVC_CXX_ARCHITECTURE_ID STREQUAL "x64") + string(APPEND CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO " /CETCOMPAT") + string(APPEND CMAKE_EXE_LINKER_FLAGS_RELEASE " /CETCOMPAT") + endif() + + # Avoid CMake's default taking of the pretty names + string(REPLACE "/DAMD64" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") + string(REPLACE "/DAMD64" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + string(REPLACE "/DARM64EC" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") + string(REPLACE "/DARM64EC" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + + if(VCPKG_DEVELOPMENT_WARNINGS) + string(APPEND CMAKE_C_FLAGS " /W4 /sdl") + string(APPEND CMAKE_CXX_FLAGS " /W4 /sdl") + if(VCPKG_COMPILER STREQUAL "clang") + string(APPEND CMAKE_C_FLAGS " -Wmissing-prototypes -Wno-missing-field-initializers") + string(APPEND CMAKE_CXX_FLAGS " -Wmissing-prototypes -Wno-missing-field-initializers") + elseif(VCPKG_MSVC_ANALYZE) + # -wd6553 is to workaround a violation in the Windows SDK + # c:\program files (x86)\windows kits\10\include\10.0.22000.0\um\winreg.h(780) : warning C6553: The annotation for function 'RegOpenKeyExW' on _Param_(3) does not apply to a value type. + string(APPEND CMAKE_C_FLAGS " -analyze -analyze:stacksize 39000 -wd6553") + string(APPEND CMAKE_CXX_FLAGS " -analyze -analyze:stacksize 39000 -wd6553") + endif() + endif() + + if(VCPKG_WARNINGS_AS_ERRORS) + string(APPEND CMAKE_C_FLAGS " /WX") + string(APPEND CMAKE_CXX_FLAGS " /WX") + endif() + else() + # Neither MSVC nor clang-cl + if(VCPKG_DEVELOPMENT_WARNINGS) + # GCC and clang have different names for the same warning + if(VCPKG_COMPILER STREQUAL "gcc") + set(DECL_WARNING "-Wmissing-declarations") + elseif(VCPKG_COMPILER STREQUAL "clang") + set(DECL_WARNING "-Wmissing-prototypes -Wno-range-loop-analysis") + endif() + + string(APPEND CMAKE_C_FLAGS " -Wall -Wextra -Wpedantic -Wno-unknown-pragmas -Wno-missing-field-initializers ${DECL_WARNING}") + string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wpedantic -Wno-unknown-pragmas -Wno-missing-field-initializers -Wno-redundant-move ${DECL_WARNING}") + endif() + + if(VCPKG_WARNINGS_AS_ERRORS) + string(APPEND CMAKE_C_FLAGS " -Werror") + string(APPEND CMAKE_CXX_FLAGS " -Werror") + endif() + endif() + + if(APPLE) + set(CMAKE_C_ARCHIVE_CREATE " Scr ") + set(CMAKE_CXX_ARCHIVE_CREATE " Scr ") + set(CMAKE_C_ARCHIVE_FINISH " -no_warning_for_no_symbols -c ") + set(CMAKE_CXX_ARCHIVE_FINISH " -no_warning_for_no_symbols -c ") + endif() +endmacro()