ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/CMakeLists.txt
Revision: 1.1
Committed: Tue Jul 6 10:58:41 2021 UTC (2 years, 11 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Log Message:
Add cmake build system

File Contents

# Content
1 cmake_minimum_required(VERSION 3.6)
2 project(libptytty VERSION 1.9)
3 set(CMAKE_CXX_STANDARD 11)
4 set(CMAKE_EXPORT_COMPILE_COMMANDS YES)
5
6 include(CheckCSourceCompiles)
7 include(CheckCSourceRuns)
8 include(CheckFunctionExists)
9 include(CheckIncludeFiles)
10 include(CheckLibraryExists)
11 include(CheckStructHasMember)
12 include(CheckTypeSize)
13 include(GNUInstallDirs)
14
15 set(VERSION ${PROJECT_VERSION})
16 set(prefix ${CMAKE_INSTALL_PREFIX})
17 set(includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
18 set(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
19
20 option(BUILD_SHARED_LIBS "build a shared library" ON)
21 option(UTMP_SUPPORT "enable utmp support" ON)
22 option(WTMP_SUPPORT "enable wtmp support (requires UTMP_SUPPORT)" ON)
23 option(LASTLOG_SUPPORT "enable lastlog support (requires UTMP_SUPPORT)" ON)
24
25 function(PT_FIND_FILE name id)
26 if(DEFINED ${id})
27 return()
28 elseif(CMAKE_CROSSCOMPILING)
29 message(STATUS "Checking for a fallback location of ${name} - define ${id} in config.h manually")
30 set(${id} "" CACHE STRING "")
31 else()
32 foreach(arg ${ARGN})
33 if(EXISTS ${arg})
34 message(STATUS "Checking for a fallback location of ${name} - ${arg}")
35 set(${id} ${arg} CACHE STRING "")
36 return()
37 endif()
38 endforeach()
39 message(STATUS "Checking for a fallback location of ${name} - not found")
40 set(${id} "" CACHE STRING "")
41 endif()
42 endfunction()
43
44 check_include_files(pty.h HAVE_PTY_H)
45 check_include_files(util.h HAVE_UTIL_H)
46 check_include_files(libutil.h HAVE_LIBUTIL_H)
47 check_include_files(sys/ioctl.h HAVE_SYS_IOCTL_H)
48 check_include_files(stropts.h HAVE_STROPTS_H)
49
50 check_function_exists(revoke HAVE_REVOKE)
51 check_function_exists(_getpty HAVE__GETPTY)
52 check_function_exists(getpt HAVE_GETPT)
53 check_function_exists(posix_openpt HAVE_POSIX_OPENPT)
54 check_function_exists(isastream HAVE_ISASTREAM)
55 check_function_exists(setuid HAVE_SETUID)
56 check_function_exists(setreuid HAVE_SETREUID)
57 check_function_exists(setresuid HAVE_SETRESUID)
58
59 check_c_source_compiles(
60 "#include <stdlib.h>
61
62 int main ()
63 {
64 grantpt (0);
65 unlockpt (0);
66 ptsname (0);
67 }
68 "
69
70 UNIX98_PTY)
71
72 if(NOT UNIX98_PTY)
73 check_function_exists(openpty HAVE_OPENPTY)
74 if(NOT HAVE_OPENPTY)
75 find_library(LIBUTIL_PATH util)
76 check_library_exists(util openpty ${LIBUTIL_PATH} HAVE_LIBUTIL)
77 if(HAVE_LIBUTIL)
78 set(HAVE_OPENPTY 1)
79 endif()
80 endif()
81 endif()
82
83 if(CMAKE_HOST_SOLARIS)
84 # Enable declarations in utmp.h on Solaris when the XPG4v2 namespace is active
85 set(__EXTENSIONS__ 1)
86 endif()
87
88 check_include_files("sys/types.h;utmp.h" HAVE_UTMP_H)
89 if(HAVE_UTMP_H)
90 set(CMAKE_EXTRA_INCLUDE_FILES "sys/types.h;utmp.h")
91 check_type_size("struct utmp" STRUCT_UTMP)
92 if (HAVE_STRUCT_UTMP)
93 check_struct_has_member(
94 "struct utmp"
95 ut_host
96 "sys/types.h;utmp.h"
97 HAVE_UTMP_HOST)
98 check_struct_has_member(
99 "struct utmp"
100 ut_pid
101 "sys/types.h;utmp.h"
102 HAVE_UTMP_PID)
103
104 check_function_exists(updwtmp HAVE_UPDWTMP)
105 check_include_files("sys/types.h;lastlog.h" HAVE_LASTLOG_H)
106 if (HAVE_LASTLOG_H)
107 set(CMAKE_EXTRA_INCLUDE_FILES "sys/types.h;lastlog.h")
108 endif()
109 check_type_size("struct lastlog" STRUCT_LASTLOG)
110 endif()
111 set(CMAKE_EXTRA_INCLUDE_FILES)
112
113 PT_FIND_FILE(
114 utmp
115 PT_UTMP_FILE
116 "/var/run/utmp"
117 "/var/adm/utmp"
118 "/etc/utmp"
119 "/usr/etc/utmp"
120 "/usr/adm/utmp")
121
122 PT_FIND_FILE(
123 wtmp
124 PT_WTMP_FILE
125 "/var/log/wtmp"
126 "/var/adm/wtmp"
127 "/etc/wtmp"
128 "/usr/etc/wtmp"
129 "/usr/adm/wtmp")
130
131 PT_FIND_FILE(
132 lastlog
133 PT_LASTLOG_FILE
134 "/var/log/lastlog"
135 "/var/adm/lastlog")
136 endif()
137
138 check_include_files(utmpx.h HAVE_UTMPX_H)
139 if(HAVE_UTMPX_H)
140 set(CMAKE_EXTRA_INCLUDE_FILES "utmpx.h")
141 check_type_size("struct utmpx" STRUCT_UTMPX)
142 if (HAVE_STRUCT_UTMPX)
143 check_struct_has_member(
144 "struct utmpx"
145 ut_host
146 "sys/types.h;utmpx.h"
147 HAVE_UTMPX_HOST)
148
149 check_function_exists(updwtmpx HAVE_UPDWTMPX)
150 check_function_exists(updlastlogx HAVE_UPDLASTLOGX)
151 check_type_size("struct lastlogx" STRUCT_LASTLOGX)
152 endif()
153 set(CMAKE_EXTRA_INCLUDE_FILES)
154
155 PT_FIND_FILE(
156 wtmpx
157 PT_WTMPX_FILE
158 "/var/log/wtmpx"
159 "/var/adm/wtmpx")
160
161 PT_FIND_FILE(
162 lastlogx
163 PT_LASTLOGX_FILE
164 "/var/log/lastlogx"
165 "/var/adm/lastlogx")
166 endif()
167
168 if(CMAKE_HOST_SOLARIS)
169 # Enable declarations of msg_control and msg_controllen on Solaris
170 set(CMAKE_REQUIRED_QUIET ON)
171 check_c_source_compiles(
172 "
173 int main ()
174 {
175 #if __STDC_VERSION__ < 199901L
176 error
177 #endif
178 }
179 "
180
181 C99)
182 set(CMAKE_REQUIRED_QUIET OFF)
183 if(C99)
184 set(_XOPEN_SOURCE 600)
185 else()
186 set(_XOPEN_SOURCE 500)
187 endif()
188 set(CMAKE_REQUIRED_DEFINITIONS -D_XOPEN_SOURCE=${_XOPEN_SOURCE})
189
190 find_library(LIBSOCKET_PATH socket)
191 check_library_exists(socket sendmsg ${LIBSOCKET_PATH} HAVE_LIBSOCKET)
192 if(HAVE_LIBSOCKET)
193 set(CMAKE_REQUIRED_LIBRARIES socket)
194 endif()
195 endif()
196
197 # Check for unix-compliant filehandle passing ability
198 check_c_source_compiles(
199 "
200 #include <stddef.h> // broken bsds (is that redundant?) need this
201 #include <sys/types.h>
202 #include <sys/socket.h>
203 #include <sys/uio.h>
204
205 int main ()
206 {
207 struct msghdr msg;
208 struct iovec iov;
209 char buf [100];
210 char data = 0;
211
212 iov.iov_base = &data;
213 iov.iov_len = 1;
214
215 msg.msg_iov = &iov;
216 msg.msg_iovlen = 1;
217 msg.msg_control = buf;
218 msg.msg_controllen = sizeof buf;
219
220 struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
221 cmsg->cmsg_level = SOL_SOCKET;
222 cmsg->cmsg_type = SCM_RIGHTS;
223 cmsg->cmsg_len = 100;
224
225 *(int *)CMSG_DATA (cmsg) = 5;
226
227 return sendmsg (3, &msg, 0);
228 }
229 "
230
231 HAVE_UNIX_FDPASS)
232 if(NOT HAVE_UNIX_FDPASS)
233 message(FATAL_ERROR "libptytty requires unix-compliant filehandle passing ability")
234 endif()
235
236 check_c_source_runs(
237 "
238 #include <grp.h>
239 #include <sys/stat.h>
240 #include <sys/types.h>
241 #include <unistd.h>
242
243 int main ()
244 {
245 struct stat st;
246 struct group *gr = getgrnam (\"tty\");
247 char *tty = ttyname (0);
248 return
249 !(gr
250 && tty
251 && stat (tty, &st) == 0
252 && st.st_gid == gr->gr_gid);
253 }
254 "
255
256 TTY_GID_SUPPORT)
257
258 configure_file(
259 config.h.cmake
260 config.h)
261
262 add_library(ptytty
263 src/c-api.C
264 src/fdpass.C
265 src/logging.C
266 src/proxy.C
267 src/ptytty.C)
268 target_include_directories(ptytty PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
269 set_target_properties(ptytty PROPERTIES VERSION 0)
270 if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
271 set_target_properties(ptytty PROPERTIES LINKER_LANGUAGE C)
272 target_link_libraries(ptytty PRIVATE supc++)
273 endif()
274 if(HAVE_LIBSOCKET)
275 target_link_libraries(ptytty PRIVATE socket)
276 list(APPEND LIBS -lsocket)
277 endif()
278 if(HAVE_LIBUTIL)
279 target_link_libraries(ptytty PRIVATE util)
280 list(APPEND LIBS -lutil)
281 endif()
282
283 configure_file(
284 libptytty.pc.in
285 libptytty.pc)
286
287 add_executable(c-sample eg/c-sample.c)
288 target_include_directories(c-sample PRIVATE src)
289 target_link_libraries(c-sample ptytty)
290
291 add_custom_command(
292 OUTPUT ${CMAKE_SOURCE_DIR}/doc/libptytty.3
293 DEPENDS ${CMAKE_SOURCE_DIR}/doc/libptytty.3.pod
294 COMMAND pod2man -n libptytty -r ${PROJECT_VERSION} -q\" -c LIBPTYTTY -s3
295 < ${CMAKE_SOURCE_DIR}/doc/libptytty.3.pod
296 > ${CMAKE_SOURCE_DIR}/doc/libptytty.3
297 VERBATIM)
298
299 add_custom_command(
300 OUTPUT ${CMAKE_SOURCE_DIR}/README
301 DEPENDS ${CMAKE_SOURCE_DIR}/doc/libptytty.3.pod
302 COMMAND pod2text
303 < ${CMAKE_SOURCE_DIR}/doc/libptytty.3.pod
304 > ${CMAKE_SOURCE_DIR}/README
305 VERBATIM)
306
307 add_custom_target(alldoc
308 DEPENDS ${CMAKE_SOURCE_DIR}/doc/libptytty.3
309 ${CMAKE_SOURCE_DIR}/README)
310
311 install(
312 TARGETS ptytty
313 DESTINATION ${CMAKE_INSTALL_LIBDIR})
314 install(
315 FILES src/libptytty.h
316 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
317 install(
318 FILES doc/libptytty.3
319 DESTINATION ${CMAKE_INSTALL_MANDIR}/man3)
320 install(
321 FILES ${CMAKE_BINARY_DIR}/libptytty.pc
322 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
323
324 set(DISTFILES
325 CMakeLists.txt
326 COPYING
327 Changes
328 README
329 config.h.cmake
330 doc/libptytty.3
331 doc/libptytty.3.pod
332 eg/c-sample.c
333 libptytty.pc.in
334 src/c-api.C
335 src/ecb.h
336 src/estl.h
337 src/fdpass.C
338 src/libptytty.h
339 src/logging.C
340 src/proxy.C
341 src/ptytty.C
342 src/ptytty.h
343 src/ptytty_conf.h)
344
345 set(DISTDIR ${PROJECT_NAME}-${PROJECT_VERSION})
346
347 add_custom_target(distdir
348 DEPENDS alldoc
349 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
350 COMMAND rm -rf ${CMAKE_BINARY_DIR}/${DISTDIR}
351 COMMAND rsync -aRv ${DISTFILES} ${CMAKE_BINARY_DIR}/${DISTDIR})
352
353 add_custom_target(dist
354 COMMAND tar cvf - ${DISTDIR} | gzip -vf9 > ${DISTDIR}.tar.gz)
355
356 add_dependencies(dist distdir)