Logo AND Algorithmique Numérique Distribuée

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