Logo AND Algorithmique Numérique Distribuée

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