Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5731bdac1547832e9669b257328f658950c7a80a
[simgrid.git] / tools / cmake / GCCFlags.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_C_COMPILER_ID MATCHES "Clang|GCC")
19     set(warnCFLAGS "${warnCFLAGS} -Wno-format-nonliteral")
20   endif()
21   if(CMAKE_COMPILER_IS_GNUCC)
22     set(warnCFLAGS "${warnCFLAGS} -Wclobbered -Wno-error=clobbered  -Wno-unused-local-typedefs -Wno-error=attributes")
23   endif()
24
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")
28   endif()
29   if(CMAKE_COMPILER_IS_GNUCXX)
30     set(warnCXXFLAGS "${warnCXXFLAGS} -Wclobbered -Wno-error=clobbered  -Wno-unused-local-typedefs -Wno-error=attributes")
31   endif()
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")
36   endif()
37
38   # the one specific to C but refused by C++
39   set(warnCFLAGS "${warnCFLAGS} -Wmissing-prototypes") 
40
41   if(CMAKE_Fotran_COMPILER_ID MATCHES "GCC|PGI")
42     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
43   endif()
44   if(CMAKE_Fotran_COMPILER_ID MATCHES "Intel")
45     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -warn all")
46   endif()
47   set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
48 endif()
49
50 # NDEBUG gives a lot of "initialized but unused variables" errors. Don't die anyway.
51 if(enable_compile_warnings AND enable_debug)
52   set(warnCFLAGS "${warnCFLAGS} -Werror")
53   set(warnCXXFLAGS "${warnCXXFLAGS} -Werror")
54 endif()
55
56 # Activate the warnings on #if FOOBAR when FOOBAR has no value
57 # It breaks on FreeBSD within Boost headers, so activate this only in Pure Hardcore debug mode.
58 if(enable_maintainer_mode)
59   set(warnCFLAGS "${warnCFLAGS} -Wundef")
60   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
61 endif()
62
63 # Se the optimisation flags
64 # NOTE, we should CMAKE_BUILD_TYPE for this
65 if(enable_compile_optimizations)
66   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
67 else()
68   set(optCFLAGS "-O0 ")
69 endif()
70 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC
71     AND (NOT enable_model-checking))
72   # This is redundant (already in -03):
73   set(optCFLAGS "${optCFLAGS} -finline-functions ")
74 endif()
75
76 # Configure LTO
77 # NOTE, cmake 3.0 has a INTERPROCEDURAL_OPTIMIZATION target
78 #       property for this (http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html)
79 if(enable_lto) # User wants LTO. Try if we can do that
80   set(enable_lto OFF)
81   if(enable_compile_optimizations
82       AND CMAKE_COMPILER_IS_GNUCC
83       AND (NOT enable_model-checking))
84     # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
85     #   We are experiencing assertion failures even with 4.8 on MinGW.
86     #   Push the support forward: will see if 4.9 works when we test it.
87     #
88     # On Linux, we got the following with GCC 4.8.4 on Centos and Ubuntu
89     #    lto1: internal compiler error: in output_die, at dwarf2out.c:8478
90     #    Please submit a full bug report, with preprocessed source if appropriate.
91     # So instead, we push the support forward
92
93     if ( (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.5")
94          AND (LINKER_VERSION VERSION_GREATER "2.22"))
95       set(enable_lto ON)
96     endif()
97   endif()
98   if(enable_lto)
99     message("-- LTO seems usable.")
100   else()
101     if(NOT enable_compile_optimizations)
102       message("-- LTO disabled: Compile-time optimizations turned off.")
103     else() 
104       if(enable_model-checking)
105         message("-- LTO disabled when compiling with model-checking.")
106       else()
107         message("-- LTO does not seem usable -- try updating your build chain.")
108       endif() 
109     endif()
110   endif()
111 else()
112   message("-- LTO disabled on the command line.")
113 endif()
114 if(enable_lto) # User wants LTO, and it seems usable. Go for it
115   set(optCFLAGS "${optCFLAGS} -flto ")
116   # See https://gcc.gnu.org/wiki/LinkTimeOptimizationFAQ#ar.2C_nm_and_ranlib:
117   # "Since version 4.9 gcc produces slim object files that only contain
118   # the intermediate representation. In order to handle archives of
119   # these objects you have to use the gcc wrappers:
120   # gcc-ar, gcc-nm and gcc-ranlib."
121   if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
122       AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8")
123     set (CMAKE_AR gcc-ar)
124     set (CMAKE_RANLIB gcc-ranlib)
125   endif()
126 endif()
127
128 if(enable_model-checking AND enable_compile_optimizations)
129   # Forget it, do not optimize the code (because it confuses the MC):
130   set(optCFLAGS "-O0 ")
131   # But you can still optimize this:
132   foreach(s
133       src/xbt/mmalloc/mm.c
134       src/xbt/log.c src/xbt/xbt_log_appender_file.c
135       src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c
136       src/xbt/dict.cpp src/xbt/dict_elm.c src/xbt/dict_cursor.c
137       src/xbt/dynar.cpp src/xbt/heap.c src/xbt/swag.c
138       src/xbt/str.c src src/xbt/snprintf.c
139       src/xbt/queue.c
140       src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c
141       src/xbt/backtrace_linux.c
142       ${MC_SRC_BASE} ${MC_SRC})
143       set (mcCFLAGS "-O3  -funroll-loops -fno-strict-aliasing")
144        if(CMAKE_COMPILER_IS_GNUCC)
145          set (mcCFLAGS "${mcCFLAGS} -finline-functions")
146       endif()
147       set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
148   endforeach()
149 endif()
150
151 if(NOT enable_debug)
152   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
153   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
154 endif()
155
156 set(CMAKE_C_FLAGS   "${warnCFLAGS} ${CMAKE_C_FLAGS}   ${optCFLAGS}")
157 set(CMAKE_CXX_FLAGS "${warnCXXFLAGS} ${CMAKE_CXX_FLAGS} ${optCFLAGS}")
158
159 # Try to make Mac a bit more complient to open source standards
160 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
161   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
162 endif()
163
164 set(TESH_OPTION "")
165 if(enable_coverage)
166   find_program(GCOV_PATH gcov)
167   if(GCOV_PATH)
168     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
169     set(COVERAGE_EXTRA_FLAGS "-l -p")
170     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
171     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
172     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
173     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
174     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
175     add_definitions(-fprofile-arcs -ftest-coverage)
176   endif()
177 endif()
178
179 if(enable_address_sanitizer)
180     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
181     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
182     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=address")
183     set(TESH_OPTION --enable-sanitizers)
184 endif()
185
186 if(enable_thread_sanitizer)
187     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
188     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -no-pie")
189     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=thread -no-pie")
190 endif()
191
192 if(enable_undefined_sanitizer)
193     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
194     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-omit-frame-pointer")
195     set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fsanitize=undefined")
196 endif()
197
198 if(NOT $ENV{CFLAGS} STREQUAL "")
199   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
200   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
201 endif()
202
203 if(NOT $ENV{CXXFLAGS} STREQUAL "")
204   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
205   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
206 endif()
207
208 if(NOT $ENV{LDFLAGS} STREQUAL "")
209   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
210   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
211 endif()
212
213 if(MINGW)
214   # http://stackoverflow.com/questions/10452262/create-64-bit-jni-under-windows
215   # We don't want to ship libgcc_s_seh-1.dll nor libstdc++-6.dll
216   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -static-libgcc")
217   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
218   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS   "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc")
219   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++")
220   
221   # JNI searches for stdcalls
222   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -Wl,--add-stdcall-alias")
223   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--add-stdcall-alias")
224   set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -Wl,--add-stdcall-alias")
225   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -Wl,--add-stdcall-alias")
226   
227   # Specify the data model that we are using (yeah it may help Java)
228   if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
229     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m32")
230     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
231   else()
232     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -m64")
233     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
234   endif()  
235 endif()