Logo AND Algorithmique Numérique Distribuée

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