1# configure.ac
2
3AC_INIT([tmux], 3.4)
4AC_PREREQ([2.60])
5
6AC_CONFIG_AUX_DIR(etc)
7AC_CONFIG_LIBOBJ_DIR(compat)
8AM_INIT_AUTOMAKE([foreign subdir-objects])
9
10AC_CANONICAL_HOST
11
12# When CFLAGS isn't set at this stage and gcc is detected by the macro below,
13# autoconf will automatically use CFLAGS="-O2 -g". Prevent that by using an
14# empty default.
15: ${CFLAGS=""}
16
17# Save user CPPFLAGS, CFLAGS and LDFLAGS. We need to change them because
18# AC_CHECK_HEADER doesn't give us any other way to update the include
19# paths. But for Makefile.am we want to use AM_CPPFLAGS and friends.
20SAVED_CFLAGS="$CFLAGS"
21SAVED_CPPFLAGS="$CPPFLAGS"
22SAVED_LDFLAGS="$LDFLAGS"
23
24# Is this oss-fuzz build?
25AC_ARG_ENABLE(
26	fuzzing,
27	AS_HELP_STRING(--enable-fuzzing, build fuzzers)
28)
29AC_ARG_VAR(
30	FUZZING_LIBS,
31	AS_HELP_STRING(libraries to link fuzzing targets with)
32)
33
34# Set up convenient fuzzing defaults before initializing compiler.
35if test "x$enable_fuzzing" = xyes; then
36	AC_DEFINE(NEED_FUZZING)
37	test "x$CC" = x && CC=clang
38	test "x$FUZZING_LIBS" = x && \
39		FUZZING_LIBS="-fsanitize=fuzzer"
40	test "x$SAVED_CFLAGS" = x && \
41		AM_CFLAGS="-g -fsanitize=fuzzer-no-link,address"
42fi
43
44# Set up the compiler in two different ways and say yes we may want to install.
45AC_PROG_CC
46AM_PROG_CC_C_O
47m4_version_prereq(2.70, [AC_PROG_CC], [AC_PROG_CC_C99])
48AC_PROG_CPP
49AC_PROG_EGREP
50AC_PROG_INSTALL
51AC_PROG_YACC
52PKG_PROG_PKG_CONFIG
53AC_USE_SYSTEM_EXTENSIONS
54
55# Default tmux.conf goes in /etc not ${prefix}/etc.
56test "$sysconfdir" = '${prefix}/etc' && sysconfdir=/etc
57
58# Is this --enable-debug?
59case "x$VERSION" in xnext*) enable_debug=yes;; esac
60AC_ARG_ENABLE(
61	debug,
62	AS_HELP_STRING(--enable-debug, enable debug build flags),
63)
64AM_CONDITIONAL(IS_DEBUG, test "x$enable_debug" = xyes)
65
66# Is this a static build?
67AC_ARG_ENABLE(
68	static,
69	AS_HELP_STRING(--enable-static, create a static build)
70)
71if test "x$enable_static" = xyes; then
72	case "$host_os" in
73		*darwin*)
74			AC_MSG_ERROR([static linking is not supported on macOS])
75			;;
76	esac
77	test "x$PKG_CONFIG" != x && PKG_CONFIG="$PKG_CONFIG --static"
78	AM_LDFLAGS="-static $AM_LDFLAGS"
79	LDFLAGS="$AM_LDFLAGS $SAVED_LDFLAGS"
80fi
81
82# Allow default TERM to be set.
83AC_ARG_WITH(
84	TERM,
85	AS_HELP_STRING(--with-TERM, set default TERM),
86	[DEFAULT_TERM=$withval],
87	[DEFAULT_TERM=]
88)
89case "x$DEFAULT_TERM" in
90	xscreen*|xtmux*|x)
91	;;
92	*)
93		AC_MSG_ERROR("unsuitable TERM (must be screen* or tmux*)")
94	;;
95esac
96
97# Do we need fuzzers?
98AM_CONDITIONAL(NEED_FUZZING, test "x$enable_fuzzing" = xyes)
99
100# Is this gcc?
101AM_CONDITIONAL(IS_GCC, test "x$GCC" = xyes -a "x$enable_fuzzing" != xyes)
102
103# Is this Sun CC?
104AC_EGREP_CPP(
105	yes,
106	[
107		#ifdef __SUNPRO_C
108		yes
109		#endif
110	],
111	found_suncc=yes,
112	found_suncc=no
113)
114AM_CONDITIONAL(IS_SUNCC, test "x$found_suncc" = xyes)
115
116# Check for various headers. Alternatives included from compat.h.
117AC_CHECK_HEADERS([ \
118	bitstring.h \
119	dirent.h \
120	fcntl.h \
121	inttypes.h \
122	libproc.h \
123	libutil.h \
124	ndir.h \
125	paths.h \
126	pty.h \
127	stdint.h \
128	sys/dir.h \
129	sys/ndir.h \
130	sys/tree.h \
131	ucred.h \
132	util.h \
133])
134
135# Look for sys_signame.
136AC_SEARCH_LIBS(sys_signame, , AC_DEFINE(HAVE_SYS_SIGNAME))
137
138# Look for fmod.
139AC_CHECK_LIB(m, fmod)
140
141# Look for library needed for flock.
142AC_SEARCH_LIBS(flock, bsd)
143
144# Check for functions that are replaced or omitted.
145AC_CHECK_FUNCS([ \
146	dirfd \
147	flock \
148	prctl \
149	proc_pidinfo \
150	getpeerucred \
151	sysconf
152])
153
154# Check for functions with a compatibility implementation.
155AC_REPLACE_FUNCS([ \
156	asprintf \
157	cfmakeraw \
158	clock_gettime \
159	closefrom \
160	explicit_bzero \
161	fgetln \
162	freezero \
163	getdtablecount \
164	getdtablesize \
165	getpeereid \
166	getline \
167	getprogname \
168	htonll \
169	memmem \
170	ntohll \
171	setenv \
172	setproctitle \
173	strcasestr \
174	strlcat \
175	strlcpy \
176	strndup \
177	strsep \
178])
179AC_FUNC_STRNLEN
180
181# Check if strtonum works.
182AC_MSG_CHECKING([for working strtonum])
183AC_RUN_IFELSE([AC_LANG_PROGRAM(
184		[#include <stdlib.h>],
185		[return (strtonum("0", 0, 1, NULL) == 0 ? 0 : 1);]
186	)],
187	[AC_DEFINE(HAVE_STRTONUM) AC_MSG_RESULT(yes)],
188	[AC_LIBOBJ(strtonum) AC_MSG_RESULT(no)],
189	[AC_LIBOBJ(strtonum) AC_MSG_RESULT(no)]
190)
191
192# Clang sanitizers wrap reallocarray even if it isn't available on the target
193# system. When compiled it always returns NULL and crashes the program. To
194# detect this we need a more complicated test.
195AC_MSG_CHECKING([for working reallocarray])
196AC_RUN_IFELSE([AC_LANG_PROGRAM(
197		[#include <stdlib.h>],
198		[return (reallocarray(NULL, 1, 1) == NULL);]
199	)],
200	AC_MSG_RESULT(yes),
201	[AC_LIBOBJ(reallocarray) AC_MSG_RESULT([no])],
202	[AC_LIBOBJ(reallocarray) AC_MSG_RESULT([no])]
203)
204AC_MSG_CHECKING([for working recallocarray])
205AC_RUN_IFELSE([AC_LANG_PROGRAM(
206		[#include <stdlib.h>],
207		[return (recallocarray(NULL, 1, 1, 1) == NULL);]
208	)],
209	AC_MSG_RESULT(yes),
210	[AC_LIBOBJ(recallocarray) AC_MSG_RESULT([no])],
211	[AC_LIBOBJ(recallocarray) AC_MSG_RESULT([no])]
212)
213
214# Look for clock_gettime. Must come before event_init.
215AC_SEARCH_LIBS(clock_gettime, rt)
216
217# Always use our getopt because 1) glibc's doesn't enforce argument order 2)
218# musl does not set optarg to NULL for flags without arguments (although it is
219# not required to, but it is helpful) 3) there are probably other weird
220# implementations.
221AC_LIBOBJ(getopt)
222
223# Look for libevent. Try libevent_core or libevent with pkg-config first then
224# look for the library.
225PKG_CHECK_MODULES(
226	LIBEVENT_CORE,
227	[libevent_core >= 2],
228	[
229		AM_CPPFLAGS="$LIBEVENT_CORE_CFLAGS $AM_CPPFLAGS"
230		CPPFLAGS="$AM_CPPFLAGS $SAVED_CPPFLAGS"
231		LIBS="$LIBEVENT_CORE_LIBS $LIBS"
232		found_libevent=yes
233	],
234	found_libevent=no
235)
236if test x$found_libevent = xno; then
237	PKG_CHECK_MODULES(
238		LIBEVENT,
239		[libevent >= 2],
240		[
241			AM_CPPFLAGS="$LIBEVENT_CFLAGS $AM_CPPFLAGS"
242			CPPFLAGS="$AM_CPPFLAGS $SAVED_CPPFLAGS"
243			LIBS="$LIBEVENT_LIBS $LIBS"
244			found_libevent=yes
245		],
246		found_libevent=no
247	)
248fi
249if test x$found_libevent = xno; then
250	AC_SEARCH_LIBS(
251		event_init,
252		[event_core event event-1.4],
253		found_libevent=yes,
254		found_libevent=no
255	)
256fi
257AC_CHECK_HEADER(
258	event2/event.h,
259	AC_DEFINE(HAVE_EVENT2_EVENT_H),
260	[
261		AC_CHECK_HEADER(
262			event.h,
263			AC_DEFINE(HAVE_EVENT_H),
264			found_libevent=no
265		)
266	]
267)
268if test "x$found_libevent" = xno; then
269	AC_MSG_ERROR("libevent not found")
270fi
271
272# Look for yacc.
273AC_CHECK_PROG(found_yacc, $YACC, yes, no)
274if test "x$found_yacc" = xno; then
275	AC_MSG_ERROR("yacc not found")
276fi
277
278# Look for ncurses or curses. Try pkg-config first then directly for the
279# library.
280PKG_CHECK_MODULES(
281	LIBTINFO,
282	tinfo,
283	[
284		AM_CPPFLAGS="$LIBTINFO_CFLAGS $AM_CPPFLAGS"
285		CPPFLAGS="$LIBTINFO_CFLAGS $SAVED_CPPFLAGS"
286		LIBS="$LIBTINFO_LIBS $LIBS"
287		found_ncurses=yes
288	],
289	found_ncurses=no
290)
291if test "x$found_ncurses" = xno; then
292	PKG_CHECK_MODULES(
293		LIBNCURSES,
294		ncurses,
295		[
296			AM_CPPFLAGS="$LIBNCURSES_CFLAGS $AM_CPPFLAGS"
297			CPPFLAGS="$LIBNCURSES_CFLAGS $SAVED_CPPFLAGS"
298			LIBS="$LIBNCURSES_LIBS $LIBS"
299			found_ncurses=yes
300		],
301		found_ncurses=no
302	)
303fi
304if test "x$found_ncurses" = xno; then
305	PKG_CHECK_MODULES(
306		LIBNCURSESW,
307		ncursesw,
308		[
309			AM_CPPFLAGS="$LIBNCURSESW_CFLAGS $AM_CPPFLAGS"
310			CPPFLAGS="$LIBNCURSESW_CFLAGS $SAVED_CPPFLAGS"
311			LIBS="$LIBNCURSESW_LIBS $LIBS"
312			found_ncurses=yes
313		],
314		found_ncurses=no
315	)
316fi
317if test "x$found_ncurses" = xno; then
318	AC_SEARCH_LIBS(
319		setupterm,
320		[tinfo terminfo ncurses ncursesw],
321		found_ncurses=yes,
322		found_ncurses=no
323	)
324	if test "x$found_ncurses" = xyes; then
325		AC_CHECK_HEADER(
326			ncurses.h,
327			LIBS="$LIBS -lncurses",
328			found_ncurses=no
329		)
330	fi
331fi
332if test "x$found_ncurses" = xyes; then
333	CPPFLAGS="$CPPFLAGS -DHAVE_NCURSES_H"
334	AC_DEFINE(HAVE_NCURSES_H)
335else
336	AC_CHECK_LIB(
337		curses,
338		setupterm,
339		found_curses=yes,
340		found_curses=no
341	)
342	AC_CHECK_HEADER(
343		curses.h,
344		,
345		found_curses=no
346	)
347	if test "x$found_curses" = xyes; then
348		LIBS="$LIBS -lcurses"
349		CPPFLAGS="$CPPFLAGS -DHAVE_CURSES_H"
350		AC_DEFINE(HAVE_CURSES_H)
351	else
352		AC_MSG_ERROR("curses not found")
353	fi
354fi
355AC_CHECK_FUNCS([ \
356	tiparm \
357	tiparm_s \
358])
359
360# Look for utempter.
361AC_ARG_ENABLE(
362	utempter,
363	AS_HELP_STRING(--enable-utempter, use utempter if it is installed)
364)
365if test "x$enable_utempter" = xyes; then
366	AC_CHECK_HEADER(utempter.h, enable_utempter=yes, enable_utempter=no)
367	if test "x$enable_utempter" = xyes; then
368		AC_SEARCH_LIBS(
369			utempter_add_record,
370			utempter,
371			enable_utempter=yes,
372			enable_utempter=no
373		)
374	fi
375	if test "x$enable_utempter" = xyes; then
376		AC_DEFINE(HAVE_UTEMPTER)
377	else
378		AC_MSG_ERROR("utempter not found")
379	fi
380fi
381
382# Look for utf8proc.
383AC_ARG_ENABLE(
384	utf8proc,
385	AS_HELP_STRING(--enable-utf8proc, use utf8proc if it is installed)
386)
387if test "x$enable_utf8proc" = xyes; then
388	PKG_CHECK_MODULES(
389		LIBUTF8PROC,
390		libutf8proc,
391		[
392			AM_CPPFLAGS="$LIBUTF8PROC_CFLAGS $AM_CPPFLAGS"
393			CPPFLAGS="$LIBUTF8PROC_CFLAGS $SAVED_CPPFLAGS"
394			LIBS="$LIBUTF8PROC_LIBS $LIBS"
395		]
396	)
397	AC_CHECK_HEADER(utf8proc.h, enable_utf8proc=yes, enable_utf8proc=no)
398	if test "x$enable_utf8proc" = xyes; then
399		AC_SEARCH_LIBS(
400			utf8proc_charwidth,
401			utf8proc,
402			enable_utf8proc=yes,
403			enable_utf8proc=no
404		)
405	fi
406	if test "x$enable_utf8proc" = xyes; then
407		AC_DEFINE(HAVE_UTF8PROC)
408	else
409		AC_MSG_ERROR("utf8proc not found")
410	fi
411fi
412AM_CONDITIONAL(HAVE_UTF8PROC, [test "x$enable_utf8proc" = xyes])
413
414# Check for systemd support.
415AC_ARG_ENABLE(
416	systemd,
417	AS_HELP_STRING(--enable-systemd, enable systemd integration)
418)
419if test x"$enable_systemd" = xyes; then
420	PKG_CHECK_MODULES(
421		SYSTEMD,
422		libsystemd,
423		[
424			AM_CPPFLAGS="$SYSTEMD_CFLAGS $AM_CPPFLAGS"
425			CPPFLAGS="$AM_CPPFLAGS $SAVED_CPPFLAGS"
426			LIBS="$SYSTEMD_LIBS $LIBS"
427			found_systemd=yes
428		],
429		found_systemd=no
430	)
431	if test "x$found_systemd" = xyes; then
432		AC_DEFINE(HAVE_SYSTEMD)
433	else
434		AC_MSG_ERROR("systemd not found")
435	fi
436fi
437AM_CONDITIONAL(HAVE_SYSTEMD, [test "x$found_systemd" = xyes])
438AC_ARG_ENABLE(
439	cgroups,
440	AS_HELP_STRING(--disable-cgroups, disable adding panes to new cgroups with systemd)
441)
442if test "x$enable_cgroups" = x; then
443	# Default to the same as $enable_systemd.
444	enable_cgroups=$enable_systemd
445fi
446if test "x$enable_cgroups" = xyes; then
447	if test "x$found_systemd" = xyes; then
448		AC_DEFINE(ENABLE_CGROUPS)
449	else
450		AC_MSG_ERROR("cgroups requires systemd to be enabled")
451	fi
452fi
453
454# Enable sixel support.
455AC_ARG_ENABLE(
456	sixel,
457	AS_HELP_STRING(--enable-sixel, enable sixel images)
458)
459if test "x$enable_sixel" = xyes; then
460	AC_DEFINE(ENABLE_SIXEL)
461fi
462AM_CONDITIONAL(ENABLE_SIXEL, [test "x$enable_sixel" = xyes])
463
464# Check for b64_ntop. If we have b64_ntop, we assume b64_pton as well.
465AC_MSG_CHECKING(for b64_ntop)
466AC_LINK_IFELSE([AC_LANG_PROGRAM(
467	[
468		#include <sys/types.h>
469		#include <netinet/in.h>
470		#include <resolv.h>
471	],
472	[
473		b64_ntop(NULL, 0, NULL, 0);
474	])],
475	found_b64_ntop=yes,
476	found_b64_ntop=no
477)
478AC_MSG_RESULT($found_b64_ntop)
479OLD_LIBS="$LIBS"
480if test "x$found_b64_ntop" = xno; then
481	AC_MSG_CHECKING(for b64_ntop with -lresolv)
482	LIBS="$OLD_LIBS -lresolv"
483	AC_LINK_IFELSE([AC_LANG_PROGRAM(
484		[
485			#include <sys/types.h>
486			#include <netinet/in.h>
487			#include <resolv.h>
488		],
489		[
490			b64_ntop(NULL, 0, NULL, 0);
491		])],
492		found_b64_ntop=yes,
493		found_b64_ntop=no
494	)
495	AC_MSG_RESULT($found_b64_ntop)
496fi
497if test "x$found_b64_ntop" = xno; then
498	AC_MSG_CHECKING(for b64_ntop with -lnetwork)
499	LIBS="$OLD_LIBS -lnetwork"
500	AC_LINK_IFELSE([AC_LANG_PROGRAM(
501		[
502			#include <sys/types.h>
503			#include <netinet/in.h>
504			#include <resolv.h>
505		],
506		[
507			b64_ntop(NULL, 0, NULL, 0);
508		])],
509		found_b64_ntop=yes,
510		found_b64_ntop=no
511	)
512	AC_MSG_RESULT($found_b64_ntop)
513fi
514if test "x$found_b64_ntop" = xyes; then
515	AC_DEFINE(HAVE_B64_NTOP)
516else
517	LIBS="$OLD_LIBS"
518	AC_LIBOBJ(base64)
519fi
520
521# Look for networking libraries.
522AC_SEARCH_LIBS(inet_ntoa, nsl)
523AC_SEARCH_LIBS(socket, socket)
524AC_CHECK_LIB(xnet, socket)
525
526# Check if using glibc and have malloc_trim(3). The glibc free(3) is pretty bad
527# about returning memory to the kernel unless the application tells it when to
528# with malloc_trim(3).
529AC_MSG_CHECKING(if free doesn't work very well)
530AC_LINK_IFELSE([AC_LANG_SOURCE(
531	[
532		#include <stdlib.h>
533		#ifdef __GLIBC__
534		#include <malloc.h>
535		int main(void) {
536			malloc_trim (0);
537			exit(0);
538		}
539		#else
540		no
541		#endif
542	])],
543	found_malloc_trim=yes,
544	found_malloc_trim=no
545)
546AC_MSG_RESULT($found_malloc_trim)
547if test "x$found_malloc_trim" = xyes; then
548	AC_DEFINE(HAVE_MALLOC_TRIM)
549fi
550
551# Check for CMSG_DATA. On some platforms like HP-UX this requires UNIX 95
552# (_XOPEN_SOURCE and _XOPEN_SOURCE_EXTENDED) (see xopen_networking(7)). On
553# others, UNIX 03 (_XOPEN_SOURCE 600, see standards(7) on Solaris).
554XOPEN_DEFINES=
555AC_MSG_CHECKING(for CMSG_DATA)
556AC_EGREP_CPP(
557	yes,
558	[
559		#include <sys/socket.h>
560		#ifdef CMSG_DATA
561		yes
562		#endif
563	],
564	found_cmsg_data=yes,
565	found_cmsg_data=no
566)
567AC_MSG_RESULT($found_cmsg_data)
568if test "x$found_cmsg_data" = xno; then
569	AC_MSG_CHECKING(if CMSG_DATA needs _XOPEN_SOURCE_EXTENDED)
570	AC_EGREP_CPP(
571		yes,
572		[
573			#define _XOPEN_SOURCE 1
574			#define _XOPEN_SOURCE_EXTENDED 1
575			#include <sys/socket.h>
576			#ifdef CMSG_DATA
577			yes
578			#endif
579		],
580		found_cmsg_data=yes,
581		found_cmsg_data=no
582	)
583	AC_MSG_RESULT($found_cmsg_data)
584	if test "x$found_cmsg_data" = xyes; then
585		XOPEN_DEFINES="-D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED"
586	fi
587fi
588if test "x$found_cmsg_data" = xno; then
589	AC_MSG_CHECKING(if CMSG_DATA needs _XOPEN_SOURCE 600)
590	AC_EGREP_CPP(
591		yes,
592		[
593			#define _XOPEN_SOURCE 600
594			#include <sys/socket.h>
595			#ifdef CMSG_DATA
596			yes
597			#endif
598		],
599		found_cmsg_data=yes,
600		found_cmsg_data=no
601	)
602	AC_MSG_RESULT($found_cmsg_data)
603	if test "x$found_cmsg_data" = xyes; then
604		XOPEN_DEFINES="-D_XOPEN_SOURCE=600"
605	else
606		AC_MSG_ERROR("CMSG_DATA not found")
607	fi
608fi
609AC_SUBST(XOPEN_DEFINES)
610
611# Look for err and friends in err.h.
612AC_CHECK_FUNC(err, found_err_h=yes, found_err_h=no)
613AC_CHECK_FUNC(errx, , found_err_h=no)
614AC_CHECK_FUNC(warn, , found_err_h=no)
615AC_CHECK_FUNC(warnx, , found_err_h=no)
616if test "x$found_err_h" = xyes; then
617	AC_CHECK_HEADER(err.h, , found_err_h=no)
618else
619	AC_LIBOBJ(err)
620fi
621
622# Look for imsg_init in libutil.
623AC_SEARCH_LIBS(imsg_init, util, found_imsg_init=yes, found_imsg_init=no)
624if test "x$found_imsg_init" = xyes; then
625	AC_DEFINE(HAVE_IMSG)
626else
627	AC_LIBOBJ(imsg)
628	AC_LIBOBJ(imsg-buffer)
629fi
630
631# Look for daemon, compat/daemon.c used if missing. Solaris 10 has it in
632# libresolv, but no declaration anywhere, so check for declaration as well as
633# function.
634AC_CHECK_FUNC(daemon, found_daemon=yes, found_daemon=no)
635AC_CHECK_DECL(
636	daemon,
637	,
638	found_daemon=no,
639	[
640		#include <stdlib.h>
641		#include <unistd.h>
642	]
643)
644if test "x$found_daemon" = xyes; then
645	AC_DEFINE(HAVE_DAEMON)
646else
647	AC_LIBOBJ(daemon)
648fi
649
650# Look for stravis, compat/{vis,unvis}.c used if missing.
651AC_CHECK_FUNC(stravis, found_stravis=yes, found_stravis=no)
652if test "x$found_stravis" = xyes; then
653	AC_MSG_CHECKING(if strnvis is broken)
654	AC_EGREP_HEADER([strnvis\(char \*, const char \*, size_t, int\)],
655			vis.h,
656			AC_MSG_RESULT(no),
657			[found_stravis=no])
658	if test "x$found_stravis" = xno; then
659		AC_MSG_RESULT(yes)
660	fi
661fi
662if test "x$found_stravis" = xyes; then
663	AC_CHECK_DECL(
664		VIS_DQ,
665		,
666		found_stravis=no,
667		[
668			#include <stdlib.h>
669			#include <vis.h>
670		]
671)
672fi
673if test "x$found_stravis" = xyes; then
674	AC_DEFINE(HAVE_VIS)
675else
676	AC_LIBOBJ(vis)
677	AC_LIBOBJ(unvis)
678fi
679
680# Look for fdforkpty and forkpty in libutil.
681AC_SEARCH_LIBS(fdforkpty, util, found_fdforkpty=yes, found_fdforkpty=no)
682if test "x$found_fdforkpty" = xyes; then
683	AC_DEFINE(HAVE_FDFORKPTY)
684else
685	AC_LIBOBJ(fdforkpty)
686fi
687AC_SEARCH_LIBS(forkpty, util, found_forkpty=yes, found_forkpty=no)
688if test "x$found_forkpty" = xyes; then
689	AC_DEFINE(HAVE_FORKPTY)
690fi
691AM_CONDITIONAL(NEED_FORKPTY, test "x$found_forkpty" = xno)
692
693# Look for kinfo_getfile in libutil.
694AC_SEARCH_LIBS(kinfo_getfile, [util util-freebsd])
695
696# Look for a suitable queue.h.
697AC_CHECK_DECL(
698	TAILQ_CONCAT,
699	found_queue_h=yes,
700	found_queue_h=no,
701	[#include <sys/queue.h>]
702)
703AC_CHECK_DECL(
704	TAILQ_PREV,
705	,
706	found_queue_h=no,
707	[#include <sys/queue.h>]
708)
709AC_CHECK_DECL(
710	TAILQ_REPLACE,
711	,
712	found_queue_h=no,
713	[#include <sys/queue.h>]
714)
715if test "x$found_queue_h" = xyes; then
716	AC_DEFINE(HAVE_QUEUE_H)
717fi
718
719# Look for __progname.
720AC_MSG_CHECKING(for __progname)
721AC_LINK_IFELSE([AC_LANG_SOURCE(
722	[
723		#include <stdio.h>
724		#include <stdlib.h>
725		extern char *__progname;
726		int main(void) {
727			const char *cp = __progname;
728			printf("%s\n", cp);
729			exit(0);
730		}
731	])],
732	[AC_DEFINE(HAVE___PROGNAME) AC_MSG_RESULT(yes)],
733	AC_MSG_RESULT(no)
734)
735
736# Look for program_invocation_short_name.
737AC_MSG_CHECKING(for program_invocation_short_name)
738AC_LINK_IFELSE([AC_LANG_SOURCE(
739	[
740		#include <errno.h>
741		#include <stdio.h>
742		#include <stdlib.h>
743		int main(void) {
744			const char *cp = program_invocation_short_name;
745			printf("%s\n", cp);
746			exit(0);
747		}
748	])],
749	[AC_DEFINE(HAVE_PROGRAM_INVOCATION_SHORT_NAME) AC_MSG_RESULT(yes)],
750	AC_MSG_RESULT(no)
751)
752
753# Look for prctl(PR_SET_NAME).
754AC_CHECK_DECL(
755	PR_SET_NAME,
756	AC_DEFINE(HAVE_PR_SET_NAME),
757	,
758	[#include <sys/prctl.h>]
759)
760
761# Look for setsockopt(SO_PEERCRED).
762AC_CHECK_DECL(
763	SO_PEERCRED,
764	AC_DEFINE(HAVE_SO_PEERCRED),
765	,
766	[#include <sys/socket.h>]
767)
768
769# Look for fcntl(F_CLOSEM).
770AC_CHECK_DECL(
771	F_CLOSEM,
772	AC_DEFINE(HAVE_FCNTL_CLOSEM),
773	,
774	[#include <fcntl.h>]
775)
776
777# Look for /proc/$$.
778AC_MSG_CHECKING(for /proc/\$\$)
779if test -d /proc/$$; then
780	AC_DEFINE(HAVE_PROC_PID)
781	AC_MSG_RESULT(yes)
782else
783	AC_MSG_RESULT(no)
784fi
785
786# Try to figure out what the best value for TERM might be.
787if test "x$DEFAULT_TERM" = x; then
788	DEFAULT_TERM=screen
789	AC_MSG_CHECKING(TERM)
790	AC_RUN_IFELSE([AC_LANG_SOURCE(
791		[
792			#include <stdio.h>
793			#include <stdlib.h>
794			#if defined(HAVE_CURSES_H)
795			#include <curses.h>
796			#elif defined(HAVE_NCURSES_H)
797			#include <ncurses.h>
798			#endif
799			#include <term.h>
800			int main(void) {
801				if (setupterm("screen-256color", -1, NULL) != OK)
802					exit(1);
803				exit(0);
804			}
805		 ])],
806		 [DEFAULT_TERM=screen-256color],
807		 ,
808		 [DEFAULT_TERM=screen]
809	)
810	AC_RUN_IFELSE([AC_LANG_SOURCE(
811		[
812			#include <stdio.h>
813			#include <stdlib.h>
814			#if defined(HAVE_CURSES_H)
815			#include <curses.h>
816			#elif defined(HAVE_NCURSES_H)
817			#include <ncurses.h>
818			#endif
819			#include <term.h>
820			int main(void) {
821				if (setupterm("tmux", -1, NULL) != OK)
822					exit(1);
823				exit(0);
824			}
825		 ])],
826		 [DEFAULT_TERM=tmux],
827		 ,
828		 [DEFAULT_TERM=screen]
829	)
830	AC_RUN_IFELSE([AC_LANG_SOURCE(
831		[
832			#include <stdio.h>
833			#include <stdlib.h>
834			#if defined(HAVE_CURSES_H)
835			#include <curses.h>
836			#elif defined(HAVE_NCURSES_H)
837			#include <ncurses.h>
838			#endif
839			#include <term.h>
840			int main(void) {
841				if (setupterm("tmux-256color", -1, NULL) != OK)
842					exit(1);
843				exit(0);
844			}
845		 ])],
846		 [DEFAULT_TERM=tmux-256color],
847		 ,
848		 [DEFAULT_TERM=screen]
849	)
850	AC_MSG_RESULT($DEFAULT_TERM)
851fi
852AC_SUBST(DEFAULT_TERM)
853
854# Man page defaults to mdoc.
855MANFORMAT=mdoc
856AC_SUBST(MANFORMAT)
857
858# Figure out the platform.
859AC_MSG_CHECKING(platform)
860case "$host_os" in
861	*aix*)
862		AC_MSG_RESULT(aix)
863		PLATFORM=aix
864		;;
865	*darwin*)
866		AC_MSG_RESULT(darwin)
867		PLATFORM=darwin
868		#
869		# macOS uses __dead2 instead of __dead, like FreeBSD. But it defines
870		# __dead away so it needs to be removed before we can replace it.
871		#
872		AC_DEFINE(BROKEN___DEAD)
873		#
874		# macOS CMSG_FIRSTHDR is broken, so redefine it with a working one.
875		# daemon works but has some stupid side effects, so use our internal
876		# version which has a workaround.
877		#
878		AC_DEFINE(BROKEN_CMSG_FIRSTHDR)
879		AC_LIBOBJ(daemon)
880		AC_LIBOBJ(daemon-darwin)
881		#
882		# macOS wcwidth(3) is bad, so complain and suggest using utf8proc
883		# instead.
884		#
885		if test "x$enable_utf8proc" = x; then
886			AC_MSG_NOTICE([])
887			AC_MSG_NOTICE([	   macOS library support for Unicode is very poor,])
888			AC_MSG_NOTICE([	   particularly for complex codepoints like emojis;])
889			AC_MSG_NOTICE([	   to use these correctly, configuring with])
890			AC_MSG_NOTICE([	   --enable-utf8proc is recommended. To build])
891			AC_MSG_NOTICE([	   without anyway, use --disable-utf8proc])
892			AC_MSG_NOTICE([])
893			AC_MSG_ERROR([must give --enable-utf8proc or --disable-utf8proc])
894		fi
895		;;
896	*dragonfly*)
897		AC_MSG_RESULT(dragonfly)
898		PLATFORM=dragonfly
899		;;
900	*linux*)
901		AC_MSG_RESULT(linux)
902		PLATFORM=linux
903		;;
904	*freebsd*)
905		AC_MSG_RESULT(freebsd)
906		PLATFORM=freebsd
907		;;
908	*netbsd*)
909		AC_MSG_RESULT(netbsd)
910		PLATFORM=netbsd
911		;;
912	*openbsd*)
913		AC_MSG_RESULT(openbsd)
914		PLATFORM=openbsd
915		;;
916	*sunos*)
917		AC_MSG_RESULT(sunos)
918		PLATFORM=sunos
919		;;
920	*solaris*)
921		AC_MSG_RESULT(sunos)
922		PLATFORM=sunos
923		case `/usr/bin/nroff --version 2>&1` in
924			*GNU*)
925				# Solaris 11.4 and later use GNU groff.
926				MANFORMAT=mdoc
927				;;
928			*)
929				# Solaris 2.0 to 11.3 use AT&T nroff.
930				MANFORMAT=man
931				;;
932		esac
933		;;
934	*hpux*)
935		AC_MSG_RESULT(hpux)
936		PLATFORM=hpux
937		;;
938	*cygwin*|*msys*)
939		AC_MSG_RESULT(cygwin)
940		PLATFORM=cygwin
941		;;
942	*haiku*)
943		AC_MSG_RESULT(haiku)
944		PLATFORM=haiku
945		;;
946	*)
947		AC_MSG_RESULT(unknown)
948		PLATFORM=unknown
949		;;
950esac
951AC_SUBST(PLATFORM)
952AM_CONDITIONAL(IS_AIX, test "x$PLATFORM" = xaix)
953AM_CONDITIONAL(IS_DARWIN, test "x$PLATFORM" = xdarwin)
954AM_CONDITIONAL(IS_DRAGONFLY, test "x$PLATFORM" = xdragonfly)
955AM_CONDITIONAL(IS_LINUX, test "x$PLATFORM" = xlinux)
956AM_CONDITIONAL(IS_FREEBSD, test "x$PLATFORM" = xfreebsd)
957AM_CONDITIONAL(IS_NETBSD, test "x$PLATFORM" = xnetbsd)
958AM_CONDITIONAL(IS_OPENBSD, test "x$PLATFORM" = xopenbsd)
959AM_CONDITIONAL(IS_SUNOS, test "x$PLATFORM" = xsunos)
960AM_CONDITIONAL(IS_HPUX, test "x$PLATFORM" = xhpux)
961AM_CONDITIONAL(IS_HAIKU, test "x$PLATFORM" = xhaiku)
962AM_CONDITIONAL(IS_UNKNOWN, test "x$PLATFORM" = xunknown)
963
964# Set the default lock command
965DEFAULT_LOCK_CMD="lock -np"
966AC_MSG_CHECKING(lock-command)
967if test "x$PLATFORM" = xlinux; then
968	AC_CHECK_PROG(found_vlock, vlock, yes, no)
969	if test "x$found_vlock" = xyes; then
970		DEFAULT_LOCK_CMD="vlock"
971	fi
972fi
973AC_MSG_RESULT($DEFAULT_LOCK_CMD)
974AC_SUBST(DEFAULT_LOCK_CMD)
975
976
977# Save our CFLAGS/CPPFLAGS/LDFLAGS for the Makefile and restore the old user
978# variables.
979AC_SUBST(AM_CPPFLAGS)
980CPPFLAGS="$SAVED_CPPFLAGS"
981AC_SUBST(AM_CFLAGS)
982CFLAGS="$SAVED_CFLAGS"
983AC_SUBST(AM_LDFLAGS)
984LDFLAGS="$SAVED_LDFLAGS"
985
986# autoconf should create a Makefile.
987AC_CONFIG_FILES(Makefile)
988AC_OUTPUT
989