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 empty to disable
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 command -v pkg-config 2>/dev/null 1>&2 && {
172 PKG_CONFIG=pkg-config
173 echo "pkg-config found" 1>&2
174 echo "pkg-config found" 1>&3
175 } || {
176 echo "pkg-config not found" 1>&2
177 echo "pkg-config not found" 1>&3
180 #----------------------------------------------------------------------
181 # Allow certain variables to be overriden on the command line.
182 #----------------------------------------------------------------------
184 while [ $# -gt 0 ]; do
185 key=${1%%=*}
186 val=${1#*=}
188 if [ "$key" = "--prefix" ]; then
189 key=PREFIX
190 if [ "$1" = "--prefix" ]; then
191 if ! shift 2>&1 >/dev/null; then
192 echo "$0: missing value for --prefix" 1>&2
193 exit 1
194 fi
195 val="$1"
196 fi
197 fi
199 if [ "$1" = "$key" ]; then
200 echo "$0: invalid key-value: $1" 1>&2
201 exit 1
202 fi
204 shift
206 case "$key" in
207 LDADD)
208 LDADD="$val" ;;
209 LDADD_LIBEVENT)
210 LDADD_LIBEVENT="$val" ;;
211 LDADD_LIBEVENT2)
212 LDADD_LIBEVENT2="$val" ;;
213 LDADD_LIBSOCKET)
214 LDADD_LIBSOCKET="$val" ;;
215 LDFLAGS)
216 LDFLAGS="$val" ;;
217 CPPFLAGS)
218 CPPFLAGS="$val" ;;
219 DESTDIR)
220 DESTDIR="$val" ;;
221 PREFIX)
222 PREFIX="$val" ;;
223 MANDIR)
224 MANDIR="$val" ;;
225 LIBDIR)
226 LIBDIR="$val" ;;
227 BINDIR)
228 BINDIR="$val" ;;
229 SHAREDIR)
230 SHAREDIR="$val" ;;
231 SBINDIR)
232 SBINDIR="$val" ;;
233 INCLUDEDIR)
234 INCLUDEDIR="$val" ;;
235 PKG_CONFIG)
236 PKG_CONFIG="$val" ;;
237 *)
238 echo "$0: invalid key: $key" 1>&2
239 exit 1
240 esac
241 done
244 #----------------------------------------------------------------------
245 # These are the values that will be pushed into config.h after we test
246 # for whether they're supported or not.
247 # Each of these must have a runtest(), below.
248 # Please sort by alpha, for clarity.
249 # You WANT to change this.
250 #----------------------------------------------------------------------
252 HAVE_GETEXECNAME=
253 HAVE_GETPROGNAME=
254 HAVE_LIBEVENT=
255 HAVE_LIBEVENT2=0 # may not be checked
256 HAVE_PLEDGE=
257 HAVE_PROGRAM_INVOCATION_SHORT_NAME=
258 HAVE_PR_SET_NAME=
259 HAVE_STRLCAT=
260 HAVE_STRLCPY=
261 HAVE_STRTONUM=
262 HAVE_UNVEIL=
263 HAVE___PROGNAME=
265 #----------------------------------------------------------------------
266 # Allow configure.local to override all variables, default settings,
267 # command-line arguments, and tested features, above.
268 # You PROBABLY DO NOT want to change this.
269 #----------------------------------------------------------------------
271 if [ -r ./configure.local ]; then
272 echo "configure.local: reading..." 1>&2
273 echo "configure.local: reading..." 1>&3
274 cat ./configure.local 1>&3
275 . ./configure.local
276 else
277 echo "configure.local: no (fully automatic configuration)" 1>&2
278 echo "configure.local: no (fully automatic configuration)" 1>&3
279 fi
281 echo 1>&3
283 #----------------------------------------------------------------------
284 # Infrastructure for running tests.
285 # These consists of a series of functions that will attempt to run the
286 # given test file and record its exit into a HAVE_xxx variable.
287 # You DO NOT want to change this.
288 #----------------------------------------------------------------------
290 COMP="${CC} ${CFLAGS} ${CPPFLAGS} -Wno-unused -Werror"
292 # Check whether this HAVE_ setting is manually overridden.
293 # If yes, use the override, if no, do not decide anything yet.
294 # Arguments: lower-case test name, manual value
296 ismanual() {
297 [ -z "${3}" ] && return 1
298 echo "${1}: manual (HAVE_${2}=${3})" 1>&2
299 echo "${1}: manual (HAVE_${2}=${3})" 1>&3
300 echo 1>&3
301 return 0
304 # Run a single autoconfiguration test.
305 # In case of success, enable the feature.
306 # In case of failure, do not decide anything yet.
307 # Arguments: lower-case test name, upper-case test name, additional
308 # CFLAGS, additional LIBS.
310 singletest() {
311 extralib=""
312 pkgcfs=""
313 pkglib=""
315 cat 1>&3 <<EOF
316 ${1}: testing...
317 ${COMP} -DTEST_${2} ${3} -o test-${1} tests.c ${LDFLAGS} ${4}
318 EOF
319 if ${COMP} -DTEST_${2} ${3} -o "test-${1}" tests.c ${LDFLAGS} ${4} 1>&3 2>&3; then
320 echo "${1}: ${CC} succeeded" 1>&3
321 else
322 if [ -n "${5}" ] ; then
323 echo "${1}: ${CC} failed with $? (retrying)" 1>&3
324 cat 1>&3 <<EOF
325 ${1}: testing...
326 ${COMP} -DTEST_${2} ${3} -o test-${1} tests.c ${LDFLAGS} ${5}
327 EOF
328 if ${COMP} -DTEST_${2} ${3} -o "test-${1}" tests.c ${LDFLAGS} ${5} 1>&3 2>&3; then
329 echo "${1}: ${CC} succeeded" 1>&3
330 extralib="(with ${5})"
331 else
332 test -n "${PKG_CONFIG}" -a -n "${6}" && ${PKG_CONFIG} "$6"
333 if [ $? -eq 0 ]; then
334 echo "${1}: ${CC} failed with $? (retrying)" 1>&3
335 pkgcfs=$($PKG_CONFIG --cflags "${6}")
336 pkglib=$($PKG_CONFIG --libs "${6}")
337 cat 1>&3 <<EOF
338 ${1}: testing...
339 ${COMP} -DTEST_${2} ${3} ${pkgcfs} -o test-${1} tests.c ${LDFLAGS} ${pkglib}
340 EOF
341 if ${COMP} -DTEST_${2} ${3} ${pkgcfs} -o test-${1} tests.c ${LDFLAGS} ${pkglib} 1>&3 2>&3; then
342 echo "${1}: ${CC} succeeded" 1>&3
343 extralib="(with ${pkgcfs} ${pkglib})"
344 else
345 echo "${1}: ${CC} failed with $?" 1>&3
346 echo 1>&3
347 return 1
348 fi
349 else
350 echo "${1}: ${CC} failed with $?" 1>&3
351 echo 1>&3
352 return 1
353 fi
354 fi
355 else
356 echo "${1}: ${CC} failed with $?" 1>&3
357 echo 1>&3
358 return 1
359 fi
360 fi
362 rm -f test-${1}.d
364 if [ -n "${pkgcfs}" -o -n "${pkglib}" ]
365 then
366 CFLAGS="${CFLAGS} ${pkgcfs}"
367 eval "LDADD_${2}=\"${pkglib}\""
368 elif [ -n "${extralib}" ]
369 then
370 eval "LDADD_${2}=\"${5}\""
371 elif [ -n "${4}" ]
372 then
373 eval "LDADD_${2}=\"${4}\""
374 fi
376 echo "${1}: yes ${extralib}" 1>&2
377 echo "${1}: yes ${extralib}" 1>&3
378 echo 1>&3
379 eval HAVE_${2}=1
380 rm "test-${1}"
381 return 0
384 # Run a complete autoconfiguration test, including the check for
385 # a manual override and disabling the feature on failure.
386 # Arguments: lower case name, upper case name, additional CFLAGS,
387 # additional LDADD, alternative LDADD, pkg-config name.
389 runtest() {
390 eval _manual=\${HAVE_${2}}
391 ismanual "${1}" "${2}" "${_manual}" && return 0
392 singletest "${1}" "${2}" "${3}" "${4}" "${5}" "${6}" && return 0
393 echo "${1}: no" 1>&2
394 eval HAVE_${2}=0
395 return 1
398 #----------------------------------------------------------------------
399 # Begin running the tests themselves.
400 # All of your tests must be defined here.
401 # Please sort as the HAVE_xxxx values were defined.
402 # You WANT to change this.
403 # It consists of the following columns:
404 # runtest
405 # (1) test file
406 # (2) macro to set
407 # (3) argument to cc *before* -o
408 # (4) argument to cc *after*
409 # (5) alternative argument to cc *after*
410 # (6) name for pkg-config auto-discovery
411 #----------------------------------------------------------------------
413 if runtest -MMD _MMD -MMD; then
414 CFLAGS="${CFLAGS} -MMD"
415 echo "adding -MMD to CFLAGS" 1>&2
416 echo "adding -MMD to CFLAGS" 1>&3
417 fi
419 runtest getexecname GETEXECNAME || true
420 runtest getprogname GETPROGNAME || true
422 runtest libevent LIBEVENT "" "" "-levent" || \
423 runtest libevent2 LIBEVENT2 "" "" "-levent_extra -levent_core" "libevent" || true
425 runtest lib_socket LIB_SOCKET "" "" "-lsocket -lnsl" || true
426 runtest pledge PLEDGE || true
427 runtest program_invocation_short_name PROGRAM_INVOCATION_SHORT_NAME || true
428 runtest PR_SET_NAME PR_SET_NAME || true
429 runtest static STATIC "" "-static" || true
430 runtest strlcat STRLCAT || true
431 runtest strlcpy STRLCPY || true
432 runtest strtonum STRTONUM || true
433 runtest unveil UNVEIL || true
434 runtest __progname __PROGNAME || true
436 if [ "${HAVE_LIBEVENT}" -eq 0 -a "${HAVE_LIBEVENT2}" -eq 0 ]; then
437 echo "Fatal: missing libevent" 1>&2
438 echo "Fatal: missing libevent" 1>&3
439 exit 1
440 fi
442 #----------------------------------------------------------------------
443 # Output writing: generate the config.h file.
444 # This file contains all of the HAVE_xxxx variables necessary for
445 # compiling your source.
446 # You must include "config.h" BEFORE any other variables.
447 # You WANT to change this.
448 #----------------------------------------------------------------------
450 exec > config.h
452 # Start with prologue.
454 cat <<EOF
455 #ifndef OCONFIGURE_CONFIG_H
456 #define OCONFIGURE_CONFIG_H
458 #ifdef __cplusplus
459 # error "Do not use C++: this is a C application."
460 #endif
461 #if !defined(__GNUC__) || (__GNUC__ < 4)
462 # define __attribute__(x)
463 #endif
464 #if defined(__linux__) || defined(__MINT__)
465 # define _GNU_SOURCE /* memmem, memrchr, setresuid... */
466 # define _DEFAULT_SOURCE /* le32toh, crypt, ... */
467 #endif
468 #if defined(__NetBSD__)
469 # define _OPENBSD_SOURCE /* reallocarray, etc. */
470 #endif
471 #if defined(__sun)
472 # ifndef _XOPEN_SOURCE /* SunOS already defines */
473 # define _XOPEN_SOURCE /* XPGx */
474 # endif
475 # define _XOPEN_SOURCE_EXTENDED 1 /* XPG4v2 */
476 # ifndef __EXTENSIONS__ /* SunOS already defines */
477 # define __EXTENSIONS__ /* reallocarray, etc. */
478 # endif
479 #endif
480 #if !defined(__BEGIN_DECLS)
481 # define __BEGIN_DECLS
482 #endif
483 #if !defined(__END_DECLS)
484 # define __END_DECLS
485 #endif
487 EOF
489 # This is just for size_t, mode_t, and dev_t.
490 # Most of these functions, in the real world, pull in <string.h> or
491 # someting that pulls in support for size_t.
492 # Our function declarations are standalone, so specify them here.
494 if [ ${HAVE_STRLCAT} -eq 0 -o \
495 ${HAVE_STRLCPY} -eq 0 ]
496 then
497 echo "#include <sys/types.h> /* size_t, mode_t, dev_t */ "
498 echo
499 fi
501 if [ ${HAVE_GETPROGNAME} -eq 0 ]; then
502 echo "#include <stdlib.h>"
503 echo
504 fi
506 if [ ${HAVE_PLEDGE} -eq 0 -o ${HAVE_UNVEIL} -eq 0 ]; then
507 echo "#include <unistd.h>"
508 echo
509 fi
511 if [ ${HAVE_LIBEVENT2} -eq 1 ]; then
512 cat <<EOF
513 #include <event2/event.h>
514 #include <event2/event_compat.h>
515 #include <event2/event_struct.h>
516 #include <event2/buffer.h>
517 #include <event2/buffer_compat.h>
518 #include <event2/bufferevent.h>
519 #include <event2/bufferevent_struct.h>
520 #include <event2/bufferevent_compat.h>
521 EOF
522 fi
524 if [ ${HAVE_LIBEVENT} -eq 1 ]; then
525 cat <<EOF
526 #include <event.h>
527 EOF
528 fi
530 # Now we handle our HAVE_xxxx values.
531 # Most will just be defined as 0 or 1.
533 cat <<EOF
535 /*
536 * Results of configuration feature-testing.
537 */
538 #define HAVE_GETEXECNAME ${HAVE_GETEXECNAME}
539 #define HAVE_GETPROGNAME ${HAVE_GETPROGNAME}
540 #define HAVE_PLEDGE ${HAVE_PLEDGE}
541 #define HAVE_PROGRAM_INVOCATION_SHORT_NAME ${HAVE_PROGRAM_INVOCATION_SHORT_NAME}
542 #define HAVE_PR_SET_NAME ${HAVE_PR_SET_NAME}
543 #define HAVE_STRLCAT ${HAVE_STRLCAT}
544 #define HAVE_STRLCPY ${HAVE_STRLCPY}
545 #define HAVE_STRTONUM ${HAVE_STRTONUM}
546 #define HAVE_UNVEIL ${HAVE_UNVEIL}
547 #define HAVE___PROGNAME ${HAVE___PROGNAME}
549 /* Now we do our function declarations for missing functions. */
551 #if !HAVE_GETPROGNAME
552 extern const char *getprogname(void);
553 #endif
555 #if !HAVE_STRLCAT
556 extern size_t strlcat(char *, const char *, size_t);
557 #endif
559 #if !HAVE_STRLCPY
560 extern size_t strlcpy(char *, const char *, size_t);
561 #endif
563 #if !HAVE_STRTONUM
564 extern long long strtonum(const char *, long long, long long, const char **);
565 #endif
567 #ifndef __dead
568 # define __dead __attribute__((noreturn))
569 #endif
571 #endif /*!OCONFIGURE_CONFIG_H*/
572 EOF
574 echo "config.h: written" 1>&2
575 echo "config.h: written" 1>&3
577 #----------------------------------------------------------------------
578 # Now we go to generate our Makefile.configure.
579 # This file is simply a bunch of Makefile variables.
580 # They'll work in both GNUmakefile and BSDmakefile.
581 # You MIGHT want to change this.
582 #----------------------------------------------------------------------
584 exec > Makefile.configure
586 [ -z "${BINDIR}" ] && BINDIR="${PREFIX}/bin"
587 [ -z "${SBINDIR}" ] && SBINDIR="${PREFIX}/sbin"
588 [ -z "${INCLUDEDIR}" ] && INCLUDEDIR="${PREFIX}/include"
589 [ -z "${LIBDIR}" ] && LIBDIR="${PREFIX}/lib"
590 [ -z "${MANDIR}" ] && MANDIR="${PREFIX}/man"
591 [ -z "${SHAREDIR}" ] && SHAREDIR="${PREFIX}/share"
593 [ -z "${INSTALL_PROGRAM}" ] && INSTALL_PROGRAM="${INSTALL} -m 0555"
594 [ -z "${INSTALL_LIB}" ] && INSTALL_LIB="${INSTALL} -m 0444"
595 [ -z "${INSTALL_MAN}" ] && INSTALL_MAN="${INSTALL} -m 0444"
596 [ -z "${INSTALL_DATA}" ] && INSTALL_DATA="${INSTALL} -m 0444"
598 cat <<EOF
599 CC = ${CC}
600 CFLAGS = ${CFLAGS}
601 CPPFLAGS = ${CPPFLAGS}
602 LDADD = ${LDADD} ${LDADD_LIB_SOCKET} ${LDADD_LIBEVENT} ${LDADD_LIBEVENT2}
603 LDADD_STATIC = ${LDADD_STATIC}
604 LDFLAGS = ${LDFLAGS}
605 PREFIX = ${PREFIX}
606 BINDIR = ${BINDIR}
607 SHAREDIR = ${SHAREDIR}
608 SBINDIR = ${SBINDIR}
609 INCLUDEDIR = ${INCLUDEDIR}
610 LIBDIR = ${LIBDIR}
611 MANDIR = ${MANDIR}
612 INSTALL = ${INSTALL}
613 INSTALL_PROGRAM = ${INSTALL_PROGRAM}
614 INSTALL_LIB = ${INSTALL_LIB}
615 INSTALL_MAN = ${INSTALL_MAN}
616 INSTALL_DATA = ${INSTALL_DATA}
617 EOF
619 echo "Makefile.configure: written" 1>&2
620 echo "Makefile.configure: written" 1>&3
622 exit 0