2 ## This file is in charge of setting our paranoid flags with regard to warnings and optimization.
4 ## It is only used for gcc and clang. MSVC builds don't load this file.
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.
9 ## Other compiler flags (C/C++ standard version) are tested and set
10 ## by the beginning of the configuration, directly in ~/CMakeList.txt
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")
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")
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 # 2330: argument of type "type" is incompatible with parameter of type "type" (dropping qualifiers)
30 set(warnCFLAGS "${warnCFLAGS} -wd1418 -wd191 -wd3179 -ww597 -ww2330")
33 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")
34 if(CMAKE_COMPILER_IS_GNUCXX AND (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0")))
35 set(warnCFLAGS "${warnCFLAGS} -Wformat-signedness")
37 if(CMAKE_COMPILER_IS_GNUCXX)
38 set(warnCXXFLAGS "${warnCXXFLAGS} -Wclobbered -Wno-error=clobbered -Wno-unused-local-typedefs -Wno-error=attributes -Wno-error=maybe-uninitialized")
40 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
41 # don't care about class that become struct, avoid issue of empty C structs
42 # size (coming from libunwind.h)
43 set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-mismatched-tags -Wno-extern-c-compat")
46 # the one specific to C but refused by C++
47 set(warnCFLAGS "${warnCFLAGS} -Wmissing-prototypes")
49 if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC|PGI")
50 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
52 if(CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
53 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -warn all")
55 set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
58 # NDEBUG gives a lot of "initialized but unused variables" errors. Don't die anyway.
59 if(enable_compile_warnings AND enable_debug)
60 set(warnCFLAGS "${warnCFLAGS} -Werror")
61 set(warnCXXFLAGS "${warnCXXFLAGS} -Werror")
64 # Activate the warnings on #if FOOBAR when FOOBAR has no value
65 # It breaks on FreeBSD within Boost headers, so activate this only in Pure Hardcore debug mode.
66 if(enable_maintainer_mode)
67 set(warnCFLAGS "${warnCFLAGS} -Wundef")
68 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
71 # Se the optimisation flags
72 # NOTE, we should CMAKE_BUILD_TYPE for this
73 if(enable_compile_optimizations)
74 set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
78 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC
79 AND (NOT enable_model-checking))
80 # This is redundant (already in -03):
81 set(optCFLAGS "${optCFLAGS} -finline-functions ")
84 # Do not leak the current directory into the binaries
85 if(CMAKE_COMPILER_IS_GNUCC)
86 execute_process(COMMAND realpath --relative-to=${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}
87 RESULT_VARIABLE RESULT OUTPUT_VARIABLE RELATIVE_SOURCE_DIR ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
89 message(STATUS "Relative source directory is \"${RELATIVE_SOURCE_DIR}\".")
91 message(WARNING "Failed to find relative source directory. Using \".\".")
92 set(RELATIVE_SOURCE_DIR ".")
94 set(optCFLAGS "${optCFLAGS} -fdebug-prefix-map=${CMAKE_SOURCE_DIR}=${RELATIVE_SOURCE_DIR}")
98 # NOTE, cmake 3.0 has a INTERPROCEDURAL_OPTIMIZATION target
99 # property for this (http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html)
100 if(enable_lto) # User wants LTO. Try if we can do that
102 if(enable_compile_optimizations
103 AND CMAKE_COMPILER_IS_GNUCC
104 AND (NOT enable_model-checking))
105 # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
106 # We are experiencing assertion failures even with 4.8 on MinGW.
107 # Push the support forward: will see if 4.9 works when we test it.
109 # On Linux, we got the following with GCC 4.8.4 on Centos and Ubuntu
110 # lto1: internal compiler error: in output_die, at dwarf2out.c:8478
111 # Please submit a full bug report, with preprocessed source if appropriate.
112 # So instead, we push the support forward
114 if ( (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.5")
115 AND (LINKER_VERSION VERSION_GREATER "2.22"))
120 message(STATUS "LTO seems usable.")
122 if(NOT enable_compile_optimizations)
123 message(STATUS "LTO disabled: Compile-time optimizations turned off.")
125 if(enable_model-checking)
126 message(STATUS "LTO disabled when compiling with model-checking.")
128 message(STATUS "LTO does not seem usable -- try updating your build chain.")
133 message(STATUS "LTO disabled on the command line.")
135 if(enable_lto) # User wants LTO, and it seems usable. Go for it
136 set(optCFLAGS "${optCFLAGS} -flto ")
137 # See https://gcc.gnu.org/wiki/LinkTimeOptimizationFAQ#ar.2C_nm_and_ranlib:
138 # "Since version 4.9 gcc produces slim object files that only contain
139 # the intermediate representation. In order to handle archives of
140 # these objects you have to use the gcc wrappers:
141 # gcc-ar, gcc-nm and gcc-ranlib."
142 if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
143 AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
144 set (CMAKE_AR gcc-ar)
145 set (CMAKE_RANLIB gcc-ranlib)
149 if(enable_model-checking AND enable_compile_optimizations)
150 # Forget it, do not optimize the code (because it confuses the MC):
151 set(optCFLAGS "-O0 ")
152 # But you can still optimize this:
154 src/simix/popping.cpp src/simix/popping_generated.cpp src/simix/smx_global.cpp
155 ${SURF_SRC} ${TRACING_SRC} ${XBT_SRC}
156 ${MC_SRC_BASE} ${MC_SRC})
157 set (mcCFLAGS "-O3 -funroll-loops -fno-strict-aliasing")
158 if(CMAKE_COMPILER_IS_GNUCC)
159 set (mcCFLAGS "${mcCFLAGS} -finline-functions")
161 set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
165 if (CMAKE_C_COMPILER_ID MATCHES "Intel")
166 # honor parentheses when determining the order of expression evaluation.
167 set(optCFLAGS "${optCFLAGS} -fprotect-parens ")
171 set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
172 set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
175 set(CMAKE_C_FLAGS "${warnCFLAGS} ${CMAKE_C_FLAGS} ${optCFLAGS}")
176 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
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")
183 # Avoid a failure seen with gcc 7.2.0 and ns3 3.27
185 set_source_files_properties(src/surf/network_ns3.cpp PROPERTIES COMPILE_FLAGS " -Wno-unused-local-typedef")
190 find_program(GCOV_PATH gcov)
192 set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
193 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
194 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
195 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
196 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
197 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCOVERAGE")
198 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
199 add_definitions(-fprofile-arcs -ftest-coverage)
203 if(enable_address_sanitizer)
204 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
205 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
206 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
207 set(TESH_OPTION --enable-sanitizers)
208 try_compile(HAVE_SANITIZER_ADDRESS ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp)
209 try_compile(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp
210 COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
212 set(HAVE_SANITIZER_ADDRESS FALSE CACHE INTERNAL "")
213 set(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT FALSE CACHE INTERNAL "")
216 if(enable_thread_sanitizer)
217 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
218 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
219 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
220 set(HAVE_SANITIZER_THREAD TRUE CACHE INTERNAL "")
222 set(HAVE_SANITIZER_THREAD FALSE CACHE INTERNAL "")
225 if(enable_undefined_sanitizer)
226 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
227 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
228 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
231 if(NOT $ENV{CFLAGS} STREQUAL "")
232 message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
233 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
236 if(NOT $ENV{CXXFLAGS} STREQUAL "")
237 message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
238 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
241 if(NOT $ENV{LDFLAGS} STREQUAL "")
242 message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
243 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
247 # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
248 # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
249 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libgcc")
250 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
251 set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
252 set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
254 # JNI searches for stdcalls
255 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--add-stdcall-alias")
256 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
257 set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
258 set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
260 # Specify the data model that we are using (yeah it may help Java)
261 if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
262 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
263 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
265 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")
266 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")