Skip to content

Commit a2d35a1

Browse files
rumgotCOM8
authored andcommitted
Replaced build types with flags (#853)
1 parent ced8e4c commit a2d35a1

File tree

3 files changed

+31
-45
lines changed

3 files changed

+31
-45
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,14 @@ jobs:
226226
- name: "[Release g++] Build & Test"
227227
env:
228228
CPR_BUILD_TESTS: ON
229+
CPR_DEBUG_SANITIZER_FLAG_ALL: ON
229230
uses: ashutoshvarma/action-cmake-build@master
230231
with:
231232
build-dir: ${{github.workspace}}/build
232233
source-dir: ${{github.workspace}}
233234
cc: gcc
234235
cxx: g++
235-
build-type: AllSan
236+
build-type: Debug
236237
run-test: true
237238
ctest-options: -V
238239

@@ -253,13 +254,14 @@ jobs:
253254
- name: "[Release g++] Build & Test"
254255
env:
255256
CPR_BUILD_TESTS: ON
257+
CPR_DEBUG_SANITIZER_FLAG_THREAD: ON
256258
uses: ashutoshvarma/action-cmake-build@master
257259
with:
258260
build-dir: ${{github.workspace}}/build
259261
source-dir: ${{github.workspace}}
260262
cc: gcc
261263
cxx: g++
262-
build-type: ThreadSan
264+
build-type: Debug
263265
run-test: true
264266
ctest-options: -V
265267

CMakeLists.txt

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ cpr_option(CPR_ENABLE_LINTING "Set to ON to enable clang linting." OFF)
4747
cpr_option(CPR_ENABLE_CPPCHECK "Set to ON to enable Cppcheck static analysis. Requires CPR_BUILD_TESTS and CPR_BUILD_TESTS_SSL to be OFF to prevent checking google tests source code." OFF)
4848
cpr_option(CPR_BUILD_TESTS "Set to ON to build cpr tests." OFF)
4949
cpr_option(CPR_BUILD_TESTS_SSL "Set to ON to build cpr ssl tests" ${CPR_BUILD_TESTS})
50+
cpr_option(CPR_DEBUG_SANITIZER_FLAG_THREAD "Enables the ThreadSanitizer for debug builds." OFF)
51+
cpr_option(CPR_DEBUG_SANITIZER_FLAG_ADDR "Enables the AddressSanitizer for debug builds." OFF)
52+
cpr_option(CPR_DEBUG_SANITIZER_FLAG_LEAK "Enables the LeakSanitizer for debug builds." OFF)
53+
cpr_option(CPR_DEBUG_SANITIZER_FLAG_UB "Enables the UndefinedBehaviorSanitizer for debug builds." OFF)
54+
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)
5055
message(STATUS "=======================================================")
5156

5257
include(GNUInstallDirs)
@@ -143,23 +148,6 @@ find_package(OpenSSL REQUIRED)
143148
add_compile_definitions(OPENSSL_BACKEND_USED)
144149
endif()
145150

146-
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
147-
if (NOT isMultiConfig)
148-
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${ALLOWED_BUILD_TYPES}")
149-
if (NOT CMAKE_BUILD_TYPE)
150-
set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
151-
elseif(NOT CMAKE_BUILD_TYPE IN_LIST ALLOWED_BUILD_TYPES)
152-
message(FATAL_ERROR "Invalid build type: ${CMAKE_BUILD_TYPE}")
153-
endif()
154-
else ()
155-
unset(CMAKE_BUILD_TYPE)
156-
foreach(TYPE ${ALLOWED_BUILD_TYPES})
157-
if (NOT ${TYPE} IN_LIST CMAKE_CONFIGURATION_TYPES)
158-
list(APPEND CMAKE_CONFIGURATION_TYPES ${TYPE})
159-
endif()
160-
endforeach()
161-
endif()
162-
163151
# Curl configuration
164152
if(CPR_FORCE_USE_SYSTEM_CURL)
165153
if(CPR_ENABLE_SSL)

cmake/sanitizer.cmake

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
include(CheckCXXCompilerFlag)
22
include(CheckCXXSourceRuns)
33

4-
set(ALLOWED_BUILD_TYPES Debug Release RelWithDebInfo MinSizeRel)
54
set(ALL_SAN_FLAGS "")
65

76
# 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)
1211
set(CMAKE_REQUIRED_FLAGS "${THREAD_SAN_FLAGS}")
1312
check_cxx_source_runs("int main() { return 0; }" THREAD_SANITIZER_AVAILABLE)
1413
set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG})
15-
if(THREAD_SANITIZER_AVAILABLE)
16-
list(APPEND ALLOWED_BUILD_TYPES ThreadSan)
17-
# Do not add Thread sanitizer to all sanitizer because it is incompatible with other sanitizer
18-
endif()
19-
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)
20-
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)
21-
set(CMAKE_SHARED_LINKER_FLAGS_THREADSAN "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during thread sanitizer builds" FORCE)
14+
# Do not add the ThreadSanitizer for builds with all sanitizers enabled because it is incompatible with other sanitizers.
2215

