From 1cb247a6dae0fddbb27ffb3ea63506b0fd83e636 Mon Sep 17 00:00:00 2001 From: rumgot Date: Thu, 24 Nov 2022 12:13:06 +0300 Subject: [PATCH] Replaced build types with flags (#853) --- .github/workflows/ci.yml | 6 +++-- CMakeLists.txt | 22 +++++------------- cmake/sanitizer.cmake | 48 ++++++++++++++++++---------------------- 3 files changed, 31 insertions(+), 45 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ccc5e8b7f..25dfe5423 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -292,13 +292,14 @@ jobs: - name: "[Release g++] Build & Test" env: CPR_BUILD_TESTS: ON + CPR_DEBUG_SANITIZER_FLAG_ALL: ON uses: ashutoshvarma/action-cmake-build@master with: build-dir: ${{github.workspace}}/build source-dir: ${{github.workspace}} cc: gcc cxx: g++ - build-type: AllSan + build-type: Debug run-test: true ctest-options: -V @@ -319,13 +320,14 @@ jobs: - name: "[Release g++] Build & Test" env: CPR_BUILD_TESTS: ON + CPR_DEBUG_SANITIZER_FLAG_THREAD: ON uses: ashutoshvarma/action-cmake-build@master with: build-dir: ${{github.workspace}}/build source-dir: ${{github.workspace}} cc: gcc cxx: g++ - build-type: ThreadSan + build-type: Debug run-test: true ctest-options: -V diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ef8b0650..27127ea48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,6 +68,11 @@ cpr_option(CPR_BUILD_TESTS_SSL "Set to ON to build cpr ssl tests" ${CPR_BUILD_TE cpr_option(CPR_BUILD_TESTS_PROXY "Set to ON to build proxy tests. They fail in case there is no valid proxy server available in proxy_tests.cpp" OFF) cpr_option(CPR_SKIP_CA_BUNDLE_SEARCH "Skip searching for Certificate Authority certs. Turn ON systems like iOS where file access is restricted and prevents https from working." OFF) cpr_option(CPR_USE_BOOST_FILESYSTEM "Set to ON to use the Boost.Filesystem library on OSX." OFF) +cpr_option(CPR_DEBUG_SANITIZER_FLAG_THREAD "Enables the ThreadSanitizer for debug builds." OFF) +cpr_option(CPR_DEBUG_SANITIZER_FLAG_ADDR "Enables the AddressSanitizer for debug builds." OFF) +cpr_option(CPR_DEBUG_SANITIZER_FLAG_LEAK "Enables the LeakSanitizer for debug builds." OFF) +cpr_option(CPR_DEBUG_SANITIZER_FLAG_UB "Enables the UndefinedBehaviorSanitizer for debug builds." OFF) +cpr_option(CPR_DEBUG_SANITIZER_FLAG_ALL "Enables all sanitizers for debug builds except the ThreadSanitizer since it is incompatible with the other sanitizers." OFF) message(STATUS "=======================================================") if (CPR_FORCE_USE_SYSTEM_CURL) @@ -169,23 +174,6 @@ find_package(OpenSSL REQUIRED) add_compile_definitions(OPENSSL_BACKEND_USED) endif() -get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) -if (NOT isMultiConfig) - set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${ALLOWED_BUILD_TYPES}") - if (NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE) - elseif(NOT CMAKE_BUILD_TYPE IN_LIST ALLOWED_BUILD_TYPES) - message(FATAL_ERROR "Invalid build type: ${CMAKE_BUILD_TYPE}") - endif() -else () - unset(CMAKE_BUILD_TYPE) - foreach(TYPE ${ALLOWED_BUILD_TYPES}) - if (NOT ${TYPE} IN_LIST CMAKE_CONFIGURATION_TYPES) - list(APPEND CMAKE_CONFIGURATION_TYPES ${TYPE}) - endif() - endforeach() -endif() - # Curl configuration if(CPR_USE_SYSTEM_CURL) if(CPR_ENABLE_SSL) diff --git a/cmake/sanitizer.cmake b/cmake/sanitizer.cmake index 090959806..10cdc4624 100644 --- a/cmake/sanitizer.cmake +++ b/cmake/sanitizer.cmake @@ -1,7 +1,6 @@ include(CheckCXXCompilerFlag) include(CheckCXXSourceRuns) -set(ALLOWED_BUILD_TYPES Debug Release RelWithDebInfo MinSizeRel) set(ALL_SAN_FLAGS "") # No sanitizers when cross compiling to prevent stuff like this: https://github.com/whoshuu/cpr/issues/582 @@ -12,13 +11,7 @@ if(NOT CMAKE_CROSSCOMPILING) set(CMAKE_REQUIRED_FLAGS "${THREAD_SAN_FLAGS}") check_cxx_source_runs("int main() { return 0; }" THREAD_SANITIZER_AVAILABLE) set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG}) - if(THREAD_SANITIZER_AVAILABLE) - list(APPEND ALLOWED_BUILD_TYPES ThreadSan) - # Do not add Thread sanitizer to all sanitizer because it is incompatible with other sanitizer - endif() - set(CMAKE_C_FLAGS_THREADSAN "${CMAKE_C_FLAGS_DEBUG} ${THREAD_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C compiler during thread sanitizer builds." FORCE) - set(CMAKE_CXX_FLAGS_THREADSAN "${CMAKE_CXX_FLAGS_DEBUG} ${THREAD_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C++ compiler during thread sanitizer builds." FORCE) - set(CMAKE_SHARED_LINKER_FLAGS_THREADSAN "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during thread sanitizer builds" FORCE) + # Do not add the ThreadSanitizer for builds with all sanitizers enabled because it is incompatible with other sanitizers. # Address sanitizer set(ADDR_SAN_FLAGS "-fsanitize=address") @@ -27,34 +20,22 @@ if(NOT CMAKE_CROSSCOMPILING) check_cxx_source_runs("int main() { return 0; }" ADDRESS_SANITIZER_AVAILABLE) set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG}) if(ADDRESS_SANITIZER_AVAILABLE) - list(APPEND ALLOWED_BUILD_TYPES AddrSan) set(ALL_SAN_FLAGS "${ALL_SAN_FLAGS} ${ADDR_SAN_FLAGS}") endif() - set(CMAKE_C_FLAGS_ADDRSAN "${CMAKE_C_FLAGS_DEBUG} ${ADDR_SAN_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls" CACHE INTERNAL "Flags used by the C compiler during address sanitizer builds." FORCE) - set(CMAKE_CXX_FLAGS_ADDRSAN "${CMAKE_CXX_FLAGS_DEBUG} ${ADDR_SAN_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls" CACHE INTERNAL "Flags used by the C++ compiler during address sanitizer builds." FORCE) - set(CMAKE_SHARED_LINKER_FLAGS_ADDRSAN "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during address sanitizer builds" FORCE) # Leak sanitizer set(LEAK_SAN_FLAGS "-fsanitize=leak") check_cxx_compiler_flag(${LEAK_SAN_FLAGS} LEAK_SANITIZER_AVAILABLE) if(LEAK_SANITIZER_AVAILABLE) - list(APPEND ALLOWED_BUILD_TYPES LeakSan) set(ALL_SAN_FLAGS "${ALL_SAN_FLAGS} ${LEAK_SAN_FLAGS}") endif() - set(CMAKE_C_FLAGS_LEAKSAN "${CMAKE_C_FLAGS_DEBUG} ${LEAK_SAN_FLAGS} -fno-omit-frame-pointer" CACHE INTERNAL "Flags used by the C compiler during leak sanitizer builds." FORCE) - set(CMAKE_CXX_FLAGS_LEAKSAN "${CMAKE_CXX_FLAGS_DEBUG} ${LEAK_SAN_FLAGS} -fno-omit-frame-pointer" CACHE INTERNAL "Flags used by the C++ compiler during leak sanitizer builds." FORCE) - set(CMAKE_SHARED_LINKER_FLAGS_LEAKSAN "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during leak sanitizer builds" FORCE) # Undefined behavior sanitizer set(UDEF_SAN_FLAGS "-fsanitize=undefined") check_cxx_compiler_flag(${UDEF_SAN_FLAGS} UNDEFINED_BEHAVIOUR_SANITIZER_AVAILABLE) if(UNDEFINED_BEHAVIOUR_SANITIZER_AVAILABLE) - list(APPEND ALLOWED_BUILD_TYPES UdefSan) set(ALL_SAN_FLAGS "${ALL_SAN_FLAGS} ${UDEF_SAN_FLAGS}") endif() - set(CMAKE_C_FLAGS_UDEFSAN "${CMAKE_C_FLAGS_DEBUG} ${UDEF_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C compiler during undefined behaviour sanitizer builds." FORCE) - set(CMAKE_CXX_FLAGS_UDEFSAN "${CMAKE_CXX_FLAGS_DEBUG} ${UDEF_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C++ compiler during undefined behaviour sanitizer builds." FORCE) - set(CMAKE_SHARED_LINKER_FLAGS_UDEFSAN "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during undefined behaviour sanitizer builds" FORCE) # All sanitizer (without thread sanitizer) if(NOT ALL_SAN_FLAGS STREQUAL "") @@ -62,12 +43,27 @@ if(NOT CMAKE_CROSSCOMPILING) set(CMAKE_REQUIRED_FLAGS "${ALL_SAN_FLAGS}") check_cxx_source_runs("int main() { return 0; }" ALL_SANITIZERS_AVAILABLE) set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG}) - if(ALL_SANITIZERS_AVAILABLE) - list(APPEND ALLOWED_BUILD_TYPES AllSan) - endif() endif() - set(CMAKE_C_FLAGS_ALLSAN "${CMAKE_C_FLAGS_DEBUG} ${ALL_SAN_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls" CACHE INTERNAL "Flags used by the C compiler during most possible sanitizer builds." FORCE) - set(CMAKE_CXX_FLAGS_ALLSAN "${CMAKE_CXX_FLAGS_DEBUG} ${ALL_SAN_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls" CACHE INTERNAL "Flags used by the C++ compiler during most possible sanitizer builds." FORCE) - set(CMAKE_SHARED_LINKER_FLAGS_ALLSAN "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during most possible sanitizer builds" FORCE) + if(CPR_DEBUG_SANITIZER_FLAG_THREAD AND THREAD_SANITIZER_AVAILABLE) + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${THREAD_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C compiler during thread sanitizer builds." FORCE) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${THREAD_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C++ compiler during thread sanitizer builds." FORCE) + set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during thread sanitizer builds" FORCE) + elseif(CPR_DEBUG_SANITIZER_FLAG_ADDR AND ADDRESS_SANITIZER_AVAILABLE) + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${ADDR_SAN_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls" CACHE INTERNAL "Flags used by the C compiler during address sanitizer builds." FORCE) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${ADDR_SAN_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls" CACHE INTERNAL "Flags used by the C++ compiler during address sanitizer builds." FORCE) + set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during address sanitizer builds" FORCE) + elseif(CPR_DEBUG_SANITIZER_FLAG_LEAK AND LEAK_SANITIZER_AVAILABLE) + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${LEAK_SAN_FLAGS} -fno-omit-frame-pointer" CACHE INTERNAL "Flags used by the C compiler during leak sanitizer builds." FORCE) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${LEAK_SAN_FLAGS} -fno-omit-frame-pointer" CACHE INTERNAL "Flags used by the C++ compiler during leak sanitizer builds." FORCE) + set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during leak sanitizer builds" FORCE) + elseif(CPR_DEBUG_SANITIZER_FLAG_UB AND UNDEFINED_BEHAVIOUR_SANITIZER_AVAILABLE) + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${UDEF_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C compiler during undefined behaviour sanitizer builds." FORCE) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${UDEF_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C++ compiler during undefined behaviour sanitizer builds." FORCE) + set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during undefined behaviour sanitizer builds" FORCE) + elseif(CPR_DEBUG_SANITIZER_FLAG_ALL AND ALL_SANITIZERS_AVAILABLE) + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${ALL_SAN_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls" CACHE INTERNAL "Flags used by the C compiler during most possible sanitizer builds." FORCE) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${ALL_SAN_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls" CACHE INTERNAL "Flags used by the C++ compiler during most possible sanitizer builds." FORCE) + set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during most possible sanitizer builds" FORCE) + endif() endif()