Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6df76ebe454256ff925ab8a6629fb858f01a0b65
[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} -std=gnu99 -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_C_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 if(enable_compile_warnings)
33   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 ")
34   if(CMAKE_COMPILER_IS_GNUCC)
35     set(warnCFLAGS "${warnCFLAGS}-Wclobbered -Wno-error=clobbered ")
36   endif()
37
38   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")
39   if(CMAKE_COMPILER_IS_GNUCXX)
40     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wclobbered -Wno-error=clobbered")
41   endif()
42
43   set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall") # FIXME: Q&D hack
44
45   set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
46 endif()
47
48 if(enable_compile_optimizations)
49   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
50   if(CMAKE_COMPILER_IS_GNUCC AND (NOT enable_model-checking))
51     set(optCFLAGS "${optCFLAGS} -finline-functions ")
52     if(WIN32)
53       if (COMPILER_C_VERSION_MAJOR_MINOR STRGREATER "4.7")
54       # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
55         set(optCFLAGS "${optCFLAGS} -flto ")
56       endif()
57     else()    
58       # On non-windows, 4.6 is enough for that
59       if(LINKER_VERSION STRGREATER "2.22")
60         set(optCFLAGS "${optCFLAGS} -flto ")
61       endif()
62     endif()
63   endif()
64 else()
65   set(optCFLAGS "-O0 ")
66 endif()
67
68 if(enable_sdt)
69   add_definitions(-DUSE_SDT)
70 endif()
71
72 if(enable_ust)
73   add_definitions(-DUSE_UST)
74 endif()
75
76 if(enable_model-checking AND enable_compile_optimizations)
77   # Forget it, do not optimize the code (because it confuses the MC):
78   set(optCFLAGS "-O0 ")
79   # But you can still optimize this:
80   foreach(s
81       src/xbt/mmalloc/mm.c
82       src/xbt/log.c src/xbt/xbt_log_appender_file.c
83       src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c
84       src/xbt/dict.c src/xbt/dict_elm.c src/xbt/dict_multi.c src/xbt/dict_cursor.c
85       src/xbt/set.c src/xbt/setset.c
86       src/xbt/dynar.c src/xbt/fifo.c src/xbt/heap.c src/xbt/swag.c
87       src/xbt/str.c src/xbt/strbuff.c src/xbt/snprintf.c
88       src/xbt/queue.c
89       src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c
90       src/xbt/sha.c
91       src/xbt/matrix.c
92       src/xbt/backtrace_linux.c
93       ${MC_SRC_BASE} ${MC_SRC})
94       set (mcCFLAGS "-O3  -funroll-loops -fno-strict-aliasing")
95        if(CMAKE_COMPILER_IS_GNUCC)
96          set (mcCFLAGS "${mcCFLAGS} -finline-functions")
97       endif()
98       set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
99   endforeach()
100 endif()
101
102 if(APPLE AND COMPILER_C_VERSION_MAJOR_MINOR MATCHES "4.6")
103   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations")
104   set(optCFLAGS "-O0 ")
105 endif()
106
107 if(NOT enable_debug)
108   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
109   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
110 endif()
111
112 if(enable_msg_deprecated)
113   set(CMAKE_C_FLAGS "-DMSG_USE_DEPRECATED ${CMAKE_C_FLAGS}")
114 endif()
115
116 set(CMAKE_C_FLAGS "${optCFLAGS} ${warnCFLAGS} ${CMAKE_C_FLAGS}")
117
118 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${optCFLAGS}")
119
120 # Try to make Mac a bit more complient to open source standards
121 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
122   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
123 endif()
124
125 set(TESH_OPTION "")
126 if(enable_coverage)
127   find_program(GCOV_PATH gcov)
128   if(GCOV_PATH)
129     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
130     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
131     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
132     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
133     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
134     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
135     set(TESH_OPTION --enable-coverage)
136     add_definitions(-fprofile-arcs -ftest-coverage)
137   endif()
138 endif()
139
140 if(NOT $ENV{CFLAGS} STREQUAL "")
141   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
142   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
143 endif()
144
145 if(NOT $ENV{CXXFLAGS} STREQUAL "")
146   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
147   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
148 endif()
149
150 if(NOT $ENV{LDFLAGS} STREQUAL "")
151   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
152   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
153 endif()