Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move version detection and compiler flags into utilities.cmake #1421

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 7 additions & 111 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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$<$<CONFIG:Debug>:Debug>")
Expand Down Expand Up @@ -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)

Expand All @@ -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 "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
endif()
vcpkg_define_version()
vcpkg_setup_compiler_flags()

# =======================
# === Paths detection ===
Expand Down
113 changes: 113 additions & 0 deletions cmake/utilities.cmake
Original file line number Diff line number Diff line change
@@ -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})
Expand Down Expand Up @@ -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 "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
endif()
endmacro()
Loading