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} -Wwrite-strings -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 # Automatically set flags for x11 on OpenBSD
131 case $(uname) in
132 OpenBSD)
133 CFLAGS="${CFLAGS} -I/usr/X11R6/include"
134 CFLAGS="${CFLAGS} -I/usr/X11R6/include/freetype2"
135 LDFLAGS="-L/usr/X11R6/lib"
136 ;;
137 esac
139 #----------------------------------------------------------------------
140 # Allow certain variables to be overriden on the command line.
141 #----------------------------------------------------------------------
143 for keyvals in "$@"
144 do
145 key=`echo $keyvals | cut -s -d '=' -f 1`
146 if [ -z "$key" ]
147 then
148 echo "$0: invalid key-value: $keyvals" 1>&2
149 exit 1
150 fi
151 val=`echo $keyvals | cut -d '=' -f 2-`
152 case "$key" in
153 LDADD)
154 LDADD="$val" ;;
155 LDFLAGS)
156 LDFLAGS="$val" ;;
157 CPPFLAGS)
158 CPPFLAGS="$val" ;;
159 DESTDIR)
160 DESTDIR="$val" ;;
161 PREFIX)
162 PREFIX="$val" ;;
163 MANDIR)
164 MANDIR="$val" ;;
165 LIBDIR)
166 LIBDIR="$val" ;;
167 BINDIR)
168 BINDIR="$val" ;;
169 SHAREDIR)
170 SHAREDIR="$val" ;;
171 SBINDIR)
172 SBINDIR="$val" ;;
173 INCLUDEDIR)
174 INCLUDEDIR="$val" ;;
175 *)
176 echo "$0: invalid key: $key" 1>&2
177 exit 1
178 esac
179 done
182 #----------------------------------------------------------------------
183 # These are the values that will be pushed into config.h after we test
184 # for whether they're supported or not.
185 # Each of these must have a runtest(), below.
186 # Please sort by alpha, for clarity.
187 # You WANT to change this.
188 #----------------------------------------------------------------------
190 HAVE_CAPSICUM=
191 HAVE_ERR=
192 HAVE_GETEXECNAME=
193 HAVE_GETPROGNAME=
194 HAVE_LANDLOCK=
195 HAVE_PLEDGE=
196 HAVE_PROGRAM_INVOCATION_SHORT_NAME=
197 HAVE_REALLOCARRAY=
198 HAVE_RECALLOCARRAY=
199 HAVE_STRTONUM=
200 HAVE___PROGNAME=
202 #----------------------------------------------------------------------
203 # Allow configure.local to override all variables, default settings,
204 # command-line arguments, and tested features, above.
205 # You PROBABLY DO NOT want to change this.
206 #----------------------------------------------------------------------
208 if [ -r ./configure.local ]; then
209 echo "configure.local: reading..." 1>&2
210 echo "configure.local: reading..." 1>&3
211 cat ./configure.local 1>&3
212 . ./configure.local
213 else
214 echo "configure.local: no (fully automatic configuration)" 1>&2
215 echo "configure.local: no (fully automatic configuration)" 1>&3
216 fi
218 echo 1>&3
220 #----------------------------------------------------------------------
221 # Infrastructure for running tests.
222 # These consists of a series of functions that will attempt to run the
223 # given test file and record its exit into a HAVE_xxx variable.
224 # You DO NOT want to change this.
225 #----------------------------------------------------------------------
227 COMP="${CC} ${CFLAGS} ${CPPFLAGS} -Wno-unused -Werror"
229 # Check whether this HAVE_ setting is manually overridden.
230 # If yes, use the override, if no, do not decide anything yet.
231 # Arguments: lower-case test name, manual value
233 ismanual() {
234 [ -z "${3}" ] && return 1
235 echo "${1}: manual (HAVE_${2}=${3})" 1>&2
236 echo "${1}: manual (HAVE_${2}=${3})" 1>&3
237 echo 1>&3
238 return 0
241 # Run a single autoconfiguration test.
242 # In case of success, enable the feature.
243 # In case of failure, do not decide anything yet.
244 # Arguments: lower-case test name, upper-case test name, additional
245 # CFLAGS, additional LIBS.
247 singletest() {
248 extralib=""
249 cat 1>&3 << __HEREDOC__
250 ${1}: testing...
251 ${COMP} ${3} -o test-${1} test-${1}.c ${LDFLAGS} ${4}
252 __HEREDOC__
253 if ${COMP} ${3} -o "test-${1}" test-${1}.c ${LDFLAGS} ${4} 1>&3 2>&3; then
254 echo "${1}: ${CC} succeeded" 1>&3
255 else
256 if [ -n "${5}" ] ; then
257 echo "${1}: ${CC} failed with $? (retrying)" 1>&3
258 cat 1>&3 << __HEREDOC__
259 ${1}: testing...
260 ${COMP} ${3} -o test-${1} test-${1}.c ${LDFLAGS} ${5}
261 __HEREDOC__
262 if ${COMP} ${3} -o "test-${1}" test-${1}.c ${LDFLAGS} ${5} 1>&3 2>&3; then
263 echo "${1}: ${CC} succeeded" 1>&3
264 extralib="(with ${5})"
265 else
266 echo "${1}: ${CC} failed with $?" 1>&3
267 echo 1>&3
268 return 1
269 fi
270 else
271 echo "${1}: ${CC} failed with $?" 1>&3
272 echo 1>&3
273 return 1
274 fi
275 fi
277 if [ -n "${extralib}" ]
278 then
279 eval "LDADD_${2}=\"${5}\""
280 elif [ -n "${4}" ]
281 then
282 eval "LDADD_${2}=\"${4}\""
283 fi
285 echo "${1}: yes ${extralib}" 1>&2
286 echo "${1}: yes ${extralib}" 1>&3
287 echo 1>&3
288 eval HAVE_${2}=1
289 rm "test-${1}"
290 return 0
293 # Run a complete autoconfiguration test, including the check for
294 # a manual override and disabling the feature on failure.
295 # Arguments: lower case name, upper case name, additional CFLAGS,
296 # additional LDADD, alternative LDADD.
298 runtest() {
299 eval _manual=\${HAVE_${2}}
300 ismanual "${1}" "${2}" "${_manual}" && return 0
301 singletest "${1}" "${2}" "${3}" "${4}" "${5}" && return 0
302 echo "${1}: no" 1>&2
303 eval HAVE_${2}=0
304 return 1
307 #----------------------------------------------------------------------
308 # Begin running the tests themselves.
309 # All of your tests must be defined here.
310 # Please sort as the HAVE_xxxx values were defined.
311 # You WANT to change this.
312 # It consists of the following columns:
313 # runtest
314 # (1) test file
315 # (2) macro to set
316 # (3) argument to cc *before* -o
317 # (4) argument to cc *after*
318 # (5) alternative argument to cc *after*
319 #----------------------------------------------------------------------
321 runtest capsicum CAPSICUM || true
322 runtest err ERR || true
323 runtest getexecname GETEXECNAME || true
324 runtest getprogname GETPROGNAME || true
325 runtest landlock LANDLOCK || true
326 runtest lib_socket LIB_SOCKET "" "" "-lsocket -lnsl" || true
327 runtest pledge PLEDGE || true
328 runtest program_invocation_short_name PROGRAM_INVOCATION_SHORT_NAME || true
329 runtest reallocarray REALLOCARRAY || true
330 runtest recallocarray RECALLOCARRAY || true
331 runtest static STATIC "" "-static" || true
332 runtest strtonum STRTONUM || true
333 runtest x11 LIB_X11 "" "" "-lX11 -lXinerama -lXft" || true
334 runtest __progname __PROGNAME || true
336 if [ "${HAVE_LIB_X11}" -eq 0 ]; then
337 echo "FATAL: libx11 not found" 1>&2
338 echo "FATAL: libx11 not found" 1>&3
339 exit 1
340 fi
342 #----------------------------------------------------------------------
343 # Output writing: generate the config.h file.
344 # This file contains all of the HAVE_xxxx variables necessary for
345 # compiling your source.
346 # You must include "config.h" BEFORE any other variables.
347 # You WANT to change this.
348 #----------------------------------------------------------------------
350 exec > config.h
352 # Start with prologue.
354 cat << __HEREDOC__
355 #ifndef OCONFIGURE_CONFIG_H
356 #define OCONFIGURE_CONFIG_H
358 #ifdef __cplusplus
359 # error "Do not use C++: this is a C application."
360 #endif
361 #if !defined(__GNUC__) || (__GNUC__ < 4)
362 # define __attribute__(x)
363 #endif
364 #if defined(__linux__) || defined(__MINT__)
365 # define _GNU_SOURCE /* memmem, memrchr, setresuid... */
366 # define _DEFAULT_SOURCE /* le32toh, crypt, ... */
367 #endif
368 #if defined(__NetBSD__)
369 # define _OPENBSD_SOURCE /* reallocarray, etc. */
370 #endif
371 #if defined(__sun)
372 # ifndef _XOPEN_SOURCE /* SunOS already defines */
373 # define _XOPEN_SOURCE /* XPGx */
374 # endif
375 # define _XOPEN_SOURCE_EXTENDED 1 /* XPG4v2 */
376 # ifndef __EXTENSIONS__ /* SunOS already defines */
377 # define __EXTENSIONS__ /* reallocarray, etc. */
378 # endif
379 #endif
380 #if !defined(__BEGIN_DECLS)
381 # define __BEGIN_DECLS
382 #endif
383 #if !defined(__END_DECLS)
384 # define __END_DECLS
385 #endif
387 __HEREDOC__
389 # This is just for size_t, mode_t, and dev_t.
390 # Most of these functions, in the real world, pull in <string.h> or
391 # someting that pulls in support for size_t.
392 # Our function declarations are standalone, so specify them here.
394 if [ ${HAVE_REALLOCARRAY} -eq 0 -o \
395 ${HAVE_RECALLOCARRAY} -eq 0 ]
396 then
397 echo "#include <sys/types.h> /* size_t, mode_t, dev_t */ "
398 echo
399 fi
401 if [ ${HAVE_ERR} -eq 0 ]
402 then
403 echo "#include <stdarg.h> /* err(3) */"
404 echo
405 fi
407 # Now we handle our HAVE_xxxx values.
408 # Most will just be defined as 0 or 1.
410 cat << __HEREDOC__
411 /*
412 * Results of configuration feature-testing.
413 */
414 #define HAVE_CAPSICUM ${HAVE_CAPSICUM}
415 #define HAVE_ERR ${HAVE_ERR}
416 #define HAVE_GETEXECNAME ${HAVE_GETEXECNAME}
417 #define HAVE_GETPROGNAME ${HAVE_GETPROGNAME}
418 #define HAVE_LANDLOCK ${HAVE_LANDLOCK}
419 #define HAVE_PLEDGE ${HAVE_PLEDGE}
420 #define HAVE_PROGRAM_INVOCATION_SHORT_NAME ${HAVE_PROGRAM_INVOCATION_SHORT_NAME}
421 #define HAVE_REALLOCARRAY ${HAVE_REALLOCARRAY}
422 #define HAVE_RECALLOCARRAY ${HAVE_RECALLOCARRAY}
423 #define HAVE_STRTONUM ${HAVE_STRTONUM}
424 #define HAVE___PROGNAME ${HAVE___PROGNAME}
426 __HEREDOC__
428 # Now we do our function declarations for missing functions.
430 [ ${HAVE_ERR} -eq 0 ] && \
431 COBJ="compat_err.c ${COBJ}" && \
432 cat << __HEREDOC__
433 /*
434 * Compatibility functions for err(3).
435 */
436 extern void err(int, const char *, ...) __attribute__((noreturn));
437 extern void errc(int, int, const char *, ...) __attribute__((noreturn));
438 extern void errx(int, const char *, ...) __attribute__((noreturn));
439 extern void verr(int, const char *, va_list) __attribute__((noreturn));
440 extern void verrc(int, int, const char *, va_list) __attribute__((noreturn));
441 extern void verrx(int, const char *, va_list) __attribute__((noreturn));
442 extern void warn(const char *, ...);
443 extern void warnx(const char *, ...);
444 extern void warnc(int, const char *, ...);
445 extern void vwarn(const char *, va_list);
446 extern void vwarnc(int, const char *, va_list);
447 extern void vwarnx(const char *, va_list);
448 __HEREDOC__
450 [ ${HAVE_GETPROGNAME} -eq 0 ] && \
451 COBJ="compat_getprogname.c ${COBJ}" && \
452 cat << __HEREDOC__
453 /*
454 * Compatibility for getprogname(3).
455 */
456 extern const char *getprogname(void);
458 __HEREDOC__
460 [ ${HAVE_REALLOCARRAY} -eq 0 ] && \
461 COBJ="compat_reallocarray.c ${COBJ}" && \
462 cat << __HEREDOC__
463 /*
464 * Compatibility for reallocarray(3).
465 */
466 extern void *reallocarray(void *, size_t, size_t);
468 __HEREDOC__
470 [ ${HAVE_RECALLOCARRAY} -eq 0 ] && \
471 COBJ="compat_recallocarray.c ${COBJ}" && \
472 cat << __HEREDOC__
473 /*
474 * Compatibility for recallocarray(3).
475 */
476 extern void *recallocarray(void *, size_t, size_t, size_t);
478 __HEREDOC__
480 [ ${HAVE_STRTONUM} -eq 0 ] && \
481 COBJ="compat_strtonum.c ${COBJ}" && \
482 cat << __HEREDOC__
483 /*
484 * Compatibility for strotnum(3).
485 */
486 extern long long strtonum(const char *, long long, long long, const char **);
488 __HEREDOC__
490 cat <<__HEREDOC__
491 /*
492 * mymenu version
493 */
494 #define VERSION "${MYMENU_VERSION}"
496 __HEREDOC__
498 cat << __HEREDOC__
499 #endif /*!OCONFIGURE_CONFIG_H*/
500 __HEREDOC__
502 echo "config.h: written" 1>&2
503 echo "config.h: written" 1>&3
505 #----------------------------------------------------------------------
506 # Now we go to generate our Makefile.configure.
507 # This file is simply a bunch of Makefile variables.
508 # They'll work in both GNUmakefile and BSDmakefile.
509 # You MIGHT want to change this.
510 #----------------------------------------------------------------------
512 exec > Makefile.configure
514 [ -z "${BINDIR}" ] && BINDIR="${PREFIX}/bin"
515 [ -z "${SBINDIR}" ] && SBINDIR="${PREFIX}/sbin"
516 [ -z "${INCLUDEDIR}" ] && INCLUDEDIR="${PREFIX}/include"
517 [ -z "${LIBDIR}" ] && LIBDIR="${PREFIX}/lib"
518 [ -z "${MANDIR}" ] && MANDIR="${PREFIX}/man"
519 [ -z "${SHAREDIR}" ] && SHAREDIR="${PREFIX}/share"
521 [ -z "${INSTALL_PROGRAM}" ] && INSTALL_PROGRAM="${INSTALL} -m 0555"
522 [ -z "${INSTALL_LIB}" ] && INSTALL_LIB="${INSTALL} -m 0444"
523 [ -z "${INSTALL_MAN}" ] && INSTALL_MAN="${INSTALL} -m 0444"
524 [ -z "${INSTALL_DATA}" ] && INSTALL_DATA="${INSTALL} -m 0444"
526 cat << __HEREDOC__
527 CC = ${CC}
528 CFLAGS = ${CFLAGS}
529 CPPFLAGS = ${CPPFLAGS}
530 LDADD = ${LDADD}
531 LDADD_LIB_SOCKET = ${LDADD_LIB_SOCKET}
532 LDADD_STATIC = ${LDADD_STATIC}
533 LDADD_LIB_X11 = ${LDADD_LIB_X11}
534 LDFLAGS = ${LDFLAGS}
535 STATIC = ${STATIC}
536 PREFIX = ${PREFIX}
537 BINDIR = ${BINDIR}
538 SHAREDIR = ${SHAREDIR}
539 SBINDIR = ${SBINDIR}
540 INCLUDEDIR = ${INCLUDEDIR}
541 LIBDIR = ${LIBDIR}
542 MANDIR = ${MANDIR}
543 INSTALL = ${INSTALL}
544 INSTALL_PROGRAM = ${INSTALL_PROGRAM}
545 INSTALL_LIB = ${INSTALL_LIB}
546 INSTALL_MAN = ${INSTALL_MAN}
547 INSTALL_DATA = ${INSTALL_DATA}
549 COBJ = ${COBJ}
550 __HEREDOC__
552 echo "Makefile.configure: written" 1>&2
553 echo "Makefile.configure: written" 1>&3
555 exit 0