Logo AND Algorithmique Numérique Distribuée

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