Logo AND Algorithmique Numérique Distribuée

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