aboutsummaryrefslogtreecommitdiff
path: root/configure
blob: bbc2a5ca3fd0c6e2af8a1ba1001a8ebadd64cc34 (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
#!/bin/sh
#
# Copyright (c) 2021, 2022 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

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

# Output file descriptor usage:
# 1 (stdout): config.h, Makefile.local
# 2 (stderr): original stderr, usually to the console
# 3: config.log

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

# --------
# default settings: initialize all vars here such that nothing is
# leaked from the environment except for CC, CFLAGS and LDFLAGS

VERSION=1.8.5

CC=`printf "all:\\n\\t@echo \\\$(CC)\\n" | make ${MAKE_FLAGS} -sf -`

if [ -z "${CFLAGS}" ]; then
	CFLAGS=`printf "all:\\n\\t@echo \\\$(CFLAGS)\\n" | make ${MAKE_FLAGS} -sf -`
fi

CFLAGS="${CFLAGS} -W -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes"
CFLAGS="${CFLAGS} -Wwrite-strings -Wno-unused-parameter"

if [ -z "${LDFLAGS}" ]; then
	LDFLAGS=`printf "all:\\n\\t@echo \\\$(LDFLAGS)\\n" | make ${MAKE_FLAGS} -sf -`
	LDFLAGS="-ltls -levent -lcrypto"
fi

LD_IMSG=
STATIC=
YACC=yacc

COBJS=

DISABLE_SANDBOX=0

NEED_GNU_SOURCE=0
NEED_OPENBSD_SOURCE=0

PREFIX="/usr/local"
BINDIR=

INSTALL="install"

add_library() {
	if pkg-config "$1"; then
		CFLAGS="${CFLAGS} $(pkg-config --cflags "$1")"
		LDFLAGS="${LDFLAGS} $(pkg-config --libs "$1")"
	fi
}

# try to auto detect CFLAGS and LDFLAGS
if command -v pkg-config >/dev/null; then
	add_library "libtls"
	add_library "openssl"

	case "$(uname)" in
		OpenBSD)
			# use libevent and imsg in base
			;;
		*)
			add_library "libevent"
			add_library "libimsg"
			;;
	esac

	case "$(uname)" in
		*BSD|DragonFly|Darwin)
			;;
		*)
			add_library "libbsd-ctor libbsd-overlay"
			;;
	esac
fi

# auto detect yacc/bison
command -v ${YACC} >/dev/null || {
	echo "${YACC} not found: trying bison" 1>&2
	echo "${YACC} not found: trying bison" 1>&3
	YACC=bison
	command -v ${YACC} >/dev/null || {
		echo "${YACC} not found: giving up" 1>&2
		echo "${YACC} not found: giving up" 1>&3
	}
}

# --------
# allow certain variables to be overridden on the command line

for keyvals in "$@"; do
	if [ "$keyvals" = "--disable-sandbox" ]; then
		DISABLE_SANDBOX=1
		continue
	fi

	if [ "$keyvals" = "--enable-sandbox" ]; then
		DISABLE_SANDBOX=0
		continue
	fi

	key=`echo $keyvals | cut -s -d '=' -f1`
	if [ -z "$key" ]; then
		echo "$0: invalid key-value: $keyvals" 1>&2
		exit 1
	fi
	val=`echo $keyvals | cut -d '=' -f 2-`
	case "$key" in
		BINDIR)   BINDIR="$val" ;;
		CC)       CC="$val" ;;
		CFLAGS)   CFLAGS="$val" ;;
		DESTDIR)  DESTDIR="$val" ;;
		LDFLAGS)  LDFLAGS="$val" ;;
		MANDIR)   MANDIR="$val" ;;
		PREFIX)   PREFIX="$val" ;;
		YACC)     YACC="$val" ;;
		--prefix) PREFIX="$val" ;;
		*)
			echo "$0: invalid key: $key" 1>&2
			exit 1
	esac
done

# --------
# Allow configure.local to override all variables, default settings,
# command-line arguments, and tested features, above.

if [ -r ./configure.local ]; then
	echo "configure.local: reading..." 1>&2
	echo "configure.local: reading..." 1>&3
	cat ./configure.local 1>&3
	. ./configure.local
else
	echo "configure.local: no (fully automatic configuration)" 1>&2
	echo "configure.local: no (fully automatic configuration)" 1>&3
fi

# --------
# tests functions

# Check whether this HAVE_ setting is manually overridden.
# If yes, use the override, if no, do not decide anything yet.
# Arguments: test file name, test var name, manual value
ismanual() {
	[ -z "${3}" ] && return 1
	echo "tested ${1}: HAVE_${2}=${3} (manual)" 1>&2
	echo "tested ${1}: HAVE_${2}=${3} (manual)" 1>&3
	echo 1>&3
	return 0
}

