-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompileOptions.cmake
261 lines (236 loc) · 8.16 KB
/
CompileOptions.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#
# Copyright (C) 2021 Swift Navigation Inc.
# Contact: Swift Navigation <[email protected]>
#
# This source is subject to the license found in the file 'LICENSE' which must
# be be distributed together with this source. All other rights reserved.
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
#
#
# Set a common set of compiler options and warning flags
#
# Call swift_set_compile_options() for any target to set
# the Swift default set of compiler options. This includes
# - exceptions disabled
# - rtti disabled
# - strict aliasing disabled
# - Warnings as errors (-Werror)
# - Extensive set of enabled warnings
#
# Exceptions and/or RTTI can be selectively enabled for a
# target be passing EXCEPTIONS and/or RTTI as a parameter, eg
#
# swift_set_compile_options(sample-target EXCEPTIONS RTTI)
#
# will enable exceptions and rtti for sample-target only
#
# Warning flags can be removed from the default set by passing
# REMOVE followed by a list of warning flags, eg
#
# swift_set_compile_options(sample-target REMOVE -Wconversion)
#
# will prevent -Wconversion from being passed to the compiler
# for sample-target only
#
# Similarly extra options can be given by passing ADD followed
# by a list of warning flags (or other compiler options), eg
#
# swift_set_compile_options(sample-target ADD -Wformat=2)
#
# will pass -Wformat=2 to the compiler for sample-target only
#
# By default -Werror is set, but this can be prevented by passing
# WARNING as a parameter, eg
#
# swift_set_compile_options(sample-target WARNING)
#
# will disable warnings-as-errors for sample-target only
#
# All flags will be checked for suitability with the in-use
# compilers before being selected. This is important since
# Swift code tends to be compiled with a wide variety of
# compilers which may not support the same set of flags and
# options. Therefore, it should be preferred to use this
# function to set compiler flags and options rather than
# target_compile_options()
#
# NOTE: user's can call on EXTRA_FLAGS to augment the default list of flags
# before flags are removed with REMOVE and subsequently added with ADD.
#
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
function(swift_set_compile_options)
set(argOption "WARNING" "NO_EXCEPTIONS" "EXCEPTIONS" "NO_RTTI" "RTTI")
set(argSingle "")
set(argMulti "ADD" "REMOVE" "EXTRA_FLAGS")
unset(x_EXCEPTIONS)
unset(x_NO_EXCEPTIONS)
unset(x_RTTI)
unset(x_NO_RTTI)
unset(x_WARNING)
unset(x_ADD)
unset(x_REMOVE)
unset(x_EXTRA_FLAGS)
cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN})
set(targets ${x_UNPARSED_ARGUMENTS})
if (x_RTTI AND x_NO_RTTI)
message(FATAL_ERROR "RTTI and NO_RTTI can't be used together")
endif()
if (NOT x_RTTI AND NOT x_NO_RTTI AND DEFINED ${PROJECT_NAME}_RTTI_DEFAULT)
set(x_RTTI ${${PROJECT_NAME}_RTTI_DEFAULT})
endif()
if (x_EXCEPTIONS AND x_NO_EXCEPTIONS)
message(FATAL_ERROR "EXCEPTIONS and NO_EXCEPTIONS can't be used together")
endif()
if (NOT x_EXCEPTIONS AND NOT x_NO_EXCEPTIONS AND DEFINED ${PROJECT_NAME}_EXCEPTIONS_DEFAULT)
set(x_EXCEPTIONS ${${PROJECT_NAME}_EXCEPTIONS_DEFAULT})
endif()
if (MSVC)
return()
endif()
foreach(flag ${x_ADD} ${x_REMOVE})
if(${flag} STREQUAL "-fexceptions")
message(FATAL_ERROR "Do not specify -fexceptions directly, use EXCEPTIONS instead")
endif()
if(${flag} STREQUAL "-fno-exceptions")
message(FATAL_ERROR "Do not specify -fno-exceptions directly, use NO_EXCEPTIONS instead")
endif()
if(${flag} STREQUAL "-frtti")
message(FATAL_ERROR "Do not specify -frtti directly, use RTTI instead")
endif()
if(${flag} STREQUAL "-fno-rtti")
message(FATAL_ERROR "Do not specify -fno-rtti directly, use NO_RTTI instead")
endif()
if(${flag} STREQUAL "-Werror")
message(FATAL_ERROR "Do not specify -Werror directly, use WARNING to disable -Werror")
endif()
if(${flag} STREQUAL "-Wno-error")
message(FATAL_ERROR "Do not specify -Wno-error directly, use WARNING to disable -Werror")
endif()
endforeach()
if (DEFINED SWIFT_COMPILER_WARNING_ARE_ERROR)
if (SWIFT_COMPILER_WARNING_ARE_ERROR)
set(all_flags -Werror -Wno-error=deprecated-declarations)
else()
set(all_flags -Wno-error)
endif()
else()
if (NOT x_WARNING)
set(all_flags -Werror -Wno-error=deprecated-declarations)
endif()
endif()
list(APPEND all_flags
-Wall
-Wcast-align
-Wcast-qual
-Wchar-subscripts
-Wcomment
-Wconversion
-Wdisabled-optimization
-Wextra
-Wfloat-equal
-Wformat
-Wformat-security
-Wformat-y2k
-Wimplicit-fallthrough
-Wimport
-Winit-self
-Winvalid-pch
-Wmissing-braces
-Wmissing-field-initializers
-Wmissing-include-dirs
-Wparentheses
-Wpointer-arith
-Wredundant-decls
-Wreturn-type
-Wsequence-point
-Wshadow
-Wsign-compare
-Wstack-protector
-Wswitch
-Wswitch-default
-Wswitch-enum
-Wtrigraphs
-Wuninitialized
-Wunknown-pragmas
-Wunreachable-code
-Wunused
-Wunused-function
-Wunused-label
-Wunused-parameter
-Wunused-value
-Wunused-variable
-Wvolatile-register-var
-Wwrite-strings
${x_EXTRA_FLAGS}
)
if(x_REMOVE)
foreach(flag ${x_REMOVE})
list(FIND all_flags ${flag} found)
if(found EQUAL -1)
message(FATAL_ERROR "Compiler flag '${flag}' specified for removal is not part of the set of common compiler flags")
endif()
endforeach()
list(REMOVE_ITEM all_flags ${x_REMOVE})
endif()
list(APPEND all_flags ${x_ADD})
unset(final_flags)
get_property(enabled_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
list(FIND enabled_languages "C" c_enabled)
list(FIND enabled_languages "CXX" cxx_enabled)
foreach(flag ${all_flags})
string(TOUPPER ${flag} sanitised_flag)
string(REPLACE "+" "X" sanitised_flag ${sanitised_flag})
string(REGEX REPLACE "[^A-Za-z_0-9]" "_" sanitised_flag ${sanitised_flag})
set(c_supported HAVE_C_FLAG_${sanitised_flag})
string(REGEX REPLACE "_+" "_" c_supported ${c_supported})
set(cxx_supported HAVE_CXX_FLAG_${sanitised_flag})
string(REGEX REPLACE "_+" "_" cxx_supported ${cxx_supported})
if(${c_enabled} GREATER -1)
check_c_compiler_flag("-Werror ${flag}" ${c_supported})
if(${${c_supported}})
list(APPEND final_flags $<$<COMPILE_LANGUAGE:C>:${flag}>)
endif()
endif()
if (${cxx_enabled} GREATER -1)
check_cxx_compiler_flag("-Werror ${flag}" ${cxx_supported})
if(${${cxx_supported}})
list(APPEND final_flags $<$<COMPILE_LANGUAGE:CXX>:${flag}>)
endif()
endif()
endforeach()
foreach(target ${targets})
if(cxx_enabled)
if(x_EXCEPTIONS)
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "IAR")
#target_compile_options(${target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:--exceptions>)
else()
target_compile_options(${target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fexceptions>)
endif()
else()
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "IAR")
target_compile_options(${target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:--no_exceptions>)
else()
target_compile_options(${target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>)
endif()
endif()
if(x_RTTI)
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "IAR")
#target_compile_options(${target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:--rtti>)
else()
target_compile_options(${target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-frtti>)
endif()
else()
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "IAR")
target_compile_options(${target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:--no_rtti>)
else()
target_compile_options(${target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
endif()
endif()
endif()
target_compile_options(${target} PRIVATE ${final_flags})
endforeach()
endfunction()