#!/bin/sh ### Custom configure script of Dinit. ## Initial prepartion set -eu cd "$(dirname "$0")" ## Helper functions # According to POSIX, echo has some unspecified behavior in some cases, for example # when its first argument is "-n" or backslash ("\"). We prefer to rely on # POSIX documented things. # So we replace shell built-in echo with a printf based function. # For more info see http://www.etalabs.net/sh_tricks.html echo() { IFS=" " printf %s\\n "$*" } info() { echo "$*" } sub_info() { echo " ... $*" } error() { >&2 echo "Error: $*" exit 1 } sub_error() { >&2 echo " ... Error: $*" exit 1 } warning() { echo echo "Warning: $*" } sub_warning() { echo " ... $*" echo } findcxx() { if type "$1" > /dev/null 2>&1; then CXX="$1" fi } cxx_works() { info Checking "$1" works... if ! $CXX -o testfile.o testfile.cc; then rm -f testfile* sub_error Seems like C++ compiler is not working! else rm -f testfile sub_info Yes. fi } try_cxx_argument() { info Checking whatever compiler accepts "$2"... if $CXX $CXXFLAGS $2 $CXXFLAGS_EXTRA $LDFLAGS $LDFLAGS_EXTRA testfile.cc -o testfile > /dev/null 2>&1; then rm testfile sub_info Yes. eval "$1=\"\${$1} \$2\"" eval "$1=\${$1# }" else if [ "$2" = "-std=c++11" ]; then sub_error "Seems like C++ compiler don't support $2 but It's required!" else sub_info No. fi fi } try_ld_argument() { info Checking whatever linker accepts "$2"... if $CXX $CXXFLAGS $CXXFLAGS_EXTRA $LDFLAGS $LDFLAGS_EXTRA $2 testfile.cc -o testfile > /dev/null 2>&1; then sub_info Yes. eval "$1=\"\${$1} \$2\"" eval "$1=\${$1# }" else sub_info No. fi } try_both_argument() { info Checking whatever compiler/linker accepts "$3"... if $CXX $CXXFLAGS $CXXFLAGS_EXTRA $LDFLAGS $LDFLAGS_EXTRA $3 testfile.cc -o testfile > /dev/null 2>&1; then sub_info Yes. eval "$1=\"\${$1} \$3\"" eval "$1=\${$1# }" eval "$2=\"\${$2} \$3\"" eval "$2=\${$2# }" else sub_info No. fi } usage() { cat << _EOF Usage: $0 [OPTION]... Defaults for the options are specified in brackets. --help This help message. --quiet Don't print normal messages, just errors. --clean Clear mconfig and configure's temp files. Target options: --platform=PLATFORM Set the platform manually (Just for cross-platform cross-compile!) [autodetected] For all cross-compiles (even this) don't forget to set correct CXX and CXX_FOR_BUILD! Installation directories: --prefix=PREFIX Main installtion prefix [/usr] --exec-prefix=EPREFIX Main executables location [/] --sbindir=SBINDIR Dinit executables [EPREFIX/sbin] --mandir=MANDIR Dinit man-pages location [PREFIX/share/man] --syscontrolsocket=SOCKETPATH Dinitctl socket location [/run/dinitctl] on Linux based systems [/var/run/dinitctl] on Other systems Optional options: --shutdown-prefix=PREFIX Name prefix for shutdown, poweroff, reboot, halt programs [] --enable-shutdown Build shutdown, poweroff, reboot, halt programs [Enabled only on Linux based systems] --disable-shutdown Don't build shutdown, poweroff, reboot, halt programs --enable-cgroups Enable Cgroups support [Enabled only on Linux based systems] --disable-cgroups Disable Cgroups support --enable-utmpx Enable manipulating the utmp/utmpx database via the related POSIX functions [auto] --disable-utmpx Disable manipulating the utmp/utmpx database via the related POSIX functions Environment variables can be used: Note: environments vars can be passed as $0 argument. Note: CXXFLAGS , TEST_CXXFLAGS, LDFLAGS and TEST_LDFLAGS will be set to the options that the configure script uses as default for the platform, filtered to remove any options not supported by the compiler/linker. For disable this thing just pass empty value as those things or pass some flags as them. CXX If you want to use specific C++ compiler. CXX_FOR_BUILD If you want to cross-compiling and set the native C++ compiler. CXXFLAGS_FOR_BUILD If you want to use some arguments in native C++ compiler. By default it's same as CXXFLAGS. CPPFLAGS_FOR_BUILD If you want to use some arguments in native C++ compiler preprocessor. By default it's same as CPPFLAGS. LDFLAGS_FOR_BUILD If you want to use some arguments in native Linker command line. By default it's same as LDFLAGS. CXXFLAGS If you want to use some arguments in C++ compiler command line. CXXFLAGS_EXTRA If you want to use some arguments in C++ compiler command line. It's passed after CXXFLAGS without overwrite it. TEST_CXXFLAGS If you want to use some arguments in C++ compiler for testing. TEST_CXXFLAGS_EXTRA If you want to use some arguments in C++ compiler for testing. It's passed after TEST_CXXFLAGS without overwrite it. CPPFLAGS If you want to use some arguments in C++ compiler preprocessor. LDFLAGS If you want to use some arguments in Linker command line. LDFLAGS_EXTRA If you want to use some arguments in Linker command line. It's passed after LDFLAGS without overwrite it. TEST_LDFLAGS If you want to use some arguments in Linker for testing. TEST_LDFLAGS_EXTRA If you want to use some arguments in Linker for testing. It's passed after TEST_LDFLAGS without overwrite it. Note: be careful about passing a path with space as prefix/eprefix/sbindir/mandir: for this thing You need to use backslash (\) with double quotation (") for every space. For example: ./configure --sbindir="/usr/bin\ with\ space/" See BUILD file for more information. _EOF exit 0 } ## Don't take values from env for these variables for var in PREFIX \ EPREFIX \ SBINDIR \ MANDIR \ NO_SANITIZE \ SHUTDOWN_PREFIX \ BUILD_SHUTDOWN \ SUPPORT_CGROUPS \ SYSCONTROLSOCKET do unset $var done ## Flag parser for arg in "$@"; do case "$arg" in --help) usage ;; --quiet) info() { true; } sub_info() { true; } warning() { true; } sub_warning() { true; } ;; --clean) rm -f test* & rm -f mconfig && exit 0 ;; --platform=*) PLATFORM="${arg#*=}" && shift ;; --prefix=*) PREFIX="${arg#*=}" && shift ;; --exec-prefix=*) EPREFIX="${arg#*=}" && shift;; --sbindir=*) SBINDIR="${arg#*=}" && shift ;; --mandir=*) MANDIR="${arg#*=}" && shift ;; --syscontrolsocket=*) SYSCONTROLSOCKET="${arg#*=}" && shift ;; --shutdown-prefix=*) SHUTDOWN_PREFIX="${arg#*=}" && shift ;; --enable-shutdown|--enable-shutdown=yes) BUILD_SHUTDOWN=yes ;; --disable-shutdown|--enable-shutdown=no) BUILD_SHUTDOWN=no ;; --enable-cgroups|--enable-cgroups=yes) SUPPORT_CGROUPS=1 ;; --disable-cgroups|--enable-cgroups=no) SUPPORT_CGROUPS=0 ;; --enable-utmpx|--enable-utmpx=yes) USE_UTMPX=1 ;; --disable-utmpx|--enable-utmpx=no) USE_UTMPX=0 ;; CXX=*|CXX_FOR_BUILD=*|CXXFLAGS_FOR_BUILD=*|CPPFLAGS_FOR_BUILD=*\ |LDFLAGS_FOR_BUILD=*|CXXFLAGS=*|CXXFLAGS_EXTRA=*|TEST_CXXFLAGS=*\ |TEST_CXXFLAGS_EXTRA|LDFLAGS=*|LDFLAGS_EXTRA=*|TEST_LDFLAGS=*\ |TEST_LDFLAGS_EXTRA=*|CPPFLAGS=*) eval "${arg%%=*}=\${arg#*=}" ;; *=*) warning Unknown variable: "${arg%%=*}" ;; *) warning Unknown argument: "$arg" ;; esac done ## General Defines : "${PLATFORM:=$(uname)}" : "${PREFIX:="/usr"}" : "${EPREFIX:="/"}" : "${SBINDIR:="/sbin"}" : "${MANDIR:="/share/man/"}" : "${NO_SANITIZE:="auto"}" : "${SHUTDOWN_PREFIX:=""}" : "${CXXFLAGS_EXTRA:=""}" : "${TEST_CXXFLAGS_EXTRA:=""}" : "${CPPFLAGS:=""}" : "${LDFLAGS_EXTRA:=""}" : "${TEST_LDFLAGS_EXTRA:=""}" if [ "$PLATFORM" = "Linux" ]; then : "${BUILD_SHUTDOWN:="yes"}" : "${SUPPORT_CGROUPS:="1"}" : "${SYSCONTROLSOCKET:="/run/dinitctl"}" else : "${BUILD_SHUTDOWN:="no"}" : "${SUPPORT_CGROUPS:="0"}" : "${SYSCONTROLSOCKET:="/var/run/dinitctl"}" fi ## Resolve final paths SBINDIR="$EPREFIX/$SBINDIR" MANDIR="$PREFIX/$MANDIR" ## Finalize $CXXFLAGS, $TEST_CXXFLAGS, $LDFLAGS, $TEST_LDFLAGS if [ -z "${CXXFLAGS+IS_SET}" ]; then CXXFLAGS="" AUTO_CXXFLAGS="true" else AUTO_CXXFLAGS="false" fi if [ -z "${TEST_CXXFLAGS+IS_SET}" ]; then TEST_CXXFLAGS="" AUTO_TEST_CXXFLAGS="true" else AUTO_TEST_CXXFLAGS="false" fi if [ -z "${LDFLAGS+IS_SET}" ]; then LDFLAGS="" AUTO_LDFLAGS="true" else AUTO_LDFLAGS="false" fi if [ -z "${TEST_LDFLAGS+IS_SET}" ]; then TEST_LDFLAGS="" AUTO_TEST_LDFLAGS="true" else AUTO_TEST_LDFLAGS="false" fi ## Verify PLATFORM value if [ "$PLATFORM" != "Linux" ] && [ "$PLATFORM" != "FreeBSD" ] && \ [ "$PLATFORM" != "OpenBSD" ] && [ "$PLATFORM" != "Darwin" ]; then warning "$PLATFORM" platform is unknown! sub_warning Known Platforms are: Linux, FreeBSD, OpenBSD, Darwin fi ## Create testfile.cc to test c++ compiler echo "int main(int argc, char **argv) { return 0; }" > testfile.cc || error Cant create test file ## Find and test C++ compiler if [ -z "${CXX:-}" ]; then info Checking C++ compiler... for guess in g++ clang++ c++; do findcxx "$guess" if [ -n "${CXX:-}" ]; then sub_info "$CXX" break # Found fi done if [ -z "${CXX:-}" ]; then sub_error No C++ compiler found! # Not found fi fi cxx_works "$CXX" if [ -n "${CXX_FOR_BUILD:-}" ]; then cxx_works "$CXX_FOR_BUILD" fi ## Test compiler/linker supported arguments if [ "$AUTO_CXXFLAGS" = "true" ]; then for argument in -std=c++11 \ -Wall \ -Os \ -fno-plt do try_cxx_argument CXXFLAGS $argument done if [ "$PLATFORM" != "Darwin" ]; then try_cxx_argument CXXFLAGS -fno-rtti fi fi if [ "$AUTO_LDFLAGS" = "true" ] && [ "$AUTO_CXXFLAGS" = "true" ]; then try_both_argument CXXFLAGS LDFLAGS -flto fi if [ "$AUTO_LDFLAGS" = "true" ] && [ "$PLATFORM" = "FreeBSD" ]; then try_ld_argument LDFLAGS -lrt fi # Add $CXXFLAGS and $LDFLAGS to $TEST_{LD,CXX}FLAGS when AUTO_TEST vars are true if [ "$AUTO_TEST_CXXFLAGS" = "true" ]; then TEST_CXXFLAGS="$CXXFLAGS" fi if [ "$AUTO_TEST_LDFLAGS" = "true" ]; then TEST_LDFLAGS="$LDFLAGS" fi if [ "$AUTO_TEST_LDFLAGS" = "true" ] && [ "$AUTO_TEST_CXXFLAGS" = "true" ]; then try_both_argument TEST_CXXFLAGS TEST_LDFLAGS -fsanitize=address,undefined fi ## Create mconfig rm -f testfile* info Creating mconfig... cat << _EOF > mconfig ## Auto-generated by "$0" for "$PLATFORM" # All changes will be lost if "$0" will be runned. # Installation path options. SBINDIR=$SBINDIR MANDIR=$MANDIR SYSCONTROLSOCKET=$SYSCONTROLSOCKET # General build options. # Linux (GCC): Note with GCC 5.x/6.x you must use the old ABI, with GCC 7.x you must use # the new ABI. See BUILD file for more information. # MacOS: Cannot use -fno-rtti: apparently prevents exception handling from working properly. # FreeBSD: Cannot use LTO with default linker. CXX=$CXX CXXFLAGS=$CXXFLAGS CXXFLAGS_EXTRA=$CXXFLAGS_EXTRA TEST_CXXFLAGS=$TEST_CXXFLAGS TEST_CXXFLAGS_EXTRA=$TEST_CXXFLAGS_EXTRA CPPFLAGS=$CPPFLAGS LDFLAGS=$LDFLAGS LDFLAGS_EXTRA=$LDFLAGS_EXTRA TEST_LDFLAGS=$TEST_LDFLAGS TEST_LDFLAGS_EXTRA=$TEST_LDFLAGS_EXTRA BUILD_SHUTDOWN=$BUILD_SHUTDOWN # Feature settings SUPPORT_CGROUPS=$SUPPORT_CGROUPS # Optional settings SHUTDOWN_PREFIX=${SHUTDOWN_PREFIX:-} _EOF if [ -n "${USE_UTMPX:-}" ]; then echo "USE_UTMPX=$USE_UTMPX" >> mconfig fi if [ -n "${CXX_FOR_BUILD:-}" ]; then { echo "" echo "# For cross-compiling" echo "CXX_FOR_BUILD=$CXX_FOR_BUILD" } >> mconfig fi if [ -n "${CXXFLAGS_FOR_BUILD:-}" ]; then echo "CXXFLAGS_FOR_BUILD=$CXXFLAGS_FOR_BUILD" >> mconfig fi if [ -n "${CPPFLAGS_FOR_BUILD:-}" ]; then echo "CPPFLAGS_FOR_BUILD=$CPPFLAGS_FOR_BUILD" >> mconfig fi if [ -n "${LDFLAGS_FOR_BUILD:-}" ]; then echo "LDFLAGS_FOR_BUILD=$LDFLAGS_FOR_BUILD" >> mconfig fi sub_info done. info Done! exit 0