Logo AND Algorithmique Numérique Distribuée

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