Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
we need std=gnu11 in C too (was std=gnu99)
[simgrid.git] / buildtools / Cmake / Flags.cmake
1 set(warnCFLAGS "")
2 set(optCFLAGS "")
3
4 ##
5 ## Request full debugging flags
6 ##
7 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g3")
8 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3")
9 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -g")
10
11 ##
12 ## We need a decent support of the c++11 standard
13 ##
14 include(CheckCXXCompilerFlag)
15 CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
16 if(COMPILER_SUPPORTS_CXX11)
17   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
18 else()
19   message(FATAL_ERROR 
20           "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. "
21           "Please use a decent C++ compiler.")
22 endif()
23 if (CMAKE_COMPILER_IS_GNUCC)
24   if (COMPILER_CXX_VERSION_MAJOR_MINOR STRLESS "4.7")
25     message(FATAL_ERROR
26             "SimGrid needs g++ version 4.7 to compile "
27             "(c++11 support of previous versions is too limited).")
28   endif()
29 endif()
30
31 ###
32 ### And we need C11 standard, too
33 ###
34 include(CheckCCompilerFlag)
35 CHECK_C_COMPILER_FLAG("-std=gnu11" COMPILER_SUPPORTS_C11)
36 if(COMPILER_SUPPORTS_C11)
37   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11")
38 else()
39   message(FATAL_ERROR 
40           "The compiler ${CMAKE_C_COMPILER} has no C11 support. "
41           "Please use a decent C compiler "
42           "(note that c++11 support of ${CMAKE_CXX_COMPILER} seems ok).")
43 endif()
44
45
46 if(enable_compile_warnings)
47   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 ")
48   if(CMAKE_COMPILER_IS_GNUCC)
49     set(warnCFLAGS "${warnCFLAGS}-Wclobbered -Wno-error=clobbered ")
50   endif()
51
52   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")
53   if(CMAKE_COMPILER_IS_GNUCXX)
54     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wclobbered -Wno-error=clobbered")
55   endif()
56
57   set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall") # FIXME: Q&D hack
58
59   set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
60 endif()
61
62 if(enable_compile_optimizations)
63   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
64   if(CMAKE_COMPILER_IS_GNUCC AND (NOT enable_model-checking))
65     set(optCFLAGS "${optCFLAGS} -finline-functions ")
66     if(WIN32)
67       if (COMPILER_C_VERSION_MAJOR_MINOR STRGREATER "4.7")
68       # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
69         set(optCFLAGS "${optCFLAGS} -flto ")
70       endif()
71     else()    
72       # On non-windows, 4.6 is enough for that
73       if(LINKER_VERSION STRGREATER "2.22")
74         set(optCFLAGS "${optCFLAGS} -flto ")
75       endif()
76     endif()
77   endif()
78 else()
79   set(optCFLAGS "-O0 ")
80 endif()
81
82 if(enable_sdt)
83   add_definitions(-DUSE_SDT)
84 endif()
85
86 if(enable_ust)
87   add_definitions(-DUSE_UST)
88 endif()
89
90 if(enable_model-checking AND enable_compile_optimizations)
91   # Forget it, do not optimize the code (because it confuses the MC):
92   set(optCFLAGS "-O0 ")
93   # But you can still optimize this:
94   foreach(s
95       src/xbt/mmalloc/mm.c
96       src/xbt/log.c src/xbt/xbt_log_appender_file.c
97       src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c
98       src/xbt/dict.c src/xbt/dict_elm.c src/xbt/dict_multi.c src/xbt/dict_cursor.c
99       src/xbt/set.c src/xbt/setset.c
100       src/xbt/dynar.c src/xbt/fifo.c src/xbt/heap.c src/xbt/swag.c
101       src/xbt/str.c src/xbt/strbuff.c src/xbt/snprintf.c
102       src/xbt/queue.c
103       src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c
104       src/xbt/sha.c
105       src/xbt/matrix.c
106       src/xbt/backtrace_linux.c
107       ${MC_SRC_BASE} ${MC_SRC})
108       set (mcCFLAGS "-O3  -funroll-loops -fno-strict-aliasing")
109        if(CMAKE_COMPILER_IS_GNUCC)
110          set (mcCFLAGS "${mcCFLAGS} -finline-functions")
111       endif()
112       set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
113   endforeach()
114 endif()
115
116 if(APPLE AND COMPILER_C_VERSION_MAJOR_MINOR MATCHES "4.6")
117   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations")
118   set(optCFLAGS "-O0 ")
119 endif()
120
121 if(NOT enable_debug)
122   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
123   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
124 endif()
125
126 if(enable_msg_deprecated)
127   set(CMAKE_C_FLAGS "-DMSG_USE_DEPRECATED ${CMAKE_C_FLAGS}")
128 endif()
129
130 set(CMAKE_C_FLAGS "${optCFLAGS} ${warnCFLAGS} ${CMAKE_C_FLAGS}")
131
132 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${optCFLAGS}")
133
134 # Try to make Mac a bit more complient to open source standards
135 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
136   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
137 endif()
138
139 set(TESH_OPTION "")
140 if(enable_coverage)
141   find_program(GCOV_PATH gcov)
142   if(GCOV_PATH)
143     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
144     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
145     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
146     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
147     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
148     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
149     set(TESH_OPTION --enable-coverage)
150     add_definitions(-fprofile-arcs -ftest-coverage)
151   endif()
152 endif()
153
154 if(NOT $ENV{CFLAGS} STREQUAL "")
155   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
156   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
157 endif()
158
159 if(NOT $ENV{CXXFLAGS} STREQUAL "")
160   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
161   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
162 endif()
163
164 if(NOT $ENV{LDFLAGS} STREQUAL "")
165   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
166   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
167 endif()