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 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 "FATAL: libx11 not found" 1>&3
341 exit 1
342 fi
344 #----------------------------------------------------------------------
345 # Output writing: generate the config.h file.
346 # This file contains all of the HAVE_xxxx variables necessary for
347 # compiling your source.
348 # You must include "config.h" BEFORE any other variables.
349 # You WANT to change this.
350 #----------------------------------------------------------------------
352 exec > config.h
354 # Start with prologue.
356 cat << __HEREDOC__
357 #ifndef OCONFIGURE_CONFIG_H
358 #define OCONFIGURE_CONFIG_H
360 #ifdef __cplusplus
361 # error "Do not use C++: this is a C application."
362 #endif
363 #if !defined(__GNUC__) || (__GNUC__ < 4)
364 # define __attribute__(x)
365 #endif
366 #if defined(__linux__) || defined(__MINT__)
367 # define _GNU_SOURCE /* memmem, memrchr, setresuid... */
368 # define _DEFAULT_SOURCE /* le32toh, crypt, ... */
369 #endif
370 #if defined(__NetBSD__)
371 # define _OPENBSD_SOURCE /* reallocarray, etc. */
372 #endif
373 #if defined(__sun)
374 # ifndef _XOPEN_SOURCE /* SunOS already defines */
375 # define _XOPEN_SOURCE /* XPGx */
376 # endif
377 # define _XOPEN_SOURCE_EXTENDED 1 /* XPG4v2 */
378 # ifndef __EXTENSIONS__ /* SunOS already defines */
379 # define __EXTENSIONS__ /* reallocarray, etc. */
380 # endif
381 #endif
382 #if !defined(__BEGIN_DECLS)
383 # define __BEGIN_DECLS
384 #endif
385 #if !defined(__END_DECLS)
386 # define __END_DECLS
387 #endif
389 __HEREDOC__
391 # This is just for size_t, mode_t, and dev_t.
392 # Most of these functions, in the real world, pull in <string.h> or
393 # someting that pulls in support for size_t.
394 # Our function declarations are standalone, so specify them here.
396 if [ ${HAVE_REALLOCARRAY} -eq 0 -o \
397 ${HAVE_RECALLOCARRAY} -eq 0 ]
398 then
399 echo "#include <sys/types.h> /* size_t, mode_t, dev_t */ "
400 echo
401 fi
403 if [ ${HAVE_ERR} -eq 0 ]
404 then
405 echo "#include <stdarg.h> /* err(3) */"
406 echo
407 fi
409 # Now we handle our HAVE_xxxx values.
410 # Most will just be defined as 0 or 1.
412 cat << __HEREDOC__
413 /*
414 * Results of configuration feature-testing.
415 */
416 #define HAVE_CAPSICUM ${HAVE_CAPSICUM}
417 #define HAVE_ERR ${HAVE_ERR}
418 #define HAVE_GETEXECNAME ${HAVE_GETEXECNAME}
419 #define HAVE_GETPROGNAME ${HAVE_GETPROGNAME}
420 #define HAVE_LANDLOCK ${HAVE_LANDLOCK}
421 #define HAVE_PLEDGE ${HAVE_PLEDGE}
422 #define HAVE_PROGRAM_INVOCATION_SHORT_NAME ${HAVE_PROGRAM_INVOCATION_SHORT_NAME}
423 #define HAVE_REALLOCARRAY ${HAVE_REALLOCARRAY}
424 #define HAVE_RECALLOCARRAY ${HAVE_RECALLOCARRAY}
425 #define HAVE_STRTONUM ${HAVE_STRTONUM}
426 #define HAVE___PROGNAME ${HAVE___PROGNAME}
428 __HEREDOC__
430 # Now we do our function declarations for missing functions.
432 [ ${HAVE_ERR} -eq 0 ] && \
433 COBJ="compat_err.c ${COBJ}" && \
434 cat << __HEREDOC__
435 /*
436 * Compatibility functions for err(3).
437 */
438 extern void err(int, const char *, ...) __attribute__((noreturn));
439 extern void errc(int, int, const char *, ...) __attribute__((noreturn));
440 extern void errx(int, const char *, ...) __attribute__((noreturn));
441 extern void verr(int, const char *, va_list) __attribute__((noreturn));
442 extern void verrc(int, int, const char *, va_list) __attribute__((noreturn));
443 extern void verrx(int, const char *, va_list) __attribute__((noreturn));
444 extern void warn(const char *, ...);
445 extern void warnx(const char *, ...);
446 extern void warnc(int, const char *, ...);
447 extern void vwarn(const char *, va_list);
448 extern void vwarnc(int, const char *, va_list);
449 extern void vwarnx(const char *, va_list);
450 __HEREDOC__
452 [ ${HAVE_GETPROGNAME} -eq 0 ] && \
453 COBJ="compat_getprogname.c ${COBJ}" && \
454 cat << __HEREDOC__
455 /*
456 * Compatibility for getprogname(3).
457 */
458 extern const char *getprogname(void);
460 __HEREDOC__
462 [ ${HAVE_REALLOCARRAY} -eq 0 ] && \
463 COBJ="compat_reallocarray.c ${COBJ}" && \
464 cat << __HEREDOC__
465 /*
466 * Compatibility for reallocarray(3).
467 */
468 extern void *reallocarray(void *, size_t, size_t);
470 __HEREDOC__
472 [ ${HAVE_RECALLOCARRAY} -eq 0 ] && \
473 COBJ="compat_recallocarray.c ${COBJ}" && \
474 cat << __HEREDOC__
475 /*
476 * Compatibility for recallocarray(3).
477 */
478 extern void *recallocarray(void *, size_t, size_t, size_t);
480 __HEREDOC__
482 [ ${HAVE_STRTONUM} -eq 0 ] && \
483 COBJ="compat_strtonum.c ${COBJ}" && \
484 cat << __HEREDOC__
485 /*
486 * Compatibility for strotnum(3).
487 */
488 extern long long strtonum(const char *, long long, long long, const char **);
490 __HEREDOC__
492 cat <<__HEREDOC__
493 /*
494 * mymenu version
495 */
496 #define VERSION "${MYMENU_VERSION}"
498 __HEREDOC__
500 cat << __HEREDOC__
501 #endif /*!OCONFIGURE_CONFIG_H*/
502 __HEREDOC__
504 echo "config.h: written" 1>&2
505 echo "config.h: written" 1>&3
507 #----------------------------------------------------------------------
508 # Now we go to generate our Makefile.configure.
509 # This file is simply a bunch of Makefile variables.
510 # They'll work in both GNUmakefile and BSDmakefile.
511 # You MIGHT want to change this.
512 #----------------------------------------------------------------------
514 exec > Makefile.configure
516 [ -z "${BINDIR}" ] && BINDIR="${PREFIX}/bin"
517 [ -z "${SBINDIR}" ] && SBINDIR="${PREFIX}/sbin"
518 [ -z "${INCLUDEDIR}" ] && INCLUDEDIR="${PREFIX}/include"
519 [ -z "${LIBDIR}" ] && LIBDIR="${PREFIX}/lib"
520 [ -z "${MANDIR}" ] && MANDIR="${PREFIX}/man"
521 [ -z "${SHAREDIR}" ] && SHAREDIR="${PREFIX}/share"
523 [ -z "${INSTALL_PROGRAM}" ] && INSTALL_PROGRAM="${INSTALL} -m 0555"
524 [ -z "${INSTALL_LIB}" ] && INSTALL_LIB="${INSTALL} -m 0444"
525 [ -z "${INSTALL_MAN}" ] && INSTALL_MAN="${INSTALL} -m 0444"
526 [ -z "${INSTALL_DATA}" ] && INSTALL_DATA="${INSTALL} -m 0444"
528 cat << __HEREDOC__
529 CC = ${CC}
530 CFLAGS = ${CFLAGS}
531 CPPFLAGS = ${CPPFLAGS}
532 LDADD = ${LDADD}
533 LDADD_LIB_SOCKET = ${LDADD_LIB_SOCKET}
534 LDADD_STATIC = ${LDADD_STATIC}
535 LDADD_LIB_X11 = ${LDADD_LIB_X11}
536 LDFLAGS = ${LDFLAGS}
537 STATIC = ${STATIC}
538 PREFIX = ${PREFIX}
539 BINDIR = ${BINDIR}
540 SHAREDIR = ${SHAREDIR}
541 SBINDIR = ${SBINDIR}
542 INCLUDEDIR = ${INCLUDEDIR}
543 LIBDIR = ${LIBDIR}
544 MANDIR = ${MANDIR}
545 INSTALL = ${INSTALL}
546 INSTALL_PROGRAM = ${INSTALL_PROGRAM}
547 INSTALL_LIB = ${INSTALL_LIB}
548 INSTALL_MAN = ${INSTALL_MAN}
549 INSTALL_DATA = ${INSTALL_DATA}
551 COBJ = ${COBJ}
552 __HEREDOC__
554 echo "Makefile.configure: written" 1>&2
555 echo "Makefile.configure: written" 1>&3
557 exit 0