Logo AND Algorithmique Numérique Distribuée

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