Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill now unused xbt_heap.
[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
28   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")
29   if(CMAKE_COMPILER_IS_GNUCXX AND (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0")))
30     set(warnCFLAGS "${warnCFLAGS} -Wformat-signedness")
31   endif()
32   if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GCC")
33     set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-format-nonliteral")
34   endif()
35   if(CMAKE_COMPILER_IS_GNUCXX)
36     set(warnCXXFLAGS "${warnCXXFLAGS} -Wclobbered -Wno-error=clobbered  -Wno-unused-local-typedefs -Wno-error=attributes")
37   endif()
38   if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
39     # don't care about class that become struct, avoid issue of empty C structs
40     # size (coming from libunwind.h)
41     set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-mismatched-tags -Wno-extern-c-compat")
42   endif()
43
44   # the one specific to C but refused by C++
45   set(warnCFLAGS "${warnCFLAGS} -Wmissing-prototypes") 
46
47   if(CMAKE_Fotran_COMPILER_ID MATCHES "GCC|PGI")
48     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
49   endif()
50   if(CMAKE_Fotran_COMPILER_ID MATCHES "Intel")
51     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -warn all")
52   endif()
53   set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
54 endif()
55
56 # NDEBUG gives a lot of "initialized but unused variables" errors. Don't die anyway.
57 if(enable_compile_warnings AND enable_debug)
58   set(warnCFLAGS "${warnCFLAGS} -Werror")
59   set(warnCXXFLAGS "${warnCXXFLAGS} -Werror")
60 endif()
61
62 # Activate the warnings on #if FOOBAR when FOOBAR has no value
63 # It breaks on FreeBSD within Boost headers, so activate this only in Pure Hardcore debug mode.
64 if(enable_maintainer_mode)
65   set(warnCFLAGS "${warnCFLAGS} -Wundef")
66   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
67 endif()
68
69 # Se the optimisation flags
70 # NOTE, we should CMAKE_BUILD_TYPE for this
71 if(enable_compile_optimizations)
72   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
73 else()
74   set(optCFLAGS "-O0 ")
75 endif()
76 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC
77     AND (NOT enable_model-checking))
78   # This is redundant (already in -03):
79   set(optCFLAGS "${optCFLAGS} -finline-functions ")
80 endif()
81
82 # Do not leak the current directory into the binaries
83 if(CMAKE_COMPILER_IS_GNUCC)
84   set(optCFLAGS "${optCFLAGS} -fdebug-prefix-map=${CMAKE_SOURCE_DIR}=.")
85 endif()
86
87 # Configure LTO
88 # NOTE, cmake 3.0 has a INTERPROCEDURAL_OPTIMIZATION target
89 #       property for this (http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html)
90 if(enable_lto) # User wants LTO. Try if we can do that
91   set(enable_lto OFF)
92   if(enable_compile_optimizations
93       AND CMAKE_COMPILER_IS_GNUCC
94       AND (NOT enable_model-checking))
95     # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
96     #   We are experiencing assertion failures even with 4.8 on MinGW.
97     #   Push the support forward: will see if 4.9 works when we test it.
98     #
99     # On Linux, we got the following with GCC 4.8.4 on Centos and Ubuntu
100     #    lto1: internal compiler error: in output_die, at dwarf2out.c:8478
101     #    Please submit a full bug report, with preprocessed source if appropriate.
102     # So instead, we push the support forward
103
104     if ( (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.5")
105          AND (LINKER_VERSION VERSION_GREATER "2.22"))
106       set(enable_lto ON)
107     endif()
108   endif()
109   if(enable_lto)
110     message("-- LTO seems usable.")
111   else()
112     if(NOT enable_compile_optimizations)
113       message("-- LTO disabled: Compile-time optimizations turned off.")
114     else() 
115       if(enable_model-checking)
116         message("-- LTO disabled when compiling with model-checking.")
117       else()
118         message("-- LTO does not seem usable -- try updating your build chain.")
119       endif() 
120     endif()
121   endif()
122 else()
123   message("-- LTO disabled on the command line.")
124 endif()
125 if(enable_lto) # User wants LTO, and it seems usable. Go for it
126   set(optCFLAGS "${optCFLAGS} -flto ")
127   # See https://gcc.gnu.org/wiki/LinkTimeOptimizationFAQ#ar.2C_nm_and_ranlib:
128   # "Since version 4.9 gcc produces slim object files that only contain
129   # the intermediate representation. In order to handle archives of
130   # these objects you have to use the gcc wrappers:
131   # gcc-ar, gcc-nm and gcc-ranlib."
132   if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
133       AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
134     set (CMAKE_AR gcc-ar)
135     set (CMAKE_RANLIB gcc-ranlib)
136   endif()
137 endif()
138
139 if(enable_model-checking AND enable_compile_optimizations)
140   # Forget it, do not optimize the code (because it confuses the MC):
141   set(optCFLAGS "-O0 ")
142   # But you can still optimize this:
143   foreach(s
144       src/xbt/mmalloc/mm.c
145       src/xbt/log.c src/xbt/xbt_log_appender_file.c
146       src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c
147       src/xbt/dict.cpp src/xbt/dict_elm.c src/xbt/dict_cursor.c
148       src/xbt/dynar.cpp src/xbt/swag.c
149       src/xbt/str.c src src/xbt/snprintf.c
150       src/xbt/queue.c
151       src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c
152       src/xbt/backtrace_linux.c
153       ${MC_SRC_BASE} ${MC_SRC})
154       set (mcCFLAGS "-O3  -funroll-loops -fno-strict-aliasing")
155        if(CMAKE_COMPILER_IS_GNUCC)
156          set (mcCFLAGS "${mcCFLAGS} -finline-functions")
157       endif()
158       set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
159   endforeach()
160 endif()
161
162 if(NOT enable_debug)
163   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
164   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
165 endif()
166
167 set(CMAKE_C_FLAGS   "${warnCFLAGS} ${CMAKE_C_FLAGS}   ${optCFLAGS}")
168 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
169
170 # Try to make Mac a bit more complient to open source standards
171 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
172   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
173 endif()
174
175 set(TESH_OPTION "")
176 if(enable_coverage)
177   find_program(GCOV_PATH gcov)
178   if(GCOV_PATH)
179     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
180     set(COVERAGE_EXTRA_FLAGS "-l -p")
181     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
182     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
183     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
184     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
185     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
186     add_definitions(-fprofile-arcs -ftest-coverage)
187   endif()
188 endif()
189
190 if(enable_address_sanitizer)
191     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
192     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
193     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
194     set(TESH_OPTION --enable-sanitizers)
195     try_compile(HAVE_SANITIZE_ADDRESS ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp)
196     try_compile(HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp
197       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
198 else()
199     set(HAVE_SANITIZE_ADDRESS FALSE CACHE INTERNAL "")
200     set(HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT FALSE CACHE INTERNAL "")
201 endif()
202
203 if(enable_thread_sanitizer)
204     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
205     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
206     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
207 endif()
208
209 if(enable_undefined_sanitizer)
210     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
211     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
212     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
213 endif()
214
215 if(NOT $ENV{CFLAGS} STREQUAL "")
216   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
217   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
218 endif()
219
220 if(NOT $ENV{CXXFLAGS} STREQUAL "")
221   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
222   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
223 endif()
224
225 if(NOT $ENV{LDFLAGS} STREQUAL "")
226   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
227   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
228 endif()
229
230 if(MINGW)
231   # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
232   # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
233   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -static-libgcc")
234   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
235   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS   "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
236   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
237   
238   # JNI searches for stdcalls
239   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -Wl,--add-stdcall-alias")
240   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
241   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
242   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
243   
244   # Specify the data model that we are using (yeah it may help Java)
245   if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
246     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m32")
247     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
248   else()
249     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m64")
250     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
251   endif()  
252 endif()