forked from Governikus/AusweisApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompilerFlags.cmake
200 lines (167 loc) · 6.73 KB
/
CompilerFlags.cmake
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
add_definitions(-DUNICODE)
add_definitions(-DQT_MESSAGELOGCONTEXT)
add_definitions(-DQT_NO_CAST_FROM_BYTEARRAY)
add_definitions(-DQT_NO_CAST_TO_ASCII)
add_definitions(-DQT_NO_FOREACH)
add_definitions(-DQT_NO_KEYWORDS)
add_definitions(-DQT_NO_EXCEPTIONS)
if(NOT MSVC AND NOT CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC" AND QT5)
add_definitions(-DQT_STRICT_ITERATORS)
endif()
add_compile_definitions($<$<CONFIG:Debug>:QT_RESTRICTED_CAST_FROM_ASCII>)
add_compile_definitions($<$<NOT:$<CONFIG:Debug>>:QT_NO_CAST_FROM_ASCII>)
if(QT_VENDOR STREQUAL "Governikus")
add_definitions(-DGOVERNIKUS_QT)
add_definitions(-DQT_DISABLE_DEPRECATED_BEFORE=0x060301)
endif()
get_property(cxx_features GLOBAL PROPERTY CMAKE_CXX_KNOWN_FEATURES)
if("cxx_std_20" IN_LIST cxx_features)
if(MINGW AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "8")
set(CMAKE_CXX_STANDARD 17)
else()
set(CMAKE_CXX_STANDARD 20)
endif()
elseif("cxx_std_17" IN_LIST cxx_features)
set(CMAKE_CXX_STANDARD 17)
else()
message(FATAL_ERROR "At least C++17 is required")
endif()
message(STATUS "CMAKE_CXX_STANDARD: ${CMAKE_CXX_STANDARD}")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(NOT DEFINED CMAKE_COMPILE_WARNING_AS_ERROR AND VENDOR_GOVERNIKUS)
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
endif()
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W3")
if(CMAKE_COMPILE_WARNING_AS_ERROR AND CMAKE_VERSION VERSION_LESS "3.24")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -WX")
endif()
ADD_FLAG(/Qspectre)
ADD_FLAG(/GS)
ADD_FLAG(/DYNAMICBASE)
ADD_FLAG(/NXCOMPAT)
ADD_FLAG(/HIGHENTROPYVA)
ADD_FLAG(/guard:cf)
ADD_FLAG(/Qcf-protection)
if (QT6)
ADD_FLAG(/Zc:__cplusplus)
endif()
if(CMAKE_CXX_COMPILER_LAUNCHER MATCHES "ccache")
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
endif()
else()
string(REPLACE "-fexceptions" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REPLACE "-frtti" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wcast-qual -Wshadow")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -g")
if(ANDROID AND ANDROID_NDK_REVISION VERSION_LESS "22")
if (CMAKE_ANDROID_ARCH_ABI STREQUAL "arm64-v8a")
set(PREFER_LD bfd CACHE STRING "")
else()
set(PREFER_LD gold CACHE STRING "")
endif()
else()
set(PREFER_LD lld CACHE STRING "")
set(FALLBACK_LD -fuse-ld=gold)
endif()
ADD_FLAG(-fuse-ld=${PREFER_LD} ${FALLBACK_LD} VAR CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS USE_SAME_FOR_LINKER USE_LINKER_ONLY)
ADD_FLAG(-flto VAR CMAKE_EXE_LINKER_FLAGS_RELEASE CMAKE_SHARED_LINKER_FLAGS_RELEASE USE_SAME_FOR_LINKER)
if(WIN32)
ADD_FLAG(-Wl,--dynamicbase VAR CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS USE_SAME_FOR_LINKER)
ADD_FLAG(-Wl,--nxcompat VAR CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS USE_SAME_FOR_LINKER)
ADD_FLAG(-Wl,--high-entropy-va VAR CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS USE_SAME_FOR_LINKER)
else()
ADD_FLAG(-fstack-protector-strong -fstack-protector)
ADD_FLAG(-fcf-protection)
ADD_FLAG(-fstack-clash-protection)
endif()
if(NOT CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
ADD_FLAG(-fno-rtti VAR CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS_MINSIZEREL)
endif()
ADD_FLAG(-fno-exceptions)
ADD_FLAG(-Wold-style-cast)
ADD_FLAG(-Wcast-align=strict -Wcast-align)
ADD_FLAG(-Wmost)
ADD_FLAG(-Wpedantic)
ADD_FLAG(-Wvla)
ADD_FLAG(-Wloop-analysis)
ADD_FLAG(-Wlogical-op)
ADD_FLAG(-Wmisleading-indentation)
ADD_FLAG(-Wduplicated-cond)
ADD_FLAG(-Wweak-vtables)
ADD_FLAG(-Wcovered-switch-default)
ADD_FLAG(-Wduplicated-branches)
ADD_FLAG(-Wdocumentation)
ADD_FLAG(-Wsuggest-override)
ADD_FLAG(-Winconsistent-missing-override)
ADD_FLAG(-Winconsistent-missing-destructor-override)
ADD_FLAG(-Wnon-virtual-dtor)
ADD_FLAG(-Winitializer-overrides)
ADD_FLAG(-Wunreachable-code-aggressive)
ADD_FLAG(-Wnewline-eof)
ADD_FLAG(-Wdate-time)
ADD_FLAG(-Wunused)
ADD_FLAG(-Wunused-template)
ADD_FLAG(-Wextra-semi)
ADD_FLAG(-Wempty-init-stmt)
ADD_FLAG(-Wno-gnu-zero-variadic-macro-arguments) # Qt (qDebug) is not compatible
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.14")
ADD_FLAG(-Wextra-semi-stmt)
ADD_FLAG(-Wuseless-cast)
endif()
if(QT6)
ADD_FLAG(-Wno-shorten-64-to-32)
else()
ADD_FLAG(-Wconversion)
endif()
if(ANDROID OR (INTEGRATED_SDK AND (NOT BUILD_SHARED_LIBS OR NOT BUILD_TESTING)))
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
endif()
if(ANDROID)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections")
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -finline-limit=64")
endif()
endif()
if(CMAKE_COMPILE_WARNING_AS_ERROR AND CMAKE_VERSION VERSION_LESS "3.24")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
endif()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
if(SANITIZER)
if(NOT SANITIZER_SKIP_ASAN)
set(SANITIZER_FLAGS "-fsanitize=address")
set(SANITIZER_LINK_FLAGS "-fsanitize=address")
endif()
set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(SANITIZER_LINK_FLAGS "${SANITIZER_LINK_FLAGS} -fsanitize=undefined")
ADD_FLAG("${SANITIZER_FLAGS} -fsanitize=unsigned-integer-overflow -fsanitize-address-use-after-scope" "${SANITIZER_FLAGS}" LINK "${SANITIZER_LINK_FLAGS}" LINK_GLOBAL)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL Intel)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd1875,1682,2259,654,177,1599")
endif()
if(CMAKE_CXX_COMPILER MATCHES "clazy")
set(clazy_level level0,level1,level2)
set(clazy_extra_warnings isempty-vs-count,signal-with-return-value,tr-non-literal,detaching-member)
set(clazy_disabled_warnings no-use-static-qregularexpression,no-copyable-polymorphic,no-ctor-missing-parent-argument,no-fully-qualified-moc-types,no-function-args-by-value)
set(clazy_all "${clazy_level},${clazy_extra_warnings},${clazy_disabled_warnings}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -plugin-arg-clazy -Xclang ${clazy_all}")
endif()
# QTBUG-82978
if(CMAKE_CXX_FLAGS MATCHES "-Wextra-semi-stmt")
list(APPEND INCOMPATIBLE_QT_COMPILER_FLAGS "-Wno-extra-semi-stmt")
endif()
if(CMAKE_CXX_FLAGS MATCHES "-Wuseless-cast")
list(APPEND INCOMPATIBLE_QT_COMPILER_FLAGS "-Wno-useless-cast")
endif()
endif()
# http://gcc.gnu.org/onlinedocs/gcc/Gcov.html
# http://llvm.org/docs/CommandGuide/llvm-cov.html
option(COVERAGE "Enable code coverage")
if(COVERAGE)
message(STATUS "Enable code coverage instrumentation")
ADD_FLAG(--coverage VAR CMAKE_CXX_FLAGS_DEBUG USE_SAME_FOR_LINKER)
endif()