Add link time optimization of available
[oonf.git] / CMake.flags
1 include(CheckCCompilerFlag)
2
3 # define a function that checks if a certain compiler flag is available
4 # use it if it is available, display a warning if not
5 function(add_compiler_flag flag)
6     check_c_compiler_flag(${flag} test${flag})
7     if (${test${flag}})
8         ADD_DEFINITIONS(${flag})
9     endif()
10 endfunction(add_compiler_flag)
11
12 # allow to remove parts of the logging/helptext for smaller executable
13 IF (OONF_REMOVE_DEBUG_LOGGING)
14     ADD_DEFINITIONS(-DREMOVE_LOG_DEBUG)
15 ENDIF (OONF_REMOVE_DEBUG_LOGGING)
16 IF (OONF_REMOVE_INFO_LOGGING)
17     ADD_DEFINITIONS(-DREMOVE_LOG_INFO)
18 ENDIF (OONF_REMOVE_INFO_LOGGING)
19 IF (OONF_REMOVE_WARN_LOGGING)
20     ADD_DEFINITIONS(-DREMOVE_LOG_WARN)
21 ENDIF (OONF_REMOVE_WARN_LOGGING)
22
23 IF (OONF_REMOVE_HELPTEXT)
24     ADD_DEFINITIONS(-DREMOVE_HELPTEXT)
25 ENDIF(OONF_REMOVE_HELPTEXT)
26
27 # set preprocessor variables for level of root access the app needs
28 IF (${OONF_NEEDS_ROOT} MATCHES "true")
29     ADD_DEFINITIONS(-DNEED_ROOT=2)
30 ENDIF (${OONF_NEEDS_ROOT} MATCHES "true")
31 IF (${OONF_NEEDS_ROOT} MATCHES "apply")
32     ADD_DEFINITIONS(-DNEED_ROOT=1)
33 ENDIF (${OONF_NEEDS_ROOT} MATCHES "apply")
34
35 # OS-specific settings
36 IF(ANDROID OR WIN32)
37     # Android and windows don't compile well with c99
38     ADD_DEFINITIONS(-std=gnu99)
39 ELSE(ANDROID OR WIN32)
40     # everything else does
41     ADD_DEFINITIONS(-std=c99 -D_XOPEN_SOURCE=700 -D_BSD_SOURCE -D__BSD_VISIBLE -D_DARWIN_C_SOURCE -D__KERNEL_STRICT_NAMES)
42 ENDIF (ANDROID OR WIN32)
43
44 # default configuration file
45 IF (NOT EXISTS ${OONF_DEFAULT_CONF})
46   # generate OLSRd default config file
47   IF(WIN32)
48     # windows default config file is in the same directory,
49     # it has the same name as the executable, just replaced the .exe
50     # with .conf 
51     string(REPLACE ".exe" ".conf" OONF_DEFAULT_CONF "${OONF_EXE}")
52   ELSE(WIN32)
53     # unix default config file is /etc/<executable-name>.conf
54     set (OONF_DEFAULT_CONF "/etc/${OONF_EXE}.conf")
55   ENDIF(WIN32)
56 ENDIF()
57
58 # add some necessary additions for win32
59 IF (WIN32)
60     ADD_DEFINITIONS(-D_WIN32_WINNT=0x0502)
61     SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--enable-auto-import")
62 ENDIF(WIN32)
63
64 # set compiler flags that are supported
65 add_compiler_flag(-finline-functions-called-once)
66 add_compiler_flag(-funit-at-a-time)
67 add_compiler_flag(-fearly-inlining)
68 add_compiler_flag(-fno-strict-aliasing)
69 add_compiler_flag(-finline-limit=350)
70 add_compiler_flag(-Wstrict-overflow=5)
71 add_compiler_flag(-fvisibility=hidden)
72
73 add_compiler_flag(-Wall)
74 add_compiler_flag(-Wextra)
75 add_compiler_flag(-Wold-style-definition)
76 add_compiler_flag(-Wdeclaration-after-statement)
77 add_compiler_flag(-Wmissing-prototypes)
78 add_compiler_flag(-Wstrict-prototypes)
79 add_compiler_flag(-Wmissing-declarations)
80 add_compiler_flag(-Wsign-compare)
81 add_compiler_flag(-Waggregate-return)
82 add_compiler_flag(-Wmissing-noreturn)
83 add_compiler_flag(-Wmissing-format-attribute)
84 add_compiler_flag(-Wno-multichar)
85 add_compiler_flag(-Wno-deprecated-declarations)
86 add_compiler_flag(-Wendif-labels)
87 add_compiler_flag(-Wwrite-strings)
88 add_compiler_flag(-Wbad-function-cast)
89 add_compiler_flag(-Wpointer-arith)
90 add_compiler_flag(-Wno-cast-qual)
91 add_compiler_flag(-Wshadow)
92 add_compiler_flag(-Wsequence-point)
93 add_compiler_flag(-Wpointer-arith)
94 add_compiler_flag(-Wnested-externs)
95 add_compiler_flag(-Winline)
96 add_compiler_flag(-Wdisabled-optimization)
97 add_compiler_flag(-Wformat)
98 add_compiler_flag(-Wformat-security)
99
100 # check for link time optimization
101 check_c_compiler_flag("-flto" test_lto)
102 if (${test_lto})
103     ADD_DEFINITIONS(-flto -fuse-linker-plugin)
104     SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto")
105     SET(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} -flto")
106 endif()