Logo AND Algorithmique Numérique Distribuée

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