Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Only test fortran for LTO if compiler is present.
[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       set(ipoLANGS C CXX)
123       if(SMPI_FORTRAN)
124         set(ipoLANGS ${ipoLANGS} Fortran)
125       endif()
126       check_ipo_supported(RESULT ipo LANGUAGES ${ipoLANGS})
127       if(ipo)
128         set(enable_lto ON)
129       endif()
130     endif()
131   endif()
132
133   if(enable_lto)
134     message(STATUS "LTO seems usable.")
135   else()
136     if(NOT enable_compile_optimizations)
137       message(STATUS "LTO disabled: Compile-time optimizations turned off.")
138     else()
139       if(enable_model-checking)
140         message(STATUS "LTO disabled when compiling with model-checking.")
141       else()
142         message(STATUS "LTO does not seem usable -- try updating your build chain.")
143       endif()
144     endif()
145   endif()
146 else()
147   message(STATUS "LTO disabled on the command line.")
148 endif()
149 if(enable_lto) # User wants LTO, and it seems usable. Go for it
150   set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
151   if(LTO_EXTRA_FLAG AND CMAKE_COMPILER_IS_GNUCC)
152     list(APPEND CMAKE_C_COMPILE_OPTIONS_IPO "-flto=${LTO_EXTRA_FLAG}")
153     list(APPEND CMAKE_CXX_COMPILE_OPTIONS_IPO "-flto=${LTO_EXTRA_FLAG}")
154   endif()
155   # "Since version 4.9 gcc produces slim object files that only contain
156   # the intermediate representation. In order to handle archives of
157   # these objects you have to use the gcc wrappers:
158   # gcc-ar, gcc-nm and gcc-ranlib."
159   if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
160       AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
161     set (CMAKE_AR gcc-ar)
162     set (CMAKE_RANLIB gcc-ranlib)
163   endif()
164 endif()
165
166 if(enable_model-checking AND enable_compile_optimizations)
167   # Forget it, do not optimize the code (because it confuses the MC):
168   set(optCFLAGS "-O0 ")
169   # But you can still optimize this:
170   foreach(s
171       src/simix/popping.cpp src/simix/popping_generated.cpp src/simix/smx_global.cpp
172       ${SURF_SRC} ${TRACING_SRC} ${XBT_SRC}
173       ${MC_SRC_BASE} ${MC_SRC})
174       set (mcCFLAGS "-O3  -funroll-loops -fno-strict-aliasing")
175        if(CMAKE_COMPILER_IS_GNUCC)
176          set (mcCFLAGS "${mcCFLAGS} -finline-functions")
177       endif()
178       set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
179   endforeach()
180 endif()
181
182 if (CMAKE_C_COMPILER_ID MATCHES "Intel")
183   # honor parentheses when determining the order of expression evaluation.
184   set(optCFLAGS "${optCFLAGS} -fprotect-parens ")
185 endif()
186
187 if(NOT enable_debug)
188   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
189   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
190 endif()
191
192 set(CMAKE_C_FLAGS   "${warnCFLAGS} ${CMAKE_C_FLAGS}   ${optCFLAGS}")
193 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
194
195 # Try to make Mac a bit more complient to open source standards
196 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
197   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
198 endif()
199
200 # Avoid a failure seen with gcc 7.2.0 and ns3 3.27
201 if(enable_ns3)
202   set_source_files_properties(src/surf/network_ns3.cpp PROPERTIES COMPILE_FLAGS " -Wno-unused-local-typedef")
203 endif()
204
205 set(TESH_OPTION "")
206 if(enable_coverage)
207   find_program(GCOV_PATH gcov)
208   if(GCOV_PATH)
209     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
210     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
211     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
212     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
213     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
214     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCOVERAGE")
215     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
216     add_definitions(-fprofile-arcs -ftest-coverage)
217   endif()
218 endif()
219
220 if(enable_address_sanitizer)
221     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
222     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
223     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
224     set(TESH_OPTION --enable-sanitizers)
225     try_compile(HAVE_SANITIZER_ADDRESS ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp)
226     try_compile(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp
227       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
228 else()
229     set(HAVE_SANITIZER_ADDRESS FALSE CACHE INTERNAL "")
230     set(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT FALSE CACHE INTERNAL "")
231 endif()
232
233 if(enable_thread_sanitizer)
234     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
235     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
236     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
237     try_compile(HAVE_SANITIZER_THREAD ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_tsan.cpp)
238     try_compile(HAVE_SANITIZER_THREAD_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_tsan.cpp
239       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
240 else()
241     set(HAVE_SANITIZER_THREAD FALSE CACHE INTERNAL "")
242     set(HAVE_SANITIZER_THREAD_FIBER_SUPPORT FALSE CACHE INTERNAL "")
243 endif()
244
245 if(enable_undefined_sanitizer)
246     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
247     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
248     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
249 endif()
250
251 if(NOT $ENV{CFLAGS} STREQUAL "")
252   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
253   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
254 endif()
255
256 if(NOT $ENV{CXXFLAGS} STREQUAL "")
257   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
258   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
259 endif()
260
261 if(NOT $ENV{LDFLAGS} STREQUAL "")
262   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
263   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
264 endif()
265
266 if(MINGW)
267   # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
268   # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
269   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -static-libgcc")
270   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
271   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS   "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
272   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
273
274   # JNI searches for stdcalls
275   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -Wl,--add-stdcall-alias")
276   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
277   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
278   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
279
280   # Specify the data model that we are using (yeah it may help Java)
281   if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
282     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m32")
283     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
284   else()
285     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m64")
286     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
287   endif()
288 endif()