Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix build with -Werror on older g++.
[simgrid.git] / tools / cmake / Flags.cmake
1 ##
2 ## This file is in charge of setting our paranoid flags with regard to warnings and optimization.
3 ##
4 ##   It is only used for gcc and clang. MSVC builds don't load this file.
5 ##
6 ##   These flags do break some classical CMake tests, so you don't
7 ##   want to do so before the very end of the configuration.
8 ##
9 ##   Other compiler flags (C/C++ standard version) are tested and set
10 ##   by the beginning of the configuration, directly in ~/CMakeList.txt
11
12 set(warnCFLAGS "")
13 set(optCFLAGS "")
14 set(warnCXXFLAGS "")
15
16 if(enable_compile_warnings)
17   set(warnCFLAGS "-fno-common -Wall -Wunused -Wmissing-declarations -Wpointer-arith -Wchar-subscripts -Wcomment -Wformat -Wwrite-strings -Wno-unused-function -Wno-unused-parameter -Wno-strict-aliasing")
18   if(CMAKE_COMPILER_IS_GNUCC AND (NOT (CMAKE_C_COMPILER_VERSION VERSION_LESS "5.0")))
19     set(warnCFLAGS "${warnCFLAGS} -Wformat-signedness")
20   endif()
21   if(CMAKE_COMPILER_IS_GNUCC)
22     set(warnCFLAGS "${warnCFLAGS} -Wclobbered -Wno-error=clobbered  -Wno-unused-local-typedefs -Wno-error=attributes -Wno-error=maybe-uninitialized")
23   endif()
24   if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
25     # ignore remarks:
26     # 191: type qualifier is meaningless on cast type
27     # 1418: external function definition with no prior declaration
28     # 2196: routine is both "inline" and "noinline"
29     # 2651: attribute does not apply to any entity
30     # 3179: deprecated conversion of string literal to char* (should be const char*)
31     # set as warning:
32     # 597: entity-kind "entity" will not be called for implicit or explicit conversions
33     # 2330: argument of type "type" is incompatible with parameter of type "type" (dropping qualifiers)
34     # 11003: no IR in object file xxxx; was the source file compiled with xxxx
35     set(warnCFLAGS "${warnCFLAGS} -diag-disable=191,1418,2196,2651,3179 -diag-warning=597,2330,11003")
36   endif()
37
38   set(warnCXXFLAGS "${warnCFLAGS} -Wall -Wextra -Wunused -Wmissing-declarations -Wpointer-arith -Wchar-subscripts -Wcomment -Wformat -Wwrite-strings -Wno-unused-function -Wno-unused-parameter -Wno-strict-aliasing")
39   if(CMAKE_COMPILER_IS_GNUCXX AND (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0")))
40     set(warnCFLAGS "${warnCFLAGS} -Wformat-signedness")
41   endif()
42   if(CMAKE_COMPILER_IS_GNUCXX)
43     set(warnCXXFLAGS "${warnCXXFLAGS} -Wclobbered -Wno-error=clobbered  -Wno-unused-local-typedefs -Wno-error=attributes -Wno-error=maybe-uninitialized")
44   endif()
45   if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "8.0")
46     # workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81767
47     set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-error=unused-variable")
48   endif()
49   if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
50     # don't care about class that become struct, avoid issue of empty C structs
51     # size (coming from libunwind.h)
52     set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-mismatched-tags -Wno-extern-c-compat")
53   endif()
54
55   # the one specific to C but refused by C++
56   set(warnCFLAGS "${warnCFLAGS} -Wmissing-prototypes")
57
58   if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC|PGI")
59     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
60   elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Flang")
61     # flang >= 7 has a bug with common and debug flags. Ignore cmake-added -g in this case.
62     # https://github.com/flang-compiler/flang/issues/671
63     set(CMAKE_Fortran_FLAGS "-Wall")
64   elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
65     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -warn all")
66   endif()
67   set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
68 endif()
69
70 # NDEBUG gives a lot of "initialized but unused variables" errors. Don't die anyway.
71 if(enable_compile_warnings AND enable_debug)
72   set(warnCFLAGS "${warnCFLAGS} -Werror")
73   set(warnCXXFLAGS "${warnCXXFLAGS} -Werror")
74   if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC")
75     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Werror -Werror=format-security")
76   endif()
77 endif()
78
79 # Activate the warnings on #if FOOBAR when FOOBAR has no value
80 # It breaks on FreeBSD within Boost headers, so activate this only in Pure Hardcore debug mode.
81 if(enable_maintainer_mode)
82   set(warnCFLAGS "${warnCFLAGS} -Wundef")
83   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
84 endif()
85
86 # Se the optimisation flags
87 # NOTE, we should CMAKE_BUILD_TYPE for this
88 if(enable_compile_optimizations)
89   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
90 else()
91   set(optCFLAGS "-O0 ")
92 endif()
93 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC
94     AND (NOT enable_model-checking))
95   # This is redundant (already in -03):
96   set(optCFLAGS "${optCFLAGS} -finline-functions ")
97 endif()
98
99 # Do not leak the current directory into the binaries
100 if(CMAKE_COMPILER_IS_GNUCC)
101   execute_process(COMMAND realpath --relative-to=${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}
102     RESULT_VARIABLE RESULT OUTPUT_VARIABLE RELATIVE_SOURCE_DIR ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
103   if(RESULT EQUAL 0)
104     message(STATUS "Relative source directory is \"${RELATIVE_SOURCE_DIR}\".")
105   else()
106     message(WARNING "Failed to find relative source directory. Using \".\".")
107     set(RELATIVE_SOURCE_DIR ".")
108   endif()
109   if (CMAKE_C_COMPILER_VERSION VERSION_LESS "8.0")
110     set(optCFLAGS "${optCFLAGS} -fdebug-prefix-map=\"${CMAKE_SOURCE_DIR}=${RELATIVE_SOURCE_DIR}\"")
111   else()
112     set(optCFLAGS "${optCFLAGS} -ffile-prefix-map=\"${CMAKE_SOURCE_DIR}=${RELATIVE_SOURCE_DIR}\"")
113   endif()
114 endif()
115
116 # Configure LTO
117 if(enable_lto) # User wants LTO. Try if we can do that
118   set(enable_lto OFF)
119   if(enable_compile_optimizations
120       AND (NOT enable_model-checking))
121     if(CMAKE_VERSION VERSION_LESS "3.9")
122       if ( CMAKE_COMPILER_IS_GNUCC
123          AND (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.5")
124          AND (LINKER_VERSION VERSION_GREATER "2.22"))
125         set(enable_lto ON)
126       endif()
127     else()
128       include(CheckIPOSupported)
129       check_ipo_supported(RESULT ipo LANGUAGES C CXX)
130       if(ipo)
131         set(enable_lto ON)
132       endif()
133     endif()
134   endif()
135
136   if(enable_lto)
137     message(STATUS "LTO seems usable.")
138   else()
139     if(NOT enable_compile_optimizations)
140       message(STATUS "LTO disabled: Compile-time optimizations turned off.")
141     else()
142       if(enable_model-checking)
143         message(STATUS "LTO disabled when compiling with model-checking.")
144       else()
145         message(STATUS "LTO does not seem usable -- try updating your build chain.")
146       endif()
147     endif()
148   endif()
149 else()
150   message(STATUS "LTO disabled on the command line.")
151 endif()
152 if(enable_lto) # User wants LTO, and it seems usable. Go for it
153   set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
154   if(LTO_EXTRA_FLAG AND CMAKE_COMPILER_IS_GNUCC)
155     list(APPEND CMAKE_C_COMPILE_OPTIONS_IPO "-flto=${LTO_EXTRA_FLAG}")
156     list(APPEND CMAKE_CXX_COMPILE_OPTIONS_IPO "-flto=${LTO_EXTRA_FLAG}")
157   endif()
158
159   # Activate fat-lto-objects in case LD and gfortran differ too much.
160   # Only test with GNU as it's the only case I know (clang+gfortran+lld)
161   execute_process(COMMAND ${CMAKE_LINKER} -v OUTPUT_VARIABLE LINKER_ID ERROR_VARIABLE LINKER_ID)
162   string(REGEX MATCH "GNU" LINKER_ID "${LINKER_ID}")
163   if(${CMAKE_Fortran_COMPILER_ID} MATCHES "GNU"
164      AND NOT "${LINKER_ID}" MATCHES "GNU")
165        list(APPEND CMAKE_Fortran_COMPILE_OPTIONS_IPO "-ffat-lto-objects")
166   endif()
167
168   # "Since version 4.9 gcc produces slim object files that only contain
169   # the intermediate representation. In order to handle archives of
170   # these objects you have to use the gcc wrappers:
171   # gcc-ar, gcc-nm and gcc-ranlib."
172   if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
173       AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
174     set (CMAKE_AR gcc-ar)
175     set (CMAKE_RANLIB gcc-ranlib)
176   endif()
177 endif()
178
179 if(enable_model-checking AND enable_compile_optimizations)
180   # Forget it, do not optimize the code (because it confuses the MC):
181   set(optCFLAGS "-O0")
182   # But you can still optimize this:
183   set(src_list ${simgrid_sources})
184   # except...
185   list(REMOVE_ITEM src_list ${SIMIX_SRC} ${S4U_SRC})
186   # but...
187   list(APPEND src_list
188     src/kernel/actor/Simcall.cpp)
189   foreach(src ${src_list})
190       set (mcCFLAGS "-O3 -funroll-loops -fno-strict-aliasing")
191       if(CMAKE_COMPILER_IS_GNUCC)
192         set (mcCFLAGS "${mcCFLAGS} -finline-functions")
193       endif()
194       set_source_files_properties(${src} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
195   endforeach()
196 endif()
197
198 if (CMAKE_C_COMPILER_ID MATCHES "Intel")
199   # honor parentheses when determining the order of expression evaluation.
200   set(optCFLAGS "${optCFLAGS} -fprotect-parens ")
201 endif()
202
203 if(NOT enable_debug)
204   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
205   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
206 endif()
207
208 set(CMAKE_C_FLAGS   "${warnCFLAGS} ${CMAKE_C_FLAGS}   ${optCFLAGS}")
209 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
210
211 # Try to make Mac a bit more compliant to open source standards
212 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
213   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
214 endif()
215
216 # Avoid a failure seen with gcc 7.2.0 and ns3 3.27
217 if(enable_ns3)
218   set_source_files_properties(src/surf/network_ns3.cpp PROPERTIES COMPILE_FLAGS " -Wno-unused-local-typedef")
219 endif()
220
221 set(TESH_OPTION "")
222 if(enable_coverage)
223   find_program(GCOV_PATH NAMES ENV{GCOV} gcov)
224   if(GCOV_PATH)
225     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE FILEPATH "Coverage testing tool (gcov)" FORCE)
226     set(COVERAGE_BUILD_FLAGS "-fprofile-arcs -ftest-coverage -fprofile-abs-path")
227     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE ${COVERAGE_BUILD_FLAGS}")
228     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${COVERAGE_BUILD_FLAGS}")
229     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${COVERAGE_BUILD_FLAGS}")
230     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCOVERAGE ${COVERAGE_BUILD_FLAGS}")
231     add_definitions(${COVERAGE_BUILD_FLAGS})
232   endif()
233 endif()
234
235 if(enable_address_sanitizer)
236     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
237     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
238     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
239     set(TESH_OPTION --enable-sanitizers)
240     try_compile(HAVE_SANITIZER_ADDRESS ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp)
241     try_compile(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp
242       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
243 else()
244     set(HAVE_SANITIZER_ADDRESS FALSE CACHE INTERNAL "")
245     set(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT FALSE CACHE INTERNAL "")
246 endif()
247
248 if(enable_thread_sanitizer)
249     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
250     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
251     if(CMAKE_COMPILER_IS_GNUCXX AND (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0")))
252         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=tsan")
253     endif()
254     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
255     try_compile(HAVE_SANITIZER_THREAD ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_tsan.cpp)
256     try_compile(HAVE_SANITIZER_THREAD_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_tsan.cpp
257       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
258 else()
259     set(HAVE_SANITIZER_THREAD FALSE CACHE INTERNAL "")
260     set(HAVE_SANITIZER_THREAD_FIBER_SUPPORT FALSE CACHE INTERNAL "")
261 endif()
262
263 if(enable_undefined_sanitizer)
264     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
265     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
266     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
267 endif()
268
269 if(NOT $ENV{CFLAGS} STREQUAL "")
270   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
271   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
272 endif()
273
274 if(NOT $ENV{CXXFLAGS} STREQUAL "")
275   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
276   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
277 endif()
278
279 if(NOT $ENV{LDFLAGS} STREQUAL "")
280   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
281   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
282 endif()
283
284 if(MINGW)
285   # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
286   # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
287   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -static-libgcc")
288   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
289   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS   "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
290   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
291
292   # JNI searches for stdcalls
293   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -Wl,--add-stdcall-alias")
294   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
295   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
296   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
297
298   # Specify the data model that we are using (yeah it may help Java)
299   if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
300     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m32")
301     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
302   else()
303     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m64")
304     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
305   endif()
306 endif()