Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more bad fixes
[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     set(warnCFLAGS "${warnCFLAGS} -wd1418 -wd191 -wd3179")
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_CXX_COMPILER_ID MATCHES "Clang|GCC")
39     set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-format-nonliteral")
40   endif()
41   if(CMAKE_COMPILER_IS_GNUCXX)
42     set(warnCXXFLAGS "${warnCXXFLAGS} -Wclobbered -Wno-error=clobbered  -Wno-unused-local-typedefs -Wno-error=attributes")
43   endif()
44   if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
45     # don't care about class that become struct, avoid issue of empty C structs
46     # size (coming from libunwind.h)
47     set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-mismatched-tags -Wno-extern-c-compat")
48   endif()
49
50   # the one specific to C but refused by C++
51   set(warnCFLAGS "${warnCFLAGS} -Wmissing-prototypes") 
52
53   if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC|PGI")
54     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
55   endif()
56   if(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 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/kernel/lmm/fair_bottleneck.cpp src/kernel/lmm/lagrange.cpp src/kernel/lmm/maxmin.cpp
159       src/xbt/mmalloc/mm.c
160       src/xbt/log.c src/xbt/xbt_log_appender_file.c
161       src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c
162       src/xbt/dict.cpp src/xbt/dict_elm.c src/xbt/dict_cursor.c
163       src/xbt/dynar.cpp
164       src/xbt/xbt_str.cpp src/xbt/snprintf.c
165       src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c
166       src/xbt/backtrace_linux.cpp
167       ${MC_SRC_BASE} ${MC_SRC})
168       set (mcCFLAGS "-O3  -funroll-loops -fno-strict-aliasing")
169        if(CMAKE_COMPILER_IS_GNUCC)
170          set (mcCFLAGS "${mcCFLAGS} -finline-functions")
171       endif()
172       set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
173   endforeach()
174 endif()
175
176 if(NOT enable_debug)
177   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
178   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
179 endif()
180
181 set(CMAKE_C_FLAGS   "${warnCFLAGS} ${CMAKE_C_FLAGS}   ${optCFLAGS}")
182 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
183
184 # Try to make Mac a bit more complient to open source standards
185 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
186   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
187 endif()
188
189 # Avoid a failure seen with gcc 7.2.0 and ns3 3.27
190 if(enable_ns3)
191   set_source_files_properties(src/surf/network_ns3.cpp PROPERTIES COMPILE_FLAGS " -Wno-unused-local-typedef")  
192 endif()
193
194 set(TESH_OPTION "")
195 if(enable_coverage)
196   find_program(GCOV_PATH gcov)
197   if(GCOV_PATH)
198     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
199     set(COVERAGE_EXTRA_FLAGS "-l -p")
200     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
201     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
202     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
203     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
204     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
205     add_definitions(-fprofile-arcs -ftest-coverage)
206   endif()
207 endif()
208
209 if(enable_address_sanitizer)
210     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
211     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
212     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
213     set(TESH_OPTION --enable-sanitizers)
214     try_compile(HAVE_SANITIZE_ADDRESS ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp)
215     try_compile(HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp
216       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
217 else()
218     set(HAVE_SANITIZE_ADDRESS FALSE CACHE INTERNAL "")
219     set(HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT FALSE CACHE INTERNAL "")
220 endif()
221
222 if(enable_thread_sanitizer)
223     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
224     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
225     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
226 endif()
227
228 if(enable_undefined_sanitizer)
229     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
230     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
231     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
232 endif()
233
234 if(NOT $ENV{CFLAGS} STREQUAL "")
235   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
236   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
237 endif()
238
239 if(NOT $ENV{CXXFLAGS} STREQUAL "")
240   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
241   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
242 endif()
243
244 if(NOT $ENV{LDFLAGS} STREQUAL "")
245   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
246   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
247 endif()
248
249 if(MINGW)
250   # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
251   # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
252   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -static-libgcc")
253   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
254   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS   "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
255   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
256   
257   # JNI searches for stdcalls
258   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -Wl,--add-stdcall-alias")
259   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
260   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
261   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
262   
263   # Specify the data model that we are using (yeah it may help Java)
264   if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
265     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m32")
266     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
267   else()
268     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m64")
269     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
270   endif()  
271 endif()