Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'energy-pstate' of https://github.com/Takishipp/simgrid into Takishipp...
[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_Fortran_COMPILER_ID MATCHES "GCC|PGI")
48     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
49   endif()
50   if(CMAKE_Fortran_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   execute_process(COMMAND realpath --relative-to=${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}
85     RESULT_VARIABLE RESULT OUTPUT_VARIABLE RELATIVE_SOURCE_DIR ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
86   if(RESULT EQUAL 0)
87     message(STATUS "Relative source directory is \"${RELATIVE_SOURCE_DIR}\".")
88   else()
89     message(WARNING "Failed to find relative source directory. Using \".\".")
90     set(RELATIVE_SOURCE_DIR ".")
91   endif()
92   set(optCFLAGS "${optCFLAGS} -fdebug-prefix-map=${CMAKE_SOURCE_DIR}=${RELATIVE_SOURCE_DIR}")
93 endif()
94
95 # Configure LTO
96 # NOTE, cmake 3.0 has a INTERPROCEDURAL_OPTIMIZATION target
97 #       property for this (http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html)
98 if(enable_lto) # User wants LTO. Try if we can do that
99   set(enable_lto OFF)
100   if(enable_compile_optimizations
101       AND CMAKE_COMPILER_IS_GNUCC
102       AND (NOT enable_model-checking))
103     # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
104     #   We are experiencing assertion failures even with 4.8 on MinGW.
105     #   Push the support forward: will see if 4.9 works when we test it.
106     #
107     # On Linux, we got the following with GCC 4.8.4 on Centos and Ubuntu
108     #    lto1: internal compiler error: in output_die, at dwarf2out.c:8478
109     #    Please submit a full bug report, with preprocessed source if appropriate.
110     # So instead, we push the support forward
111
112     if ( (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.5")
113          AND (LINKER_VERSION VERSION_GREATER "2.22"))
114       set(enable_lto ON)
115     endif()
116   endif()
117   if(enable_lto)
118     message(STATUS "LTO seems usable.")
119   else()
120     if(NOT enable_compile_optimizations)
121       message(STATUS "LTO disabled: Compile-time optimizations turned off.")
122     else() 
123       if(enable_model-checking)
124         message(STATUS "LTO disabled when compiling with model-checking.")
125       else()
126         message(STATUS "LTO does not seem usable -- try updating your build chain.")
127       endif() 
128     endif()
129   endif()
130 else()
131   message(STATUS "LTO disabled on the command line.")
132 endif()
133 if(enable_lto) # User wants LTO, and it seems usable. Go for it
134   set(optCFLAGS "${optCFLAGS} -flto ")
135   # See https://gcc.gnu.org/wiki/LinkTimeOptimizationFAQ#ar.2C_nm_and_ranlib:
136   # "Since version 4.9 gcc produces slim object files that only contain
137   # the intermediate representation. In order to handle archives of
138   # these objects you have to use the gcc wrappers:
139   # gcc-ar, gcc-nm and gcc-ranlib."
140   if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
141       AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
142     set (CMAKE_AR gcc-ar)
143     set (CMAKE_RANLIB gcc-ranlib)
144   endif()
145 endif()
146
147 if(enable_model-checking AND enable_compile_optimizations)
148   # Forget it, do not optimize the code (because it confuses the MC):
149   set(optCFLAGS "-O0 ")
150   # But you can still optimize this:
151   foreach(s
152       src/kernel/lmm/fair_bottleneck.cpp src/kernel/lmm/lagrange.cpp src/kernel/lmm/maxmin.cpp
153       src/xbt/mmalloc/mm.c
154       src/xbt/log.c src/xbt/xbt_log_appender_file.c
155       src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c
156       src/xbt/dict.cpp src/xbt/dict_elm.c src/xbt/dict_cursor.c
157       src/xbt/dynar.cpp src/xbt/swag.c
158       src/xbt/xbt_str.cpp src/xbt/snprintf.c
159       src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c
160       src/xbt/backtrace_linux.cpp
161       ${MC_SRC_BASE} ${MC_SRC})
162       set (mcCFLAGS "-O3  -funroll-loops -fno-strict-aliasing")
163        if(CMAKE_COMPILER_IS_GNUCC)
164          set (mcCFLAGS "${mcCFLAGS} -finline-functions")
165       endif()
166       set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
167   endforeach()
168 endif()
169
170 if(NOT enable_debug)
171   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
172   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
173 endif()
174
175 set(CMAKE_C_FLAGS   "${warnCFLAGS} ${CMAKE_C_FLAGS}   ${optCFLAGS}")
176 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
177
178 # Try to make Mac a bit more complient to open source standards
179 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
180   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
181 endif()
182
183 set(TESH_OPTION "")
184 if(enable_coverage)
185   find_program(GCOV_PATH gcov)
186   if(GCOV_PATH)
187     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
188     set(COVERAGE_EXTRA_FLAGS "-l -p")
189     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
190     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
191     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
192     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
193     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
194     add_definitions(-fprofile-arcs -ftest-coverage)
195   endif()
196 endif()
197
198 if(enable_address_sanitizer)
199     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
200     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
201     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
202     set(TESH_OPTION --enable-sanitizers)
203     try_compile(HAVE_SANITIZE_ADDRESS ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp)
204     try_compile(HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp
205       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
206 else()
207     set(HAVE_SANITIZE_ADDRESS FALSE CACHE INTERNAL "")
208     set(HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT FALSE CACHE INTERNAL "")
209 endif()
210
211 if(enable_thread_sanitizer)
212     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
213     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
214     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
215 endif()
216
217 if(enable_undefined_sanitizer)
218     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
219     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
220     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
221 endif()
222
223 if(NOT $ENV{CFLAGS} STREQUAL "")
224   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
225   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
226 endif()
227
228 if(NOT $ENV{CXXFLAGS} STREQUAL "")
229   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
230   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
231 endif()
232
233 if(NOT $ENV{LDFLAGS} STREQUAL "")
234   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
235   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
236 endif()
237
238 if(MINGW)
239   # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
240   # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
241   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -static-libgcc")
242   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
243   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS   "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
244   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
245   
246   # JNI searches for stdcalls
247   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -Wl,--add-stdcall-alias")
248   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
249   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
250   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
251   
252   # Specify the data model that we are using (yeah it may help Java)
253   if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
254     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m32")
255     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
256   else()
257     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m64")
258     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
259   endif()  
260 endif()