Logo AND Algorithmique Numérique Distribuée

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