Skip to content

Commit 1cb247a

Browse files
authored
Replaced build types with flags (libcpr#853)
1 parent ea00eba commit 1cb247a

File tree

3 files changed

+31
-45
lines changed

3 files changed

+31
-45
lines changed

.github/workflows/ci.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,14 @@ jobs:
292292
- name: "[Release g++] Build & Test"
293293
env:
294294
CPR_BUILD_TESTS: ON
295+
CPR_DEBUG_SANITIZER_FLAG_ALL: ON
295296
uses: ashutoshvarma/action-cmake-build@master
296297
with:
297298
build-dir: ${{github.workspace}}/build
298299
source-dir: ${{github.workspace}}
299300
cc: gcc
300301
cxx: g++
301-
build-type: AllSan
302+
build-type: Debug
302303
run-test: true
303304
ctest-options: -V
304305

@@ -319,13 +320,14 @@ jobs:
319320
- name: "[Release g++] Build & Test"
320321
env:
321322
CPR_BUILD_TESTS: ON
323+
CPR_DEBUG_SANITIZER_FLAG_THREAD: ON
322324
uses: ashutoshvarma/action-cmake-build@master
323325
with:
324326
build-dir: ${{github.workspace}}/build
325327
source-dir: ${{github.workspace}}
326328
cc: gcc
327329
cxx: g++
328-
build-type: ThreadSan
330+
build-type: Debug
329331
run-test: true
330332
ctest-options: -V
331333

CMakeLists.txt

+5-17
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ cpr_option(CPR_BUILD_TESTS_SSL "Set to ON to build cpr ssl tests" ${CPR_BUILD_TE
6868
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)
6969
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)
7070
cpr_option(CPR_USE_BOOST_FILESYSTEM "Set to ON to use the Boost.Filesystem library on OSX." OFF)
71+
cpr_option(CPR_DEBUG_SANITIZER_FLAG_THREAD "Enables the ThreadSanitizer for debug builds." OFF)
72+
cpr_option(CPR_DEBUG_SANITIZER_FLAG_ADDR "Enables the AddressSanitizer for debug builds." OFF)
73+
cpr_option(CPR_DEBUG_SANITIZER_FLAG_LEAK "Enables the LeakSanitizer for debug builds." OFF)
74+
cpr_option(CPR_DEBUG_SANITIZER_FLAG_UB "Enables the UndefinedBehaviorSanitizer for debug builds." OFF)
75+
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)
7176
message(STATUS "=======================================================")
7277

7378
if (CPR_FORCE_USE_SYSTEM_CURL)
@@ -169,23 +174,6 @@ find_package(OpenSSL REQUIRED)
169174
add_compile_definitions(OPENSSL_BACKEND_USED)
170175
endif()
171176

172-
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
173-
if (NOT isMultiConfig)
174-
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${ALLOWED_BUILD_TYPES}")
175-
if (NOT CMAKE_BUILD_TYPE)
176-
set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
177-
elseif(NOT CMAKE_BUILD_TYPE IN_LIST ALLOWED_BUILD_TYPES)
178-
message(FATAL_ERROR "Invalid build type: ${CMAKE_BUILD_TYPE}")
179-
endif()
180-
else ()
181-
unset(CMAKE_BUILD_TYPE)
182-
foreach(TYPE ${ALLOWED_BUILD_TYPES})
183-
if (NOT ${TYPE} IN_LIST CMAKE_CONFIGURATION_TYPES)
184-
list(APPEND CMAKE_CONFIGURATION_TYPES ${TYPE})
185-
endif()
186-
endforeach()
187-
endif()
188-
189177
# Curl configuration
190178
if(CPR_USE_SYSTEM_CURL)
191179
if(CPR_ENABLE_SSL)

cmake/sanitizer.cmake

+22-26
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)