Logo AND Algorithmique Numérique Distribuée

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