Logo AND Algorithmique Numérique Distribuée

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