Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ignore new deprecation warning on freebsd
[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 -Wwrite-strings -Wno-unused-function -Wno-unused-parameter -Wno-strict-aliasing")
18   if(CMAKE_COMPILER_IS_GNUCC)
19     set(warnCFLAGS "${warnCFLAGS} -Wclobbered -Wformat-signedness -Wno-error=clobbered -Wno-unused-local-typedefs -Wno-error=attributes -Wno-error=maybe-uninitialized")
20   endif()
21   if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
22     # ignore remarks:
23     # 191: type qualifier is meaningless on cast type
24     # 1418: external function definition with no prior declaration
25     # 2196: routine is both "inline" and "noinline"
26     # 2651: attribute does not apply to any entity
27     # 3179: deprecated conversion of string literal to char* (should be const char*)
28     # set as warning:
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=191,1418,2196,2651,3179 -diag-warning=597,2330,11003")
33   endif()
34
35   set(warnCXXFLAGS "${warnCFLAGS} -Wall -Wextra -Wunused -Wmissing-declarations -Wpointer-arith -Wwrite-strings -Wno-unused-function -Wno-unused-parameter -Wno-strict-aliasing")
36   if(CMAKE_COMPILER_IS_GNUCXX)
37     set(warnCXXFLAGS "${warnCXXFLAGS} -Wclobbered -Wformat-signedness -Wno-error=clobbered -Wno-free-nonheap-object -Wno-unused-local-typedefs -Wno-error=attributes -Wno-error=maybe-uninitialized")
38   endif()
39   if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "8.0")
40     # workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81767
41     set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-error=unused-variable")
42   endif()
43   if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
44     # don't care about class that become struct, avoid issue of empty C structs
45     # size (coming from libunwind.h)
46     # also ignore deprecated builtins (seen with clang 15 + boost 1.79)
47     set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-mismatched-tags -Wno-extern-c-compat -Wno-deprecated-builtins")
48   endif()
49
50   # the one specific to C but refused by C++
51   set(warnCFLAGS "${warnCFLAGS} -Wmissing-prototypes")
52
53   if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC|PGI")
54     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
55   elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Flang")
56     # flang >= 7 has a bug with common and debug flags. Ignore cmake-added -g in this case.
57     # https://github.com/flang-compiler/flang/issues/671
58     set(CMAKE_Fortran_FLAGS "-Wall")
59   elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
60     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -warn all")
61   endif()
62   set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
63 endif()
64
65 # NDEBUG gives a lot of "initialized but unused variables" errors. Don't die anyway.
66 if(enable_compile_warnings AND enable_debug)
67   set(warnCFLAGS "${warnCFLAGS} -Werror")
68   set(warnCXXFLAGS "${warnCXXFLAGS} -Werror")
69   if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC")
70     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Werror -Werror=format-security")
71   endif()
72 endif()
73
74 # Activate the warnings on #if FOOBAR when FOOBAR has no value
75 # It breaks on FreeBSD within Boost headers, so activate this only in Pure Hardcore debug mode.
76 if(enable_maintainer_mode)
77   set(warnCFLAGS "${warnCFLAGS} -Wundef")
78   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
79 endif()
80
81 # Se the optimisation flags
82 # NOTE, we should CMAKE_BUILD_TYPE for this
83 if(enable_compile_optimizations)
84   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
85 else()
86   set(optCFLAGS "-O0 ")
87 endif()
88
89 #ARM platforms have signed char by default, switch to unsigned for consistancy
90 if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
91   set(optCFLAGS "${optCFLAGS} -fsigned-char")
92 endif()
93
94 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC
95     AND (NOT enable_model-checking))
96   # This is redundant (already in -03):
97   set(optCFLAGS "${optCFLAGS} -finline-functions ")
98 endif()
99
100 # Do not leak the current directory into the binaries
101 if(CMAKE_COMPILER_IS_GNUCC)
102   execute_process(COMMAND realpath --relative-to=${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}
103     RESULT_VARIABLE RESULT OUTPUT_VARIABLE RELATIVE_SOURCE_DIR ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
104   if(RESULT EQUAL 0)
105     message(STATUS "Relative source directory is \"${RELATIVE_SOURCE_DIR}\".")
106   else()
107     message(WARNING "Failed to find relative source directory. Using \".\".")
108     set(RELATIVE_SOURCE_DIR ".")
109   endif()
110   if (CMAKE_C_COMPILER_VERSION VERSION_LESS "8.0")
111     set(optCFLAGS "${optCFLAGS} -fdebug-prefix-map=\"${CMAKE_SOURCE_DIR}=${RELATIVE_SOURCE_DIR}\"")
112   else()
113     set(optCFLAGS "${optCFLAGS} -ffile-prefix-map=\"${CMAKE_SOURCE_DIR}=${RELATIVE_SOURCE_DIR}\"")
114   endif()
115 endif()
116
117 # Configure LTO
118 if(enable_lto) # User wants LTO. Try if we can do that
119   set(enable_lto OFF)
120   if(enable_compile_optimizations
121       AND (NOT enable_model-checking))
122     if(CMAKE_VERSION VERSION_LESS "3.9")
123       if ( CMAKE_COMPILER_IS_GNUCC
124          AND (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.5")
125          AND (LINKER_VERSION VERSION_GREATER "2.22"))
126         set(enable_lto ON)
127       endif()
128     else()
129       include(CheckIPOSupported)
130       check_ipo_supported(RESULT ipo LANGUAGES C CXX)
131       if(ipo)
132         set(enable_lto ON)
133       endif()
134     endif()
135   endif()
136
137   if(enable_lto)
138     message(STATUS "LTO seems usable.")
139   else()
140     if(NOT enable_compile_optimizations)
141       message(STATUS "LTO disabled: Compile-time optimizations turned off.")
142     else()
143       if(enable_model-checking)
144         message(STATUS "LTO disabled when compiling with model-checking.")
145       else()
146         message(STATUS "LTO does not seem usable -- try updating your build chain.")
147       endif()
148     endif()
149   endif()
150 else()
151   message(STATUS "LTO disabled on the command line.")
152 endif()
153 if(enable_lto) # User wants LTO, and it seems usable. Go for it
154   set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
155   if(LTO_EXTRA_FLAG AND CMAKE_COMPILER_IS_GNUCC)
156     list(APPEND CMAKE_C_COMPILE_OPTIONS_IPO "-flto=${LTO_EXTRA_FLAG}")
157     list(APPEND CMAKE_CXX_COMPILE_OPTIONS_IPO "-flto=${LTO_EXTRA_FLAG}")
158   endif()
159
160   # Activate fat-lto-objects in case LD and gfortran differ too much.
161   # Only test with GNU as it's the only case I know (clang+gfortran+lld)
162   execute_process(COMMAND ${CMAKE_LINKER} -v OUTPUT_VARIABLE LINKER_ID ERROR_VARIABLE LINKER_ID)
163   string(REGEX MATCH "GNU" LINKER_ID "${LINKER_ID}")
164   if(${CMAKE_Fortran_COMPILER_ID} MATCHES "GNU"
165      AND NOT "${LINKER_ID}" MATCHES "GNU")
166        list(APPEND CMAKE_Fortran_COMPILE_OPTIONS_IPO "-ffat-lto-objects")
167   endif()
168
169   # "Since version 4.9 gcc produces slim object files that only contain
170   # the intermediate representation. In order to handle archives of
171   # these objects you have to use the gcc wrappers:
172   # gcc-ar, gcc-nm and gcc-ranlib."
173   if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
174       AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
175     set (CMAKE_AR gcc-ar)
176     set (CMAKE_RANLIB gcc-ranlib)
177   endif()
178 endif()
179
180 if(enable_model-checking AND enable_compile_optimizations)
181   # Forget it, do not optimize the code (because it confuses the MC):
182   set(optCFLAGS "-O0")
183   # But you can still optimize this:
184   set(src_list ${simgrid_sources})
185   # except...
186   list(REMOVE_ITEM src_list ${SIMIX_SRC} ${S4U_SRC})
187   # but...
188   list(APPEND src_list
189     src/kernel/actor/Simcall.cpp)
190   foreach(src ${src_list})
191       set (mcCFLAGS "-O3 -funroll-loops -fno-strict-aliasing")
192       if(CMAKE_COMPILER_IS_GNUCC)
193         set (mcCFLAGS "${mcCFLAGS} -finline-functions")
194       endif()
195       set_source_files_properties(${src} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
196   endforeach()
197 endif()
198
199 if (CMAKE_C_COMPILER_ID MATCHES "Intel")
200   # honor parentheses when determining the order of expression evaluation.
201   # disallow optimizations for floating-point arithmetic with Nans or +-Infs (breaks Eigen3)
202   set(optCFLAGS "${optCFLAGS} -fprotect-parens -fno-finite-math-only")
203 endif()
204
205 if(NOT enable_debug)
206   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
207   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
208 endif()
209
210 set(CMAKE_C_FLAGS   "${warnCFLAGS} ${CMAKE_C_FLAGS}   ${optCFLAGS}")
211 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
212
213 # Try to make Mac a bit more compliant to open source standards
214 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
215   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
216 endif()
217
218 # Avoid a failure seen with gcc 7.2.0 and ns3 3.27
219 if(enable_ns3)
220   set_source_files_properties(src/surf/network_ns3.cpp PROPERTIES COMPILE_FLAGS " -Wno-unused-local-typedef")
221 endif()
222
223 set(TESH_OPTION "")
224 if(enable_coverage)
225   find_program(GCOV_PATH NAMES ENV{GCOV} gcov)
226   if(GCOV_PATH)
227     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE FILEPATH "Coverage testing tool (gcov)" FORCE)
228     set(COVERAGE_BUILD_FLAGS "-fprofile-arcs -ftest-coverage -fprofile-abs-path")
229     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE ${COVERAGE_BUILD_FLAGS}")
230     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${COVERAGE_BUILD_FLAGS}")
231     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${COVERAGE_BUILD_FLAGS}")
232     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCOVERAGE ${COVERAGE_BUILD_FLAGS}")
233     add_definitions(${COVERAGE_BUILD_FLAGS})
234   endif()
235 endif()
236
237 if(enable_address_sanitizer)
238     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
239     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
240     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
241     set(TESH_OPTION --enable-sanitizers)
242     try_compile(HAVE_SANITIZER_ADDRESS ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp)
243     try_compile(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp
244       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
245 else()
246     set(HAVE_SANITIZER_ADDRESS FALSE CACHE INTERNAL "")
247     set(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT FALSE CACHE INTERNAL "")
248 endif()
249
250 if(enable_thread_sanitizer)
251     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
252     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
253     if(CMAKE_COMPILER_IS_GNUCXX AND (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0")))
254         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=tsan")
255     endif()
256     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
257     try_compile(HAVE_SANITIZER_THREAD ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_tsan.cpp)
258     try_compile(HAVE_SANITIZER_THREAD_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_tsan.cpp
259       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
260 else()
261     set(HAVE_SANITIZER_THREAD FALSE CACHE INTERNAL "")
262     set(HAVE_SANITIZER_THREAD_FIBER_SUPPORT FALSE CACHE INTERNAL "")
263 endif()
264
265 if(enable_undefined_sanitizer)
266     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
267     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
268     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
269 endif()
270
271 if(NOT $ENV{CFLAGS} STREQUAL "")
272   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
273   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
274 endif()
275
276 if(NOT $ENV{CXXFLAGS} STREQUAL "")
277   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
278   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
279 endif()
280
281 if(NOT $ENV{LDFLAGS} STREQUAL "")
282   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
283   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
284 endif()
285
286 if(MINGW)
287   # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
288   # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
289   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -static-libgcc")
290   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
291   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS   "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
292   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
293
294   # JNI searches for stdcalls
295   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -Wl,--add-stdcall-alias")
296   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
297   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
298   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
299
300   # Specify the data model that we are using (yeah it may help Java)
301   if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
302     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m32")
303     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
304   else()
305     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m64")
306     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
307   endif()
308 endif()