-
Notifications
You must be signed in to change notification settings - Fork 47
/
compiler.cmake
172 lines (152 loc) · 5.31 KB
/
compiler.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
# Copyright (C) 2008-2014 LAAS-CNRS, JRL AIST-CNRS.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
include(CheckCXXCompilerFlag)
macro(_SETUP_PROJECT_WARNINGS)
# -Wmissing-declarations is disabled for now as older GCC version does not
# support it but CMake doest not check for the flag acceptance correctly.
set(
GNU_FLAGS
-pedantic
-Wno-long-long
-Wall
-Wextra
-Wcast-align
-Wcast-qual
-Wformat
-Wwrite-strings
-Wconversion
)
if(NOT DEFINED CXX_DISABLE_WERROR)
list(APPEND GNU_FLAGS -Werror)
endif(NOT DEFINED CXX_DISABLE_WERROR)
# For win32 systems, it is impossible to use Wall, especially with boost,
# which is way too verbose The default levels (W3/W4) are enough The next
# macro remove warnings on deprecations due to stl.
set(
MSVC_FLAGS
-D_SCL_SECURE_NO_WARNINGS
-D_CRT_SECURE_NO_WARNINGS
-D_CRT_SECURE_NO_DEPRECATE
# -- The following warnings are removed to highlight the output C4101 The
# local variable is never used removed since happens frequently in
# headers.
/wd4101
# C4250 'class1' : inherits 'class2::member' via dominance
/wd4250
# C4251 class 'type' needs to have dll-interface to be used by clients of
# class 'type2' ~ in practice, raised by the classes that have non-dll
# attribute (such as std::vector)
/wd4251
# C4275 non - DLL-interface used as base for DLL-interface
/wd4275
# C4355 "this" used in base member initializer list
/wd4355
)
cxx_flags_by_compiler_frontend(
GNU
${GNU_FLAGS}
MSVC
${MSVC_FLAGS}
OUTPUT
WARNING_CXX_FLAGS_LIST
FILTER
)
string(REPLACE ";" " " WARNING_CXX_FLAGS "${WARNING_CXX_FLAGS_LIST}")
set(CMAKE_CXX_FLAGS "${WARNING_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
list(APPEND LOGGING_WATCHED_VARIABLES WARNING_CXX_FLAGS)
endmacro(_SETUP_PROJECT_WARNINGS)
#[=======================================================================[.rst:
.. command:: CXX_FLAGS_BY_COMPILER_FRONTEND(<GCC [<flags1>...]>
<MSVC [<flags1>...]>
OUTPUT flags
<FILTER>)
Detect the compiler frontend (the command line interface) and output the
corresponding ``CXX_FLAGS``.
The following arguments allow to specify ``CXX_FLAGS`` for a compiler
frontend:
:param GNU: List of flags for GNU compiler frontend (Gcc, G++)
:param MSVC: List of flags for MSVC compiler frontend (MSVC, ClangCl)
Detected compiler frontend flags are then outputed in the ``OUTPUT``
parameter.
Optional ``FILTER`` parameter filter outputed flags with check_cxx_compiler_flag.
.. warning:: When ``FILTER`` option is activated, definition should by passed with
-D prefix to be valid compiler parameter.
Example
^^^^^^^
.. code-block:: cmake
CXX_FLAGS_BY_COMPILER_FRONTEND(
GNU -Wno-conversion -Wno-comment -Wno-self-assign-overloaded
MSVC "/bigobj"
OUTPUT COMPLIE_OPTIONS
FILTER)
#]=======================================================================]
function(CXX_FLAGS_BY_COMPILER_FRONTEND)
set(options FILTER)
set(oneValueArgs OUTPUT)
set(multiValueArgs GNU MSVC)
cmake_parse_arguments(
ARGS
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
# Before CMake 3.14, the CMAKE_CXX_COMPILER_FRONTEND_VARIANT doesn't exists.
# Before CMake 3.26, when CMAKE_CXX_COMPILER_ID is set to GNU, MSVC or
# AppleClang, CMAKE_CXX_COMPILER_FRONTEND_VARIANT doesn't exists either
if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT)
if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU")
set(FLAGS ${ARGS_GNU})
elseif(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
set(FLAGS ${ARGS_MSVC})
else()
message(
WARNING
"Unknown compiler frontend for '${CMAKE_CXX_COMPILER_ID}' "
"with frontend '${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}'\n"
"No flags outputed"
)
endif()
else()
if(
CMAKE_CXX_COMPILER_ID MATCHES "Clang"
AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC"
)
set(FLAGS ${ARGS_MSVC})
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(FLAGS ${ARGS_MSVC})
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
set(FLAGS ${ARGS_GNU})
else()
message(
WARNING
"Unknown compiler frontend for '${CMAKE_CXX_COMPILER_ID}' "
"with simulated ID '${CMAKE_CXX_SIMULATED_ID}'\n"
"No flags outputed"
)
endif()
endif()
if(ARGS_FILTER)
foreach(FLAG ${FLAGS})
check_cxx_compiler_flag(${FLAG} res_${FLAG})
if(${res_${FLAG}})
list(APPEND FILTERED_FLAGS ${FLAG})
endif()
endforeach()
else()
set(FILTERED_FLAGS ${FLAGS})
endif()
set(${ARGS_OUTPUT} ${FILTERED_FLAGS} PARENT_SCOPE)
endfunction()