# Run a single autoconfiguration test.
# In case of success, enable the feature.
# In case of failure, do not decide anything yet.
# Arguments: test file name, test var name, additional CFLAGS
singletest() {
	n=${1}${3}
	cat 1>&3 << __HEREDOC__
testing ${n} ...
${COMP} -o have/${1} have/${1}.c ${3} ${LDFLAGS}
__HEREDOC__

	if ${COMP} -o "have/${1}" "have/${1}.c" ${3} ${LDFLAGS} 1>&3 2>&3
	then
		echo "tested ${n}: yes" 1>&2
		echo "tested ${n}: yes" 1>&3
		echo 1>&3
		eval HAVE_${2}=1
		[ "${3}" = "-D_GNU_SOURCE" ] && NEED_GNU_SOURCE=1
		[ "${3}" = "-D_OPENBSD_SOURCE" ] && NEED_OPENBSD_SOURCE=1
		[ "${3}" = "-lutil" ] && LD_IMSG="-lutil"
		rm "have/${1}"
		return 0
	else
		echo "tested ${n}: no (compilation failed)" 1>&2
		echo "result of ${n}: ${CC} failed with exit status $?" 1>&3
		echo "result of compiling ${n}: no" 1>&3
		echo 1>&3
		return 1
	fi
}

# Run a complete autoconfiguration test, including the check for
# a manual override and disabling the feature on failure.
# Arguments: test file name, test var name, additional CFLAGS
# The final argument can optionally be repeated a second time.
runtest() {
	eval _manual=\${HAVE_${2}}
	ismanual "${1}" "${2}" "${_manual}" && return 0
	singletest "${1}" "${2}" "${3}" && return 0
	[ -n "${4}" ] && singletest "${1}" "${2}" "${4}" && return 0
	eval HAVE_${2}=0
	return 1
}

# --------
# compiler options

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

echo "selected CFLAGS=\"${CFLAGS}\"" 1>&2
echo "selected CFLAGS=\"${CFLAGS}\"" 1>&3
echo 1>&3

if [ -n "${STATIC}" ]; then
	echo "selected STATIC=\"${STATIC}\" (manual)" 1>&2
	echo "selected STATIC=\"${STATIC}\" (manual)" 1>&3
	echo 1>&3
else
	runtest noop STATIC -static || true
	[ ${HAVE_STATIC} -eq 0 ] || STATIC="-static"
	echo "selected STATIC=\"${STATIC}\"" 1>&2
	echo "selected STATIC=\"${STATIC}\"" 1>&3
	echo 1>&3
fi

# --------
# tests for config.h

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 || true
runtest landlock	LANDLOCK	|| true
runtest libevent	LIBEVENT	|| true
runtest libevent2	LIBEVENT2	|| true
runtest libtls		LIBTLS		|| true
runtest memmem		MEMMEM		"" -D_GNU_SOURCE || true
runtest openssl		OPENSSL		|| 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	|| 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	|| true
runtest tree_h		TREE_H		|| true
runtest vasprintf	VASPRINTF	"" -D_GNU_SOURCE || 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_QUEUE_H} -eq 0 -o ${HAVE_IMSG} -eq 0 -o ${HAVE_TREE_H} -eq 0 ]; then
	CFLAGS="${CFLAGS} -I ${PWD}/compat"
fi

# --------
# write config.h

exec > config.h

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

[ ${NEED_GNU_SOURCE} -eq 0 ] || echo "#define _GNU_SOURCE"
[ ${NEED_OPENBSD_SOURCE} -eq 0 ] || echo "#define _OPENBSD_SOURCE"

