configure (2168B)
- #!/bin/sh
- # SPDX-FileCopyrightText: 2019 Haelwenn (lanodan) Monnier <contact+httpc@hacktivis.me>
- # SPDX-License-Identifier: MPL-2.0
- DEPS="libtls"
- arg0="$0"
- args="$@"
- usage() {
- cat <<END
- Usage: [variables] configure [variables]
- Variables:
- PREFIX=DIR
- BINDIR=DIR
- MANDIR=DIR
- PKGCONFIG=BIN
- CC=BIN
- CFLAGS=OPTIONS
- LDFLAGS=OPTIONS
- Variables are set in the following order: Default, Environment, Arguments
- Dependencies: See README.md
- END
- }
- is_ok() {
- status="$?"
- if test $status -eq 0; then
- printf " OK\n"
- else
- printf " FAIL\n"
- fi
- return $status
- }
- required() {
- is_ok || exit 1
- }
- pkg_config_check() {
- printf 'Checking: %s %s ...' "${PKGCONFIG}" "$*"
- "${PKGCONFIG}" "$@"
- is_ok
- }
- ## User configuration
- # defaults
- : ${PREFIX:=/usr/local}
- : ${PKGCONFIG:=pkg-config}
- : ${CC:=cc}
- : ${CFLAGS:=-g -O2 -D_FORTIFY_SOURCE=2}
- # 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:=${PREFIX}/bin}
- : ${MANDIR:=${PREFIX}/share/man}
- ## System checks
- # commands
- printf 'Checking %s command existance ...' "${PKGCONFIG}"
- command -v "${PKGCONFIG}" >/dev/null ; required
- printf 'Checking %s command existance ...' "${CC}"
- command -v "${CC}" >/dev/null ; required
- echo
- # pkg-config
- for dep in ${DEPS}
- do
- pkg_config_check --exists "$dep" || exit 1
- done
- printf 'Using pkg-config to get CFLAGS for %s ...' "${DEPS}"
- get_cflags() { "${PKGCONFIG}" --cflags "${DEPS}"; }
- DEPS_cflags="$(get_cflags)"
- required
- printf 'Using pkg-config to get LIBS for %s ...' "${DEPS}"
- get_libs() { "${PKGCONFIG}" --libs "${DEPS}"; }
- DEPS_libs="$(get_libs)"
- required
- echo
- ## Configuration write
- printf 'Writing to config.mk ...'
- cat >config.mk <<EOF
- # Autogenerated by $arg0 $args
- PREFIX = ${PREFIX}
- CC = ${CC}
- CFLAGS = ${CFLAGS}
- LDFLAGS = ${LDFLAGS}
- DEPS_cflags = ${DEPS_cflags}
- DEPS_libs = ${DEPS_libs}
- EOF
- is_ok
- echo 'Done, you can now run make'