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"
20 MYMENU_VERSION="0.2"
22 #
23 # This script outputs two files: config.h and Makefile.configure.
24 # It tries to read from configure.local, which contains predefined
25 # values we won't autoconfigure.
26 #
27 # If you want to use configure with your project, have your GNUmakefile
28 # or BSDmakefile---whichever---try to import/include Makefile.configure
29 # at the beginning of the file.
30 #
31 # Like so (note no quotes, no period, etc.):
32 #
33 # include Makefile.configure
34 #
35 # If it exists, configure was run; otherwise, it wasn't.
36 #
37 # You'll probably want to change parts of this file. I've noted the
38 # parts that you'll probably change in the section documentation.
39 #
40 # See https://github.com/kristapsdz/oconfigure for more.
42 set -e
44 #----------------------------------------------------------------------
45 # Prepare for running: move aside previous configure runs.
46 # Output file descriptor usage:
47 # 1 (stdout): config.h or Makefile.configure
48 # 2 (stderr): original stderr, usually to the console
49 # 3: config.log
50 # You DO NOT want to change this.
51 #----------------------------------------------------------------------
53 [ -w config.log ] && mv config.log config.log.old
54 [ -w config.h ] && mv config.h config.h.old
56 exec 3> config.log
57 echo "config.log: writing..."
59 # GNU submake prints different output if invoked recursively, which
60 # messes up CC and CFLAGS detection. Pass --no-print-directory if
61 # we have a MAKELEVEL (GNU and FreeBSD make) and the argument is
62 # allowed.
64 MAKE_FLAGS=""
66 if [ -n "${MAKELEVEL}" ]; then
67 if [ "${MAKELEVEL}" -gt 0 ] ; then
68 MAKE_FLAGS="--no-print-directory"
69 echo "all:" | make ${MAKE_FLAGS} -sf - 2>/dev/null || MAKE_FLAGS=""
70 fi
71 fi
73 if [ -n "$MAKE_FLAGS" ]; then
74 echo "GNU submake detected: using --no-print-directory" 1>&2
75 echo "GNU submake detected: using --no-print-directory" 1>&3
76 fi
78 #----------------------------------------------------------------------
79 # Initialize all variables here such that nothing can leak in from the
80 # environment except for CC and CFLAGS, which we might have passed in.
81 #----------------------------------------------------------------------
83 CC=`printf "all:\\n\\t@echo \\\$(CC)\\n" | make ${MAKE_FLAGS} -sf -`
84 CFLAGS=`printf "all:\\n\\t@echo \\\$(CFLAGS)\\n" | make ${MAKE_FLAGS} -sf -`
85 CFLAGS="${CFLAGS} -g -W -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes"
86 CFLAGS="${CFLAGS} -Wno-unused-parameter"
87 LDADD=
88 LDADD_LIB_SOCKET=
89 LDADD_STATIC=
90 LDADD_LIB_X11=
91 CPPFLAGS=
92 LDFLAGS=
93 DESTDIR=
94 PREFIX="/usr/local"
95 BINDIR=
96 SBINDIR=
97 INCLUDEDIR=
98 LIBDIR=
99 MANDIR=
100 SHAREDIR=
101 INSTALL="install"
102 INSTALL_PROGRAM=
103 INSTALL_LIB=
104 INSTALL_MAN=
105 INSTALL_DATA=
107 # compats needed
108 COBJ=
110 # SunOS sets "cc", but this doesn't exist.
111 # It does have gcc, so try that instead.
112 # Prefer clang, though.
114 command -v ${CC} 2>/dev/null 1>&2 || {
115 echo "${CC} not found: trying clang" 1>&2
116 echo "${CC} not found: trying clang" 1>&3
117 CC=clang
118 command -v ${CC} 2>/dev/null 1>&2 || {
119 echo "${CC} not found: trying gcc" 1>&2
120 echo "${CC} not found: trying gcc" 1>&3
121 CC=gcc
122 command -v ${CC} 2>/dev/null 1>&2 || {
123 echo "gcc not found: giving up" 1>&2
124 echo "gcc not found: giving up" 1>&3
125 exit 1
130 command -v pkg-config 2>/dev/null 1>&2 && {
131 if extra="$(pkg-config --cflags x11 xinerama xft || true)"; then
132 echo "Adding to CFLAGS: $extra (pkg-config)"
133 CFLAGS="$extra ${CFLAGS}"
134 fi
135 if extra="$(pkg-config --libs x11 xinerama xft || true)"; then
136 echo "Adding to LDFLAGS: $extra (pkg-config)"
137 LDFLAGS="$extra ${LDFLAGS}"
138 fi
141 #----------------------------------------------------------------------
142 # Allow certain variables to be overriden on the command line.
143 #----------------------------------------------------------------------
145 for keyvals in "$@"
146 do
147 key=`echo $keyvals | cut -s -d '=' -f 1`
148 if [ -z "$key" ]
149 then
150 echo "$0: invalid key-value: $keyvals" 1>&2
151 exit 1
152 fi
153 val=`echo $keyvals | cut -d '=' -f 2-`
154 case "$key" in
155 LDADD)
156 LDADD="$val" ;;
157 LDFLAGS)
158 LDFLAGS="$val" ;;
159 CPPFLAGS)
160 CPPFLAGS="$val" ;;
161 DESTDIR)
162 DESTDIR="$val" ;;
163 PREFIX)
164 PREFIX="$val" ;;
165 MANDIR)
166 MANDIR="$val" ;;
167 LIBDIR)
168 LIBDIR="$val" ;;
169 BINDIR)
170 BINDIR="$val" ;;
171 SHAREDIR)
172 SHAREDIR="$val" ;;
173 SBINDIR)
174 SBINDIR="$val" ;;
175 INCLUDEDIR)
176 INCLUDEDIR="$val" ;;
177 *)
178 echo "$0: invalid key: $key" 1>&2
179 exit 1
180 esac
181 done
184 #----------------------------------------------------------------------
185 # These are the values that will be pushed into config.h after we test
186 # for whether they're supported or not.
187 # Each of these must have a runtest(), below.
188 # Please sort by alpha, for clarity.
189 # You WANT to change this.
190 #----------------------------------------------------------------------
192 HAVE_CAPSICUM=
193 HAVE_ERR=
194 HAVE_GETEXECNAME=
195 HAVE_GETPROGNAME=
196 HAVE_LANDLOCK=
197 HAVE_PLEDGE=
198 HAVE_PROGRAM_INVOCATION_SHORT_NAME=
199 HAVE_REALLOCARRAY=
200 HAVE_RECALLOCARRAY=
201 HAVE_STRTONUM=
202 HAVE___PROGNAME=
204 #----------------------------------------------------------------------
205 # Allow configure.local to override all variables, default settings,
206 # command-line arguments, and tested features, above.
207 # You PROBABLY DO NOT want to change this.
208 #----------------------------------------------------------------------
210 if [ -r ./configure.local ]; then
211 echo "configure.local: reading..." 1>&2
212 echo "configure.local: reading..." 1>&3
213 cat ./configure.local 1>&3
214 . ./configure.local
215 else
216 echo "configure.local: no (fully automatic configuration)" 1>&2
217 echo "configure.local: no (fully automatic configuration)" 1>&3
218 fi
220 echo 1>&3
222 #----------------------------------------------------------------------
223 # Infrastructure for running tests.
224 # These consists of a series of functions that will attempt to run the
225 # given test file and record its exit into a HAVE_xxx variable.
226 # You DO NOT want to change this.
227 #----------------------------------------------------------------------
229 COMP="${CC} ${CFLAGS} ${CPPFLAGS} -Wno-unused -Werror"
231 # Check whether this HAVE_ setting is manually overridden.
232 # If yes, use the override, if no, do not decide anything yet.
233 # Arguments: lower-case test name, manual value
235 ismanual() {
236 [ -z "${3}" ] && return 1
237 echo "${1}: manual (HAVE_${2}=${3})" 1>&2
238 echo "${1}: manual (HAVE_${2}=${3})" 1>&3
239 echo 1>&3
240 return 0
243 # Run a single autoconfiguration test.
244 # In case of success, enable the feature.
245 # In case of failure, do not decide anything yet.
246 # Arguments: lower-case test name, upper-case test name, additional
247 # CFLAGS, additional LIBS.
249 singletest() {
250 extralib=""
251 cat 1>&3 << __HEREDOC__
252 ${1}: testing...
253 ${COMP} ${3} -o test-${1} test-${1}.c ${LDFLAGS} ${4}
254 __HEREDOC__
255 if ${COMP} ${3} -o "test-${1}" test-${1}.c ${LDFLAGS} ${4} 1>&3 2>&3; then
256 echo "${1}: ${CC} succeeded" 1>&3
257 else
258 if [ -n "${5}" ] ; then
259 echo "${1}: ${CC} failed with $? (retrying)" 1>&3
260 cat 1>&3 << __HEREDOC__
261 ${1}: testing...
262 ${COMP} ${3} -o test-${1} test-${1}.c ${LDFLAGS} ${5}
263 __HEREDOC__
264 if ${COMP} ${3} -o "test-${1}" test-${1}.c ${LDFLAGS} ${5} 1>&3 2>&3; then
265 echo "${1}: ${CC} succeeded" 1>&3
266 extralib="(with ${5})"
267 else
268 echo "${1}: ${CC} failed with $?" 1>&3
269 echo 1>&3
270 return 1
271 fi
272 else
273 echo "${1}: ${CC} failed with $?" 1>&3
274 echo 1>&3
275 return 1
276 fi
277 fi
279 if [ -n "${extralib}" ]
280 then
281 eval "LDADD_${2}=\"${5}\""
282 elif [ -n "${4}" ]
283 then
284 eval "LDADD_${2}=\"${4}\""
285 fi
287 echo "${1}: yes ${extralib}" 1>&2
288 echo "${1}: yes ${extralib}" 1>&3
289 echo 1>&3
290 eval HAVE_${2}=1
291 rm "test-${1}"
292 return 0
295 # Run a complete autoconfiguration test, including the check for
296 # a manual override and disabling the feature on failure.
297 # Arguments: lower case name, upper case name, additional CFLAGS,
298 # additional LDADD, alternative LDADD.
300 runtest() {
301 eval _manual=\${HAVE_${2}}
302 ismanual "${1}" "${2}" "${_manual}" && return 0
303 singletest "${1}" "${2}" "${3}" "${4}" "${5}" && return 0
304 echo "${1}: no" 1>&2
305 eval HAVE_${2}=0
306 return 1
309 #----------------------------------------------------------------------
310 # Begin running the tests themselves.
311 # All of your tests must be defined here.
312 # Please sort as the HAVE_xxxx values were defined.
313 # You WANT to change this.
314 # It consists of the following columns:
315 # runtest
316 # (1) test file
317 # (2) macro to set
318 # (3) argument to cc *before* -o
319 # (4) argument to cc *after*
320 # (5) alternative argument to cc *after*
321 #----------------------------------------------------------------------
323 runtest capsicum CAPSICUM || true
324 runtest err ERR || true
325 runtest getexecname GETEXECNAME || true
326 runtest getprogname GETPROGNAME || true
327 runtest landlock LANDLOCK || true
328 runtest lib_socket LIB_SOCKET "" "" "-lsocket -lnsl" || true
329 runtest pledge PLEDGE || true
330 runtest program_invocation_short_name PROGRAM_INVOCATION_SHORT_NAME || true
331 runtest reallocarray REALLOCARRAY || true
332 runtest recallocarray RECALLOCARRAY || true
333 runtest static STATIC "" "-static" || true
334 runtest strtonum STRTONUM || true
335 runtest x11 LIB_X11 "" "" "-lX11 -lXinerama -lXft" || true
336 runtest __progname __PROGNAME || true
338 if [ "${HAVE_LIB_X11}" -eq 0 ]; then
339 echo "FATAL: libx11 not found" 1>&2
340 echo "make sure to have libx11, libxinerama and libxft installed" 1>&2
341 echo "FATAL: libx11 not found" 1>&3
342 exit 1
343 fi
345 #----------------------------------------------------------------------
346 # Output writing: generate the config.h file.
347 # This file contains all of the HAVE_xxxx variables necessary for
348 # compiling your source.
349 # You must include "config.h" BEFORE any other variables.
350 # You WANT to change this.
351 #----------------------------------------------------------------------
353 exec > config.h
355 # Start with prologue.
357 cat << __HEREDOC__
358 #ifndef OCONFIGURE_CONFIG_H
359 #define OCONFIGURE_CONFIG_H
361 #ifdef __cplusplus
362 # error "Do not use C++: this is a C application."
363 #endif
364 #if !defined(__GNUC__) || (__GNUC__ < 4)
365 # define __attribute__(x)
366 #endif
367 #if defined(__linux__) || defined(__MINT__)
368 # define _GNU_SOURCE /* memmem, memrchr, setresuid... */
369 # define _DEFAULT_SOURCE /* le32toh, crypt, ... */
370 #endif
371 #if defined(__NetBSD__)
372 # define _OPENBSD_SOURCE /* reallocarray, etc. */
373 #endif
374 #if defined(__sun)
375 # ifndef _XOPEN_SOURCE /* SunOS already defines */
376 # define _XOPEN_SOURCE /* XPGx */
377 # endif
378 # define _XOPEN_SOURCE_EXTENDED 1 /* XPG4v2 */
379 # ifndef __EXTENSIONS__ /* SunOS already defines */
380 # define __EXTENSIONS__ /* reallocarray, etc. */
381 # endif
382 #endif
383 #if !defined(__BEGIN_DECLS)
384 # define __BEGIN_DECLS
385 #endif
386 #if !defined(__END_DECLS)
387 # define __END_DECLS
388 #endif
390 __HEREDOC__
392 # This is just for size_t, mode_t, and dev_t.
393 # Most of these functions, in the real world, pull in <string.h> or
394 # someting that pulls in support for size_t.
395 # Our function declarations are standalone, so specify them here.
397 if [ ${HAVE_REALLOCARRAY} -eq 0 -o \
398 ${HAVE_RECALLOCARRAY} -eq 0 ]
399 then
400 echo "#include <sys/types.h> /* size_t, mode_t, dev_t */ "
401 echo
402 fi
404 if [ ${HAVE_ERR} -eq 0 ]
405 then
406 echo "#include <stdarg.h> /* err(3) */"
407 echo
408 fi
410 # Now we handle our HAVE_xxxx values.
411 # Most will just be defined as 0 or 1.
413 cat << __HEREDOC__
414 /*
415 * Results of configuration feature-testing.
416 */
417 #define HAVE_CAPSICUM ${HAVE_CAPSICUM}
418 #define HAVE_ERR ${HAVE_ERR}
419 #define HAVE_GETEXECNAME ${HAVE_GETEXECNAME}
420 #define HAVE_GETPROGNAME ${HAVE_GETPROGNAME}
421 #define HAVE_LANDLOCK ${HAVE_LANDLOCK}
422 #define HAVE_PLEDGE ${HAVE_PLEDGE}
423 #define HAVE_PROGRAM_INVOCATION_SHORT_NAME ${HAVE_PROGRAM_INVOCATION_SHORT_NAME}
424 #define HAVE_REALLOCARRAY ${HAVE_REALLOCARRAY}
425 #define HAVE_RECALLOCARRAY ${HAVE_RECALLOCARRAY}
426 #define HAVE_STRTONUM ${HAVE_STRTONUM}
427 #define HAVE___PROGNAME ${HAVE___PROGNAME}
429 __HEREDOC__
431 # Now we do our function declarations for missing functions.
433 [ ${HAVE_ERR} -eq 0 ] && \
434 COBJ="compat_err.c ${COBJ}" && \
435 cat << __HEREDOC__
436 /*
437 * Compatibility functions for err(3).
438 */
439 extern void err(int, const char *, ...) __attribute__((noreturn));
440 extern void errc(int, int, const char *, ...) __attribute__((noreturn));
441 extern void errx(int, const char *, ...) __attribute__((noreturn));
442 extern void verr(int, const char *, va_list) __attribute__((noreturn));
443 extern void verrc(int, int, const char *, va_list) __attribute__((noreturn));
444 extern void verrx(int, const char *, va_list) __attribute__((noreturn));
445 extern void warn(const char *, ...);
446 extern void warnx(const char *, ...);
447 extern void warnc(int, const char *, ...);
448 extern void vwarn(const char *, va_list);
449 extern void vwarnc(int, const char *, va_list);
450 extern void vwarnx(const char *, va_list);
451 __HEREDOC__
453 [ ${HAVE_GETPROGNAME} -eq 0 ] && \
454 COBJ="compat_getprogname.c ${COBJ}" && \
455 cat << __HEREDOC__
456 /*
457 * Compatibility for getprogname(3).
458 */
459 extern const char *getprogname(void);
461 __HEREDOC__
463 [ ${HAVE_REALLOCARRAY} -eq 0 ] && \
464 COBJ="compat_reallocarray.c ${COBJ}" && \
465 cat << __HEREDOC__
466 /*
467 * Compatibility for reallocarray(3).
468 */
469 extern void *reallocarray(void *, size_t, size_t);
471 __HEREDOC__
473 [ ${HAVE_RECALLOCARRAY} -eq 0 ] && \
474 COBJ="compat_recallocarray.c ${COBJ}" && \
475 cat << __HEREDOC__
476 /*
477 * Compatibility for recallocarray(3).
478 */
479 extern void *recallocarray(void *, size_t, size_t, size_t);
481 __HEREDOC__
483 [ ${HAVE_STRTONUM} -eq 0 ] && \
484 COBJ="compat_strtonum.c ${COBJ}" && \
485 cat << __HEREDOC__
486 /*
487 * Compatibility for strotnum(3).
488 */
489 extern long long strtonum(const char *, long long, long long, const char **);
491 __HEREDOC__
493 cat <<__HEREDOC__
494 /*
495 * mymenu version
496 */
497 #define VERSION "${MYMENU_VERSION}"
499 __HEREDOC__
501 cat << __HEREDOC__
502 #endif /*!OCONFIGURE_CONFIG_H*/
503 __HEREDOC__
505 echo "config.h: written" 1>&2
506 echo "config.h: written" 1>&3
508 #----------------------------------------------------------------------
509 # Now we go to generate our Makefile.configure.
510 # This file is simply a bunch of Makefile variables.
511 # They'll work in both GNUmakefile and BSDmakefile.
512 # You MIGHT want to change this.
513 #----------------------------------------------------------------------
515 exec > Makefile.configure
517 [ -z "${BINDIR}" ] && BINDIR="${PREFIX}/bin"
518 [ -z "${SBINDIR}" ] && SBINDIR="${PREFIX}/sbin"
519 [ -z "${INCLUDEDIR}" ] && INCLUDEDIR="${PREFIX}/include"
520 [ -z "${LIBDIR}" ] && LIBDIR="${PREFIX}/lib"
521 [ -z "${MANDIR}" ] && MANDIR="${PREFIX}/man"
522 [ -z "${SHAREDIR}" ] && SHAREDIR="${PREFIX}/share"
524 [ -z "${INSTALL_PROGRAM}" ] && INSTALL_PROGRAM="${INSTALL} -m 0555"
525 [ -z "${INSTALL_LIB}" ] && INSTALL_LIB="${INSTALL} -m 0444"
526 [ -z "${INSTALL_MAN}" ] && INSTALL_MAN="${INSTALL} -m 0444"
527 [ -z "${INSTALL_DATA}" ] && INSTALL_DATA="${INSTALL} -m 0444"
529 cat << __HEREDOC__
530 CC = ${CC}
531 CFLAGS = ${CFLAGS}
532 CPPFLAGS = ${CPPFLAGS}
533 LDADD = ${LDADD}
534 LDADD_LIB_SOCKET = ${LDADD_LIB_SOCKET}
535 LDADD_STATIC = ${LDADD_STATIC}
536 LDADD_LIB_X11 = ${LDADD_LIB_X11}
537 LDFLAGS = ${LDFLAGS}
538 STATIC = ${STATIC}
539 PREFIX = ${PREFIX}
540 BINDIR = ${BINDIR}
541 SHAREDIR = ${SHAREDIR}
542 SBINDIR = ${SBINDIR}
543 INCLUDEDIR = ${INCLUDEDIR}
544 LIBDIR = ${LIBDIR}
545 MANDIR = ${MANDIR}
546 INSTALL = ${INSTALL}
547 INSTALL_PROGRAM = ${INSTALL_PROGRAM}
548 INSTALL_LIB = ${INSTALL_LIB}
549 INSTALL_MAN = ${INSTALL_MAN}
550 INSTALL_DATA = ${INSTALL_DATA}
552 COBJ = ${COBJ}
553 __HEREDOC__
555 echo "Makefile.configure: written" 1>&2
556 echo "Makefile.configure: written" 1>&3
558 exit 0