aboutsummaryrefslogtreecommitdiff
path: root/configure
blob: 7ae1d5a7a1b485c7e396bb215c903e7b84c33c87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#!/bin/sh
#
# Copyright (c) 2021, 2022, 2023 Omar Polo <op@omarpolo.com>
# Copyright (c) 2011, 2013-2022 Ingo Schwarze <schwarze@openbsd.org>
# Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

set -e

RELEASE=no
VERSION=2.0-unstable

usage()
{
	echo "usage: $0 [--help] [--prefix=prefix] [OPTION=VALUE...]" >&2
	exit 1
}

if command -v yacc 2>/dev/null >&2; then
	YACC=yacc
elif command -v bison 2>/dev/null >&2; then
	YACC=bison
else
	# assume yacc by default.  Make will fail building parse.y if
	# not from a release tarball, but at least it'll have a decent
	# error message.
	YACC=yacc
fi

pkgconfig=

CC=${CC:-cc}

if [ "$RELEASE" = no ]; then
	CFLAGS=${CFLAGS:--O0 -g3}
else
	CFLAGS=${CFLAGS:--O2 -pipe}
fi

PREFIX=${PREFIX-/usr/local}

CDIAGFLAGS=
CDIAGFLAGS="${CDIAGFLAGS} -W -Wall -Wextra -Wpointer-arith -Wuninitialized"
CDIAGFLAGS="${CDIAGFLAGS} -Wstrict-prototypes -Wmissing-prototypes -Wunused"
CDIAGFLAGS="${CDIAGFLAGS} -Wsign-compare -Wno-unused-parameter" # -Wshadow
CDIAGFLAGS="${CDIAGFLAGS} -Wno-missing-field-initializers"
CDIAGFLAGS="${CDIAGFLAGS} -Wpointer-sign"

while [ $# -gt 0 ]; do
	key="${1%%=*}"
	val="${1#*=}"

	if [ "$key" = --help ]; then
		usage
	fi

	if [ "$key" = -Werror ]; then
		CDIAGFLAGS="$CDIAGFLAGS -Werror"
		shift
		continue
	fi

	if [ "$key" = --enable-sandbox ]; then
		key=DISABLE_SANDBOX
		val=0
	fi

	if [ "$key" = --disable-sandbox ]; then
		key=DISABLE_SANDBOX
		val=1
	fi

	if [ "$key" = "$1" ]; then
		# if no --xy=, look at the next arg
		if ! shift 2>/dev/null; then
			echo "$0: missing value for $key" >&2
			exit 1
		fi
		val="$1"
	fi

	case "$key" in
	--bindir)	key=BINDIR ;;
	--mandir)	key=MANDIR ;;
	--prefix)	key=PREFIX ;;
	esac

	case "$key" in
	BINDIR)		BINDIR="$val" ;;
	CC)		CC="$val" ;;
	CFLAGS)		CFLAGS="$val" ;;
	CDIAGFLAGS)	CDIAGFLAGS="$val" ;;
	DISABLE_SANDBOX) DISABLE_SANDBOX="$val" ;;
	LDFLAGS)	LDFLAGS="$val" ;;
	PKG_CONFIG)	PKG_CONFIG="$val" ;;
	PREFIX)		PREFIX="$val" ;;
	YACC)		YACC="$val" ;;
	*)		usage
	esac

	shift
done

[ -w config.log ] && mv config.log config.log.old
[ -w config.h   ] && mv config.h   config.h.old

exec 3> config.log
echo "file config.log: writing..."

NEED_GNU_SOURCE=0
NEED_OPENBSD_SOURCE=0
NEED_LIBBSD_OPENBSD_VIS=0

COMPATS=
COMP="${CC} ${CFLAGS} -Wno-unused -Werror"

