3 # Copyright (c) 2021, 2022, 2023 Omar Polo <op@omarpolo.com>
4 # Copyright (c) 2011, 2013-2022 Ingo Schwarze <schwarze@openbsd.org>
5 # Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
7 # Permission to use, copy, modify, and distribute this software for any
8 # purpose with or without fee is hereby granted, provided that the above
9 # copyright notice and this permission notice appear in all copies.
11 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 echo "usage: $0 [--help] [--prefix=prefix] [OPTION=VALUE...]" >&2
30 if command -v yacc 2>/dev/null >&2; then
32 elif command -v bison 2>/dev/null >&2; then
35 # assume yacc by default. Make will fail building parse.y if
36 # not from a release tarball, but at least it'll have a decent
45 if [ "$RELEASE" = no ]; then
46 CFLAGS=${CFLAGS:--O0 -g3}
48 CFLAGS=${CFLAGS:--O2 -pipe}
51 INSTALL=${INSTALL-install}
52 PREFIX=${PREFIX-/usr/local}
55 CDIAGFLAGS="${CDIAGFLAGS} -W -Wall -Wextra -Wpointer-arith -Wuninitialized"
56 CDIAGFLAGS="${CDIAGFLAGS} -Wstrict-prototypes -Wmissing-prototypes -Wunused"
57 CDIAGFLAGS="${CDIAGFLAGS} -Wsign-compare -Wno-unused-parameter" # -Wshadow
58 CDIAGFLAGS="${CDIAGFLAGS} -Wno-missing-field-initializers"
59 CDIAGFLAGS="${CDIAGFLAGS} -Wno-pointer-sign"
61 LIBTLS=bundled # or system
62 if [ "$(uname || true)" = OpenBSD ]; then
66 while [ $# -gt 0 ]; do
70 if [ "$key" = --help ]; then
74 if [ "$key" = -Werror ]; then
75 CDIAGFLAGS="$CDIAGFLAGS -Werror"
80 if [ "$key" = --enable-sandbox ]; then
85 if [ "$key" = --disable-sandbox ]; then
90 if [ "$key" = "$1" ]; then
91 # if no --xy=, look at the next arg
92 if ! shift 2>/dev/null; then
93 echo "$0: missing value for $key" >&2
100 --bindir) key=BINDIR ;;
101 --mandir) key=MANDIR ;;
102 --prefix) key=PREFIX ;;
103 --with-libtls) key=LIBTLS ;;
109 bundled) LIBTLS=bundled ;;
110 system) LIBTLS=system ;;
114 BINDIR) BINDIR="$val" ;;
116 CFLAGS) CFLAGS="$val" ;;
117 CDIAGFLAGS) CDIAGFLAGS="$val" ;;
118 DISABLE_SANDBOX) DISABLE_SANDBOX="$val" ;;
119 INSTALL) INSTALL="$val" ;;
120 LDFLAGS) LDFLAGS="$val" ;;
121 PKG_CONFIG) PKG_CONFIG="$val" ;;
122 PREFIX) PREFIX="$val" ;;
130 [ -w config.log ] && mv config.log config.log.old
131 [ -w config.h ] && mv config.h config.h.old
134 echo "file config.log: writing..."
137 NEED_OPENBSD_SOURCE=0
138 NEED_LIBBSD_OPENBSD_VIS=0
141 COMP="${CC} ${CFLAGS} -Wno-unused -Werror"
143 # singletest name var extra-cflags extra-libs msg
146 if [ -z "$msg" ]; then
149 elif [ -n "$4" ]; then
152 elif [ "$msg" = no ]; then
158 $COMP have/${1}.c $3 -o test-$1 $LDFLAGS $4
160 if $COMP have/${1}.c $3 -o test-$1 $LDFLAGS $4 >&3 2>&3; then
161 rm -f test-${1} test-${1}.d
163 echo "${1}: $CC$msg succeeded" >&3
170 echo "${1}: $CC$msg failed $?" >&3
179 if singletest "$1" "$2" "${CFLAGS}" "${LIBS}" no; then
184 if [ -f compat/${1}.c ]; then
185 COMPATS="compat/$1.c $COMPATS"
192 # runtest name var extra-cflags extra-libs pkgconfig-name
194 if singletest "$1" "$2" "" ""; then
199 if [ -n "$3" -o -n "$4" ]; then
200 echo "retrying with ${3+$3 }$4" >&3
201 if singletest "$1" "$2" "$3" "$4"; then
203 if [ "$3" = -D_GNU_SOURCE ]; then
207 if [ "$4" = -D_OPENBSD_SOURCE ]; then
208 NEED_OPENBSD_SOURCE=1
211 if [ "$4" = -DLIBBSD_OPENBSD_VIS ]; then
212 NEED_LIBBSD_OPENBSD_VIS=1
225 if [ -n "$5" -a -n "$pkgconfig" ]; then
226 if $pkgconfig $5; then
227 cflags="$($pkgconfig --cflags $5)"
228 ldflags="$($pkgconfig --libs $5)"
229 echo "retrying with pkg-config" >&3
230 if singletest "$1" "$2" "$cflags" "$ldflags"; then
231 CFLAGS="$CFLAGS $cflags"
232 LIBS="$LIBS $ldflags"
239 if [ -f compat/$1.c ]; then
240 COMPATS="compat/$1.c $COMPATS"
247 if [ "$PKG_CONFIG" = no ]; then
248 echo "pkg-config: disabled"
249 elif [ -n "$PKG_CONFIG" ]; then
250 pkgconfig="$PKG_CONFIG"
251 echo "pkg-config: (manual) $PKG_CONFIG"
252 elif command -v pkg-config 2>/dev/null >&2; then
253 pkgconfig="pkg-config"
254 echo "pkg-config: (auto) pkg-config"
256 echo "pkg-config: not found"
259 if singletest noop MMD -MMD; then
260 CFLAGS="${CFLAGS} -MMD"
263 if ! runtest wait_any WAIT_ANY; then
264 CFLAGS="${CFLAGS} -DWAIT_ANY=-1"
269 HAVE_MACHINE_ENDIAN=0
271 runtest endian_h ENDIAN_H || \
272 runtest sys_endian_h SYS_ENDIAN_H || \
273 runtest machine_endian MACHINE_ENDIAN || true
275 if [ ${HAVE_ENDIAN_H} -eq 0 -a \
276 ${HAVE_SYS_ENDIAN_H} -eq 0 -a \
277 ${HAVE_MACHINE_ENDIAN} -eq 0 ]; then
278 echo "FATAL: no endian header found" 1>&2
279 echo "FATAL: no endian header found" 1>&3
283 runtest arc4random ARC4RANDOM || true
284 runtest arc4random_buf ARC4RANDOM_BUF || true
285 runtest err ERR || true
286 runtest explicit_bzero EXPLICIT_BZERO || true
287 runtest freezero FREEZERO || true
288 runtest getdtablecount GETDTABLECOUNT || true
289 runtest getdtablesize GETDTABLESIZE || true
290 runtest getprogname GETPROGNAME || true
291 runtest imsg IMSG "" -lutil libimsg || true
292 runtest landlock LANDLOCK || true
293 runtest libevent LIBEVENT "" -levent libevent_core|| true
294 runtest memmem MEMMEM -D_GNU_SOURCE || true
295 runtest openssl OPENSSL "" '-lcrypto -lssl' 'libcrypto libssl' || true
296 runtest pr_set_name PR_SET_NAME || true
297 runtest program_invocation_short_name PROGRAM_INVOCATION_SHORT_NAME -D_GNU_SOURCE || true
298 runtest queue_h QUEUE_H || true
299 runtest reallocarray REALLOCARRAY -D_OPENBSD_SOURCE || true
300 runtest recallocarray RECALLOCARRAY || true
301 runtest setproctitle SETPROCTITLE || true
302 runtest setresgid SETRESGID -D_GNU_SOURCE || true
303 runtest setresuid SETRESUID -D_GNU_SOURCE || true
304 runtest strlcat STRLCAT || true
305 runtest strlcpy STRLCPY || true
306 runtest strtonum STRTONUM -D_OPENBSD_SOURCE || true
307 runtest timingsafe_memcmp TIMINGSAFE_MEMCMP || true
308 runtest tree_h TREE_H || true
309 runtest vasprintf VASPRINTF -D_GNU_SOURCE || true
310 runtest vis VIS -DLIBBSD_OPENBSD_VIS || true
312 if [ ${HAVE_ARC4RANDOM} -eq 1 -a ${HAVE_ARC4RANDOM_BUF} -eq 0 ]; then
313 COMPATS="compat/arc4random.c ${COMPATS}"
316 if [ ${HAVE_ARC4RANDOM} -eq 0 ]; then
317 runtest getentropy GETENTROPY || true
323 if [ ${HAVE_ARC4RANDOM} -eq 0 -a ${HAVE_GETENTROPY} -eq 1 ]; then
324 COMPATS="compat/getentropy.c ${COMPATS}"
327 if [ "${LIBTLS}" = system ]; then
328 runtest libtls LIBTLS "" -ltls libtls || true
330 # not actually needed
331 HAVE_ASN1_TIME_TM_CMP=1
332 HAVE_ASN1_TIME_TM_CLAMP_NOTAFTER=1
333 HAVE_ASN1_TIME_PARSE=1
336 HAVE_X509_LOOKUP_MEM=1
340 for f in compat/libtls/*.c; do
341 COMPATS="$f ${COMPATS}"
344 CFLAGS="-Icompat/libtls -I../compat/libtls ${CFLAGS}"
346 deptest ASN1_time_tm_cmp ASN1_TIME_TM_CMP || true
347 deptest ASN1_time_tm_clamp_notafter ASN1_TIME_TM_CLAMP_NOTAFTER || true
348 deptest ASN1_time_parse ASN1_TIME_PARSE || true
349 deptest SSL_CTX_use_certificate_chain_mem SSL_CTX_UCCM || true
350 deptest SSL_CTX_load_verify_mem SSL_CTX_LVM || true
351 deptest X509_LOOKUP_mem X509_LOOKUP_MEM || true
354 deptest libevent2 LIBEVENT2 || true
356 if [ ${HAVE_LIBTLS} -eq 0 ]; then
357 echo "FATAL: openssl not found" 1>&2
358 echo "FATAL: openssl not found" 1>&3
362 if [ ${HAVE_OPENSSL} -eq 0 ]; then
363 echo "FATAL: openssl not found" 1>&2
364 echo "FATAL: openssl not found" 1>&3
368 if [ ${HAVE_LIBEVENT} -eq 0 ]; then
369 echo "FATAL: libevent not found" 1>&2
370 echo "FATAL: libevent not found" 1>&3
374 if [ ${HAVE_IMSG} -eq 0 ]; then
375 # compat/imsg.c is implicitly added
376 COMPATS="$COMPATS compat/imsg-buffer.c"
379 if [ ${HAVE_QUEUE_H} -eq 0 -o ${HAVE_IMSG} -eq 0 -o ${HAVE_TREE_H} -eq 0 ]; then
380 CFLAGS="${CFLAGS} -I ${PWD}/compat"
383 if [ ${HAVE_VIS} -eq 0 ]; then
384 CFLAGS="${CFLAGS} -I ${PWD}/compat/vis"
387 if [ $HAVE_LIBEVENT2 -eq 1 ]; then
388 CFLAGS="$CFLAGS -DHAVE_LIBEVENT2=1"
391 if [ $NEED_GNU_SOURCE = 1 ]; then
392 CFLAGS="$CFLAGS -D_GNU_SOURCE"
394 if [ $NEED_OPENBSD_SOURCE = 1 ]; then
395 CFLAGS="$CFLAGS -D_OPENBSD_SOURCE"
397 if [ $NEED_LIBBSD_OPENBSD_VIS = 1 ]; then
398 CFLAGS="$CFLAGS -DLIBBSD_OPENBSD_VIS"
401 CFLAGS="-I. ${CFLAGS} ${CDIAGFLAGS}"
404 echo "config.h: writing.." >&2
408 #error "Do not use C++."
412 [ ${HAVE_STRLCAT} -eq 0 -o ${HAVE_STRLCPY} -eq 0 -o ${HAVE_IMSG} -eq 0 ] \
413 && echo "#include <sys/types.h>"
414 [ ${HAVE_VASPRINTF} -eq 0 ] && echo "#include <stdarg.h>"
416 if [ ${HAVE_QUEUE_H} -eq 1 ]; then
417 echo "#include <sys/queue.h>"
419 echo "#include <queue.h>"
422 if [ ${HAVE_TREE_H} -eq 1 ]; then
423 echo "#include <sys/tree.h>"
425 echo "#include <tree.h>"
428 echo "#include <sys/types.h>"
429 echo "#include <sys/uio.h>"
430 echo "#include <stdint.h>"
431 echo "#include <imsg.h>"
432 echo "#include <limits.h>"
436 #define VERSION "${VERSION}"
437 #define DISABLE_SANDBOX ${DISABLE_SANDBOX}
438 #define HAVE_LANDLOCK ${HAVE_LANDLOCK}
442 if [ ${HAVE_ENDIAN_H} -eq 1 ]; then
443 echo "#include <endian.h>"
444 elif [ ${HAVE_SYS_ENDIAN_H} -eq 1 ]; then
445 echo "#include <sys/endian.h>"
446 elif [ ${HAVE_MACHINE_ENDIAN} -eq 1 ]; then
448 #include <machine/endian.h>
449 #include <libkern/OSByteOrder.h>
450 #define htobe16(x) OSSwapHostToBigInt16(x)
451 #define htobe32(x) OSSwapHostToBigInt32(x)
452 #define htobe64(x) OSSwapHostToBigInt64(x)
456 [ ${HAVE_ARC4RANDOM_BUF} -eq 0 -o \
457 ${HAVE_ASN1_TIME_PARSE} -eq 0 -o \
458 ${HAVE_EXPLICIT_BZERO} -eq 0 -o \
459 ${HAVE_FREEZERO} -eq 0 -o \
460 ${HAVE_GETENTROPY} -eq 0 -o \
461 ${HAVE_REALLOCARRAY} -eq 0 -o \
462 ${HAVE_RECALLOCARRAY} -eq 0 -o \
463 ${HAVE_STRLCAT} -eq 0 -o \
464 ${HAVE_STRLCPY} -eq 0 -o \
465 ${HAVE_STRTONUM} -eq 0 -o \
466 ${HAVE_TIMINGSAFE_MEMCMP} -eq 0 ] && echo "#include <stddef.h>"
468 [ ${HAVE_ARC4RANDOM} -eq 0 ] && echo "#include <stdint.h>"
470 [ ${HAVE_SETRESGID} -eq 0 -o \
471 ${HAVE_SETRESUID} -eq 0 ] && echo "#include <unistd.h>"
473 if [ ${HAVE_GETENTROPY} -eq 1 ]; then
474 echo "#define HAVE_GETENTROPY 1"
476 echo "#define WITH_OPENSSL 1"
477 echo "#define OPENSSL_PRNG_ONLY 1"
480 if [ ${HAVE_ARC4RANDOM} -eq 0 ]; then
481 echo "extern uint32_t arc4random(void);"
483 echo "#define HAVE_ARC4RANDOM 1"
485 if [ ${HAVE_ARC4RANDOM_BUF} -eq 0 ]; then
486 echo "extern void arc4random_buf(void *, size_t);"
488 echo "#define HAVE_ARC4RANDOM_BUF 1"
491 if [ ${HAVE_ASN1_TIME_TM_CMP} -eq 0 ]; then
493 echo "extern int ASN1_time_tm_cmp(struct tm *, struct tm *);"
495 echo "#define HAVE_ASN1_TIME_TM_CMP 1"
498 if [ ${HAVE_ASN1_TIME_TM_CLAMP_NOTAFTER} -eq 0 ]; then
500 echo "extern int ASN1_time_tm_clamp_notafter(struct tm *);"
502 echo "#define HAVE_ASN1_TIME_TM_CLAMP_NOTAFTER 1"
505 if [ ${HAVE_ASN1_TIME_PARSE} -eq 0 ]; then
507 echo "extern int ASN1_time_parse(const char *, size_t, struct tm *, int);"
509 echo "#define HAVE_ASN1_TIME_PARSE 1"
512 if [ ${HAVE_ERR} -eq 0 ]; then
513 echo "extern void err(int, const char*, ...);"
514 echo "extern void errx(int, const char*, ...);"
515 echo "extern void warn(const char*, ...);"
516 echo "extern void warnx(const char*, ...);"
518 echo "#include <err.h>"
520 if [ ${HAVE_EXPLICIT_BZERO} -eq 0 ]; then
521 echo "extern void explicit_bzero(void*, size_t);"
523 if [ ${HAVE_FREEZERO} -eq 0 ]; then
524 echo "extern void freezero(void*, size_t);"
526 if [ ${HAVE_GETDTABLECOUNT} -eq 0 ]; then
527 echo "extern int getdtablecount(void);"
529 if [ ${HAVE_GETDTABLESIZE} -eq 0 ]; then
530 echo "extern int getdtablesize(void);"
532 if [ ${HAVE_GETENTROPY} -eq 0 ]; then
533 echo "extern int getentropy(void *, size_t);"
535 if [ ${HAVE_GETPROGNAME} -eq 0 ]; then
536 echo "extern const char *getprogname(void);"
538 if [ ${HAVE_MEMMEM} -eq 0 ]; then
539 echo "extern void *memmem(const void *, size_t, const void *, size_t);"
541 if [ ${HAVE_REALLOCARRAY} -eq 0 ]; then
542 echo "extern void *reallocarray(void*, size_t, size_t);"
544 if [ ${HAVE_RECALLOCARRAY} -eq 0 ]; then
545 echo "extern void *recallocarray(void*, size_t, size_t, size_t);"
547 if [ ${HAVE_SETPROCTITLE} -eq 0 ]; then
548 echo "extern void setproctitle(const char *fmt, ...);"
550 if [ ${HAVE_SETRESGID} -eq 0 ]; then
551 echo "extern int setresgid(gid_t, gid_t, gid_t);"
553 if [ ${HAVE_SETRESUID} -eq 0 ]; then
554 echo "extern int setresuid(uid_t, uid_t, uid_t);"
556 if [ ${HAVE_STRLCAT} -eq 0 ]; then
557 echo "extern size_t strlcat(char*, const char*, size_t);"
559 if [ ${HAVE_STRLCPY} -eq 0 ]; then
560 echo "extern size_t strlcpy(char*, const char*, size_t);"
562 if [ ${HAVE_STRTONUM} -eq 0 ]; then
563 echo "extern long long strtonum(const char*, long long, long long, const char**);"
565 if [ ${HAVE_TIMINGSAFE_MEMCMP} -eq 0 ]; then
566 echo "extern int timingsafe_memcmp(const void *, const void *, size_t);"
568 if [ ${HAVE_VASPRINTF} -eq 0 ]; then
569 echo "extern int vasprintf(char**, const char*, va_list);"
572 if [ ${HAVE_ASN1_TIME_TM_CMP} -eq 0 ]; then
573 echo "#include <openssl/asn1.h>"
575 echo "int ASN1_time_tm_cmp(struct tm *, struct tm *);"
577 echo "#define HAVE_ASN1_TIME_TM_CMP 1"
580 if [ ${HAVE_SSL_CTX_UCCM} -eq 0 -o ${HAVE_SSL_CTX_LVM} -eq 0 ]; then
581 echo "#include <openssl/ssl.h>"
584 if [ ${HAVE_SSL_CTX_UCCM} -eq 0 ]; then
585 echo "int SSL_CTX_use_certificate_chain_mem(SSL_CTX *, void *, int);"
587 echo "#define HAVE_SSL_CTX_USE_CERTIFICATE_CHAIN_MEM 1"
590 if [ ${HAVE_SSL_CTX_LVM} -eq 0 ]; then
591 echo "int SSL_CTX_load_verify_mem(SSL_CTX *, void *, int);"
593 echo "#define HAVE_SSL_CTX_LOAD_VERIFY_MEM 1"
596 if [ ${HAVE_X509_LOOKUP_MEM} -eq 0 ]; then
597 echo "#include <openssl/x509_vfy.h>"
598 echo "X509_LOOKUP_METHOD *X509_LOOKUP_mem(void);"
600 echo "#define HAVE_X509_LOOKUP_MEM 1"
606 #define __dead __attribute__((noreturn))
609 /* Linux and OpenBSD have LOGIN_NAME_MAX, FreeBSD MAXLOGNAME. */
610 #ifndef LOGIN_NAME_MAX
611 # if defined(MAXLOGNAME)
612 # define LOGIN_NAME_MAX MAXLOGNAME
613 # elif defined(_POSIX_LOGIN_NAME_MAX)
614 # define LOGIN_NAME_MAX _POSIX_LOGIN_NAME_MAX
616 # define LOGIN_NAME_MAX 32
620 #ifndef HOST_NAME_MAX
621 # if defined(_POSIX_HOST_NAME_MAX)
622 # define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
624 # define HOST_NAME_MAX 255
629 echo "file config.h: written" 1>&2
630 echo "file config.h: written" 1>&3
634 [ -z "${BINDIR}" ] && BINDIR="\${PREFIX}/bin"
635 [ -z "${MANDIR}" ] && MANDIR="\${PREFIX}/man"
649 INSTALL_PROGRAM = \${INSTALL} -m 0555
650 INSTALL_LIB = \${INSTALL} -m 0444
651 INSTALL_MAN = \${INSTALL} -m 0444
652 INSTALL_DATA = \${INSTALL} -m 0444
660 echo "file config.mk: written" 1>&2
661 echo "file config.mk: written" 1>&3
664 echo "Now run \`make' to compile." >&2