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_C_COMPILER_ID MATCHES "Clang|GCC")
19 set(warnCFLAGS "${warnCFLAGS} -Wno-format-nonliteral")
21 if(CMAKE_COMPILER_IS_GNUCC)
22 set(warnCFLAGS "${warnCFLAGS} -Wclobbered -Wno-error=clobbered -Wno-unused-local-typedefs -Wno-error=attributes")
25 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")
26 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GCC")
27 set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-format-nonliteral")
29 if(CMAKE_COMPILER_IS_GNUCXX)
30 set(warnCXXFLAGS "${warnCXXFLAGS} -Wclobbered -Wno-error=clobbered -Wno-unused-local-typedefs -Wno-error=attributes")
32 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
33 # don't care about class that become struct, avoid issue of empty C structs
34 # size (coming from libunwind.h)
35 set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-mismatched-tags -Wno-extern-c-compat")
38 # the one specific to C but refused by C++
39 set(warnCFLAGS "${warnCFLAGS} -Wmissing-prototypes")
41 if(CMAKE_Fotran_COMPILER_ID MATCHES "GCC|PGI")
42 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
44 if(CMAKE_Fotran_COMPILER_ID MATCHES "Intel")
45 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -warn all")
47 set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
50 # NDEBUG gives a lot of "initialized but unused variables" errors. Don't die anyway.
51 if(enable_compile_warnings AND enable_debug)
52 set(warnCFLAGS "${warnCFLAGS} -Werror")
53 set(warnCXXFLAGS "${warnCXXFLAGS} -Werror")
56 # Activate the warnings on #if FOOBAR when FOOBAR has no value
57 # It breaks on FreeBSD within Boost headers, so activate this only in Pure Hardcore debug mode.
58 if(enable_maintainer_mode)
59 set(warnCFLAGS "${warnCFLAGS} -Wundef")
60 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
63 # Se the optimisation flags
64 # NOTE, we should CMAKE_BUILD_TYPE for this
65 if(enable_compile_optimizations)
66 set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
70 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC
71 AND (NOT enable_model-checking))
72 # This is redundant (already in -03):
73 set(optCFLAGS "${optCFLAGS} -finline-functions ")
77 # NOTE, cmake 3.0 has a INTERPROCEDURAL_OPTIMIZATION target
78 # property for this (http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html)
79 if(enable_lto) # User wants LTO. Try if we can do that
81 if(enable_compile_optimizations
82 AND CMAKE_COMPILER_IS_GNUCC
83 AND (NOT enable_model-checking))
84 # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
85 # We are experiencing assertion failures even with 4.8 on MinGW.
86 # Push the support forward: will see if 4.9 works when we test it.
88 # On Linux, we got the following with GCC 4.8.4 on Centos and Ubuntu
89 # lto1: internal compiler error: in output_die, at dwarf2out.c:8478
90 # Please submit a full bug report, with preprocessed source if appropriate.
91 # So instead, we push the support forward
93 if ( (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.5")
94 AND (LINKER_VERSION VERSION_GREATER "2.22"))
99 message("-- LTO seems usable.")
101 if(NOT enable_compile_optimizations)
102 message("-- LTO disabled: Compile-time optimizations turned off.")
104 if(enable_model-checking)
105 message("-- LTO disabled when compiling with model-checking.")
107 message("-- LTO does not seem usable -- try updating your build chain.")
112 message("-- LTO disabled on the command line.")
114 if(enable_lto) # User wants LTO, and it seems usable. Go for it
115 set(optCFLAGS "${optCFLAGS} -flto ")
116 # See https://gcc.gnu.org/wiki/LinkTimeOptimizationFAQ#ar.2C_nm_and_ranlib:
117 # "Since version 4.9 gcc produces slim object files that only contain
118 # the intermediate representation. In order to handle archives of
119 # these objects you have to use the gcc wrappers:
120 # gcc-ar, gcc-nm and gcc-ranlib."
121 if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
122 AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
123 set (CMAKE_AR gcc-ar)
124 set (CMAKE_RANLIB gcc-ranlib)
128 if(enable_model-checking AND enable_compile_optimizations)
129 # Forget it, do not optimize the code (because it confuses the MC):
130 set(optCFLAGS "-O0 ")
131 # But you can still optimize this:
134 src/xbt/log.c src/xbt/xbt_log_appender_file.c
135 src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c
136 src/xbt/dict.cpp src/xbt/dict_elm.c src/xbt/dict_cursor.c
137 src/xbt/dynar.cpp src/xbt/heap.c src/xbt/swag.c
138 src/xbt/str.c src src/xbt/snprintf.c
140 src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c
141 src/xbt/backtrace_linux.c
142 ${MC_SRC_BASE} ${MC_SRC})
143 set (mcCFLAGS "-O3 -funroll-loops -fno-strict-aliasing")
144 if(CMAKE_COMPILER_IS_GNUCC)
145 set (mcCFLAGS "${mcCFLAGS} -finline-functions")
147 set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
152 set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
153 set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
156 set(CMAKE_C_FLAGS "${warnCFLAGS} ${CMAKE_C_FLAGS} ${optCFLAGS}")
157 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
159 # Try to make Mac a bit more complient to open source standards
160 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
161 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
166 find_program(GCOV_PATH gcov)
168 set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
169 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
170 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
171 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
172 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
173 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
174 set(TESH_OPTION --enable-coverage)
175 add_definitions(-fprofile-arcs -ftest-coverage)
179 if(enable_address_sanitizer)
180 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
181 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
182 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
183 set(TESH_OPTION --enable-sanitizers)
186 if(enable_thread_sanitizer)
187 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
188 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
189 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
192 if(enable_undefined_sanitizer)
193 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
194 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
195 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
198 if(NOT $ENV{CFLAGS} STREQUAL "")
199 message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
200 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
203 if(NOT $ENV{CXXFLAGS} STREQUAL "")
204 message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
205 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
208 if(NOT $ENV{LDFLAGS} STREQUAL "")
209 message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
210 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
214 # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
215 # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
216 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libgcc")
217 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
218 set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
219 set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
221 # JNI searches for stdcalls
222 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--add-stdcall-alias")
223 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
224 set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
225 set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
227 # Specify the data model that we are using (yeah it may help Java)
228 if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
229 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
230 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
232 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")
233 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")