3 # Copyright (c) 2014, 2015, 2016 Ingo Schwarze <schwarze@openbsd.org>
4 # Copyright (c) 2017, 2018 Kristaps Dzonsons <kristaps@bsd.lv>
5 # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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.
19 OCONFIGURE_VERSION="0.3.8"
22 # This script outputs two files: config.h and Makefile.configure.
23 # It tries to read from configure.local, which contains predefined
24 # values we won't autoconfigure.
26 # If you want to use configure with your project, have your GNUmakefile
27 # or BSDmakefile---whichever---try to import/include Makefile.configure
28 # at the beginning of the file.
30 # Like so (note no quotes, no period, etc.):
32 # include Makefile.configure
34 # If it exists, configure was run; otherwise, it wasn't.
36 # You'll probably want to change parts of this file. I've noted the
37 # parts that you'll probably change in the section documentation.
39 # See https://github.com/kristapsdz/oconfigure for more.
47 \`configure' configures lstun to adapt to many kinds of systems.
49 Usage: $0 [-h] [--prefix=path] [VAR=VALUE]...
51 The options are as follows:
53 -h, --help print this help message
55 --prefix=path equivalent to specifying the PREFIX variable, supported
56 for compatibility with other common "configure" scripts.
60 LDADD generic linker flags
61 LDADD_LIBEVENT linker flags for libevent
62 LDADD_LIBEVENT2 linker flags for libevent2
63 LDADD_LIBSOCKET linker flags for libsocket
64 LDFLAGS extra linker flags
65 CPPFLAGS C preprocessors flags
66 DESTDIR destination directory
67 PREFIX where to install files
68 MANDIR where to install man pages (PREFIX/man)
69 LIBDIR where to install libraries (PREFIX/lib)
70 BINDIR where to install executables (PREFIX/bin)
71 SHAREDIR where to install misc files (PREFIX/share)
72 SBINDIR where to install system executables (PREFIX/sbin)
73 INCLUDEDIR where to install header files (PREFIX/include)
74 PKG_CONFIG path to the pkg-config program or \`false'
76 Additionally, the following environment variables are used if set:
78 CC the C compiler to use (defaults to cc, clang or gcc)
79 CFLAGS generic C compiler flags
85 #----------------------------------------------------------------------
86 # Prepare for running: move aside previous configure runs.
87 # Output file descriptor usage:
88 # 1 (stdout): config.h or Makefile.configure
89 # 2 (stderr): original stderr, usually to the console
91 # You DO NOT want to change this.
92 #----------------------------------------------------------------------
94 [ -w config.log ] && mv config.log config.log.old
95 [ -w config.h ] && mv config.h config.h.old
98 echo "config.log: writing..."
100 # GNU submake prints different output if invoked recursively, which
101 # messes up CC and CFLAGS detection. Pass --no-print-directory if
102 # we have a MAKELEVEL (GNU and FreeBSD make) and the argument is
107 if [ -n "${MAKELEVEL}" ]; then
108 if [ "${MAKELEVEL}" -gt 0 ] ; then
109 MAKE_FLAGS="--no-print-directory"
110 echo "all:" | make ${MAKE_FLAGS} -sf - 2>/dev/null || MAKE_FLAGS=""
114 if [ -n "$MAKE_FLAGS" ]; then
115 echo "GNU submake detected: using --no-print-directory" 1>&2
116 echo "GNU submake detected: using --no-print-directory" 1>&3
119 #----------------------------------------------------------------------
120 # Initialize all variables here such that nothing can leak in from the
121 # environment except for CC and CFLAGS, which we might have passed in.
122 #----------------------------------------------------------------------
124 CC=`printf "all:\\n\\t@echo \\\$(CC)\\n" | make ${MAKE_FLAGS} -sf -`
125 CFLAGS=`printf "all:\\n\\t@echo \\\$(CFLAGS)\\n" | make ${MAKE_FLAGS} -sf -`
126 CFLAGS="${CFLAGS} -g -W -Wall -Wmissing-prototypes -Wstrict-prototypes"
127 CFLAGS="${CFLAGS} -Wmissing-declarations -Wno-unused-parameter"
128 CFLAGS="${CFLAGS} -Wno-sign-compare"
151 # SunOS sets "cc", but this doesn't exist.
152 # It does have gcc, so try that instead.
153 # Prefer clang, though.
155 command -v ${CC} 2>/dev/null 1>&2 || {
156 echo "${CC} not found: trying clang" 1>&2
157 echo "${CC} not found: trying clang" 1>&3
159 command -v ${CC} 2>/dev/null 1>&2 || {
160 echo "${CC} not found: trying gcc" 1>&2
161 echo "${CC} not found: trying gcc" 1>&3
163 command -v ${CC} 2>/dev/null 1>&2 || {
164 echo "gcc not found: giving up" 1>&2
165 echo "gcc not found: giving up" 1>&3
171 #----------------------------------------------------------------------
172 # Allow certain variables to be overriden on the command line.
173 #----------------------------------------------------------------------
175 while [ $# -gt 0 ]; do
179 if [ "$key" = "--prefix" ]; then
181 if [ "$1" = "--prefix" ]; then
182 if ! shift 2>&1 >/dev/null; then
183 echo "$0: missing value for --prefix" 1>&2
190 if [ "$1" = "$key" ]; then
191 echo "$0: invalid key-value: $1" 1>&2
201 LDADD_LIBEVENT="$val" ;;
203 LDADD_LIBEVENT2="$val" ;;
205 LDADD_LIBSOCKET="$val" ;;
229 echo "$0: invalid key: $key" 1>&2
234 test -z "${PKG_CONFIG}" && {
235 command -v pkg-config 2>/dev/null >&2 && {
236 PKG_CONFIG="$(command -v pkg-config)"
237 echo "found pkg-config: $PKG_CONFIG" 1>&2
238 echo "found pkg-config: $PKG_CONFIG" 1>&3
241 echo "pkg-config not found" 1>&2
242 echo "pkg-config not found" 1>&3
246 #----------------------------------------------------------------------
247 # These are the values that will be pushed into config.h after we test
248 # for whether they're supported or not.
249 # Each of these must have a runtest(), below.
250 # Please sort by alpha, for clarity.
251 # You WANT to change this.
252 #----------------------------------------------------------------------
257 HAVE_LIBEVENT2=0 # may not be checked
259 HAVE_PROGRAM_INVOCATION_SHORT_NAME=
267 #----------------------------------------------------------------------
268 # Allow configure.local to override all variables, default settings,
269 # command-line arguments, and tested features, above.
270 # You PROBABLY DO NOT want to change this.
271 #----------------------------------------------------------------------
273 if [ -r ./configure.local ]; then
274 echo "configure.local: reading..." 1>&2
275 echo "configure.local: reading..." 1>&3
276 cat ./configure.local 1>&3
279 echo "configure.local: no (fully automatic configuration)" 1>&2
280 echo "configure.local: no (fully automatic configuration)" 1>&3
285 #----------------------------------------------------------------------
286 # Infrastructure for running tests.
287 # These consists of a series of functions that will attempt to run the
288 # given test file and record its exit into a HAVE_xxx variable.
289 # You DO NOT want to change this.
290 #----------------------------------------------------------------------
292 COMP="${CC} ${CFLAGS} ${CPPFLAGS} -Wno-unused -Werror"
294 # Check whether this HAVE_ setting is manually overridden.
295 # If yes, use the override, if no, do not decide anything yet.
296 # Arguments: lower-case test name, manual value
299 [ -z "${3}" ] && return 1
300 echo "${1}: manual (HAVE_${2}=${3})" 1>&2
301 echo "${1}: manual (HAVE_${2}=${3})" 1>&3
306 # Run a single autoconfiguration test.
307 # In case of success, enable the feature.
308 # In case of failure, do not decide anything yet.
309 # Arguments: lower-case test name, upper-case test name, additional
310 # CFLAGS, additional LIBS.
319 ${COMP} -DTEST_${2} ${3} -o test-${1} tests.c ${LDFLAGS} ${4}
321 if ${COMP} -DTEST_${2} ${3} -o "test-${1}" tests.c ${LDFLAGS} ${4} 1>&3 2>&3; then
322 echo "${1}: ${CC} succeeded" 1>&3
324 if [ -n "${5}" ] ; then
325 echo "${1}: ${CC} failed with $? (retrying)" 1>&3
328 ${COMP} -DTEST_${2} ${3} -o test-${1} tests.c ${LDFLAGS} ${5}
330 if ${COMP} -DTEST_${2} ${3} -o "test-${1}" tests.c ${LDFLAGS} ${5} 1>&3 2>&3; then
331 echo "${1}: ${CC} succeeded" 1>&3
332 extralib="(with ${5})"
334 test -n "${6}" && ${PKG_CONFIG} "$6"
335 if [ $? -eq 0 ]; then
336 echo "${1}: ${CC} failed with $? (retrying)" 1>&3
337 pkgcfs=$($PKG_CONFIG --cflags "${6}")
338 pkglib=$($PKG_CONFIG --libs "${6}")
341 ${COMP} -DTEST_${2} ${3} ${pkgcfs} -o test-${1} tests.c ${LDFLAGS} ${pkglib}
343 if ${COMP} -DTEST_${2} ${3} ${pkgcfs} -o test-${1} tests.c ${LDFLAGS} ${pkglib} 1>&3 2>&3; then
344 echo "${1}: ${CC} succeeded" 1>&3
345 extralib="(with ${pkgcfs} ${pkglib})"
347 echo "${1}: ${CC} failed with $?" 1>&3
352 echo "${1}: ${CC} failed with $?" 1>&3
358 echo "${1}: ${CC} failed with $?" 1>&3
366 if [ -n "${pkgcfs}" -o -n "${pkglib}" ]
368 CFLAGS="${CFLAGS} ${pkgcfs}"
369 eval "LDADD_${2}=\"${pkglib}\""
370 elif [ -n "${extralib}" ]
372 eval "LDADD_${2}=\"${5}\""
375 eval "LDADD_${2}=\"${4}\""
378 echo "${1}: yes ${extralib}" 1>&2
379 echo "${1}: yes ${extralib}" 1>&3
386 # Run a complete autoconfiguration test, including the check for
387 # a manual override and disabling the feature on failure.
388 # Arguments: lower case name, upper case name, additional CFLAGS,
389 # additional LDADD, alternative LDADD, pkg-config name.
392 eval _manual=\${HAVE_${2}}
393 ismanual "${1}" "${2}" "${_manual}" && return 0
394 singletest "${1}" "${2}" "${3}" "${4}" "${5}" "${6}" && return 0
400 #----------------------------------------------------------------------
401 # Begin running the tests themselves.
402 # All of your tests must be defined here.
403 # Please sort as the HAVE_xxxx values were defined.
404 # You WANT to change this.
405 # It consists of the following columns:
409 # (3) argument to cc *before* -o
410 # (4) argument to cc *after*
411 # (5) alternative argument to cc *after*
412 # (6) name for pkg-config auto-discovery
413 #----------------------------------------------------------------------
415 if runtest -MMD _MMD -MMD; then
416 CFLAGS="${CFLAGS} -MMD"
417 echo "adding -MMD to CFLAGS" 1>&2
418 echo "adding -MMD to CFLAGS" 1>&3
421 runtest getexecname GETEXECNAME || true
422 runtest getprogname GETPROGNAME || true
424 runtest libevent LIBEVENT "" "" "-levent" || \
425 runtest libevent2 LIBEVENT2 "" "" "-levent_extra -levent_core" "libevent" || true
427 runtest lib_socket LIB_SOCKET "" "" "-lsocket -lnsl" || true
428 runtest pledge PLEDGE || true
429 runtest program_invocation_short_name PROGRAM_INVOCATION_SHORT_NAME || true
430 runtest PR_SET_NAME PR_SET_NAME || true
431 runtest static STATIC "" "-static" || true
432 runtest strlcat STRLCAT || true
433 runtest strlcpy STRLCPY || true
434 runtest strtonum STRTONUM || true
435 runtest unveil UNVEIL || true
436 runtest __progname __PROGNAME || true
438 if [ "${HAVE_LIBEVENT}" -eq 0 -a "${HAVE_LIBEVENT2}" -eq 0 ]; then
439 echo "Fatal: missing libevent" 1>&2
440 echo "Fatal: missing libevent" 1>&3
444 #----------------------------------------------------------------------
445 # Output writing: generate the config.h file.
446 # This file contains all of the HAVE_xxxx variables necessary for
447 # compiling your source.
448 # You must include "config.h" BEFORE any other variables.
449 # You WANT to change this.
450 #----------------------------------------------------------------------
454 # Start with prologue.
457 #ifndef OCONFIGURE_CONFIG_H
458 #define OCONFIGURE_CONFIG_H
461 # error "Do not use C++: this is a C application."
463 #if !defined(__GNUC__) || (__GNUC__ < 4)
464 # define __attribute__(x)
466 #if defined(__linux__) || defined(__MINT__)
467 # define _GNU_SOURCE /* memmem, memrchr, setresuid... */
468 # define _DEFAULT_SOURCE /* le32toh, crypt, ... */
470 #if defined(__NetBSD__)
471 # define _OPENBSD_SOURCE /* reallocarray, etc. */
474 # ifndef _XOPEN_SOURCE /* SunOS already defines */
475 # define _XOPEN_SOURCE /* XPGx */
477 # define _XOPEN_SOURCE_EXTENDED 1 /* XPG4v2 */
478 # ifndef __EXTENSIONS__ /* SunOS already defines */
479 # define __EXTENSIONS__ /* reallocarray, etc. */
482 #if !defined(__BEGIN_DECLS)
483 # define __BEGIN_DECLS
485 #if !defined(__END_DECLS)
491 # This is just for size_t, mode_t, and dev_t.
492 # Most of these functions, in the real world, pull in <string.h> or
493 # someting that pulls in support for size_t.
494 # Our function declarations are standalone, so specify them here.
496 if [ ${HAVE_STRLCAT} -eq 0 -o \
497 ${HAVE_STRLCPY} -eq 0 ]
499 echo "#include <sys/types.h> /* size_t, mode_t, dev_t */ "
503 if [ ${HAVE_GETPROGNAME} -eq 0 ]; then
504 echo "#include <stdlib.h>"
508 if [ ${HAVE_PLEDGE} -eq 0 -o ${HAVE_UNVEIL} -eq 0 ]; then
509 echo "#include <unistd.h>"
513 if [ ${HAVE_LIBEVENT2} -eq 1 ]; then
515 #include <event2/event.h>
516 #include <event2/event_compat.h>
517 #include <event2/event_struct.h>
518 #include <event2/buffer.h>
519 #include <event2/buffer_compat.h>
520 #include <event2/bufferevent.h>
521 #include <event2/bufferevent_struct.h>
522 #include <event2/bufferevent_compat.h>
526 if [ ${HAVE_LIBEVENT} -eq 1 ]; then
532 # Now we handle our HAVE_xxxx values.
533 # Most will just be defined as 0 or 1.
538 * Results of configuration feature-testing.
540 #define HAVE_GETEXECNAME ${HAVE_GETEXECNAME}
541 #define HAVE_GETPROGNAME ${HAVE_GETPROGNAME}
542 #define HAVE_PLEDGE ${HAVE_PLEDGE}
543 #define HAVE_PROGRAM_INVOCATION_SHORT_NAME ${HAVE_PROGRAM_INVOCATION_SHORT_NAME}
544 #define HAVE_PR_SET_NAME ${HAVE_PR_SET_NAME}
545 #define HAVE_STRLCAT ${HAVE_STRLCAT}
546 #define HAVE_STRLCPY ${HAVE_STRLCPY}
547 #define HAVE_STRTONUM ${HAVE_STRTONUM}
548 #define HAVE_UNVEIL ${HAVE_UNVEIL}
549 #define HAVE___PROGNAME ${HAVE___PROGNAME}
551 /* Now we do our function declarations for missing functions. */
553 #if !HAVE_GETPROGNAME
554 extern const char *getprogname(void);
558 extern size_t strlcat(char *, const char *, size_t);
562 extern size_t strlcpy(char *, const char *, size_t);
566 extern long long strtonum(const char *, long long, long long, const char **);
570 # define __dead __attribute__((noreturn))
573 #endif /*!OCONFIGURE_CONFIG_H*/
576 echo "config.h: written" 1>&2
577 echo "config.h: written" 1>&3
579 #----------------------------------------------------------------------
580 # Now we go to generate our Makefile.configure.
581 # This file is simply a bunch of Makefile variables.
582 # They'll work in both GNUmakefile and BSDmakefile.
583 # You MIGHT want to change this.
584 #----------------------------------------------------------------------
586 exec > Makefile.configure
588 [ -z "${BINDIR}" ] && BINDIR="${PREFIX}/bin"
589 [ -z "${SBINDIR}" ] && SBINDIR="${PREFIX}/sbin"
590 [ -z "${INCLUDEDIR}" ] && INCLUDEDIR="${PREFIX}/include"
591 [ -z "${LIBDIR}" ] && LIBDIR="${PREFIX}/lib"
592 [ -z "${MANDIR}" ] && MANDIR="${PREFIX}/man"
593 [ -z "${SHAREDIR}" ] && SHAREDIR="${PREFIX}/share"
595 [ -z "${INSTALL_PROGRAM}" ] && INSTALL_PROGRAM="${INSTALL} -m 0555"
596 [ -z "${INSTALL_LIB}" ] && INSTALL_LIB="${INSTALL} -m 0444"
597 [ -z "${INSTALL_MAN}" ] && INSTALL_MAN="${INSTALL} -m 0444"
598 [ -z "${INSTALL_DATA}" ] && INSTALL_DATA="${INSTALL} -m 0444"
603 CPPFLAGS = ${CPPFLAGS}
604 LDADD = ${LDADD} ${LDADD_LIB_SOCKET} ${LDADD_LIBEVENT} ${LDADD_LIBEVENT2}
605 LDADD_STATIC = ${LDADD_STATIC}
609 SHAREDIR = ${SHAREDIR}
611 INCLUDEDIR = ${INCLUDEDIR}
615 INSTALL_PROGRAM = ${INSTALL_PROGRAM}
616 INSTALL_LIB = ${INSTALL_LIB}
617 INSTALL_MAN = ${INSTALL_MAN}
618 INSTALL_DATA = ${INSTALL_DATA}
621 echo "Makefile.configure: written" 1>&2
622 echo "Makefile.configure: written" 1>&3