Cleanup build installation directories
[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_DEFAULT_SOURCE -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 ${CMAKE_BINARY_DIR})
82 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
83 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
84
85 # set release specific compiler options
86 set(CMAKE_C_FLAGS_DEBUG "-g")
87 set(CMAKE_C_FLAGS_RELEASE "-O2 -g0 -DNDEBUG")
88 set(CMAKE_C_FLAGS_MINSIZEREL "-Os -g0 -DNDEBUG")
89 set(CMAKE_C_FLAGS_RELWITHDEBINFO "-DNDEBUG")
90
91 # Add address sanitizer to debug compilation
92 IF (OONF_SANITIZE)
93     IF (CMAKE_BUILD_TYPE STREQUAL "Debug")
94         SET(CMAKE_C_FLAGS_DEBUG       "${CMAKE_C_FLAGS_DEBUG}       -fsanitize=address -fno-omit-frame-pointer")
95         SET(CMAKE_EXE_LINKER_FLAGS    "${CMAKE_EXE_LINKER_FLAGS}    -static-libasan")
96     ENDIF()
97 ENDIF()
98
99 # add -Werror compiler options
100 IF (OONF_NO_WERROR)
101         message ("Skip -Werror")
102 ELSE (OONF_NO_WERROR)
103         ADD_DEFINITIONS(-Werror)
104 ENDIF (OONF_NO_WERROR)
105
106 # set compiler flags that are supported
107 add_compiler_flag(-finline-functions-called-once)
108 add_compiler_flag(-funit-at-a-time)
109 add_compiler_flag(-fearly-inlining)
110 add_compiler_flag(-fno-strict-aliasing)
111 add_compiler_flag(-finline-limit=350)
112 add_compiler_flag(-fvisibility=hidden)
113
114 add_compiler_flag(-Wall)
115 add_compiler_flag(-Wextra)
116 add_compiler_flag(-Wold-style-definition)
117 add_compiler_flag(-Wdeclaration-after-statement)
118 add_compiler_flag(-Wmissing-prototypes)
119 add_compiler_flag(-Wstrict-prototypes)
120 add_compiler_flag(-Wmissing-declarations)
121 add_compiler_flag(-Wsign-compare)
122 add_compiler_flag(-Waggregate-return)
123 add_compiler_flag(-Wmissing-noreturn)
124 add_compiler_flag(-Wmissing-format-attribute)
125 add_compiler_flag(-Wno-multichar)
126 add_compiler_flag(-Wno-deprecated-declarations)
127 add_compiler_flag(-Wendif-labels)
128 add_compiler_flag(-Wwrite-strings)
129 add_compiler_flag(-Wbad-function-cast)
130 add_compiler_flag(-Wpointer-arith)
131 add_compiler_flag(-Wno-cast-qual)
132 add_compiler_flag(-Wshadow)
133 add_compiler_flag(-Wsequence-point)
134 add_compiler_flag(-Wpointer-arith)
135 add_compiler_flag(-Wnested-externs)
136 add_compiler_flag(-Winline)
137 add_compiler_flag(-Wdisabled-optimization)
138 add_compiler_flag(-Wformat)
139 add_compiler_flag(-Wformat-security)
140 add_compiler_flag(-Wstrict-overflow=5)
141 add_compiler_flag(-Wdouble-promotion)
142 add_compiler_flag(-Wformat-y2k)
143 add_compiler_flag(-Wtrampolines)
144 add_compiler_flag(-Wlogical-op)
145 add_compiler_flag(-Wswitch-default)
146 add_compiler_flag(-Winit-self)
147 add_compiler_flag(-Wsync-nand)
148 add_compiler_flag(-Wundef)
149 add_compiler_flag(-Wunused-parameter)
150 add_compiler_flag(-Wjump-misses-init)
151 add_compiler_flag(-Wlogical-op)
152
153 # check for link time optimization
154 # check_c_compiler_flag("-flto" test_lto)
155 # if (${test_lto})
156 #     if (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
157 #         ADD_DEFINITIONS(-flto -fuse-linker-plugin)
158 #         SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto")
159 #         SET(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} -flto")
160 #     endif()
161 # endif()