configure (3550B)
- #!/bin/sh
- # SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- # SPDX-License-Identifier: MPL-2.0
- DEPS=""
- usage() {
- cat <<END
- Usage: [variables] configure [variables]
- Variables:
- PREFIX=DIR
- BINDIR=DIR
- MANDIR=DIR
- PKGCONFIG=BIN
- MSGFMT=BIN
- CC=BIN
- MAKE=BIN
- MANDOC=BIN
- SHELLCHECK=BIN
- FLAWFINDER=BIN
- GCOV=BIN
- REUSE=BIN
- CFLAGS=OPTIONS
- EXTRA_CFLAGS=OPTIONS
- LDFLAGS=OPTIONS
- LDSTATIC=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
- }
- pkg_config_check() {
- printf 'Checking: %s %s ...' "${PKGCONFIG}" "$*"
- "${PKGCONFIG}" "$@"
- is_ok
- }
- gen_targets() {
- printf 'EXE = '
- printf '%s\n ' cmd/*.c sh/* | sed -e 's;.c$;;' | tr -d '\n'
- echo
- printf 'MAN1 = '
- printf '%s\n ' cmd/*.1 sh-man/*.1 | tr -d '\n'
- 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
- }
- ## User configuration
- # defaults
- PREFIX="${PREFIX:-/usr/local}"
- PKGCONFIG="${PKGCONFIG:-pkg-config}"
- MSGFMT="${MSGFMT:-msgfmt}"
- CC="${CC:-cc}"
- MAKE="${MAKE:-make}"
- GCOV="${GCOV:-gcov}"
- # -DDEBUG: Otherwise assert() does nothing, fine to be removed in production
- CFLAGS="${CFLAGS:--g -O2 -DDEBUG}"
- MANDOC="${MANDOC:-mandoc}"
- SHELLCHECK="${SHELLCHECK:-shellcheck}"
- FLAWFINDER="${FLAWFINDER:-flawfinder}"
- 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}"
- MANDIR="${MANDIR:-${PREFIX}/share/man}"
- # Add some extra CFLAGS
- CFLAGS="${CFLAGS} ${EXTRA_CFLAGS}"
- printf 'Pruning old configurations ...'
- rm -f config.mk ; or_die
- ## System checks
- # commands
- check_cmd PKGCONFIG "$PKGCONFIG" || exit 1
- check_cmd CC "$CC" || exit 1
- check_cmd MAKE "$MAKE" || exit 1
- if check_cmd GCOV "$GCOV"
- then
- :
- else
- echo 'Notice: "coverage" target will fail'
- GCOV="false"
- fi
- if check_cmd MANDOC "$MANDOC"
- then
- :
- else
- echo 'Notice: Linting depending on mandoc disabled'
- MANDOC="true"
- fi
- if check_cmd SHELLCHECK "$SHELLCHECK"
- then
- :
- else
- echo 'Notice: Linting depending on shellcheck disabled'
- SHELLCHECK="true"
- fi
- if check_cmd FLAWFINDER "$FLAWFINDER"
- then
- :
- else
- echo 'Notice: Linting depending on flawfinder disabled'
- FLAWFINDER="true"
- fi
- if check_cmd REUSE "$REUSE"
- then
- :
- else
- echo 'Notice: Copyright linting depending on reuse disabled'
- REUSE="true"
- fi
- echo
- # pkg-config
- for dep in ${DEPS}
- do
- pkg_config_check --exists "$dep" || exit 1
- done
- echo
- ## Configuration write
- printf 'Writing to config.mk ...'
- cat >config.mk <<EOF
- # Autogenerated by ./configure
- PREFIX = ${PREFIX}
- BINDIR = ${BINDIR}
- MANDIR = ${MANDIR}
- PKGCONFIG = ${PKGCONFIG}
- CC = ${CC}
- MAKE = ${MAKE}
- MANDOC = ${MANDOC}
- SHELLCHECK = ${SHELLCHECK}
- FLAWFINDER = ${FLAWFINDER}
- MSGFMT = ${MSGFMT}
- DBG = ${DBG}
- GCOV = ${GCOV}
- REUSE = ${REUSE}
- CFLAGS = ${CFLAGS}
- LDFLAGS = ${LDFLAGS}
- LDSTATIC = ${LDSTATIC}
- EOF
- is_ok
- gen_targets >> config.mk ; or_die
- echo
- echo 'Done, you can now run make'