Blob


1 #! /bin/sh
2 #
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>
6 #
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.
10 #
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"
21 #
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.
25 #
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.
29 #
30 # Like so (note no quotes, no period, etc.):
31 #
32 # include Makefile.configure
33 #
34 # If it exists, configure was run; otherwise, it wasn't.
35 #
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.
38 #
39 # See https://github.com/kristapsdz/oconfigure for more.
41 set -e
43 # try to be helpful
44 case "$1" in
45 --help|-h)
46 cat <<EOF
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.
58 Variables available:
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
81 EOF
82 exit 0 ;;
83 esac
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
90 # 3: config.log
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
97 exec 3> config.log
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
103 # allowed.
105 MAKE_FLAGS=""
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=""
111 fi
112 fi
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
117 fi
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"
129 LDADD=
130 LDADD_LIBEVENT=
131 LDADD_LIBEVENT2=
132 LDADD_LIB_SOCKET=
133 LDADD_STATIC=
134 CPPFLAGS=
135 LDFLAGS=
136 DESTDIR=
137 PREFIX="/usr/local"
138 BINDIR=
139 SBINDIR=
140 INCLUDEDIR=
141 LIBDIR=
142 MANDIR=
143 SHAREDIR=
144 INSTALL="install"
145 INSTALL_PROGRAM=
146 INSTALL_LIB=
147 INSTALL_MAN=
148 INSTALL_DATA=
149 PKG_CONFIG=
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
158 CC=clang
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
162 CC=gcc
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
166 exit 1
171 #----------------------------------------------------------------------
172 # Allow certain variables to be overriden on the command line.
173 #----------------------------------------------------------------------
175 while [ $# -gt 0 ]; do
176 key=${1%%=*}
177 val=${1#*=}
179 if [ "$key" = "--prefix" ]; then
180 key=PREFIX
181 if [ "$1" = "--prefix" ]; then
182 if ! shift 2>&1 >/dev/null; then
183 echo "$0: missing value for --prefix" 1>&2
184 exit 1
185 fi
186 val="$1"
187 fi
188 fi
190 if [ "$1" = "$key" ]; then
191 echo "$0: invalid key-value: $1" 1>&2
192 exit 1
193 fi
195 shift
197 case "$key" in
198 LDADD)
199 LDADD="$val" ;;
200 LDADD_LIBEVENT)
201 LDADD_LIBEVENT="$val" ;;
202 LDADD_LIBEVENT2)
203 LDADD_LIBEVENT2="$val" ;;
204 LDADD_LIBSOCKET)
205 LDADD_LIBSOCKET="$val" ;;
206 LDFLAGS)
207 LDFLAGS="$val" ;;
208 CPPFLAGS)
209 CPPFLAGS="$val" ;;
210 DESTDIR)
211 DESTDIR="$val" ;;
212 PREFIX)
213 PREFIX="$val" ;;
214 MANDIR)
215 MANDIR="$val" ;;
216 LIBDIR)
217 LIBDIR="$val" ;;
218 BINDIR)
219 BINDIR="$val" ;;
220 SHAREDIR)
221 SHAREDIR="$val" ;;
222 SBINDIR)
223 SBINDIR="$val" ;;
224 INCLUDEDIR)
225 INCLUDEDIR="$val" ;;
226 PKG_CONFIG)
227 PKG_CONFIG="$val" ;;
228 *)
229 echo "$0: invalid key: $key" 1>&2
230 exit 1
231 esac
232 done
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
239 } || {
240 PKG_CONFIG=false
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 #----------------------------------------------------------------------
254 HAVE_GETEXECNAME=
255 HAVE_GETPROGNAME=
256 HAVE_LIBEVENT=
257 HAVE_LIBEVENT2=0 # may not be checked
258 HAVE_PLEDGE=
259 HAVE_PROGRAM_INVOCATION_SHORT_NAME=
260 HAVE_PR_SET_NAME=
261 HAVE_STRLCAT=
262 HAVE_STRLCPY=
263 HAVE_STRTONUM=
264 HAVE_UNVEIL=
265 HAVE___PROGNAME=
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
277 . ./configure.local
278 else
279 echo "configure.local: no (fully automatic configuration)" 1>&2
280 echo "configure.local: no (fully automatic configuration)" 1>&3
281 fi
283 echo 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
298 ismanual() {
299 [ -z "${3}" ] && return 1
300 echo "${1}: manual (HAVE_${2}=${3})" 1>&2
301 echo "${1}: manual (HAVE_${2}=${3})" 1>&3
302 echo 1>&3
303 return 0
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.
312 singletest() {
313 extralib=""
314 pkgcfs=""
315 pkglib=""
317 cat 1>&3 <<EOF
318 ${1}: testing...
319 ${COMP} -DTEST_${2} ${3} -o test-${1} tests.c ${LDFLAGS} ${4}
320 EOF
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
323 else
324 if [ -n "${5}" ] ; then
325 echo "${1}: ${CC} failed with $? (retrying)" 1>&3
326 cat 1>&3 <<EOF
327 ${1}: testing...
328 ${COMP} -DTEST_${2} ${3} -o test-${1} tests.c ${LDFLAGS} ${5}
329 EOF
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})"
333 else
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}")
339 cat 1>&3 <<EOF
340 ${1}: testing...
341 ${COMP} -DTEST_${2} ${3} ${pkgcfs} -o test-${1} tests.c ${LDFLAGS} ${pkglib}
342 EOF
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})"
346 else
347 echo "${1}: ${CC} failed with $?" 1>&3
348 echo 1>&3
349 return 1
350 fi
351 else
352 echo "${1}: ${CC} failed with $?" 1>&3
353 echo 1>&3
354 return 1
355 fi
356 fi
357 else
358 echo "${1}: ${CC} failed with $?" 1>&3
359 echo 1>&3
360 return 1
361 fi
362 fi
364 rm -f test-${1}.d
366 if [ -n "${pkgcfs}" -o -n "${pkglib}" ]
367 then
368 CFLAGS="${CFLAGS} ${pkgcfs}"
369 eval "LDADD_${2}=\"${pkglib}\""
370 elif [ -n "${extralib}" ]
371 then
372 eval "LDADD_${2}=\"${5}\""
373 elif [ -n "${4}" ]
374 then
375 eval "LDADD_${2}=\"${4}\""
376 fi
378 echo "${1}: yes ${extralib}" 1>&2
379 echo "${1}: yes ${extralib}" 1>&3
380 echo 1>&3
381 eval HAVE_${2}=1
382 rm "test-${1}"
383 return 0
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.
391 runtest() {
392 eval _manual=\${HAVE_${2}}
393 ismanual "${1}" "${2}" "${_manual}" && return 0
394 singletest "${1}" "${2}" "${3}" "${4}" "${5}" "${6}" && return 0
395 echo "${1}: no" 1>&2
396 eval HAVE_${2}=0
397 return 1
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:
406 # runtest
407 # (1) test file
408 # (2) macro to set
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
419 fi
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
441 exit 1
442 fi
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 #----------------------------------------------------------------------
452 exec > config.h
454 # Start with prologue.
456 cat <<EOF
457 #ifndef OCONFIGURE_CONFIG_H
458 #define OCONFIGURE_CONFIG_H
460 #ifdef __cplusplus
461 # error "Do not use C++: this is a C application."
462 #endif
463 #if !defined(__GNUC__) || (__GNUC__ < 4)
464 # define __attribute__(x)
465 #endif
466 #if defined(__linux__) || defined(__MINT__)
467 # define _GNU_SOURCE /* memmem, memrchr, setresuid... */
468 # define _DEFAULT_SOURCE /* le32toh, crypt, ... */
469 #endif
470 #if defined(__NetBSD__)
471 # define _OPENBSD_SOURCE /* reallocarray, etc. */
472 #endif
473 #if defined(__sun)
474 # ifndef _XOPEN_SOURCE /* SunOS already defines */
475 # define _XOPEN_SOURCE /* XPGx */
476 # endif
477 # define _XOPEN_SOURCE_EXTENDED 1 /* XPG4v2 */
478 # ifndef __EXTENSIONS__ /* SunOS already defines */
479 # define __EXTENSIONS__ /* reallocarray, etc. */
480 # endif
481 #endif
482 #if !defined(__BEGIN_DECLS)
483 # define __BEGIN_DECLS
484 #endif
485 #if !defined(__END_DECLS)
486 # define __END_DECLS
487 #endif
489 EOF
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 ]
498 then
499 echo "#include <sys/types.h> /* size_t, mode_t, dev_t */ "
500 echo
501 fi
503 if [ ${HAVE_GETPROGNAME} -eq 0 ]; then
504 echo "#include <stdlib.h>"
505 echo
506 fi
508 if [ ${HAVE_PLEDGE} -eq 0 -o ${HAVE_UNVEIL} -eq 0 ]; then
509 echo "#include <unistd.h>"
510 echo
511 fi
513 if [ ${HAVE_LIBEVENT2} -eq 1 ]; then
514 cat <<EOF
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>
523 EOF
524 fi
526 if [ ${HAVE_LIBEVENT} -eq 1 ]; then
527 cat <<EOF
528 #include <event.h>
529 EOF
530 fi
532 # Now we handle our HAVE_xxxx values.
533 # Most will just be defined as 0 or 1.
535 cat <<EOF
537 /*
538 * Results of configuration feature-testing.
539 */
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);
555 #endif
557 #if !HAVE_STRLCAT
558 extern size_t strlcat(char *, const char *, size_t);
559 #endif
561 #if !HAVE_STRLCPY
562 extern size_t strlcpy(char *, const char *, size_t);
563 #endif
565 #if !HAVE_STRTONUM
566 extern long long strtonum(const char *, long long, long long, const char **);
567 #endif
569 #ifndef __dead
570 # define __dead __attribute__((noreturn))
571 #endif
573 #endif /*!OCONFIGURE_CONFIG_H*/
574 EOF
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"
600 cat <<EOF
601 CC = ${CC}
602 CFLAGS = ${CFLAGS}
603 CPPFLAGS = ${CPPFLAGS}
604 LDADD = ${LDADD} ${LDADD_LIB_SOCKET} ${LDADD_LIBEVENT} ${LDADD_LIBEVENT2}
605 LDADD_STATIC = ${LDADD_STATIC}
606 LDFLAGS = ${LDFLAGS}
607 PREFIX = ${PREFIX}
608 BINDIR = ${BINDIR}
609 SHAREDIR = ${SHAREDIR}
610 SBINDIR = ${SBINDIR}
611 INCLUDEDIR = ${INCLUDEDIR}
612 LIBDIR = ${LIBDIR}
613 MANDIR = ${MANDIR}
614 INSTALL = ${INSTALL}
615 INSTALL_PROGRAM = ${INSTALL_PROGRAM}
616 INSTALL_LIB = ${INSTALL_LIB}
617 INSTALL_MAN = ${INSTALL_MAN}
618 INSTALL_DATA = ${INSTALL_DATA}
619 EOF
621 echo "Makefile.configure: written" 1>&2
622 echo "Makefile.configure: written" 1>&3
624 exit 0