2316
# Address sanitizer
2417
set(ADDR_SAN_FLAGS "-fsanitize=address")
@@ -27,47 +20,50 @@ if(NOT CMAKE_CROSSCOMPILING)
2720
check_cxx_source_runs("int main() { return 0; }" ADDRESS_SANITIZER_AVAILABLE)
2821
set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG})
2922
if(ADDRESS_SANITIZER_AVAILABLE)
30-
list(APPEND ALLOWED_BUILD_TYPES AddrSan)
3123
set(ALL_SAN_FLAGS "${ALL_SAN_FLAGS} ${ADDR_SAN_FLAGS}")
3224
endif()
33-
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)
34-
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)
35-
set(CMAKE_SHARED_LINKER_FLAGS_ADDRSAN "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during address sanitizer builds" FORCE)
3625

3726
# Leak sanitizer
3827
set(LEAK_SAN_FLAGS "-fsanitize=leak")
3928
check_cxx_compiler_flag(${LEAK_SAN_FLAGS} LEAK_SANITIZER_AVAILABLE)
4029
if(LEAK_SANITIZER_AVAILABLE)
41-
list(APPEND ALLOWED_BUILD_TYPES LeakSan)
4230
set(ALL_SAN_FLAGS "${ALL_SAN_FLAGS} ${LEAK_SAN_FLAGS}")
4331
endif()
44-
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)
45-
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)
46-
set(CMAKE_SHARED_LINKER_FLAGS_LEAKSAN "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during leak sanitizer builds" FORCE)
4732

4833
# Undefined behavior sanitizer
4934
set(UDEF_SAN_FLAGS "-fsanitize=undefined")
5035
check_cxx_compiler_flag(${UDEF_SAN_FLAGS} UNDEFINED_BEHAVIOUR_SANITIZER_AVAILABLE)
5136
if(UNDEFINED_BEHAVIOUR_SANITIZER_AVAILABLE)
52-
list(APPEND ALLOWED_BUILD_TYPES UdefSan)
5337
set(ALL_SAN_FLAGS "${ALL_SAN_FLAGS} ${UDEF_SAN_FLAGS}")
5438
endif()
55-
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)
56-
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)
57-
set(CMAKE_SHARED_LINKER_FLAGS_UDEFSAN "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during undefined behaviour sanitizer builds" FORCE)
5839

5940
# All sanitizer (without thread sanitizer)
6041
if(NOT ALL_SAN_FLAGS STREQUAL "")
6142
set(PREV_FLAG ${CMAKE_REQUIRED_FLAGS})
6243
set(CMAKE_REQUIRED_FLAGS "${ALL_SAN_FLAGS}")
6344
check_cxx_source_runs("int main() { return 0; }" ALL_SANITIZERS_AVAILABLE)
6445
set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG})
65-
if(ALL_SANITIZERS_AVAILABLE)
66-
list(APPEND ALLOWED_BUILD_TYPES AllSan)
67-
endif()
6846
endif()
6947

70-
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)
71-
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)
72-
set(CMAKE_SHARED_LINKER_FLAGS_ALLSAN "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during most possible sanitizer builds" FORCE)
48+
if(CPR_DEBUG_SANITIZER_FLAG_THREAD AND THREAD_SANITIZER_AVAILABLE)
49+
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)
50+
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)
51+
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during thread sanitizer builds" FORCE)
52+
elseif(CPR_DEBUG_SANITIZER_FLAG_ADDR AND ADDRESS_SANITIZER_AVAILABLE)
53+
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)
54+
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)
55+
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during address sanitizer builds" FORCE)
56+
elseif(CPR_DEBUG_SANITIZER_FLAG_LEAK AND LEAK_SANITIZER_AVAILABLE)
57+
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)
58+
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)
59+
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during leak sanitizer builds" FORCE)
60+
elseif(CPR_DEBUG_SANITIZER_FLAG_UB AND UNDEFINED_BEHAVIOUR_SANITIZER_AVAILABLE)
61+
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)
62+
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)
63+
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during undefined behaviour sanitizer builds" FORCE)
64+
elseif(CPR_DEBUG_SANITIZER_FLAG_ALL AND ALL_SANITIZERS_AVAILABLE)
65+
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)
66+
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)
67+
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during most possible sanitizer builds" FORCE)
68+
endif()
7369
endif()

0 commit comments

Comments
 (0)