Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
create the directory before populating it
[simgrid.git] / tools / 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   if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # don't care about class that become struct
26     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-mismatched-tags")
27   endif()
28
29   set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall")
30   set(CMAKE_JAVA_COMPILE_FLAGS "-Xlint")
31 endif()
32
33 # Se the optimisation flags
34 # NOTE, we should CMAKE_BUILD_TYPE for this
35 if(enable_compile_optimizations)
36   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
37 else()
38   set(optCFLAGS "-O0 ")
39 endif()
40 if(enable_compile_optimizations AND CMAKE_COMPILER_IS_GNUCC
41     AND (NOT enable_model-checking))
42   # This is redundant (already in -03):
43   set(optCFLAGS "${optCFLAGS} -finline-functions ")
44 endif()
45
46 # Configure LTO
47 # TODO, provide an option to manually choose whether to use LTO
48 # NOTE, cmake 3.0 has a INTERPROCEDURAL_OPTIMIZATION target
49 #       property for this (http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html)
50 set(enable_lto OFF)
51 if(enable_compile_optimizations
52     AND CMAKE_COMPILER_IS_GNUCC
53     AND (NOT enable_model-checking))
54   if(WIN32)
55     if (COMPILER_C_VERSION_MAJOR_MINOR STRGREATER "4.8")
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       #
58       # We are experiencing assertion failures even with 4.8 on MinGW.
59       # Push the support forward: will see if 4.9 works when we test it.
60       set(enable_lto ON)
61     endif()
62   elseif(LINKER_VERSION STRGREATER "2.22")
63     set(enable_lto ON)
64   endif()
65 endif()
66 if(enable_lto)
67   set(optCFLAGS "${optCFLAGS} -flto ")
68   # See https://gcc.gnu.org/wiki/LinkTimeOptimizationFAQ#ar.2C_nm_and_ranlib:
69   # "Since version 4.9 gcc produces slim object files that only contain
70   # the intermediate representation. In order to handle archives of
71   # these objects you have to use the gcc wrappers:
72   # gcc-ar, gcc-nm and gcc-ranlib."
73   if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU"
74       AND COMPILER_C_VERSION_MAJOR_MINOR STRGREATER "4.8")
75     set (CMAKE_AR gcc-ar)
76     set (CMAKE_RANLIB gcc-ranlib)
77   endif()
78 endif()
79
80 if(enable_model-checking AND enable_compile_optimizations)
81   # Forget it, do not optimize the code (because it confuses the MC):
82   set(optCFLAGS "-O0 ")
83   # But you can still optimize this:
84   foreach(s
85       src/xbt/mmalloc/mm.c
86       src/xbt/log.c src/xbt/xbt_log_appender_file.c
87       src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c
88       src/xbt/dict.c src/xbt/dict_elm.c src/xbt/dict_multi.c src/xbt/dict_cursor.c
89       src/xbt/set.c src/xbt/setset.c
90       src/xbt/dynar.c src/xbt/fifo.c src/xbt/heap.c src/xbt/swag.c
91       src/xbt/str.c src/xbt/strbuff.c src/xbt/snprintf.c
92       src/xbt/queue.c
93       src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c
94       src/xbt/sha.c
95       src/xbt/matrix.c
96       src/xbt/backtrace_linux.c
97       ${MC_SRC_BASE} ${MC_SRC})
98       set (mcCFLAGS "-O3  -funroll-loops -fno-strict-aliasing")
99        if(CMAKE_COMPILER_IS_GNUCC)
100          set (mcCFLAGS "${mcCFLAGS} -finline-functions")
101       endif()
102       set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
103   endforeach()
104 endif()
105
106 if(NOT enable_debug)
107   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
108   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
109 endif()
110
111 if(enable_msg_deprecated)
112   set(CMAKE_C_FLAGS "-DMSG_USE_DEPRECATED ${CMAKE_C_FLAGS}")
113 endif()
114
115 set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   ${optCFLAGS} ${warnCFLAGS}")
116 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${optCFLAGS}")
117
118 # Try to make Mac a bit more complient to open source standards
119 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
120   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
121 endif()
122
123 set(TESH_OPTION "")
124 if(enable_coverage)
125   find_program(GCOV_PATH gcov)
126   if(GCOV_PATH)
127     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
128     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
129     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
130     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
131     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
132     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
133     set(TESH_OPTION --enable-coverage)
134     add_definitions(-fprofile-arcs -ftest-coverage)
135   endif()
136 endif()
137
138 if(NOT $ENV{CFLAGS} STREQUAL "")
139   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
140   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
141 endif()
142
143 if(NOT $ENV{CXXFLAGS} STREQUAL "")
144   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
145   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
146 endif()
147
148 if(NOT $ENV{LDFLAGS} STREQUAL "")
149   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
150   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
151 endif()