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 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
42 set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
45 # NDEBUG gives a lot of "initialized but unused variables" errors. Don't die anyway.
46 if(enable_compile_warnings AND enable_debug)
47 set(warnCFLAGS "${warnCFLAGS} -Werror")
48 set(warnCXXFLAGS "${warnCXXFLAGS} -Werror")
51 # Activate the warnings on #if FOOBAR when FOOBAR has no value
52 # It breaks on FreeBSD within Boost headers, so activate this only in Pure Hardcore debug mode.
53 if(enable_maintainer_mode)
54 set(warnCFLAGS "${warnCFLAGS} -Wundef")
55 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
58 # Se the optimisation flags
59 # NOTE, we should CMAKE_BUILD_TYPE for this
60 if(enable_compile_optimizations)
61 set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
65 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC
66 AND (NOT enable_model-checking))
67 # This is redundant (already in -03):
68 set(optCFLAGS "${optCFLAGS} -finline-functions ")
72 # NOTE, cmake 3.0 has a INTERPROCEDURAL_OPTIMIZATION target
73 # property for this (http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html)
74 if(enable_lto) # User wants LTO. Try if we can do that
76 if(enable_compile_optimizations
77 AND CMAKE_COMPILER_IS_GNUCC
78 AND (NOT enable_model-checking))
79 # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
80 # We are experiencing assertion failures even with 4.8 on MinGW.
81 # Push the support forward: will see if 4.9 works when we test it.
83 # On Linux, we got the following with GCC 4.8.4 on Centos and Ubuntu
84 # lto1: internal compiler error: in output_die, at dwarf2out.c:8478
85 # Please submit a full bug report, with preprocessed source if appropriate.
86 # So instead, we push the support forward
88 if ( (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.5")
89 AND (LINKER_VERSION VERSION_GREATER "2.22"))
94 message("-- LTO seems usable.")
96 if(NOT enable_compile_optimizations)
97 message("-- LTO disabled: Compile-time optimizations turned off.")
99 if(enable_model-checking)
100 message("-- LTO disabled when compiling with model-checking.")
102 message("-- LTO does not seem usable -- try updating your build chain.")
107 message("-- LTO disabled on the command line.")
109 if(enable_lto) # User wants LTO, and it seems usable. Go for it
110 set(optCFLAGS "${optCFLAGS} -flto ")
111 # See https://gcc.gnu.org/wiki/LinkTimeOptimizationFAQ#ar.2C_nm_and_ranlib:
112 # "Since version 4.9 gcc produces slim object files that only contain
113 # the intermediate representation. In order to handle archives of
114 # these objects you have to use the gcc wrappers:
115 # gcc-ar, gcc-nm and gcc-ranlib."
116 if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
117 AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
118 set (CMAKE_AR gcc-ar)
119 set (CMAKE_RANLIB gcc-ranlib)
123 if(enable_model-checking AND enable_compile_optimizations)
124 # Forget it, do not optimize the code (because it confuses the MC):
125 set(optCFLAGS "-O0 ")
126 # But you can still optimize this:
129 src/xbt/log.c src/xbt/xbt_log_appender_file.c
130 src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c
131 src/xbt/dict.cpp src/xbt/dict_elm.c src/xbt/dict_cursor.c
132 src/xbt/dynar.cpp src/xbt/heap.c src/xbt/swag.c
133 src/xbt/str.c src src/xbt/snprintf.c
135 src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c
136 src/xbt/backtrace_linux.c
137 ${MC_SRC_BASE} ${MC_SRC})
138 set (mcCFLAGS "-O3 -funroll-loops -fno-strict-aliasing")
139 if(CMAKE_COMPILER_IS_GNUCC)
140 set (mcCFLAGS "${mcCFLAGS} -finline-functions")
142 set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
147 set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
148 set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
151 set(CMAKE_C_FLAGS "${warnCFLAGS} ${CMAKE_C_FLAGS} ${optCFLAGS}")
152 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
154 # Try to make Mac a bit more complient to open source standards
155 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
156 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
161 find_program(GCOV_PATH gcov)
163 set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
164 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
165 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
166 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
167 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
168 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
169 set(TESH_OPTION --enable-coverage)
170 add_definitions(-fprofile-arcs -ftest-coverage)
174 if(enable_address_sanitizer)
175 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
176 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
177 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
178 set(TESH_OPTION --enable-sanitizers)
181 if(enable_thread_sanitizer)
182 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
183 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
184 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
187 if(enable_undefined_sanitizer)
188 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
189 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
190 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
193 if(NOT $ENV{CFLAGS} STREQUAL "")
194 message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
195 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
198 if(NOT $ENV{CXXFLAGS} STREQUAL "")
199 message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
200 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
203 if(NOT $ENV{LDFLAGS} STREQUAL "")
204 message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
205 set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
209 # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
210 # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
211 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libgcc")
212 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
213 set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
214 set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
216 # JNI searches for stdcalls
217 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--add-stdcall-alias")
218 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
219 set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
220 set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
222 # Specify the data model that we are using (yeah it may help Java)
223 if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
224 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
225 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
227 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")
228 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")