1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
include(CheckCCompilerFlag)
# C++17 Support
if(NOT ANDROID)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
# Position Independent Code
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Ccache support
if(ANDROID OR UNIX OR APPLE)
find_program(CCACHE_FOUND ccache)
set(CCACHE_SUPPORT OFF CACHE BOOL "Enable ccache support")
if((CCACHE_FOUND OR ANDROID) AND CCACHE_SUPPORT MATCHES ON)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
endif()
endif()
# Add security (hardening flags)
if(UNIX OR APPLE OR ANDROID)
# Older compilers are predefining _FORTIFY_SOURCE, so defining it causes a
# warning, which is then considered an error. Second issue is that for
# these compilers, _FORTIFY_SOURCE must be used while optimizing, else
# causes a warning, which also results in an error. And finally, CMake is
# not using optimization when testing for libraries, hence breaking the build.
check_c_compiler_flag("-Werror -D_FORTIFY_SOURCE=2" COMPATIBLE_FORTIFY_SOURCE)
if(
COMPATIBLE_FORTIFY_SOURCE
AND NOT CMAKE_BUILD_TYPE MATCHES "Debug"
)
set(SEC_COMP_FLAGS "-D_FORTIFY_SOURCE=2")
endif()
set(SEC_COMP_FLAGS "${SEC_COMP_FLAGS} -fstack-protector-all -fPIE")
# Make sure to add optimization flag. Some systems require this for _FORTIFY_SOURCE.
if(
NOT CMAKE_BUILD_TYPE MATCHES "MinSizeRel"
AND NOT CMAKE_BUILD_TYPE MATCHES "Release"
AND NOT CMAKE_BUILD_TYPE MATCHES "Debug"
)
set(SEC_COMP_FLAGS "${SEC_COMP_FLAGS} -O1")
endif()
if(
NOT ANDROID
AND NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang"
AND NOT APPLE
AND NOT CYGWIN
)
set(SEC_COMP_FLAGS "${SEC_COMP_FLAGS} -Wa,--noexecstack")
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SEC_COMP_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SEC_COMP_FLAGS}")
set(SEC_LINK_FLAGS "")
if(NOT APPLE AND NOT CYGWIN)
set(
SEC_LINK_FLAGS
"${SEC_LINK_FLAGS} -Wl,-z,nodump -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now"
)
endif()
if(NOT ANDROID AND NOT APPLE)
set(SEC_LINK_FLAGS "${SEC_LINK_FLAGS} -pie")
endif()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SEC_LINK_FLAGS}")
set(
CMAKE_SHARED_LINKER_FLAGS
"${CMAKE_SHARED_LINKER_FLAGS} ${SEC_LINK_FLAGS} "
)
endif()
# Warning, debug and linker flags
set(
FIX_WARNINGS
OFF
CACHE BOOL
"Enable strict compilation mode to turn compiler warnings to errors"
)
if(UNIX OR APPLE)
set(COMP_FLAGS "")
set(LINKER_FLAGS "")
# Verbose warnings and turns all to errors
set(COMP_FLAGS "${COMP_FLAGS} -Wall -Wextra")
if(FIX_WARNINGS)
set(COMP_FLAGS "${COMP_FLAGS} -Werror")
endif()
# Omit problematic warnings
if(
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU"
AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 6.9.9
)
set(COMP_FLAGS "${COMP_FLAGS} -Wno-format-truncation")
endif()
check_c_compiler_flag(
"-Werror=stringop-truncation"
COMPATIBLE_STRINGOP_TRUNCATION
)
if(${COMPATIBLE_STRINGOP_TRUNCATION})
set(COMP_FLAGS "${COMP_FLAGS} -Werror=stringop-truncation")
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
set(COMP_FLAGS "${COMP_FLAGS} -Wno-nonnull -Wno-deprecated-declarations")
endif()
check_c_compiler_flag("-Werror=unused-parameter" COMPATIBLE_UNUSED_PARAMETER)
if(${COMPATIBLE_UNUSED_PARAMETER})
set(COMP_FLAGS "${COMP_FLAGS} -Werror=unused-parameter")
endif()
check_c_compiler_flag(
"-Werror=unused-but-set-variable"
COMPATIBLE_UNUSED_BUT_SET_VARIABLE
)
if(${COMPATIBLE_UNUSED_BUT_SET_VARIABLE})
set(COMP_FLAGS "${COMP_FLAGS} -Werror=unused-but-set-variable")
endif()
# Minimal debug info with Clang
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(COMP_FLAGS "${COMP_FLAGS} -gline-tables-only")
else()
set(COMP_FLAGS "${COMP_FLAGS} -g")
endif()
# Note: The following flags are problematic on older systems with gcc 4.8
if(
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang"
OR
(
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU"
AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9.9
)
)
if(
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang"
OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang"
)
set(COMP_FLAGS "${COMP_FLAGS} -Wno-unused-command-line-argument")
endif()
find_program(LDGOLD_FOUND ld.gold)
set(LDGOLD_SUPPORT OFF CACHE BOOL "Enable ld.gold support")
# Optional ld.gold is 2x faster than normal ld
if(
LDGOLD_FOUND
AND LDGOLD_SUPPORT MATCHES ON
AND NOT APPLE
AND NOT CMAKE_SYSTEM_PROCESSOR MATCHES arm
)
set(LINKER_FLAGS "${LINKER_FLAGS} -fuse-ld=gold")
# Use Identical Code Folding
set(COMP_FLAGS "${COMP_FLAGS} -ffunction-sections")
set(LINKER_FLAGS "${LINKER_FLAGS} -Wl,--icf=safe")
# Compress the debug sections
# Note: Before valgrind 3.12.0, patch should be applied for valgrind (https://bugs.kde.org/show_bug.cgi?id=303877)
if(
NOT APPLE
AND NOT ANDROID
AND NOT CMAKE_SYSTEM_PROCESSOR MATCHES arm
AND NOT CMAKE_CXX_CLANG_TIDY
)
set(COMP_FLAGS "${COMP_FLAGS} -Wa,--compress-debug-sections")
set(LINKER_FLAGS "${LINKER_FLAGS} -Wl,--compress-debug-sections=zlib")
endif()
endif()
endif()
# Apply the flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMP_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMP_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS} -shared")
endif()
# Sanitizer support
set(CLANG_SANITIZERS OFF CACHE BOOL "Clang's sanitizer support")
if(
CLANG_SANITIZERS
AND
(
(UNIX AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
OR
(APPLE AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
)
)
set(
CMAKE_C_FLAGS
"${CMAKE_C_FLAGS} -fsanitize=address,undefined -fno-omit-frame-pointer"
)
set(
CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -fsanitize=address,undefined -fno-omit-frame-pointer"
)
set(
CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address,undefined -fno-omit-frame-pointer"
)
set(
CMAKE_SHARED_LINKER_FLAGS
"${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address,undefined -fno-omit-frame-pointer"
)
endif()
# Unity Build support
include(UnityBuild)
|