Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cb7a0c22766ccb3fcd54053aa2dea69fff7f6d10
[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   endif()
53   if(CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
54     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -warn all")
55   endif()
56   set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
57 endif()
58
59 # NDEBUG gives a lot of "initialized but unused variables" errors. Don't die anyway.
60 if(enable_compile_warnings AND enable_debug)
61   set(warnCFLAGS "${warnCFLAGS} -Werror")
62   set(warnCXXFLAGS "${warnCXXFLAGS} -Werror")
63   if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC")
64     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Werror -Werror=format-security")
65   endif()
66 endif()
67
68 # Activate the warnings on #if FOOBAR when FOOBAR has no value
69 # It breaks on FreeBSD within Boost headers, so activate this only in Pure Hardcore debug mode.
70 if(enable_maintainer_mode)
71   set(warnCFLAGS "${warnCFLAGS} -Wundef")
72   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
73 endif()
74
75 # Se the optimisation flags
76 # NOTE, we should CMAKE_BUILD_TYPE for this
77 if(enable_compile_optimizations)
78   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
79 else()
80   set(optCFLAGS "-O0 ")
81 endif()
82 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC
83     AND (NOT enable_model-checking))
84   # This is redundant (already in -03):
85   set(optCFLAGS "${optCFLAGS} -finline-functions ")
86 endif()
87
88 # Do not leak the current directory into the binaries
89 if(CMAKE_COMPILER_IS_GNUCC)
90   execute_process(COMMAND realpath --relative-to=${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}
91     RESULT_VARIABLE RESULT OUTPUT_VARIABLE RELATIVE_SOURCE_DIR ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
92   if(RESULT EQUAL 0)
93     message(STATUS "Relative source directory is \"${RELATIVE_SOURCE_DIR}\".")
94   else()
95     message(WARNING "Failed to find relative source directory. Using \".\".")
96     set(RELATIVE_SOURCE_DIR ".")
97   endif()
98   set(optCFLAGS "${optCFLAGS} -fdebug-prefix-map=${CMAKE_SOURCE_DIR}=${RELATIVE_SOURCE_DIR}")
99 endif()
100
101 # Configure LTO
102 # NOTE, cmake 3.0 has a INTERPROCEDURAL_OPTIMIZATION target
103 #       property for this (http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html)
104 if(enable_lto) # User wants LTO. Try if we can do that
105   set(enable_lto OFF)
106   if(enable_compile_optimizations
107       AND CMAKE_COMPILER_IS_GNUCC
108       AND (NOT enable_model-checking))
109     # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
110     #   We are experiencing assertion failures even with 4.8 on MinGW.
111     #   Push the support forward: will see if 4.9 works when we test it.
112     #
113     # On Linux, we got the following with GCC 4.8.4 on Centos and Ubuntu
114     #    lto1: internal compiler error: in output_die, at dwarf2out.c:8478
115     #    Please submit a full bug report, with preprocessed source if appropriate.
116     # So instead, we push the support forward
117
118     if ( (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.5")
119          AND (LINKER_VERSION VERSION_GREATER "2.22"))
120       set(enable_lto ON)
121     endif()
122   endif()
123   if(enable_lto)
124     message(STATUS "LTO seems usable.")
125   else()
126     if(NOT enable_compile_optimizations)
127       message(STATUS "LTO disabled: Compile-time optimizations turned off.")
128     else()
129       if(enable_model-checking)
130         message(STATUS "LTO disabled when compiling with model-checking.")
131       else()
132         message(STATUS "LTO does not seem usable -- try updating your build chain.")
133       endif()
134     endif()
135   endif()
136 else()
137   message(STATUS "LTO disabled on the command line.")
138 endif()
139 if(enable_lto) # User wants LTO, and it seems usable. Go for it
140   set(optCFLAGS "${optCFLAGS} -flto ")
141   # See https://gcc.gnu.org/wiki/LinkTimeOptimizationFAQ#ar.2C_nm_and_ranlib:
142   # "Since version 4.9 gcc produces slim object files that only contain
143   # the intermediate representation. In order to handle archives of
144   # these objects you have to use the gcc wrappers:
145   # gcc-ar, gcc-nm and gcc-ranlib."
146   if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
147       AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
148     set (CMAKE_AR gcc-ar)
149     set (CMAKE_RANLIB gcc-ranlib)
150   endif()
151 endif()
152
153 if(enable_model-checking AND enable_compile_optimizations)
154   # Forget it, do not optimize the code (because it confuses the MC):
155   set(optCFLAGS "-O0 ")
156   # But you can still optimize this:
157   foreach(s
158       src/simix/popping.cpp src/simix/popping_generated.cpp src/simix/smx_global.cpp
159       ${SURF_SRC} ${TRACING_SRC} ${XBT_SRC}
160       ${MC_SRC_BASE} ${MC_SRC})
161       set (mcCFLAGS "-O3  -funroll-loops -fno-strict-aliasing")
162        if(CMAKE_COMPILER_IS_GNUCC)
163          set (mcCFLAGS "${mcCFLAGS} -finline-functions")
164       endif()
165       set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
166   endforeach()
167 endif()
168
169 if (CMAKE_C_COMPILER_ID MATCHES "Intel")
170   # honor parentheses when determining the order of expression evaluation.
171   set(optCFLAGS "${optCFLAGS} -fprotect-parens ")
172 endif()
173
174 if(NOT enable_debug)
175   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
176   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
177 endif()
178
179 set(CMAKE_C_FLAGS   "${warnCFLAGS} ${CMAKE_C_FLAGS}   ${optCFLAGS}")
180 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
181
182 # Try to make Mac a bit more complient to open source standards
183 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
184   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
185 endif()
186
187 # Avoid a failure seen with gcc 7.2.0 and ns3 3.27
188 if(enable_ns3)
189   set_source_files_properties(src/surf/network_ns3.cpp PROPERTIES COMPILE_FLAGS " -Wno-unused-local-typedef")
190 endif()
191
192 set(TESH_OPTION "")
193 if(enable_coverage)
194   find_program(GCOV_PATH gcov)
195   if(GCOV_PATH)
196     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
197     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
198     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
199     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
200     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
201     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCOVERAGE")
202     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
203     add_definitions(-fprofile-arcs -ftest-coverage)
204   endif()
205 endif()
206
207 if(enable_address_sanitizer)
208     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
209     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
210     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
211     set(TESH_OPTION --enable-sanitizers)
212     try_compile(HAVE_SANITIZER_ADDRESS ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp)
213     try_compile(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp
214       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
215 else()
216     set(HAVE_SANITIZER_ADDRESS FALSE CACHE INTERNAL "")
217     set(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT FALSE CACHE INTERNAL "")
218 endif()
219
220 if(enable_thread_sanitizer)
221     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
222     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
223     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
224     set(HAVE_SANITIZER_THREAD TRUE CACHE INTERNAL "")
225 else()
226     set(HAVE_SANITIZER_THREAD FALSE CACHE INTERNAL "")
227 endif()
228
229 if(enable_undefined_sanitizer)
230     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
231     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
232     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
233 endif()
234
235 if(NOT $ENV{CFLAGS} STREQUAL "")
236   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
237   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
238 endif()
239
240 if(NOT $ENV{CXXFLAGS} STREQUAL "")
241   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
242   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
243 endif()
244
245 if(NOT $ENV{LDFLAGS} STREQUAL "")
246   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
247   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
248 endif()
249
250 if(MINGW)
251   # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
252   # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
253   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -static-libgcc")
254   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
255   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS   "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
256   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
257
258   # JNI searches for stdcalls
259   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -Wl,--add-stdcall-alias")
260   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
261   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
262   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
263
264   # Specify the data model that we are using (yeah it may help Java)
265   if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
266     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m32")
267     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
268   else()
269     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m64")
270     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
271   endif()
272 endif()