From 92b3c2dcff3e85ad3d455f6ab845d9a97d3b525b Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 7 Dec 2020 12:22:15 +0200 Subject: Rewrite meson build scripts into cmakelists --- cmake/CompilerWarnings.cmake | 77 +++++++++++++++++++++++++++++++++++++ cmake/Sanitize.cmake | 33 ++++++++++++++++ cmake/StandardProjectSettings.cmake | 44 +++++++++++++++++++++ cmake/ThirdParty.cmake | 17 ++++++++ cmake/lcov.cmake | 4 ++ 5 files changed, 175 insertions(+) create mode 100644 cmake/CompilerWarnings.cmake create mode 100644 cmake/Sanitize.cmake create mode 100644 cmake/StandardProjectSettings.cmake create mode 100644 cmake/ThirdParty.cmake create mode 100644 cmake/lcov.cmake (limited to 'cmake') diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake new file mode 100644 index 0000000..c53660f --- /dev/null +++ b/cmake/CompilerWarnings.cmake @@ -0,0 +1,77 @@ +# Based on: +# https://github.com/lefticus/cpp_starter_project/blob/master/cmake/CompilerWarnings.cmake +# https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md + +function(enable_warnings) + # only enable warnings for debug builds + if(NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug") + return() + endif() + + set(MSVC_WARNINGS + /W4 # Baseline reasonable warnings + /w14242 # 'identifier': conversion from 'type1' to 'type1', possible loss of data + /w14254 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data + /w14263 # 'function': member function does not override any base class virtual member function + /w14265 # 'classname': class has virtual functions, but destructor is not virtual instances of this class may not + # be destructed correctly + /w14287 # 'operator': unsigned/negative constant mismatch + /we4289 # nonstandard extension used: 'variable': loop control variable declared in the for-loop is used outside + # the for-loop scope + /w14296 # 'operator': expression is always 'boolean_value' + /w14311 # 'variable': pointer truncation from 'type1' to 'type2' + /w14545 # expression before comma evaluates to a function which is missing an argument list + /w14546 # function call before comma missing argument list + /w14547 # 'operator': operator before comma has no effect; expected operator with side-effect + /w14549 # 'operator': operator before comma has no effect; did you intend 'operator'? + /w14555 # expression has no effect; expected expression with side- effect + /w14619 # pragma warning: there is no warning number 'number' + /w14640 # Enable warning on thread un-safe static member initialization + /w14826 # Conversion from 'type1' to 'type_2' is sign-extended. This may cause unexpected runtime behavior. + /w14905 # wide string literal cast to 'LPSTR' + /w14906 # string literal cast to 'LPWSTR' + /w14928 # illegal copy-initialization; more than one user-defined conversion has been implicitly applied + /permissive- # standards conformance mode for MSVC compiler. + ) + + set(BASE_WARNINGS + -Wall # reasonable and standard + -Wextra + -Werror=pedantic # warn if non-standard C++ is used + + -Wunused # warn on anything being unused + -Wshadow # warn the user if a variable declaration shadows one from a parent context + + -Wnon-virtual-dtor # warn the user if a class with virtual functions has a non-virtual destructor. + # This helps catch hard to track down memory errors + -Werror=redundant-decls + -Woverloaded-virtual # warn if you overload (not override) a virtual function + -Werror=return-type + + -Wold-style-cast # warn for c-style casts + -Wcast-align # warn for potential performance problem casts + -Wconversion # warn on type conversions that may lose data + -Wsign-conversion # warn on sign conversions + -Wnull-dereference # warn if a null dereference is detected + -Wdouble-promotion # warn if float is implicit promoted to double + -Wformat=2 # warn on security issues around functions that format output (ie printf) + + -Wconsumed + #-Wlifetime + -mspeculative-load-hardening # Spectre v1 mitigation + + -Wmisleading-indentation # warn if indentation implies blocks where blocks do not exist + #-Wduplicated-cond # warn if if / else chain has duplicated conditions + #-Wduplicated-branches # warn if if / else branches have duplicated code + #-Wlogical-op # warn about logical operations being used where bitwise were probably wanted + #-Wuseless-cast # warn if you perform a cast to the same type + ) + + if(MSVC) + add_compile_options(${MSVC_WARNINGS}) + message(STATUS "added ${MSVC_WARNINGS}") + else() + add_compile_options(${BASE_WARNINGS}) + message(STATUS "added ${BASE_WARNINGS}") + endif() +endfunction() diff --git a/cmake/Sanitize.cmake b/cmake/Sanitize.cmake new file mode 100644 index 0000000..29f7fa5 --- /dev/null +++ b/cmake/Sanitize.cmake @@ -0,0 +1,33 @@ +include(CheckCXXCompilerFlag) +include(CheckLinkerFlag) + +check_linker_flag(CXX -fsanitize=address cxx_asan) +check_cxx_compiler_flag(-fsanitize=leak cxx_lsan) + +function(target_sanitize target_name) + option(sanitize_address "Use address sanitizer" ${cxx_asan}) + option(sanitize_leak "Use leak sanitizer" ${cxx_lsan}) + + if(sanitize_address) + message(">>> using asan on ${target_name}") + target_compile_options(${target_name} PRIVATE -fsanitize=address -fno-optimize-sibling-calls -fsanitize-address-use-after-scope -fno-omit-frame-pointer) + target_link_options(${target_name} PRIVATE -fsanitize=address) + endif() + + if(sanitize_leak) + message(">>> using lsan on ${target_name}") + target_compile_options(${target_name} PRIVATE -fsanitize=leak -fno-omit-frame-pointer) + target_link_options(${target_name} PRIVATE -fsanitize=leak) + endif() +endfunction() + +function(target_sanitize_fuzzer target_name) + if(NOT ${CMAKE_CXX_COMPILER_ID} MATCHES Clang) + message("Cannot sanitize with ${CMAKE_CXX_COMPILER_ID} (needs clang)") + return() + endif() + + set_target_properties(${target_name} PROPERTIES + COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=fuzzer" + LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=fuzzer") +endfunction() diff --git a/cmake/StandardProjectSettings.cmake b/cmake/StandardProjectSettings.cmake new file mode 100644 index 0000000..bd88a73 --- /dev/null +++ b/cmake/StandardProjectSettings.cmake @@ -0,0 +1,44 @@ +# from here: +# https://github.com/lefticus/cpp_starter_project/blob/master/cmake/StandardProjectSettings.cmake + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +# Set a default build type if none was specified +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.") + set(CMAKE_BUILD_TYPE + RelWithDebInfo + CACHE STRING "Choose the type of build." FORCE) + # Set the possible values of build type for cmake-gui, ccmake + set_property( + CACHE CMAKE_BUILD_TYPE + PROPERTY STRINGS + "Debug" + "Release" + "MinSizeRel" + "RelWithDebInfo") +endif() + +# Generate compile_commands.json to make it easier to work with clang based tools +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +option(ENABLE_IPO "Enable Interprocedural Optimization, aka Link Time Optimization (LTO)" OFF) +if(ENABLE_IPO) + include(CheckIPOSupported) + check_ipo_supported(RESULT result OUTPUT output) + if(result) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) + else() + message(SEND_ERROR "IPO is not supported: ${output}") + endif() +endif() + +option(VCS_TAG "Read version information from git" ON) +if(VCS_TAG) + find_program(VCS git) + execute_process(COMMAND git describe --abbrev=0 OUTPUT_VARIABLE CMAKE_PROJECT_SHORT_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process(COMMAND git describe --long --abbrev=40 OUTPUT_VARIABLE CMAKE_PROJECT_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE) +else() + set(CMAKE_PROJECT_SHORT_VERSION ${CMAKE_PROJECT_VERSION}) +endif() \ No newline at end of file diff --git a/cmake/ThirdParty.cmake b/cmake/ThirdParty.cmake new file mode 100644 index 0000000..8e5b1cc --- /dev/null +++ b/cmake/ThirdParty.cmake @@ -0,0 +1,17 @@ +function(download_third_party path url archive hash_type hash) + + if(NOT EXISTS ${CMAKE_SOURCE_DIR}/third-party/${path}) + + if(NOT EXISTS ${CMAKE_BINARY_DIR}/${archive}) + message("${CMAKE_BINARY_DIR}/${archive} missing") + message("Downloading ${url}...") + file(DOWNLOAD ${url} ${CMAKE_BINARY_DIR}/${archive} + TIMEOUT 60 TLS_VERIFY ON + EXPECTED_HASH ${hash_type}=${hash}) + endif() + + file(ARCHIVE_EXTRACT INPUT ${CMAKE_BINARY_DIR}/${archive} + DESTINATION ${CMAKE_SOURCE_DIR}/third-party) + endif() + +endfunction() diff --git a/cmake/lcov.cmake b/cmake/lcov.cmake new file mode 100644 index 0000000..d6f990d --- /dev/null +++ b/cmake/lcov.cmake @@ -0,0 +1,4 @@ +function(target_lcov target_name) + target_compile_options(${target_name} INTERFACE --coverage -O0 -g) + target_link_libraries(${target_name} INTERFACE --coverage) +endfunction() -- cgit v1.2.1