# singletest name var extra-cflags extra-libs msg
singletest() {
	msg="$5"
	if [ -z "$msg" ]; then
		if [ -n "$3" ]; then
			msg=" ($3)"
		elif [ -n "$4" ]; then
			msg=" ($4)"
		fi
	elif [ "$msg" = no ]; then
		msg=""
	fi

	cat >&3 <<EOF
${1}: testing...
$COMP have/${1}.c $3 -o test-$1 $LDFLAGS $4
EOF
	if $COMP have/${1}.c $3 -o test-$1 $LDFLAGS $4 >&3 2>&3; then
		rm -f test-${1} test-${1}.d

		echo "${1}: $CC$msg succeeded" >&3
		echo "${1}$msg: yes"
		echo >&3

		return 0
	fi

	echo "${1}: $CC$msg failed $?" >&3
	echo "${1}$msg: no"
	echo >&3

	return 1
}

# deptest name var
deptest() {
	if singletest "$1" "$2" "${CFLAGS}" "${LIBS}" no; then
		eval HAVE_${2}=1
		return 0
	fi

	if [ -f compat/${1}.c ]; then
		COMPATS="compat/$1.c $COMPATS"
	fi

	eval HAVE_${2}=0
	return 1
}

# runtest name var extra-cflags extra-libs pkgconfig-name
runtest() {
	if singletest "$1" "$2" "" ""; then
		eval HAVE_${2}=1
		return 0
	fi

	if [ -n "$3" -o -n "$4" ]; then
		echo "retrying with ${3+$3 }$4" >&3
		if singletest "$1" "$2" "$3" "$4"; then
			eval HAVE_${2}=1
			if [ "$3" = -D_GNU_SOURCE ]; then
				NEED_GNU_SOURCE=1
				return 0
			fi
			if [ "$4" = -D_OPENBSD_SOURCE ]; then
				NEED_OPENBSD_SOURCE=1
				return 0
			fi
			if [ "$4" = -DLIBBSD_OPENBSD_VIS ]; then
				NEED_LIBBSD_OPENBSD_VIS=1
				return 0
			fi
			if [ -n "$3" ]; then
				CFLAGS="$CFLAGS $3"
			fi
			if [ -n "$4" ]; then
				LIBS="$LIBS $4"
			fi
			return 0
		fi
	fi

	if [ -n "$5" -a -n "$pkgconfig" ]; then
		if $pkgconfig $5; then
			cflags="$($pkgconfig --cflags $5)"
			ldflags="$($pkgconfig --libs $5)"
			echo "retrying with pkg-config" >&3
			if singletest "$1" "$2" "$cflags" "$ldflags"; then
				CFLAGS="$CFLAGS $cflags"
				LIBS="$LIBS $ldflags"
				eval HAVE_$2=1
				return 0
			fi
		fi
	fi

	if [ -f compat/$1.c ]; then
		COMPATS="compat/$1.c $COMPATS"
	fi

	eval HAVE_$2=0
	return 1
}

if [ "$PKG_CONFIG" = no ]; then
	echo "pkg-config: disabled"
elif [ -n "$PKG_CONFIG" ]; then
	pkgconfig="$PKG_CONFIG"
	echo "pkg-config: (manual) $PKG_CONFIG"
elif command -v pkg-config 2>/dev/null >&2; then
	pkgconfig="pkg-config"
	echo "pkg-config: (auto) pkg-config"
else
	echo "pkg-config: not found"
fi

if singletest noop MMD -MMD; then
	CFLAGS="${CFLAGS} -MMD"
fi

if ! runtest wait_any WAIT_ANY; then
	CFLAGS="${CFLAGS} -DWAIT_ANY=-1"
fi

HAVE_ENDIAN_H=0
HAVE_SYS_ENDIAN_H=0
HAVE_MACHINE_ENDIAN=0

runtest endian_h	ENDIAN_H	|| \
runtest sys_endian_h	SYS_ENDIAN_H	|| \
runtest machine_endian	MACHINE_ENDIAN	|| true

