Reworking layer2 subsystem
[oonf.git] / cmake / cc_flags.cmake
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 # Make sure this file is not run again
13 SET(OONF_FLAGS_SET true)
14
15 # detect operation system and add missing variables for easy CMAKE OS detection
16 STRING(TOLOWER ${CMAKE_SYSTEM_NAME} SYSTEM_NAME)
17
18 IF (${SYSTEM_NAME} MATCHES "android" OR ANDROID)
19     message("Android detected")
20     SET(LINUX true)
21     SET(ANDROID true)
22     SET(UNIX true)
23 ELSEIF (${SYSTEM_NAME} MATCHES "linux")
24     message("Linux detected")
25     SET(LINUX true)
26 ENDIF (${SYSTEM_NAME} MATCHES "android" OR ANDROID)
27
28 IF (APPLE)
29     message("Mac OS detected")
30     set(BSD true)
31 ELSEIF (${SYSTEM_NAME} MATCHES "bsd")
32     message("BSD detected")
33     SET(BSD true)
34 ENDIF (APPLE)
35
36 IF (WIN32)
37     message("Win32 detected")
38 ENDIF (WIN32)
39
40 # add build directory to include path for autogenerated files
41 include_directories(${CMAKE_BINARY_DIR})
42
43 # Add a compiler flag for unix systems
44 IF (UNIX)
45     ADD_DEFINITIONS(-D__unix__)
46 ENDIF (UNIX)
47
48 # compiler flags that needs to be there both for API and application
49 IF (OONF_LOGGING_LEVEL STREQUAL "warn")
50   # only display warnings, no comment necessary
51 ELSEIF (OONF_LOGGING_LEVEL STREQUAL "info")
52   ADD_DEFINITIONS(-DOONF_LOG_INFO)
53 ELSEIF (OONF_LOGGING_LEVEL STREQUAL "debug")
54   ADD_DEFINITIONS(-DOONF_LOG_INFO)
55   ADD_DEFINITIONS(-DOONF_LOG_DEBUG_INFO)
56 ELSE (OONF_LOGGING_LEVEL STREQUAL "none")
57   message(FATAL_ERROR "Unknown debug level '${OONF_LOGGING_LEVEL}'")
58 ENDIF (OONF_LOGGING_LEVEL STREQUAL "warn")
59
60 IF (OONF_REMOVE_HELPTEXT)
61     ADD_DEFINITIONS(-DREMOVE_HELPTEXT)
62 ENDIF(OONF_REMOVE_HELPTEXT)
63
64 # OS-specific compiler settings
65 IF(ANDROID OR WIN32)
66     # Android and windows don't compile well with c99
67     ADD_DEFINITIONS(-std=gnu99)
68 ELSE(ANDROID OR WIN32)
69     # everything else does
70     ADD_DEFINITIONS(-std=c99 -D_XOPEN_SOURCE=700 -D_BSD_SOURCE -D__BSD_VISIBLE -D_DARWIN_C_SOURCE -D__KERNEL_STRICT_NAMES)
71 ENDIF (ANDROID OR WIN32)
72
73 # add some necessary additions for win32
74 IF (WIN32)
75     ADD_DEFINITIONS(-D_WIN32_WINNT=0x0502)
76     SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--enable-auto-import")
77     SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--export-all-symbols")
78 ENDIF(WIN32)
79
80 # create all data inside the build directory
81 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
82 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
83 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
84
85 # set release specific compiler options
86 set(CMAKE_C_FLAGS "-g")
87 set(CMAKE_C_FLAGS_DEBUG "-g")
88 set(CMAKE_C_FLAGS_RELEASE "-O4 -g0 -DNDEBUG")
89 set(CMAKE_C_FLAGS_MINSIZEREL "-Os -g0 -DNDEBUG")
90 set(CMAKE_C_FLAGS_RELWITHDEBINFO "-DNDEBUG")
91
92 # always add -Werror compiler options
93 ADD_DEFINITIONS(-Werror)
94
95 # set compiler flags that are supported
96 add_compiler_flag(-finline-functions-called-once)
97 add_compiler_flag(-funit-at-a-time)
98 add_compiler_flag(-fearly-inlining)
99 add_compiler_flag(-fno-strict-aliasing)
100 add_compiler_flag(-finline-limit=350)
101 add_compiler_flag(-fvisibility=hidden)
102
103 add_compiler_flag(-Wall)
104 add_compiler_flag(-Wextra)
105 add_compiler_flag(-Wold-style-definition)
106 add_compiler_flag(-Wdeclaration-after-statement)
107 add_compiler_flag(-Wmissing-prototypes)
108 add_compiler_flag(-Wstrict-prototypes)
109 add_compiler_flag(-Wmissing-declarations)
110 add_compiler_flag(-Wsign-compare)
111 add_compiler_flag(-Waggregate-return)
112 add_compiler_flag(-Wmissing-noreturn)
113 add_compiler_flag(-Wmissing-format-attribute)
114 add_compiler_flag(-Wno-multichar)
115 add_compiler_flag(-Wno-deprecated-declarations)
116 add_compiler_flag(-Wendif-labels)
117 add_compiler_flag(-Wwrite-strings)
118 add_compiler_flag(-Wbad-function-cast)
119 add_compiler_flag(-Wpointer-arith)
120 add_compiler_flag(-Wno-cast-qual)
121 add_compiler_flag(-Wshadow)
122 add_compiler_flag(-Wsequence-point)
123 add_compiler_flag(-Wpointer-arith)
124 add_compiler_flag(-Wnested-externs)
125 add_compiler_flag(-Winline)
126 add_compiler_flag(-Wdisabled-optimization)
127 add_compiler_flag(-Wformat)
128 add_compiler_flag(-Wformat-security)
129 add_compiler_flag(-Wstrict-overflow=5)
130 add_compiler_flag(-Wdouble-promotion)
131 add_compiler_flag(-Wformat-y2k)
132 add_compiler_flag(-Wtrampolines)
133 add_compiler_flag(-Wlogical-op)
134 add_compiler_flag(-Wswitch-default)
135 add_compiler_flag(-Winit-self)
136 add_compiler_flag(-Wsync-nand)
137 add_compiler_flag(-Wundef)
138 add_compiler_flag(-Wunused-parameter)
139 add_compiler_flag(-Wjump-missed-init)
140
141 # check for link time optimization
142 check_c_compiler_flag("-flto" test_lto)
143 if (${test_lto})
144     if (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
145         ADD_DEFINITIONS(-flto -fuse-linker-plugin)
146         SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto")
147         SET(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} -flto")
148     endif()
149 endif()