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 # 2196: routine is both "inline" and "noinline"
27 # 3179: deprecated conversion of string literal to char* (should be const char*)
28 # 191: type qualifier is meaningless on cast type
29 # 597: entity-kind "entity" will not be called for implicit or explicit conversions
30 # 2330: argument of type "type" is incompatible with parameter of type "type" (dropping qualifiers)
31 # 11003: no IR in object file xxxx; was the source file compiled with xxxx
32 set(warnCFLAGS "${warnCFLAGS} -diag-disable=1418,191,2196,3179 -diag-warning=2330,597,11003")
35 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")
36 if(CMAKE_COMPILER_IS_GNUCXX AND (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0")))
37 set(warnCFLAGS "${warnCFLAGS} -Wformat-signedness")
39 if(CMAKE_COMPILER_IS_GNUCXX)
40 set(warnCXXFLAGS "${warnCXXFLAGS} -Wclobbered -Wno-error=clobbered -Wno-unused-local-typedefs -Wno-error=attributes -Wno-error=maybe-uninitialized")
42 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
43 # don't care about class that become struct, avoid issue of empty C structs
44 # size (coming from libunwind.h)
45 set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-mismatched-tags -Wno-extern-c-compat")
48 # the one specific to C but refused by C++
49 set(warnCFLAGS "${warnCFLAGS} -Wmissing-prototypes")
51 if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC|PGI")
52 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
53 elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Flang")
54 # flang >= 7 has a bug with common and debug flags. Ignore cmake-added -g in this case.
55 # https://github.com/flang-compiler/flang/issues/671
56 set(CMAKE_Fortran_FLAGS "-Wall")
57 elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
58 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -warn all")
60 set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
63 # NDEBUG gives a lot of "initialized but unused variables" errors. Don't die anyway.
64 if(enable_compile_warnings AND enable_debug)
65 set(warnCFLAGS "${warnCFLAGS} -Werror")
66 set(warnCXXFLAGS "${warnCXXFLAGS} -Werror")
67 if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC")
68 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Werror -Werror=format-security")
72 # Activate the warnings on #if FOOBAR when FOOBAR has no value
73 # It breaks on FreeBSD within Boost headers, so activate this only in Pure Hardcore debug mode.
74 if(enable_maintainer_mode)
75 set(warnCFLAGS "${warnCFLAGS} -Wundef")
76 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
79 # Se the optimisation flags
80 # NOTE, we should CMAKE_BUILD_TYPE for this
81 if(enable_compile_optimizations)
82 set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
86 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC
87 AND (NOT enable_model-checking))
88 # This is redundant (already in -03):
89 set(optCFLAGS "${optCFLAGS} -finline-functions ")
92 # Do not leak the current directory into the binaries
93 if(CMAKE_COMPILER_IS_GNUCC)
94 execute_process(COMMAND realpath --relative-to=${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}
95 RESULT_VARIABLE RESULT OUTPUT_VARIABLE RELATIVE_SOURCE_DIR ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
97 message(STATUS "Relative source directory is \"${RELATIVE_SOURCE_DIR}\".")
99 message(WARNING "Failed to find relative source directory. Using \".\".")
100 set(RELATIVE_SOURCE_DIR ".")
102 if (CMAKE_C_COMPILER_VERSION VERSION_LESS "8.0")
103 set(optCFLAGS "${optCFLAGS} -fdebug-prefix-map=${CMAKE_SOURCE_DIR}=${RELATIVE_SOURCE_DIR}")
105 set(optCFLAGS "${optCFLAGS} -ffile-prefix-map=${CMAKE_SOURCE_DIR}=${RELATIVE_SOURCE_DIR}")
110 if(enable_lto) # User wants LTO. Try if we can do that
112 if(enable_compile_optimizations
113 AND (NOT enable_model-checking))
114 if(CMAKE_VERSION VERSION_LESS "3.9")
115 if ( CMAKE_COMPILER_IS_GNUCC
116 AND (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.5")
117 AND (LINKER_VERSION VERSION_GREATER "2.22"))
121 include(CheckIPOSupported)
122 check_ipo_supported(RESULT ipo)
130 message(STATUS "LTO seems usable.")
132 if(NOT enable_compile_optimizations)
133 message(STATUS "LTO disabled: Compile-time optimizations turned off.")
135 if(enable_model-checking)
136 message(STATUS "LTO disabled when compiling with model-checking.")
138 message(STATUS "LTO does not seem usable -- try updating your build chain.")
143 message(STATUS "LTO disabled on the command line.")
145 if(enable_lto) # User wants LTO, and it seems usable. Go for it
146 set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
147 if(LTO_EXTRA_FLAG AND CMAKE_COMPILER_IS_GNUCC)
148 list(APPEND CMAKE_C_COMPILE_OPTIONS_IPO "-flto=${LTO_EXTRA_FLAG}")
149 list(APPEND CMAKE_CXX_COMPILE_OPTIONS_IPO "-flto=${LTO_EXTRA_FLAG}")
151 # "Since version 4.9 gcc produces slim object files that only contain
152 # the intermediate representation. In order to handle archives of
153 # these objects you have to use the gcc wrappers:
154 # gcc-ar, gcc-nm and gcc-ranlib."
155 if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
156 AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
157 set (CMAKE_AR gcc-ar)
158 set (CMAKE_RANLIB gcc-ranlib)
162 if(enable_model-checking AND enable_compile_optimizations)
163 # Forget it, do not optimize the code (because it confuses the MC):
164 set(optCFLAGS "-O0 ")
165 # But you can still optimize this:
167 src/simix/popping.cpp src/simix/popping_generated.cpp src/simix/smx_global.cpp
168 ${SURF_SRC} ${TRACING_SRC} ${XBT_SRC}
169 ${MC_SRC_BASE} ${MC_SRC})
170 set (mcCFLAGS "-O3 -funroll-loops -fno-strict-aliasing")
171 if(CMAKE_COMPILER_IS_GNUCC)
172 set (mcCFLAGS "${mcCFLAGS} -finline-functions")
174 set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
178 if (CMAKE_C_COMPILER_ID MATCHES "Intel")
179 # honor parentheses when determining the order of expression evaluation.
180 set(optCFLAGS "${optCFLAGS} -fprotect-parens ")
184 set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
185 set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
188 set(CMAKE_C_FLAGS "${warnCFLAGS} ${CMAKE_C_FLAGS} ${optCFLAGS}")
189 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
191 # Try to make Mac a bit more complient to open source standards
192 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
193 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
196 # Avoid a failure seen with gcc 7.2.0 and ns3 3.27
198 set_source_files_properties(src/surf/network_ns3.cpp PROPERTIES COMPILE_FLAGS " -Wno-unused-local-typedef")
203 find_program(GCOV_PATH gcov)
205 set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
206 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
207 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
208 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
209 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
210 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCOVERAGE")
211 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
212 add_definitions(-fprofile-arcs -ftest-coverage)
216 if(enable_address_sanitizer)
217 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
218 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
219 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
220 set(TESH_OPTION --enable-sanitizers)
221 try_compile(HAVE_SANITIZER_ADDRESS ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp)
222 try_compile(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp
223 COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
225 set(HAVE_SANITIZER_ADDRESS FALSE CACHE INTERNAL "")
226 set(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT FALSE CACHE INTERNAL "")
229 if(enable_thread_sanitizer)
230 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
231 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
232 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
233 try_compile(HAVE_SANITIZER_THREAD ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_tsan.cpp)
234 try_compile(HAVE_SANITIZER_THREAD_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_tsan.cpp
235 COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
237 set(HAVE_SANITIZER_THREAD FALSE CACHE INTERNAL "")
238 set(HAVE_SANITIZER_THREAD_FIBER_SUPPORT FALSE CACHE INTERNAL "")
241 if(enable_undefined_sanitizer)
242 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
243 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
244 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
247 if(NOT $ENV{CFLAGS} STREQUAL "")
248 message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
249 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
252 if(NOT $ENV{CXXFLAGS} STREQUAL "")
253 message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
254 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
257 if(NOT $ENV{LDFLAGS} STREQUAL "")
258 message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
259 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
263 # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
264 # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
265 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libgcc")
266 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
267 set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
268 set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
270 # JNI searches for stdcalls
271 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--add-stdcall-alias")
272 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
273 set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
274 set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
276 # Specify the data model that we are using (yeah it may help Java)
277 if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
278 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
279 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
281 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")
282 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")