if [ ${HAVE_ENDIAN_H} -eq 0 -a \
     ${HAVE_SYS_ENDIAN_H} -eq 0 -a \
     ${HAVE_MACHINE_ENDIAN} -eq 0 ]; then
	echo "FATAL: no endian header found" 1>&2
	echo "FATAL: no endian header found" 1>&3
	exit 1
fi

runtest err		ERR		|| true
runtest explicit_bzero	EXPLICIT_BZERO	|| true
runtest freezero	FREEZERO	|| true
runtest getdtablecount	GETDTABLECOUNT	|| true
runtest getdtablesize	GETDTABLESIZE	|| true
runtest getprogname	GETPROGNAME	|| true
runtest imsg		IMSG		"" -lutil libimsg || true
runtest libevent	LIBEVENT	"" -levent libevent_core|| true
runtest libtls		LIBTLS		"" -ltls libtls || true
runtest memmem		MEMMEM		-D_GNU_SOURCE || true
runtest openssl		OPENSSL		"" '-lcrypto -lssl' 'libcrypto libssl' || true
runtest pr_set_name	PR_SET_NAME	|| true
runtest program_invocation_short_name PROGRAM_INVOCATION_SHORT_NAME -D_GNU_SOURCE || true
runtest queue_h		QUEUE_H		|| true
runtest reallocarray	REALLOCARRAY	-D_OPENBSD_SOURCE	|| true
runtest recallocarray	RECALLOCARRAY	|| true
runtest setproctitle	SETPROCTITLE	|| true
runtest setresgid	SETRESGID	-D_GNU_SOURCE || true
runtest setresuid	SETRESUID	-D_GNU_SOURCE || true
runtest strlcat		STRLCAT		|| true
runtest strlcpy		STRLCPY		|| true
runtest strtonum	STRTONUM	-D_OPENBSD_SOURCE	|| true
runtest tree_h		TREE_H		|| true
runtest vasprintf	VASPRINTF	-D_GNU_SOURCE || true
runtest vis		VIS		-DLIBBSD_OPENBSD_VIS	|| true

deptest libevent2	LIBEVENT2	|| true

if [ ${HAVE_LIBTLS} -eq 0 ]; then
	echo "FATAL: libtls not found" 1>&2
	echo "FATAL: libtls not found" 1>&3
	exit 1
fi

if [ ${HAVE_OPENSSL} -eq 0 ]; then
	echo "FATAL: openssl not found" 1>&2
	echo "FATAL: openssl not found" 1>&3
	exit 1
fi

if [ ${HAVE_LIBEVENT} -eq 0 ]; then
	echo "FATAL: libevent not found" 1>&2
	echo "FATAL: libevent not found" 1>&3
	exit 1
fi

if [ ${HAVE_IMSG} -eq 0 ]; then
	# compat/imsg.c is implicitly added
	COMPATS="$COMPATS compat/imsg-buffer.c"
fi

if [ ${HAVE_QUEUE_H} -eq 0 -o ${HAVE_IMSG} -eq 0 -o ${HAVE_TREE_H} -eq 0 ]; then
	CFLAGS="${CFLAGS} -I ${PWD}/compat"
fi

if [ ${HAVE_VIS} -eq 0 ]; then
	CFLAGS="${CFLAGS} -I ${PWD}/compat/vis"
fi

if [ $HAVE_LIBEVENT2 -eq 1 ]; then
	CFLAGS="$CFLAGS -DHAVE_LIBEVENT2=1"
fi

if [ $NEED_GNU_SOURCE = 1 ]; then
	CFLAGS="$CFLAGS -D_GNU_SOURCE"
fi
if [ $NEED_OPENBSD_SOURCE = 1 ]; then
	CFLAGS="$CFLAGS -D_OPENBSD_SOURCE"
fi
if [ $NEED_LIBBSD_OPENBSD_VIS = 1 ]; then
	CFLAGS="$CFLAGS -DLIBBSD_OPENBSD_VIS"
