configure (5688B)
- #!/bin/sh
- # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- # SPDX-License-Identifier: MPL-2.0
- target_filter='-e cmd/expr.tab'
- usage() {
- cat <<END
- Usage: [variables] configure [variables]
- Variables:
- PREFIX=DIR
- BINDIR=DIR
- SHELLDIR=DIR
- PERLDIR=DIR
- MANDIR=DIR
- CROSS_COMPILE=TRIPLE
- MSGFMT=BIN
- AR=BIN
- CC=BIN
- YACC=BIN
- MAKE=BIN
- M4=BIN
- MANDOC=BIN
- GCOV=BIN
- CRAM=BIN
- REUSE=BIN
- CFLAGS=OPTIONS
- ARFLAGS=OPTIONS
- LDFLAGS=OPTIONS
- LDSTATIC=OPTIONS
- EXTRA_CFLAGS=OPTIONS
- Variables are set in the following order: Default, Environment, Arguments
- Dependencies: See the README.md file
- END
- }
- is_ok() {
- status="$?"
- if test $status -eq 0; then
- printf " OK\n"
- else
- printf " FAIL\n"
- fi
- return $status
- }
- or_die() {
- is_ok || exit 1
- }
- gen_targets() {
- printf 'EXE = '
- printf '%s\n ' cmd/*.c cmd/*.y | grep -v -F ${target_filter} | sed -e 's;\.c$;;' -e 's;\.y$;;' | tr -d '\n'
- echo
- printf 'MAN1SO = '
- printf '%s\n' cmd/*.1.in \
- | grep -v -F ${target_filter} \
- | sed -e 's;\.in$;;' -e 's;^cmd/;build/cmd/;' \
- | tr '\n' ' '
- echo
- printf 'MAN1 = ${MAN1SO} '
- printf '%s\n ' cmd/*.1 | grep -v -F ${target_filter} | tr -d '\n'
- echo
- printf 'LIBUTILS_C = '
- echo lib/*.c
- echo
- printf 'LIBUTILS_H = '
- echo lib/*.h
- echo
- }
- check_cmd() {
- var="$1"
- full_cmd="$2"
- # shellcheck disable=SC2086
- set -- $full_cmd
- cmd="$1"
- printf 'Checking $%s = %s command existence ...' "$var" "$full_cmd"
- command -v "$cmd" >/dev/null ; is_ok
- }
- check_header() {
- header="$1"
- set -- $CC -E -x c -
- printf 'Checking <%s> header (printf "#include <%s>" | %s) ...' "$header" "$header" "$*"
- printf '#include <%s>\n' "$header" | $@ >/dev/null
- is_ok
- }
- check_conftest() {
- conftest="$1"
- printf 'Checking if conftest %s compiles ...' "$conftest"
- ${CC} ${CFLAGS} ${CFLAGS_CHECK} -o "${conftest}.bin" "${conftest}" ${LDFLAGS} ${LDSTATIC} 2>&1
- is_ok
- }
- check_cflag() {
- printf "checking whether compiler accepts %s... " "$@"
- ${CC} ${CFLAGS} ${CFLAGS_CHECK} "$@" -c -o "configure.d/cflag_test.bin" "configure.d/cflag_test.c" 2>&1
- is_ok
- }
- set -o pipefail
- ## User configuration
- # defaults
- PREFIX="${PREFIX:-/usr/local}"
- MSGFMT="${MSGFMT:-msgfmt}"
- CC="${CC:-${CROSS_COMPILE}cc}"
- AR="${AR:-${CROSS_COMPILE}ar}"
- YACC="${YACC:-yacc}"
- MAKE="${MAKE:-make}"
- GCOV="${GCOV:-${CROSS_COMPILE}gcov}"
- CFLAGS="${CFLAGS:--g -O2}"
- M4="${M4:-m4}"
- MANDOC="${MANDOC:-mandoc}"
- CRAM="${CRAM:-cram}"
- REUSE="${REUSE:-reuse}"
- # Also allow variables through arguments
- for i; do
- case "$i" in
- -h|--help)
- usage
- exit 1
- ;;
- -*)
- printf "Unknown argument ‘%s’\n" "${i}"
- usage
- exit 1
- ;;
- *=*)
- # shellcheck disable=SC2163
- export "$i"
- shift
- ;;
- *)
- printf "Unknown argument ‘%s’\n" "${i}"
- usage
- exit 1
- ;;
- esac
- done
- # Fallback definitions for dirs, based on $PREFIX
- BINDIR="${BINDIR:-${PREFIX}/bin}"
- SHELLDIR="${SHELLDIR:-${BINDIR}}"
- PERLDIR="${PERLDIR:-${BINDIR}}"
- MANDIR="${MANDIR:-${PREFIX}/share/man}"
- # Add some extra CFLAGS
- CFLAGS="${CFLAGS} ${EXTRA_CFLAGS}"
- # Otherwise assert() can end up doing nothing, don't remove
- CFLAGS="${CFLAGS} -DDEBUG -UNDEBUG"
- ## System checks
- # commands
- check_cmd CC "$CC" || exit 1
- check_cmd AR "$AR" || exit 1
- check_cmd YACC "$YACC" || exit 1
- check_cmd MAKE "$MAKE" || exit 1
- check_cmd M4 "$M4" || exit 1
- if ! check_cmd GCOV "$GCOV"
- then
- echo 'Notice: "coverage" target will fail'
- GCOV="false"
- fi
- if ! check_cmd MANDOC "$MANDOC"
- then
- echo 'Notice: Linting depending on mandoc disabled'
- MANDOC="true"
- fi
- if ! check_cmd CRAM "$CRAM"
- then
- echo "Notice: cram not found, trying prysk"
- CRAM="prysk"
- if check_cmd CRAM "$CRAM"
- then
- :
- else
- CRAM="false"
- echo 'Notice: Testsuite depending on cram/psyk disabled'
- fi
- fi
- if ! check_cmd REUSE "$REUSE"
- then
- echo 'Notice: Copyright linting depending on reuse disabled'
- REUSE="true"
- fi
- echo
- check_cflag "-Werror=implicit-function-declaration" && CFLAGS_CHECK="${CFLAGS_CHECK} -Werror=implicit-function-declaration"
- for f in -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes
- do
- check_cflag "$f" && CFLAGS="${CFLAGS} $f"
- done
- echo
- if ! check_header mntent.h; then
- echo 'Disabling cmd/df'
- target_filter="${target_filter} -e cmd/df"
- fi
- if ! check_header sys/sysmacros.h; then
- echo 'Disabling cmd/mknod'
- target_filter="${target_filter} -e cmd/mknod"
- fi
- # As seen missing in Haiku beta5 <https://dev.haiku-os.org/ticket/16257>
- check_header wordexp.h && CFLAGS="${CFLAGS} -DHAS_WORDEXP"
- check_conftest configure.d/sendfile_linux.c && CFLAGS="${CFLAGS} -DHAS_SENDFILE"
- check_conftest configure.d/copy_file_range.c && CFLAGS="${CFLAGS} -DHAS_COPY_FILE_RANGE"
- if ! check_conftest configure.d/reallocarray.c; then
- echo 'Disabling cmd/tr cmd/cut'
- target_filter="${target_filter} -e cmd/tr. -e cmd/cut."
- fi
- check_conftest configure.d/getopt_long.c && CFLAGS="${CFLAGS} -DHAS_GETOPT_LONG"
- check_conftest configure.d/syncfs.c && CFLAGS="${CFLAGS} -DHAS_SYNCFS"
- if ! check_conftest configure.d/fgetln.c; then
- echo 'Disabling cmd/join'
- target_filter="${target_filter} -e cmd/join."
- fi
- rm -f configure.d/*.bin
- echo
- ## Configuration write
- printf 'Writing to config.mk ...'
- cat >config.mk <<EOF
- # Autogenerated by ./configure
- PREFIX = ${PREFIX}
- BINDIR = ${BINDIR}
- SHELLDIR = ${SHELLDIR}
- PERLDIR = ${PERLDIR}
- MANDIR = ${MANDIR}
- CC = ${CC}
- AR = ${AR}
- YACC = ${YACC}
- MAKE = ${MAKE}
- M4 = ${M4}
- MANDOC = ${MANDOC}
- MSGFMT = ${MSGFMT}
- DBG = ${DBG}
- GCOV = ${GCOV}
- CRAM = ${CRAM}
- REUSE = ${REUSE}
- CFLAGS = ${CFLAGS}
- ARFLAGS = ${ARFLAGS}
- LDFLAGS = ${LDFLAGS}
- LDSTATIC = ${LDSTATIC}
- EOF
- is_ok
- printf 'Adding targets to config.mk ...'
- gen_targets >> config.mk ; or_die
- echo
- echo 'Done, you can now run make'