comparison cmake/IrccdSystem.cmake @ 0:1158cffe5a5e

Initial import
author David Demelier <markand@malikania.fr>
date Mon, 08 Feb 2016 16:43:14 +0100
parents
children c67e734c9241
comparison
equal deleted inserted replaced
-1:000000000000 0:1158cffe5a5e
1 #
2 # Config.cmake -- CMake build system for irccd
3 #
4 # Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
5 #
6 # Permission to use, copy, modify, and/or distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #
18
19 include(CheckFunctionExists)
20 include(CheckIncludeFile)
21 include(CheckStructHasMember)
22 include(CheckSymbolExists)
23 include(CheckTypeSize)
24
25 # ---------------------------------------------------------
26 # Global compile flags
27 # ---------------------------------------------------------
28
29 #
30 # Recent versions of CMake has nice C++ feature detection for modern
31 # C++ but they are still a bit buggy so we use this
32 # instead.
33 #
34 if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
35 #
36 # For GCC, we require at least GCC 5.1
37 #
38 if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.1")
39 message(FATAL_ERROR "You need at least GCC 5.1")
40 endif ()
41
42 set(CMAKE_CXX_FLAGS "-Wall -Wextra -std=c++14 ${CMAKE_CXX_FLAGS}")
43 elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
44 #
45 # LLVM/clang implemented C++14 starting from version 3.4 but the
46 # switch -std=c++14 was not available.
47 #
48 if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.4")
49 message(FATAL_ERROR "You need at least Clang 3.4")
50 endif ()
51
52 if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.5")
53 set(CMAKE_CXX_FLAGS "-Wall -Wextra -std=c++1y ${CMAKE_CXX_FLAGS}")
54 else ()
55 set(CMAKE_CXX_FLAGS "-Wall -Wextra -std=c++14 ${CMAKE_CXX_FLAGS}")
56 endif ()
57 else ()
58 message(WARNING "Unsupported ${CMAKE_CXX_COMPILER_ID}, may not build correctly.")
59 endif ()
60
61 if (WIN32)
62 set(CMAKE_CXX_FLAGS "-D_WIN32_WINNT=0x0600 ${CMAKE_CXX_FLAGS}")
63 endif ()
64
65 if (CMAKE_SIZEOF_VOID_P MATCHES "8")
66 set(IRCCD_64BITS TRUE)
67 else ()
68 set(IRCCD_64BITS FALSE)
69 endif ()
70
71 # ---------------------------------------------------------
72 # System identification
73 # ---------------------------------------------------------
74
75 if (WIN32)
76 set(IRCCD_SYSTEM_WINDOWS TRUE)
77 elseif (APPLE)
78 set(IRCCD_SYSTEM_MAC TRUE)
79 elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
80 set(IRCCD_SYSTEM_FREEBSD TRUE)
81 elseif (CMAKE_SYSTEM_NAME MATCHES "NetBSD")
82 set(IRCCD_SYSTEM_NETBSD TRUE)
83 elseif (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
84 set(IRCCD_SYSTEM_OPENBSD TRUE)
85 elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
86 set(IRCCD_SYSTEM_LINUX TRUE)
87 endif ()
88
89 # ---------------------------------------------------------
90 # Portability requirements
91 # ---------------------------------------------------------
92
93 check_type_size(int8_t HAVE_INT8)
94 check_type_size(uint8_t HAVE_UINT8)
95 check_type_size(int16_t HAVE_INT16)
96 check_type_size(uint16_t HAVE_UINT16)
97 check_type_size(int32_t HAVE_INT32)
98 check_type_size(uint32_t HAVE_UINT32)
99 check_type_size(int64_t HAVE_INT64)
100 check_type_size(uint64_t HAVE_UINT64)
101
102 if (NOT HAVE_STDINT_H)
103 message("irccd requires stdint.h or cstdint header")
104 endif ()
105
106 #
107 # Where any of these function / feature is required, include the <irccd-config.h> file.
108 #
109 # The following variables are defined in irccd-config.h
110 #
111 # HAVE_ACCESS - True if has access(2) function (and sys/types.h and sys/stat.h),
112 # HAVE_DAEMON - True if daemon(3),
113 # HAVE_GETPID - True if has getpid(2) function (and sys/types.h and unistd.h and grp.h),
114 # HAVE_POPEN - True if has popen(3) function (in stdio.h)
115 # HAVE_SETGID - True if has setgid(2) function and getgrnam(3) (and sys/types.h and unistd.h and pwd.h),
116 # HAVE_SETPROGNAME - True if setprogname(3) is available from C library,
117 # HAVE_SETUID - True if has setuid(2) function and getpwnam(3) (and sys/types.h and unistd.h and pwd.h),
118 # HAVE_STAT - True if has stat(2) function (and sys/types.h and sys/stat.h),
119 # HAVE_STAT_ST_DEV - The struct stat has st_dev field,
120 # HAVE_STAT_ST_INO - The struct stat has st_ino field,
121 # HAVE_STAT_ST_NLINK - The struct stat has st_nlink field,
122 # HAVE_STAT_ST_UID - The struct stat has st_uid field,
123 # HAVE_STAT_ST_GID - The struct stat has st_gid field,
124 # HAVE_STAT_ST_ATIME - The struct stat has st_atime field,
125 # HAVE_STAT_ST_MTIME - The struct stat has st_mtime field,
126 # HAVE_STAT_ST_CTIME - The struct stat has st_ctime field,
127 # HAVE_STAT_ST_SIZE - The struct stat has st_size field,
128 # HAVE_STAT_ST_BLKSIZE - The struct stat has st_blksize field,
129 # HAVE_STAT_ST_BLOCKS - The struct stat has st_blocks field,
130 # HAVE_SYSLOG - True if syslog functions are available (and syslog.h),
131 #
132
133 # Check for unistd.h
134 check_include_file(unistd.h HAVE_UNISTD_H)
135
136 # Check for sys/types.h
137 check_include_file(sys/types.h HAVE_SYS_TYPES_H)
138
139 # Check for daemon(3) function, include:
140 #
141 # #include <cstdlib>
142 check_function_exists(daemon HAVE_DAEMON)
143
144 # Check of setprogname(3) function, include:
145 #
146 # #include <cstdlib>
147 check_function_exists(setprogname HAVE_SETPROGNAME)
148
149 # access() POSIX function
150 #
151 # If HAVE_ACCESS is defined, include:
152 #
153 # #include <unistd.h>
154 check_function_exists(access HAVE_ACCESS)
155
156 if (NOT HAVE_UNISTD_H)
157 set(HAVE_ACCESS FALSE)
158 endif ()
159
160 # getpid() function
161 #
162 # If HAVE_GETPID is defined, include:
163 #
164 # #include <sys/types.h>
165 # #include <unistd.h>
166 check_function_exists(getpid HAVE_GETPID)
167
168 if (NOT HAVE_UNISTD_H OR NOT HAVE_SYS_TYPES_H)
169 set(HAVE_GETPID FALSE)
170 endif ()
171
172 # setgid() function (and getgrnam)
173 #
174 # If HAVE_SETGID is defined, include:
175 #
176 # #include <sys/types.h>
177 # #include <unistd.h>
178 # #include <grp.h> // only for getgrnam
179 check_include_file(grp.h HAVE_GRP_H)
180 check_function_exists(getgrnam HAVE_GETGRNAM)
181 check_function_exists(setgid HAVE_SETGID)
182
183 if (NOT HAVE_UNISTD_H OR NOT HAVE_SYS_TYPES_H OR NOT HAVE_GETGRNAM OR NOT HAVE_GRP_H)
184 set(HAVE_SETGID FALSE)
185 endif ()
186
187 # popen() function
188 #
189 # If HAVE_POPEN is defined, include;
190 #
191 # #include <cstdio>
192 check_function_exists(popen HAVE_POPEN)
193
194 # setuid() function (and getpwnam)
195 #
196 # If HAVE_SETUID is defined, include:
197 #
198 # #include <sys/types.h>
199 # #include <unistd.h>
200 # #include <pwd.h> // only for getpwnam
201 check_include_file(pwd.h HAVE_PWD_H)
202 check_function_exists(getpwnam HAVE_GETPWNAM)
203 check_function_exists(setuid HAVE_SETUID)
204
205 if (NOT HAVE_UNISTD_H OR NOT HAVE_SYS_TYPES_H OR NOT HAVE_GETPWNAM OR NOT HAVE_PWD_H)
206 set(HAVE_SETUID FALSE)
207 endif ()
208
209 # stat(2) function
210 #
211 # If HAVE_STAT is defined, include:
212 #
213 # #include <sys/types.h>
214 # #include <sys/stat.h>
215 check_include_file(sys/stat.h HAVE_SYS_STAT_H)
216 check_function_exists(stat HAVE_STAT)
217
218 # If the sys/stat.h is not found, we disable stat(2)
219 if (NOT HAVE_SYS_STAT_H OR NOT HAVE_SYS_TYPES_H)
220 set(HAVE_STAT FALSE)
221 endif ()
222
223 # syslog functions
224 #
225 # If HAVE_SYSLOG is defined, include:
226 #
227 # #include <syslog.h>
228 check_include_file(syslog.h HAVE_SYSLOG_H)
229 check_function_exists(openlog HAVE_OPENLOG)
230 check_function_exists(syslog HAVE_SYSLOG)
231 check_function_exists(closelog HAVE_CLOSELOG)
232
233 if (NOT HAVE_SYSLOG_H OR NOT HAVE_OPENLOG OR NOT HAVE_CLOSELOG)
234 set(HAVE_SYSLOG FALSE)
235 endif ()
236
237 # Check for struct stat fields.
238 check_struct_has_member("struct stat" st_atime sys/stat.h HAVE_STAT_ST_ATIME)
239 check_struct_has_member("struct stat" st_blksize sys/stat.h HAVE_STAT_ST_BLKSIZE)
240 check_struct_has_member("struct stat" st_blocks sys/stat.h HAVE_STAT_ST_BLOCKS)
241 check_struct_has_member("struct stat" st_ctime sys/stat.h HAVE_STAT_ST_CTIME)
242 check_struct_has_member("struct stat" st_dev sys/stat.h HAVE_STAT_ST_DEV)
243 check_struct_has_member("struct stat" st_gid sys/stat.h HAVE_STAT_ST_GID)
244 check_struct_has_member("struct stat" st_ino sys/stat.h HAVE_STAT_ST_INO)
245 check_struct_has_member("struct stat" st_mode sys/stat.h HAVE_STAT_ST_MODE)
246 check_struct_has_member("struct stat" st_mtime sys/stat.h HAVE_STAT_ST_MTIME)
247 check_struct_has_member("struct stat" st_nlink sys/stat.h HAVE_STAT_ST_NLINK)
248 check_struct_has_member("struct stat" st_rdev sys/stat.h HAVE_STAT_ST_RDEV)
249 check_struct_has_member("struct stat" st_size sys/stat.h HAVE_STAT_ST_SIZE)
250 check_struct_has_member("struct stat" st_uid sys/stat.h HAVE_STAT_ST_UID)
251
252 # Configuration file
253 configure_file(
254 ${CMAKE_CURRENT_LIST_DIR}/internal/irccd-config.h.in
255 ${CMAKE_BINARY_DIR}/irccd-config.h
256 )