fi

CFLAGS="${CFLAGS} ${CDIAGFLAGS}"

exec > config.h
echo "config.h: writing.." >&2

cat <<__HEREDOC__
#ifdef __cplusplus
#error "Do not use C++."
#endif
__HEREDOC__

[ ${HAVE_STRLCAT} -eq 0 -o ${HAVE_STRLCPY} -eq 0 -o ${HAVE_IMSG} -eq 0 ] \
	&& echo "#include <sys/types.h>"
[ ${HAVE_VASPRINTF} -eq 0 ] && echo "#include <stdarg.h>"

if [ ${HAVE_QUEUE_H} -eq 1 ]; then
	echo "#include <sys/queue.h>"
else
	echo "#include <queue.h>"
fi

if [ ${HAVE_TREE_H} -eq 1 ]; then
	echo "#include <sys/tree.h>"
else
	echo "#include <tree.h>"
fi

echo "#include <sys/types.h>"
echo "#include <sys/uio.h>"
echo "#include <stdint.h>"
echo "#include <imsg.h>"
echo "#include <limits.h>"

cat <<__HEREDOC__

#define VERSION			"${VERSION}"
#define DISABLE_SANDBOX		${DISABLE_SANDBOX}

__HEREDOC__

if [ ${HAVE_ENDIAN_H} -eq 1 ]; then
	echo "#include <endian.h>"
elif [ ${HAVE_SYS_ENDIAN_H} -eq 1 ]; then
	echo "#include <sys/endian.h>"
elif [ ${HAVE_MACHINE_ENDIAN} -eq 1 ]; then
	cat <<__HEREDOC__
#include <machine/endian.h>
#include <libkern/OSByteOrder.h>
#define htobe16(x) OSSwapHostToBigInt16(x)
#define htobe32(x) OSSwapHostToBigInt32(x)
#define htobe64(x) OSSwapHostToBigInt64(x)
__HEREDOC__
fi

[ ${HAVE_EXPLICIT_BZERO} -eq 0 -o \
  ${HAVE_FREEZERO} -eq 0 -o \
  ${HAVE_REALLOCARRAY} -eq 0 -o \
  ${HAVE_RECALLOCARRAY} -eq 0 -o \
  ${HAVE_STRLCAT} -eq 0 -o \
  ${HAVE_STRLCPY} -eq 0 -o \
  ${HAVE_STRTONUM} -eq 0 ] && echo "#include <stddef.h>"

[ ${HAVE_SETRESGID} -eq 0 -o \
  ${HAVE_SETRESUID} -eq 0 ] && echo "#include <unistd.h>"

if [ ${HAVE_ERR} -eq 0 ]; then
	echo "extern	void	 err(int, const char*, ...);"
	echo "extern	void	 errx(int, const char*, ...);"
	echo "extern	void	 warn(const char*, ...);"
	echo "extern	void	 warnx(const char*, ...);"
else
	echo "#include <err.h>"
fi
if [ ${HAVE_EXPLICIT_BZERO} -eq 0 ]; then
	echo "extern	void	 explicit_bzero(void*, size_t);"
fi
if [ ${HAVE_FREEZERO} -eq 0 ]; then
	echo "extern	void	 freezero(void*, size_t);"
fi
if [ ${HAVE_GETDTABLECOUNT} -eq 0 ]; then
	echo "extern	int	 getdtablecount(void);"
fi
if [ ${HAVE_GETDTABLESIZE} -eq 0 ]; then
	echo "extern	int	 getdtablesize(void);"
fi
if [ ${HAVE_GETPROGNAME} -eq 0 ]; then
	echo "extern	const char *getprogname(void);"
fi
if [ ${HAVE_MEMMEM} -eq 0 ]; then
	echo "extern void *memmem(const void *, size_t, const void *, size_t);"
