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