Logo AND Algorithmique Numérique Distribuée

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