Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e15cbdb37a71fcec51555bedf96b807f4bf431ce
[simgrid.git] / tools / cmake / GCCFlags.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_C_COMPILER_ID MATCHES "Clang|GCC")
22     set(warnCFLAGS "${warnCFLAGS} -Wno-format-nonliteral")
23   endif()
24   if(CMAKE_COMPILER_IS_GNUCC)
25     set(warnCFLAGS "${warnCFLAGS} -Wclobbered -Wno-error=clobbered  -Wno-unused-local-typedefs -Wno-error=attributes")
26   endif()
27   if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
28     # ignore remark  #1418: external function definition with no prior declaration
29     # 3179: deprecated conversion of string literal to char* (should be const char*)
30     # 191: type qualifier is meaningless on cast type
31     # 597: entity-kind "entity" will not be called for implicit or explicit conversions
32     set(warnCFLAGS "${warnCFLAGS} -wd1418 -wd191 -wd3179 -ww597")
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_CXX_COMPILER_ID MATCHES "Clang|GCC")
40     set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-format-nonliteral")
41   endif()
42   if(CMAKE_COMPILER_IS_GNUCXX)
43     set(warnCXXFLAGS "${warnCXXFLAGS} -Wclobbered -Wno-error=clobbered  -Wno-unused-local-typedefs -Wno-error=attributes")
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   endif()
57   if(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 endif()
68
69 # Activate the warnings on #if FOOBAR when FOOBAR has no value
70 # It breaks on FreeBSD within Boost headers, so activate this only in Pure Hardcore debug mode.
71 if(enable_maintainer_mode)
72   set(warnCFLAGS "${warnCFLAGS} -Wundef")
73   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
74 endif()
75
76 # Se the optimisation flags
77 # NOTE, we should CMAKE_BUILD_TYPE for this
78 if(enable_compile_optimizations)
79   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
80 else()
81   set(optCFLAGS "-O0 ")
82 endif()
83 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC
84     AND (NOT enable_model-checking))
85   # This is redundant (already in -03):
86   set(optCFLAGS "${optCFLAGS} -finline-functions ")
87 endif()
88
89 # Do not leak the current directory into the binaries
90 if(CMAKE_COMPILER_IS_GNUCC)
91   execute_process(COMMAND realpath --relative-to=${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}
92     RESULT_VARIABLE RESULT OUTPUT_VARIABLE RELATIVE_SOURCE_DIR ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
93   if(RESULT EQUAL 0)
94     message(STATUS "Relative source directory is \"${RELATIVE_SOURCE_DIR}\".")
95   else()
96     message(WARNING "Failed to find relative source directory. Using \".\".")
97     set(RELATIVE_SOURCE_DIR ".")
98   endif()
99   set(optCFLAGS "${optCFLAGS} -fdebug-prefix-map=${CMAKE_SOURCE_DIR}=${RELATIVE_SOURCE_DIR}")
100 endif()
101
102 # Configure LTO
103 # NOTE, cmake 3.0 has a INTERPROCEDURAL_OPTIMIZATION target
104 #       property for this (http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html)
105 if(enable_lto) # User wants LTO. Try if we can do that
106   set(enable_lto OFF)
107   if(enable_compile_optimizations
108       AND CMAKE_COMPILER_IS_GNUCC
109       AND (NOT enable_model-checking))
110     # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
111     #   We are experiencing assertion failures even with 4.8 on MinGW.
112     #   Push the support forward: will see if 4.9 works when we test it.
113     #
114     # On Linux, we got the following with GCC 4.8.4 on Centos and Ubuntu
115     #    lto1: internal compiler error: in output_die, at dwarf2out.c:8478
116     #    Please submit a full bug report, with preprocessed source if appropriate.
117     # So instead, we push the support forward
118
119     if ( (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   endif()
124   if(enable_lto)
125     message(STATUS "LTO seems usable.")
126   else()
127     if(NOT enable_compile_optimizations)
128       message(STATUS "LTO disabled: Compile-time optimizations turned off.")
129     else()
130       if(enable_model-checking)
131         message(STATUS "LTO disabled when compiling with model-checking.")
132       else()
133         message(STATUS "LTO does not seem usable -- try updating your build chain.")
134       endif()
135     endif()
136   endif()
137 else()
138   message(STATUS "LTO disabled on the command line.")
139 endif()
140 if(enable_lto) # User wants LTO, and it seems usable. Go for it
141   set(optCFLAGS "${optCFLAGS} -flto ")
142   # See https://gcc.gnu.org/wiki/LinkTimeOptimizationFAQ#ar.2C_nm_and_ranlib:
143   # "Since version 4.9 gcc produces slim object files that only contain
144   # the intermediate representation. In order to handle archives of
145   # these objects you have to use the gcc wrappers:
146   # gcc-ar, gcc-nm and gcc-ranlib."
147   if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
148       AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
149     set (CMAKE_AR gcc-ar)
150     set (CMAKE_RANLIB gcc-ranlib)
151   endif()
152 endif()
153
154 if(enable_model-checking AND enable_compile_optimizations)
155   # Forget it, do not optimize the code (because it confuses the MC):
156   set(optCFLAGS "-O0 ")
157   # But you can still optimize this:
158   foreach(s
159       src/kernel/lmm/fair_bottleneck.cpp src/kernel/lmm/lagrange.cpp src/kernel/lmm/maxmin.cpp
160       src/xbt/mmalloc/mm.c
161       src/xbt/log.c src/xbt/xbt_log_appender_file.c
162       src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c
163       src/xbt/dict.cpp src/xbt/dict_elm.c src/xbt/dict_cursor.c
164       src/xbt/dynar.cpp
165       src/xbt/xbt_str.cpp src/xbt/snprintf.c
166       src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c
167       src/xbt/backtrace_linux.cpp
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(COVERAGE_EXTRA_FLAGS "-l -p")
206     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
207     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
208     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
209     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
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     set(HAVE_SANITIZER_THREAD TRUE CACHE INTERNAL "")
233 else()
234     set(HAVE_SANITIZER_THREAD FALSE CACHE INTERNAL "")
235 endif()
236
237 if(enable_undefined_sanitizer)
238     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
239     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
240     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
241 endif()
242
243 if(NOT $ENV{CFLAGS} STREQUAL "")
244   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
245   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
246 endif()
247
248 if(NOT $ENV{CXXFLAGS} STREQUAL "")
249   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
250   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
251 endif()
252
253 if(NOT $ENV{LDFLAGS} STREQUAL "")
254   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
255   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
256 endif()
257
258 if(MINGW)
259   # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
260   # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
261   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -static-libgcc")
262   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
263   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS   "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
264   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
265
266   # JNI searches for stdcalls
267   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -Wl,--add-stdcall-alias")
268   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
269   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
270   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
271
272   # Specify the data model that we are using (yeah it may help Java)
273   if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
274     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m32")
275     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
276   else()
277     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m64")
278     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
279   endif()
280 endif()