Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
NS3 libraries were not versionned on Ubuntu Precise
[simgrid.git] / buildtools / Cmake / Flags.cmake
1 ##
2 ## This file is in charge of setting our paranoid flags with regard to warnings and optimization.
3 ##
4 ##   These flags do break some classical CMake tests, so you don't
5 ##   want to do so before the very end of the configuration.
6 ## 
7 ##   Other compiler flags (C/C++ standard version) are tested and set
8 ##   by the beginning of the configuration, directly in ~/CMakeList.txt
9
10 set(warnCFLAGS "")
11 set(optCFLAGS "")
12
13
14
15 if(enable_compile_warnings)
16   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 ")
17   if(CMAKE_COMPILER_IS_GNUCC)
18     set(warnCFLAGS "${warnCFLAGS}-Wclobbered -Wno-error=clobbered ")
19   endif()
20
21   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")
22   if(CMAKE_COMPILER_IS_GNUCXX)
23     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wclobbered -Wno-error=clobbered")
24   endif()
25
26   set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall") # FIXME: Q&D hack
27
28   set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
29 endif()
30
31 if(enable_compile_optimizations)
32   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
33   if(CMAKE_COMPILER_IS_GNUCC AND (NOT enable_model-checking))
34     set(optCFLAGS "${optCFLAGS} -finline-functions ")
35     if(WIN32)
36       if (COMPILER_C_VERSION_MAJOR_MINOR STRGREATER "4.7")
37       # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
38         set(optCFLAGS "${optCFLAGS} -flto ")
39       endif()
40     else()    
41       # On non-windows, 4.6 is enough for that
42       if(LINKER_VERSION STRGREATER "2.22")
43         set(optCFLAGS "${optCFLAGS} -flto ")
44       endif()
45     endif()
46   endif()
47 else()
48   set(optCFLAGS "-O0 ")
49 endif()
50
51 if(enable_sdt)
52   add_definitions(-DUSE_SDT)
53 endif()
54
55 if(enable_ust)
56   add_definitions(-DUSE_UST)
57 endif()
58
59 if(enable_model-checking AND enable_compile_optimizations)
60   # Forget it, do not optimize the code (because it confuses the MC):
61   set(optCFLAGS "-O0 ")
62   # But you can still optimize this:
63   foreach(s
64       src/xbt/mmalloc/mm.c
65       src/xbt/log.c src/xbt/xbt_log_appender_file.c
66       src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c
67       src/xbt/dict.c src/xbt/dict_elm.c src/xbt/dict_multi.c src/xbt/dict_cursor.c
68       src/xbt/set.c src/xbt/setset.c
69       src/xbt/dynar.c src/xbt/fifo.c src/xbt/heap.c src/xbt/swag.c
70       src/xbt/str.c src/xbt/strbuff.c src/xbt/snprintf.c
71       src/xbt/queue.c
72       src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c
73       src/xbt/sha.c
74       src/xbt/matrix.c
75       src/xbt/backtrace_linux.c
76       ${MC_SRC_BASE} ${MC_SRC})
77       set (mcCFLAGS "-O3  -funroll-loops -fno-strict-aliasing")
78        if(CMAKE_COMPILER_IS_GNUCC)
79          set (mcCFLAGS "${mcCFLAGS} -finline-functions")
80       endif()
81       set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
82   endforeach()
83 endif()
84
85 if(APPLE AND COMPILER_C_VERSION_MAJOR_MINOR MATCHES "4.6")
86   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations")
87   set(optCFLAGS "-O0 ")
88 endif()
89
90 if(NOT enable_debug)
91   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
92   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
93 endif()
94
95 if(enable_msg_deprecated)
96   set(CMAKE_C_FLAGS "-DMSG_USE_DEPRECATED ${CMAKE_C_FLAGS}")
97 endif()
98
99 set(CMAKE_C_FLAGS "${optCFLAGS} ${warnCFLAGS} ${CMAKE_C_FLAGS}")
100
101 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${optCFLAGS}")
102
103 # Try to make Mac a bit more complient to open source standards
104 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
105   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
106 endif()
107
108 set(TESH_OPTION "")
109 if(enable_coverage)
110   find_program(GCOV_PATH gcov)
111   if(GCOV_PATH)
112     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
113     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
114     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
115     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
116     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
117     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
118     set(TESH_OPTION --enable-coverage)
119     add_definitions(-fprofile-arcs -ftest-coverage)
120   endif()
121 endif()
122
123 if(NOT $ENV{CFLAGS} STREQUAL "")
124   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
125   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
126 endif()
127
128 if(NOT $ENV{CXXFLAGS} STREQUAL "")
129   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
130   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
131 endif()
132
133 if(NOT $ENV{LDFLAGS} STREQUAL "")
134   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
135   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
136 endif()