Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b6e62226781d60c3f21c30f77022d91c0e6c24b0
[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, clang and Intel compilers.
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-local-typedefs -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-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     if (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL "13.2.0")
45       # workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101361
46       set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-error=stringop-overread")
47     endif()
48   endif()
49
50   if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
51     # don't care about class that become struct, avoid issue of empty C structs
52     # size (coming from libunwind.h)
53     set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-mismatched-tags -Wno-extern-c-compat")
54     # also ignore deprecated builtins (seen with clang 15 + boost 1.79)
55     if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "15.0")
56       set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-deprecated-builtins")
57     endif()
58   endif()
59
60   # the one specific to C but refused by C++
61   set(warnCFLAGS "${warnCFLAGS} -Wmissing-prototypes")
62
63   if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC|PGI")
64     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
65   elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Flang")
66     # flang >= 7 has a bug with common and debug flags. Ignore cmake-added -g in this case.
67     # https://github.com/flang-compiler/flang/issues/671
68     set(CMAKE_Fortran_FLAGS "-Wall")
69   elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
70     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -warn all")
71   endif()
72 endif()
73
74 # NDEBUG gives a lot of "initialized but unused variables" errors. Don't die anyway.
75 if(enable_compile_warnings AND enable_debug)
76   set(warnCFLAGS "${warnCFLAGS} -Werror")
77   set(warnCXXFLAGS "${warnCXXFLAGS} -Werror")
78   if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC")
79     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Werror -Werror=format-security")
80   endif()
81 endif()
82
83 # Activate the warnings on #if FOOBAR when FOOBAR has no value
84 if("${CMAKE_SYSTEM}" MATCHES "Linux") # Breaks on FreeBSD within Boost headers :(
85   set(warnCFLAGS "${warnCFLAGS} -Wundef")
86   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
87 endif()
88
89 # Set the optimisation flags
90 # NOTE, we should CMAKE_BUILD_TYPE for this
91 if(enable_compile_optimizations)
92   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
93 else()
94   set(optCFLAGS "-O0 ")
95 endif()
96
97 # ARM platforms have signed char by default, switch to unsigned for consistancy
98 if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
99   set(optCFLAGS "${optCFLAGS} -fsigned-char")
100 endif()
101
102 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC)
103   # This is redundant (already in -03):
104   set(optCFLAGS "${optCFLAGS} -finline-functions ")
105 endif()
106
107 # Do not leak the current directory into the binaries
108 if(CMAKE_COMPILER_IS_GNUCC AND NOT enable_coverage)
109   if (CMAKE_VERSION VERSION_LESS "3.20")
110     file(RELATIVE_PATH RELATIVE_SOURCE_DIR ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR})
111   else() # cmake >= 3.20
112     cmake_path(RELATIVE_PATH CMAKE_SOURCE_DIR BASE_DIRECTORY ${CMAKE_BINARY_DIR} OUTPUT_VARIABLE RELATIVE_SOURCE_DIR)
113   endif()
114   message(STATUS "Relative source directory is \"${RELATIVE_SOURCE_DIR}\".")
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     include(CheckIPOSupported)
127     check_ipo_supported(RESULT ipo LANGUAGES C CXX)
128     if(ipo)
129       set(enable_lto ON)
130     endif()
131   endif()
132
133   if(enable_lto)
134     message(STATUS "LTO seems usable.")
135   else()
136     if(NOT enable_compile_optimizations)
137       message(STATUS "LTO disabled: Compile-time optimizations turned off.")
138     else()
139       message(STATUS "LTO does not seem usable -- try updating your build chain.")
140     endif()
141   endif()
142 else()
143   message(STATUS "LTO disabled on the command line.")
144 endif()
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}")
150   endif()
151
152   # Activate fat-lto-objects in case LD and gfortran differ too much.
153   # Only test with GNU as it's the only case I know (clang+gfortran+lld)
154   execute_process(COMMAND ${CMAKE_LINKER} -v OUTPUT_VARIABLE LINKER_ID ERROR_VARIABLE LINKER_ID)
155   string(REGEX MATCH "GNU" LINKER_ID "${LINKER_ID}")
156   if(${CMAKE_Fortran_COMPILER_ID} MATCHES "GNU"
157      AND NOT "${LINKER_ID}" MATCHES "GNU")
158        list(APPEND CMAKE_Fortran_COMPILE_OPTIONS_IPO "-ffat-lto-objects")
159   endif()
160
161   # "Since version 4.9 gcc produces slim object files that only contain
162   # the intermediate representation. In order to handle archives of
163   # these objects you have to use the gcc wrappers:
164   # gcc-ar, gcc-nm and gcc-ranlib."
165   if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
166       AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
167     set (CMAKE_AR gcc-ar)
168     set (CMAKE_RANLIB gcc-ranlib)
169   endif()
170 endif()
171
172 if (CMAKE_C_COMPILER_ID MATCHES "Intel")
173   # honor parentheses when determining the order of expression evaluation.
174   # disallow optimizations for floating-point arithmetic with Nans or +-Infs (breaks Eigen3)
175   set(optCFLAGS "${optCFLAGS} -fprotect-parens -fno-finite-math-only")
176 endif()
177
178 if(NOT enable_debug)
179   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
180   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
181 endif()
182
183 set(CMAKE_C_FLAGS   "${warnCFLAGS} ${CMAKE_C_FLAGS}   ${optCFLAGS}")
184 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
185
186 # Try to make Mac a bit more compliant to open source standards
187 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
188   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
189 endif()
190
191 set(TESH_OPTION "")
192 if(enable_coverage)
193   find_program(GCOV_PATH NAMES ENV{GCOV} gcov)
194   if(GCOV_PATH)
195     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE FILEPATH "Coverage testing tool (gcov)" FORCE)
196     set(COVERAGE_BUILD_FLAGS "-fprofile-arcs -ftest-coverage -fprofile-abs-path")
197     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE ${COVERAGE_BUILD_FLAGS}")
198     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${COVERAGE_BUILD_FLAGS}")
199     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${COVERAGE_BUILD_FLAGS}")
200     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCOVERAGE ${COVERAGE_BUILD_FLAGS}")
201     add_definitions(${COVERAGE_BUILD_FLAGS})
202   endif()
203 endif()
204
205 if(enable_address_sanitizer)
206     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
207     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
208     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
209     set(TESH_OPTION --enable-sanitizers)
210     try_compile(HAVE_SANITIZER_ADDRESS ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp)
211     try_compile(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp
212       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
213 else()
214     set(HAVE_SANITIZER_ADDRESS FALSE CACHE INTERNAL "")
215     set(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT FALSE CACHE INTERNAL "")
216 endif()
217
218 if(enable_thread_sanitizer)
219     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
220     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
221     if(CMAKE_COMPILER_IS_GNUCXX AND (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0")))
222         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=tsan")
223     endif()
224     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
225     try_compile(HAVE_SANITIZER_THREAD ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_tsan.cpp)
226     try_compile(HAVE_SANITIZER_THREAD_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_tsan.cpp
227       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
228 else()
229     set(HAVE_SANITIZER_THREAD FALSE CACHE INTERNAL "")
230     set(HAVE_SANITIZER_THREAD_FIBER_SUPPORT FALSE CACHE INTERNAL "")
231 endif()
232
233 if(enable_undefined_sanitizer)
234     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
235     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
236     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
237 endif()
238
239 if(NOT $ENV{CFLAGS} STREQUAL "")
240   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
241   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
242 endif()
243
244 if(NOT $ENV{CXXFLAGS} STREQUAL "")
245   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
246   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
247 endif()
248
249 if(NOT $ENV{LDFLAGS} STREQUAL "")
250   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
251   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
252 endif()