Logo AND Algorithmique Numérique Distribuée

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