Logo AND Algorithmique Numérique Distribuée

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