Logo AND Algorithmique Numérique Distribuée

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