Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid looping over single, constant value (codefactor.io/ShellCheck).
[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 -Wchar-subscripts -Wcomment -Wformat -Wwrite-strings -Wno-unused-function -Wno-unused-parameter -Wno-strict-aliasing")
18   if(CMAKE_COMPILER_IS_GNUCC AND (NOT (CMAKE_C_COMPILER_VERSION VERSION_LESS "5.0")))
19     set(warnCFLAGS "${warnCFLAGS} -Wformat-signedness")
20   endif()
21   if(CMAKE_COMPILER_IS_GNUCC)
22     set(warnCFLAGS "${warnCFLAGS} -Wclobbered -Wno-error=clobbered  -Wno-unused-local-typedefs -Wno-error=attributes -Wno-error=maybe-uninitialized")
23   endif()
24   if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
25     # ignore remark  #1418: external function definition with no prior declaration
26     # 2196: routine is both "inline" and "noinline"
27     # 3179: deprecated conversion of string literal to char* (should be const char*)
28     # 191: type qualifier is meaningless on cast type
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     set(warnCFLAGS "${warnCFLAGS} -wd1418 -wd191 -wd2196 -wd3179 -ww597 -ww2330")
32   endif()
33
34   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")
35   if(CMAKE_COMPILER_IS_GNUCXX AND (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0")))
36     set(warnCFLAGS "${warnCFLAGS} -Wformat-signedness")
37   endif()
38   if(CMAKE_COMPILER_IS_GNUCXX)
39     set(warnCXXFLAGS "${warnCXXFLAGS} -Wclobbered -Wno-error=clobbered  -Wno-unused-local-typedefs -Wno-error=attributes -Wno-error=maybe-uninitialized")
40   endif()
41   if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
42     # don't care about class that become struct, avoid issue of empty C structs
43     # size (coming from libunwind.h)
44     set(warnCXXFLAGS "${warnCXXFLAGS} -Wno-mismatched-tags -Wno-extern-c-compat")
45   endif()
46
47   # the one specific to C but refused by C++
48   set(warnCFLAGS "${warnCFLAGS} -Wmissing-prototypes")
49
50   if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC|PGI")
51     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
52   elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Flang")
53     # flang >= 7 has a bug with common and debug flags. Ignore cmake-added -g in this case.
54     # https://github.com/flang-compiler/flang/issues/671
55     set(CMAKE_Fortran_FLAGS "-Wall")
56   elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
57     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -warn all")
58   endif()
59   set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
60 endif()
61
62 # NDEBUG gives a lot of "initialized but unused variables" errors. Don't die anyway.
63 if(enable_compile_warnings AND enable_debug)
64   set(warnCFLAGS "${warnCFLAGS} -Werror")
65   set(warnCXXFLAGS "${warnCXXFLAGS} -Werror")
66   if(CMAKE_Fortran_COMPILER_ID MATCHES "GCC")
67     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Werror -Werror=format-security")
68   endif()
69 endif()
70
71 # Activate the warnings on #if FOOBAR when FOOBAR has no value
72 # It breaks on FreeBSD within Boost headers, so activate this only in Pure Hardcore debug mode.
73 if(enable_maintainer_mode)
74   set(warnCFLAGS "${warnCFLAGS} -Wundef")
75   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
76 endif()
77
78 # Se the optimisation flags
79 # NOTE, we should CMAKE_BUILD_TYPE for this
80 if(enable_compile_optimizations)
81   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
82 else()
83   set(optCFLAGS "-O0 ")
84 endif()
85 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC
86     AND (NOT enable_model-checking))
87   # This is redundant (already in -03):
88   set(optCFLAGS "${optCFLAGS} -finline-functions ")
89 endif()
90
91 # Do not leak the current directory into the binaries
92 if(CMAKE_COMPILER_IS_GNUCC)
93   execute_process(COMMAND realpath --relative-to=${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}
94     RESULT_VARIABLE RESULT OUTPUT_VARIABLE RELATIVE_SOURCE_DIR ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
95   if(RESULT EQUAL 0)
96     message(STATUS "Relative source directory is \"${RELATIVE_SOURCE_DIR}\".")
97   else()
98     message(WARNING "Failed to find relative source directory. Using \".\".")
99     set(RELATIVE_SOURCE_DIR ".")
100   endif()
101   set(optCFLAGS "${optCFLAGS} -fdebug-prefix-map=${CMAKE_SOURCE_DIR}=${RELATIVE_SOURCE_DIR}")
102 endif()
103
104 # Configure LTO
105 # NOTE, cmake 3.0 has a INTERPROCEDURAL_OPTIMIZATION target
106 #       property for this (http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html)
107 if(enable_lto) # User wants LTO. Try if we can do that
108   set(enable_lto OFF)
109   if(enable_compile_optimizations
110       AND CMAKE_COMPILER_IS_GNUCC
111       AND (NOT enable_model-checking))
112     # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
113     #   We are experiencing assertion failures even with 4.8 on MinGW.
114     #   Push the support forward: will see if 4.9 works when we test it.
115     #
116     # On Linux, we got the following with GCC 4.8.4 on Centos and Ubuntu
117     #    lto1: internal compiler error: in output_die, at dwarf2out.c:8478
118     #    Please submit a full bug report, with preprocessed source if appropriate.
119     # So instead, we push the support forward
120
121     if ( (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.5")
122          AND (LINKER_VERSION VERSION_GREATER "2.22"))
123       set(enable_lto ON)
124     endif()
125   endif()
126   if(enable_lto)
127     message(STATUS "LTO seems usable.")
128   else()
129     if(NOT enable_compile_optimizations)
130       message(STATUS "LTO disabled: Compile-time optimizations turned off.")
131     else()
132       if(enable_model-checking)
133         message(STATUS "LTO disabled when compiling with model-checking.")
134       else()
135         message(STATUS "LTO does not seem usable -- try updating your build chain.")
136       endif()
137     endif()
138   endif()
139 else()
140   message(STATUS "LTO disabled on the command line.")
141 endif()
142 if(enable_lto) # User wants LTO, and it seems usable. Go for it
143   set(optCFLAGS "${optCFLAGS} -flto ")
144   # See https://gcc.gnu.org/wiki/LinkTimeOptimizationFAQ#ar.2C_nm_and_ranlib:
145   # "Since version 4.9 gcc produces slim object files that only contain
146   # the intermediate representation. In order to handle archives of
147   # these objects you have to use the gcc wrappers:
148   # gcc-ar, gcc-nm and gcc-ranlib."
149   if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
150       AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
151     set (CMAKE_AR gcc-ar)
152     set (CMAKE_RANLIB gcc-ranlib)
153   endif()
154 endif()
155
156 if(enable_model-checking AND enable_compile_optimizations)
157   # Forget it, do not optimize the code (because it confuses the MC):
158   set(optCFLAGS "-O0 ")
159   # But you can still optimize this:
160   foreach(s
161       src/simix/popping.cpp src/simix/popping_generated.cpp src/simix/smx_global.cpp
162       ${SURF_SRC} ${TRACING_SRC} ${XBT_SRC}
163       ${MC_SRC_BASE} ${MC_SRC})
164       set (mcCFLAGS "-O3  -funroll-loops -fno-strict-aliasing")
165        if(CMAKE_COMPILER_IS_GNUCC)
166          set (mcCFLAGS "${mcCFLAGS} -finline-functions")
167       endif()
168       set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
169   endforeach()
170 endif()
171
172 if (CMAKE_C_COMPILER_ID MATCHES "Intel")
173   # honor parentheses when determining the order of expression evaluation.
174   set(optCFLAGS "${optCFLAGS} -fprotect-parens ")
175 endif()
176
177 if(NOT enable_debug)
178   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
179   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
180 endif()
181
182 set(CMAKE_C_FLAGS   "${warnCFLAGS} ${CMAKE_C_FLAGS}   ${optCFLAGS}")
183 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
184
185 # Try to make Mac a bit more complient to open source standards
186 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
187   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
188 endif()
189
190 # Avoid a failure seen with gcc 7.2.0 and ns3 3.27
191 if(enable_ns3)
192   set_source_files_properties(src/surf/network_ns3.cpp PROPERTIES COMPILE_FLAGS " -Wno-unused-local-typedef")
193 endif()
194
195 set(TESH_OPTION "")
196 if(enable_coverage)
197   find_program(GCOV_PATH gcov)
198   if(GCOV_PATH)
199     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
200     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
201     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
202     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
203     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
204     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCOVERAGE")
205     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
206     add_definitions(-fprofile-arcs -ftest-coverage)
207   endif()
208 endif()
209
210 if(enable_address_sanitizer)
211     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
212     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
213     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
214     set(TESH_OPTION --enable-sanitizers)
215     try_compile(HAVE_SANITIZER_ADDRESS ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp)
216     try_compile(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_asan.cpp
217       COMPILE_DEFINITIONS -DCHECK_FIBER_SUPPORT)
218 else()
219     set(HAVE_SANITIZER_ADDRESS FALSE CACHE INTERNAL "")
220     set(HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT FALSE CACHE INTERNAL "")
221 endif()
222
223 if(enable_thread_sanitizer)
224     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
225     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
226     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
227     set(HAVE_SANITIZER_THREAD TRUE CACHE INTERNAL "")
228 else()
229     set(HAVE_SANITIZER_THREAD FALSE CACHE INTERNAL "")
230 endif()
231
232 if(enable_undefined_sanitizer)
233     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
234     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
235     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
236 endif()
237
238 if(NOT $ENV{CFLAGS} STREQUAL "")
239   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
240   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
241 endif()
242
243 if(NOT $ENV{CXXFLAGS} STREQUAL "")
244   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
245   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
246 endif()
247
248 if(NOT $ENV{LDFLAGS} STREQUAL "")
249   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
250   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
251 endif()
252
253 if(MINGW)
254   # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
255   # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
256   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -static-libgcc")
257   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
258   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS   "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
259   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
260
261   # JNI searches for stdcalls
262   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -Wl,--add-stdcall-alias")
263   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
264   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
265   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
266
267   # Specify the data model that we are using (yeah it may help Java)
268   if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
269     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m32")
270     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
271   else()
272     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m64")
273     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
274   endif()
275 endif()