[ ${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>"

cat <<__HEREDOC__

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

#define HAVE_ERR		${HAVE_ERR}
#define HAVE_EXPLICIT_BZERO	${HAVE_EXPLICIT_BZERO}
#define HAVE_FREEZERO		${HAVE_FREEZERO}
#define HAVE_GETDTABLECOUNT	${HAVE_GETDTABLECOUNT}
#define HAVE_GETDTABLESIZE	${HAVE_GETDTABLESIZE}
#define HAVE_GETPROGNAME	${HAVE_GETPROGNAME}
#define HAVE_IMSG		${HAVE_IMSG}
#define HAVE_LANDLOCK		${HAVE_LANDLOCK}
#define HAVE_LIBEVENT		${HAVE_LIBEVENT}
#define HAVE_LIBEVENT2		${HAVE_LIBEVENT2}
#define HAVE_PROGRAM_INVOCATION_SHORT_NAME ${HAVE_PROGRAM_INVOCATION_SHORT_NAME}
#define HAVE_PR_SET_NAME	${HAVE_PR_SET_NAME}
#define HAVE_QUEUE_H		${HAVE_QUEUE_H}
#define HAVE_REALLOCARRAY	${HAVE_REALLOCARRAY}
#define HAVE_RECALLOCARRAY	${HAVE_RECALLOCARRAY}
#define HAVE_SETPROCTITLE	${HAVE_SETPROCTITLE}
#define HAVE_SETRESGID		${HAVE_SETRESGID}
#define HAVE_SETRESUID		${HAVE_SETRESUID}
#define HAVE_STRLCAT		${HAVE_STRLCAT}
#define HAVE_STRLCPY		${HAVE_STRLCPY}
#define HAVE_STRTONUM		${HAVE_STRTONUM}
#define HAVE_TREE_H		${HAVE_TREE_H}
#define HAVE_VASPRINTF		${HAVE_VASPRINTF}

__HEREDOC__

[ ${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*, ...);"
	COBJS="${COBJS} compat/err.o"
else
	echo "#include <err.h>"
fi
if [ ${HAVE_EXPLICIT_BZERO} -eq 0 ]; then
	echo "extern	void	 explicit_bzero(void*, size_t);"
	COBJS="${COBJS} compat/explicit_bzero.o"
fi
if [ ${HAVE_FREEZERO} -eq 0 ]; then
	echo "extern	void	 freezero(void*, size_t);"
	COBJS="${COBJS} compat/freezero.o"
fi
if [ ${HAVE_GETDTABLECOUNT} -eq 0 ]; then
	echo "extern	int	 getdtablecount(void);"
	COBJS="${COBJS} compat/getdtablecount.o"
fi
if [ ${HAVE_GETDTABLESIZE} -eq 0 ]; then
	echo "extern	int	 getdtablesize(void);"
	COBJS="${COBJS} compat/getdtablesize.o"
fi
if [ ${HAVE_GETPROGNAME} -eq 0 ]; then
	echo "extern	const char *getprogname(void);"
	COBJS="${COBJS} compat/getprogname.o"
fi
if [ ${HAVE_IMSG} -eq 0 ]; then
	COBJS="${COBJS} compat/imsg.o compat/imsg-buffer.o"
fi
if [ ${HAVE_MEMMEM} -eq 0 ]; then
	echo "extern void *memmem(const void *, size_t, const void *, size_t);"
	COBJS="${COBJS} compat/memmem.o"
fi
if [ ${HAVE_REALLOCARRAY} -eq 0 ]; then
	echo "extern	void	*reallocarray(void*, size_t, size_t);"
	COBJS="${COBJS} compat/reallocarray.o"
fi
if [ ${HAVE_RECALLOCARRAY} -eq 0 ]; then
	echo "extern	void	*recallocarray(void*, size_t, size_t, size_t);"
	COBJS="${COBJS} compat/recallocarray.o"
fi
if [ ${HAVE_SETPROCTITLE} -eq 0 ]; then
	echo "extern	void	 setproctitle(const char *fmt, ...);"
	COBJS="${COBJS} compat/setproctitle.o"
fi
if [ ${HAVE_SETRESGID} -eq 0 ]; then
	echo "extern	int	 setresgid(gid_t, gid_t, gid_t);"
	COBJS="${COBJS} compat/setresgid.o"
fi
if [ ${HAVE_SETRESUID} -eq 0 ]; then
	echo "extern	int	 setresuid(uid_t, uid_t, uid_t);"
	COBJS="${COBJS} compat/setresuid.o"
fi
if [ ${HAVE_STRLCAT} -eq 0 ]; then
	echo "extern	size_t	 strlcat(char*, const char*, size_t);"
	COBJS="${COBJS} compat/strlcat.o"
fi
if [ ${HAVE_STRLCPY} -eq 0 ]; then
	echo "extern	size_t	 strlcpy(char*, const char*, size_t);"
	COBJS="${COBJS} compat/strlcpy.o"
fi
if [ ${HAVE_STRTONUM} -eq 0 ]; then
	echo "extern	long long strtonum(const char*, long long, long long, const char**);"
	COBJS="${COBJS} compat/strtonum.o"
fi
if [ ${HAVE_VASPRINTF} -eq 0 ]; then
	echo "extern	int	 vasprintf(char**, const char*, va_list);"
	COBJS="${COBJS} compat/vasprintf.o"
fi
echo "file config.h: written" 1>&2
echo "file config.h: written" 1>&3

# --------
# tests for Makefile.local

exec > Makefile.local

[ -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} ${LD_IMSG}
YACC		= ${YACC}
STATIC		= ${STATIC}
PREFIX		= ${PREFIX}
BINDIR		= ${BINDIR}
MANDIR		= ${MANDIR}
INCLUDEDIR	= ${INCLUDEDIR}
INSTALL		= ${INSTALL}
INSTALL_PROGRAM	= ${INSTALL_PROGRAM}
INSTALL_LIB	= ${INSTALL_LIB}
INSTALL_MAN	= ${INSTALL_MAN}
INSTALL_DATA	= ${INSTALL_DATA}

COBJS		= ${COBJS}

VERSION		= ${VERSION}

__HEREDOC__

echo "file Makefile.local: written" 1>&2
echo "file Makefile.local: written" 1>&3

exit 0