Logo AND Algorithmique Numérique Distribuée

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