fi
if [ ${HAVE_REALLOCARRAY} -eq 0 ]; then
	echo "extern	void	*reallocarray(void*, size_t, size_t);"
fi
if [ ${HAVE_RECALLOCARRAY} -eq 0 ]; then
	echo "extern	void	*recallocarray(void*, size_t, size_t, size_t);"
fi
if [ ${HAVE_SETPROCTITLE} -eq 0 ]; then
	echo "extern	void	 setproctitle(const char *fmt, ...);"
fi
if [ ${HAVE_SETRESGID} -eq 0 ]; then
	echo "extern	int	 setresgid(gid_t, gid_t, gid_t);"
fi
if [ ${HAVE_SETRESUID} -eq 0 ]; then
	echo "extern	int	 setresuid(uid_t, uid_t, uid_t);"
fi
if [ ${HAVE_STRLCAT} -eq 0 ]; then
	echo "extern	size_t	 strlcat(char*, const char*, size_t);"
fi
if [ ${HAVE_STRLCPY} -eq 0 ]; then
	echo "extern	size_t	 strlcpy(char*, const char*, size_t);"
fi
if [ ${HAVE_STRTONUM} -eq 0 ]; then
	echo "extern	long long strtonum(const char*, long long, long long, const char**);"
fi
if [ ${HAVE_VASPRINTF} -eq 0 ]; then
	echo "extern	int	 vasprintf(char**, const char*, va_list);"
fi

cat <<__HEREDOC__

#ifndef __dead
#define __dead __attribute__((noreturn))
#endif

/* Linux and OpenBSD have LOGIN_NAME_MAX, FreeBSD MAXLOGNAME. */
#ifndef LOGIN_NAME_MAX
# if defined(MAXLOGNAME)
#  define LOGIN_NAME_MAX MAXLOGNAME
# elif defined(_POSIX_LOGIN_NAME_MAX)
#  define LOGIN_NAME_MAX _POSIX_LOGIN_NAME_MAX
# else
#  define LOGIN_NAME_MAX 32
# endif
#endif

#ifndef HOST_NAME_MAX
# if defined(_POSIX_HOST_NAME_MAX)
#  define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
# else
#  define HOST_NAME_MAX 255
# endif
#endif
__HEREDOC__

echo "file config.h: written" 1>&2
echo "file config.h: written" 1>&3

exec > config.mk

[ -z "${BINDIR}"          ] && BINDIR="\${PREFIX}/bin"
[ -z "${MANDIR}"          ] && MANDIR="\${PREFIX}/man"

[ -z "${INSTALL_PROGRAM}" ] && INSTALL_PROGRAM="${INSTALL} -m 0555"
[ -z "${INSTALL_LIB}"     ] && INSTALL_LIB="${INSTALL} -m 0444"
[ -z "${INSTALL_MAN}"     ] && INSTALL_MAN="${INSTALL} -m 0444"
[ -z "${INSTALL_DATA}"    ] && INSTALL_DATA="${INSTALL} -m 0444"

cat << __HEREDOC__
CC		= ${CC}
CFLAGS		= ${CFLAGS}
LDFLAGS		= ${LDFLAGS}
LIBS		= ${LIBS}
YACC		= ${YACC}
STATIC		= ${STATIC}
PREFIX		= ${PREFIX}
BINDIR		= ${BINDIR}
MANDIR		= ${MANDIR}

INSTALL		= install
INSTALL_PROGRAM	= ${INSTALL} -m 0555
INSTALL_LIB	= ${INSTALL} -m 0444
INSTALL_MAN	= ${INSTALL} -m 0444
INSTALL_DATA	= ${INSTALL} -m 0444

COMPATS=	${COMPATS}

VERSION		= ${VERSION}

__HEREDOC__

echo "file config.mk: written" 1>&2
echo "file config.mk: written" 1>&3

echo >&2
echo "Now run \`make' to compile." >&2
echo >&2

exit 0