Skip to content

Commit 66a8dcf

Browse files
committed
Improve readability of sanitizer and hardening build options
This has the added bonus of ensuring that appropriate compiler options are used for GCC on MacOS.
1 parent 0ac6212 commit 66a8dcf

File tree

1 file changed

+26
-37
lines changed

1 file changed

+26
-37
lines changed

CMakeLists.txt

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,68 +28,57 @@ endif()
2828
# Configure compiler
2929
set(CMAKE_C_STANDARD 11)
3030
set(CMAKE_C_EXTENSIONS OFF)
31-
option(TR31_ENABLE_SANITIZERS "Enable debugging sanitizers" OFF)
32-
option(TR31_ENABLE_HARDENING "Enable runtime security hardening" OFF)
3331
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
3432
add_compile_options(-Wall)
3533
add_compile_options($<$<CONFIG:Debug>:-ggdb3>)
3634
add_compile_options($<$<CONFIG:Debug>:-fno-omit-frame-pointer>)
3735
add_compile_options($<$<CONFIG:RelWithDebInfo>:-ggdb3>)
38-
39-
if(TR31_ENABLE_SANITIZERS)
40-
add_compile_options(-fsanitize=address)
41-
add_compile_options(-fsanitize=undefined)
42-
add_compile_options(-fsanitize=leak)
43-
add_link_options(-fsanitize=address)
44-
add_link_options(-fsanitize=undefined)
45-
add_link_options(-fsanitize=leak)
46-
endif()
47-
48-
if(TR31_ENABLE_HARDENING)
49-
add_compile_options(-fstack-protector-strong)
50-
add_compile_definitions(_FORTIFY_SOURCE=2)
51-
endif()
5236
endif()
5337
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
5438
add_compile_options(-Wall)
5539
add_compile_options($<$<CONFIG:Debug>:-g3>)
5640
add_compile_options($<$<CONFIG:Debug>:-fno-omit-frame-pointer>)
5741
add_compile_options($<$<CONFIG:RelWithDebInfo>:-g3>)
58-
59-
if(TR31_ENABLE_SANITIZERS)
60-
add_compile_options(-fsanitize=address)
61-
add_compile_options(-fsanitize=undefined)
62-
add_link_options(-fsanitize=address)
63-
add_link_options(-fsanitize=undefined)
64-
if(NOT APPLE)
65-
# LeakSanitizer not available on MacOS
66-
add_compile_options(-fsanitize=leak)
67-
add_link_options(-fsanitize=leak)
68-
endif()
69-
endif()
70-
71-
if(TR31_ENABLE_HARDENING)
72-
add_compile_options(-fstack-protector-strong)
73-
# FORTIFY_SOURCE less reliable using Clang
74-
endif()
7542
endif()
7643
if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
7744
add_compile_options(-Wall)
7845
add_compile_options($<$<CONFIG:Debug>:-g3>)
7946
add_compile_options($<$<CONFIG:Debug>:-fno-omit-frame-pointer>)
8047
add_compile_options($<$<CONFIG:RelWithDebInfo>:-g3>)
48+
endif()
8149

82-
if(TR31_ENABLE_SANITIZERS)
50+
# Configure debugging sanitizers
51+
option(TR31_ENABLE_SANITIZERS "Enable debugging sanitizers" OFF)
52+
if(TR31_ENABLE_SANITIZERS)
53+
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR
54+
CMAKE_C_COMPILER_ID STREQUAL "Clang" OR
55+
CMAKE_C_COMPILER_ID STREQUAL "AppleClang"
56+
)
8357
add_compile_options(-fsanitize=address)
8458
add_compile_options(-fsanitize=undefined)
8559
add_link_options(-fsanitize=address)
8660
add_link_options(-fsanitize=undefined)
87-
# LeakSanitizer not available on MacOS
61+
if(NOT APPLE)
62+
# LeakSanitizer not available on MacOS
63+
add_compile_options(-fsanitize=leak)
64+
add_link_options(-fsanitize=leak)
65+
endif()
8866
endif()
67+
endif()
8968

90-
if(TR31_ENABLE_HARDENING)
69+
# Configure runtime security hardening
70+
option(TR31_ENABLE_HARDENING "Enable runtime security hardening" OFF)
71+
if(TR31_ENABLE_HARDENING)
72+
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR
73+
CMAKE_C_COMPILER_ID STREQUAL "Clang" OR
74+
CMAKE_C_COMPILER_ID STREQUAL "AppleClang"
75+
)
9176
add_compile_options(-fstack-protector-strong)
92-
# FORTIFY_SOURCE not recommended on MacOS
77+
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
78+
# FORTIFY_SOURCE less reliable using Clang
79+
# FORTIFY_SOURCE not recommended on MacOS
80+
add_compile_definitions(_FORTIFY_SOURCE=2)
81+
endif()
9382
endif()
9483
endif()
9584

0 commit comments

Comments
 (0)