Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[appveyor] show compile command lines (plus: kill dead code)
[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   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 if(enable_compile_optimizations)
34   set(optCFLAGS "-O3 -funroll-loops -fno-strict-aliasing ")
35   if(CMAKE_COMPILER_IS_GNUCC AND (NOT enable_model-checking))
36     set(optCFLAGS "${optCFLAGS} -finline-functions ")
37     if(WIN32)
38       if (COMPILER_C_VERSION_MAJOR_MINOR STRGREATER "4.7")
39       # On windows, we need 4.8 or higher to enable lto because of http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50293
40         set(optCFLAGS "${optCFLAGS} -flto ")
41       endif()
42     else()    
43       # On non-windows, 4.6 is enough for that
44       if(LINKER_VERSION STRGREATER "2.22")
45         set(optCFLAGS "${optCFLAGS} -flto ")
46       endif()
47     endif()
48   endif()
49 else()
50   set(optCFLAGS "-O0 ")
51 endif()
52
53 if(enable_sdt)
54   add_definitions(-DUSE_SDT)
55 endif()
56
57 if(enable_ust)
58   add_definitions(-DUSE_UST)
59 endif()
60
61 if(enable_model-checking AND enable_compile_optimizations)
62   # Forget it, do not optimize the code (because it confuses the MC):
63   set(optCFLAGS "-O0 ")
64   # But you can still optimize this:
65   foreach(s
66       src/xbt/mmalloc/mm.c
67       src/xbt/log.c src/xbt/xbt_log_appender_file.c
68       src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c
69       src/xbt/dict.c src/xbt/dict_elm.c src/xbt/dict_multi.c src/xbt/dict_cursor.c
70       src/xbt/set.c src/xbt/setset.c
71       src/xbt/dynar.c src/xbt/fifo.c src/xbt/heap.c src/xbt/swag.c
72       src/xbt/str.c src/xbt/strbuff.c src/xbt/snprintf.c
73       src/xbt/queue.c
74       src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c
75       src/xbt/sha.c
76       src/xbt/matrix.c
77       src/xbt/backtrace_linux.c
78       ${MC_SRC_BASE} ${MC_SRC})
79       set (mcCFLAGS "-O3  -funroll-loops -fno-strict-aliasing")
80        if(CMAKE_COMPILER_IS_GNUCC)
81          set (mcCFLAGS "${mcCFLAGS} -finline-functions")
82       endif()
83       set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS ${mcCFLAGS})
84   endforeach()
85 endif()
86
87 if(NOT enable_debug)
88   set(CMAKE_C_FLAGS "-DNDEBUG ${CMAKE_C_FLAGS}")
89   set(CMAKE_CXX_FLAGS "-DNDEBUG ${CMAKE_CXX_FLAGS}")
90 endif()
91
92 if(enable_msg_deprecated)
93   set(CMAKE_C_FLAGS "-DMSG_USE_DEPRECATED ${CMAKE_C_FLAGS}")
94 endif()
95
96 set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   ${optCFLAGS} ${warnCFLAGS}")
97 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${optCFLAGS}")
98
99 # Try to make Mac a bit more complient to open source standards
100 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
101   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE")
102 endif()
103
104 set(TESH_OPTION "")
105 if(enable_coverage)
106   find_program(GCOV_PATH gcov)
107   if(GCOV_PATH)
108     set(COVERAGE_COMMAND "${GCOV_PATH}" CACHE TYPE FILEPATH FORCE)
109     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOVERAGE")
110     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
111     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
112     set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage")
113     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
114     set(TESH_OPTION --enable-coverage)
115     add_definitions(-fprofile-arcs -ftest-coverage)
116   endif()
117 endif()
118
119 if(NOT $ENV{CFLAGS} STREQUAL "")
120   message(STATUS "Add CFLAGS: \"$ENV{CFLAGS}\" to CMAKE_C_FLAGS")
121   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
122 endif()
123
124 if(NOT $ENV{CXXFLAGS} STREQUAL "")
125   message(STATUS "Add CXXFLAGS: \"$ENV{CXXFLAGS}\" to CMAKE_CXX_FLAGS")
126   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
127 endif()
128
129 if(NOT $ENV{LDFLAGS} STREQUAL "")
130   message(STATUS "Add LDFLAGS: \"$ENV{LDFLAGS}\" to CMAKE_C_LINK_FLAGS")
131   set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} $ENV{LDFLAGS}")
132 endif()