test/configure

Tue, 12 Nov 2024 17:00:51 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 12 Nov 2024 17:00:51 +0100
changeset 123
d89b75fdf0aa
parent 122
5706694c17c4
child 127
33bc4ae5d7cf
permissions
-rwxr-xr-x

add target data for disabled features - resolves #476

#!/bin/sh


# some utility functions
isplatform()
{
    for p in $PLATFORM
    do
        if [ "$p" = "$1" ]; then
            return 0
        fi
    done
    return 1
}
notisplatform()
{
    for p in $PLATFORM
    do
        if [ "$p" = "$1" ]; then
            return 1
        fi
    done
    return 0
}
istoolchain()
{
    for t in $TOOLCHAIN
    do
        if [ "$t" = "$1" ]; then
            return 0
        fi
    done
    return 1
}
notistoolchain()
{
    for t in $TOOLCHAIN
    do
        if [ "$t" = "$1" ]; then
            return 1
        fi
    done
    return 0
}

# clean abort
abort_configure()
{
    rm -Rf "$TEMP_DIR"
    exit 1
}

# Test for availability of pkg-config
PKG_CONFIG=`command -v pkg-config`
: ${PKG_CONFIG:="false"}

# Simple uname based platform detection
# $PLATFORM is used for platform dependent dependency selection
OS=`uname -s`
OS_VERSION=`uname -r`
printf "detect platform... "
if [ "$OS" = "SunOS" ]; then
    PLATFORM="solaris sunos unix svr4"
elif [ "$OS" = "Linux" ]; then
    PLATFORM="linux unix"
elif [ "$OS" = "FreeBSD" ]; then
    PLATFORM="freebsd bsd unix"
elif [ "$OS" = "OpenBSD" ]; then
    PLATFORM="openbsd bsd unix"
elif [ "$OS" = "NetBSD" ]; then
    PLATFORM="netbsd bsd unix"
elif [ "$OS" = "Darwin" ]; then
    PLATFORM="macos osx bsd unix"
elif echo "$OS" | grep -i "MINGW" > /dev/null; then
    PLATFORM="windows mingw"
fi
: ${PLATFORM:="unix"}

PLATFORM_NAME=`echo "$PLATFORM" | cut -f1 -d' ' -`
echo "$PLATFORM_NAME"


# help text
printhelp()
{
    echo "Usage: $0 [OPTIONS]..."
    cat << __EOF__
Installation directories:
  --prefix=PREFIX         path prefix for architecture-independent files
                          [$prefix]
  --exec-prefix=EPREFIX   path prefix for architecture-dependent files
                          [PREFIX]

  --bindir=DIR            user executables [EPREFIX/bin]
  --sbindir=DIR           system admin executables [EPREFIX/sbin]
  --libexecdir=DIR        program executables [EPREFIX/libexec]
  --sysconfdir=DIR        system configuration files [PREFIX/etc]
  --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]
  --localstatedir=DIR     modifiable single-machine data [PREFIX/var]
  --runstatedir=DIR       run-time variable data [LOCALSTATEDIR/run]
  --libdir=DIR            object code libraries [EPREFIX/lib]
  --includedir=DIR        C header files [PREFIX/include]
  --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]
  --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]
  --infodir=DIR           info documentation [DATAROOTDIR/info]
  --mandir=DIR            man documentation [DATAROOTDIR/man]
  --localedir=DIR         locale-dependent data [DATAROOTDIR/locale]

Optional Features:
  --disable-pg

__EOF__
}

# create temporary directory
TEMP_DIR=".tmp-`uname -n`"
rm -Rf "$TEMP_DIR"
if mkdir -p "$TEMP_DIR"; then
    :
else
    echo "Cannot create tmp dir $TEMP_DIR"
    echo "Abort"
    exit 1
fi
touch "$TEMP_DIR/options"
touch "$TEMP_DIR/features"

# define standard variables
# also define standard prefix (this is where we will search for config.site)
prefix=/usr
exec_prefix=
bindir=
sbindir=
libdir=
libexecdir=
datarootdir=
datadir=
sysconfdir=
sharedstatedir=
localstatedir=
runstatedir=
includedir=
infodir=
localedir=
mandir=

# custom variables
if true \
      && notisplatform "mingw" \
      ; then
    prefix="`pwd`/work"
fi
if true \
    && isplatform "mingw" \
      ; then
    prefix="`pwd -W`/work"
fi
if true \
      ; then
    HOST=`uname -n`
    install_dir="$prefix"
fi

# features
FEATURE_PG=auto

#
# parse arguments
#
BUILD_TYPE="default"
for ARG in "$@"
do
    case "$ARG" in
        "--prefix="*)         prefix=${ARG#--prefix=} ;;
        "--exec-prefix="*)    exec_prefix=${ARG#--exec-prefix=} ;;
        "--bindir="*)         bindir=${ARG#----bindir=} ;;
        "--sbindir="*)        sbindir=${ARG#--sbindir=} ;;
        "--libdir="*)         libdir=${ARG#--libdir=} ;;
        "--libexecdir="*)     libexecdir=${ARG#--libexecdir=} ;;
        "--datarootdir="*)    datarootdir=${ARG#--datarootdir=} ;;
        "--datadir="*)        datadir=${ARG#--datadir=} ;;
        "--sysconfdir="*)     sysconfdir=${ARG#--sysconfdir=} ;;
        "--sharedstatedir="*) sharedstatedir=${ARG#--sharedstatedir=} ;;
        "--localstatedir="*)  localstatedir=${ARG#--localstatedir=} ;;
        "--includedir="*)     includedir=${ARG#--includedir=} ;;
        "--infodir="*)        infodir=${ARG#--infodir=} ;;
        "--mandir"*)          mandir=${ARG#--mandir} ;;
        "--localedir"*)       localedir=${ARG#--localedir} ;;
        "--help"*) printhelp; abort_configure ;;
        "--debug")           BUILD_TYPE="debug" ;;
        "--release")         BUILD_TYPE="release" ;;
        "--enable-pg") FEATURE_PG=on ;;
        "--disable-pg") unset FEATURE_PG ;;
        "-"*) echo "unknown option: $ARG"; abort_configure ;;
    esac
done



# set defaults for dir variables
: ${exec_prefix:="$prefix"}
: ${bindir:='${exec_prefix}/bin'}
: ${sbindir:='${exec_prefix}/sbin'}
: ${libdir:='${exec_prefix}/lib'}
: ${libexecdir:='${exec_prefix}/libexec'}
: ${datarootdir:='${prefix}/share'}
: ${datadir:='${datarootdir}'}
: ${sysconfdir:='${prefix}/etc'}
: ${sharedstatedir:='${prefix}/com'}
: ${localstatedir:='${prefix}/var'}
: ${runstatedir:='${localstatedir}/run'}
: ${includedir:='${prefix}/include'}
: ${infodir:='${datarootdir}/info'}
: ${mandir:='${datarootdir}/man'}
: ${localedir:='${datarootdir}/locale'}

# check if a config.site exists and load it
if [ -n "$CONFIG_SITE" ]; then
    # CONFIG_SITE may contain space separated file names
    for cs in $CONFIG_SITE; do
        printf "loading defaults from $cs... "
        . "$cs"
        echo ok
    done
elif [ -f "$prefix/share/config.site" ]; then
    printf "loading site defaults... "
    . "$prefix/share/config.site"
    echo ok
elif [ -f "$prefix/etc/config.site" ]; then
    printf "loading site defaults... "
    . "$prefix/etc/config.site"
    echo ok
fi


# generate vars.mk
cat > "$TEMP_DIR/vars.mk" << __EOF__
prefix=$prefix
exec_prefix=$exec_prefix
bindir=$bindir
sbindir=$sbindir
libdir=$libdir
libexecdir=$libexecdir
datarootdir=$datarootdir
datadir=$datadir
sysconfdir=$sysconfdir
sharedstatedir=$sharedstatedir
localstatedir=$localstatedir
runstatedir=$runstatedir
includedir=$includedir
infodir=$infodir
mandir=$mandir
localedir=$localedir
__EOF__

# toolchain detection utilities
. make/toolchain.sh

#
# DEPENDENCIES
#

# check languages
lang_c=
lang_cpp=
if detect_cpp_compiler ; then
    lang_cpp=1
fi
if detect_c_compiler ; then
    lang_c=1
fi

# create buffer for make variables required by dependencies
echo > "$TEMP_DIR/make.mk"

test_pkg_config()
{
    if "$PKG_CONFIG" --exists "$1" ; then :
    else return 1 ; fi
    if [ -z "$2" ] || "$PKG_CONFIG" --atleast-version="$2" "$1" ; then :
    else return 1 ; fi
    if [ -z "$3" ] || "$PKG_CONFIG" --exact-version="$3" "$1" ; then :
    else return 1 ; fi
    if [ -z "$4" ] || "$PKG_CONFIG" --max-version="$4" "$1" ; then :
    else return 1 ; fi
    return 0
}

print_check_msg()
{
    if [ -z "$1" ]; then
        shift
        printf "$@"
    fi
}

dependency_error_cpp()
{
    print_check_msg "$dep_checked_cpp" "checking for cpp... "
    # dependency cpp
    while true
    do
        if [ -z "$lang_cpp" ] ; then
            break
        fi
        print_check_msg "$dep_checked_cpp" "yes\n"
        dep_checked_cpp=1
        return 1
    done

    print_check_msg "$dep_checked_cpp" "no\n"
    dep_checked_cpp=1
    return 0
}
dependency_error_libpq()
{
    print_check_msg "$dep_checked_libpq" "checking for libpq... "
    # dependency libpq
    while true
    do
        if [ -z "$PKG_CONFIG" ]; then
            break
        fi
        if test_pkg_config "libpq" "" "" "" ; then
            TEMP_CFLAGS="$TEMP_CFLAGS `"$PKG_CONFIG" --cflags libpq`"
            TEMP_LDFLAGS="$TEMP_LDFLAGS `"$PKG_CONFIG" --libs libpq`"
        else
            break
        fi
        print_check_msg "$dep_checked_libpq" "yes\n"
        dep_checked_libpq=1
        return 1
    done

    print_check_msg "$dep_checked_libpq" "no\n"
    dep_checked_libpq=1
    return 0
}
dependency_error_openssl()
{
    print_check_msg "$dep_checked_openssl" "checking for openssl... "
    # dependency openssl toolchain="clang"
    while true
    do
        if notistoolchain "clang"; then
            break
        fi
        if [ -z "$lang_c" ] ; then
            break
        fi
        if [ -z "$PKG_CONFIG" ]; then
            break
        fi
        if test_pkg_config "openssl" "" "" "" ; then
            TEMP_CFLAGS="$TEMP_CFLAGS `"$PKG_CONFIG" --cflags openssl`"
            TEMP_LDFLAGS="$TEMP_LDFLAGS `"$PKG_CONFIG" --libs openssl`"
        else
            break
        fi
        TEMP_CFLAGS="$TEMP_CFLAGS -DOPENSSL_CLANG"
        print_check_msg "$dep_checked_openssl" "yes\n"
        dep_checked_openssl=1
        return 1
    done

    # dependency openssl platform="bsd"
    while true
    do
        if notisplatform "bsd"; then
            break
        fi
        TEMP_LDFLAGS="$TEMP_LDFLAGS -lssl -lcrypto"
        print_check_msg "$dep_checked_openssl" "yes\n"
        dep_checked_openssl=1
        return 1
    done

    # dependency openssl
    while true
    do
        if [ -z "$lang_c" ] ; then
            break
        fi
        if [ -z "$PKG_CONFIG" ]; then
            break
        fi
        if test_pkg_config "openssl" "" "" "" ; then
            TEMP_CFLAGS="$TEMP_CFLAGS `"$PKG_CONFIG" --cflags openssl`"
            TEMP_LDFLAGS="$TEMP_LDFLAGS `"$PKG_CONFIG" --libs openssl`"
        else
            break
        fi
        print_check_msg "$dep_checked_openssl" "yes\n"
        dep_checked_openssl=1
        return 1
    done

    print_check_msg "$dep_checked_openssl" "no\n"
    dep_checked_openssl=1
    return 0
}
dependency_error_libxml2()
{
    print_check_msg "$dep_checked_libxml2" "checking for libxml2... "
    # dependency libxml2 platform="macos"
    while true
    do
        if notisplatform "macos"; then
            break
        fi
        if tmp_flags=`xml2-config --cflags` ; then
            TEMP_CFLAGS="$TEMP_CFLAGS $tmp_flags"
        else
            break
        fi
        if tmp_flags=`xml2-config --libs` ; then
            TEMP_LDFLAGS="$TEMP_LDFLAGS $tmp_flags"
        else
            break
        fi
        print_check_msg "$dep_checked_libxml2" "yes\n"
        dep_checked_libxml2=1
        return 1
    done

    # dependency libxml2
    while true
    do
        if [ -z "$PKG_CONFIG" ]; then
            break
        fi
        if test_pkg_config "libxml-2.0" "" "" "" ; then
            TEMP_CFLAGS="$TEMP_CFLAGS `"$PKG_CONFIG" --cflags libxml-2.0`"
            TEMP_LDFLAGS="$TEMP_LDFLAGS `"$PKG_CONFIG" --libs libxml-2.0`"
        else
            break
        fi
        print_check_msg "$dep_checked_libxml2" "yes\n"
        dep_checked_libxml2=1
        return 1
    done

    print_check_msg "$dep_checked_libxml2" "no\n"
    dep_checked_libxml2=1
    return 0
}

# start collecting dependency information
echo > "$TEMP_DIR/flags.mk"

DEPENDENCIES_FAILED=
ERROR=0
# unnamed dependencies
TEMP_CFLAGS=
TEMP_CXXFLAGS=
TEMP_LDFLAGS=
while true
do
    while true
    do
        if [ -z "$lang_c" ] ; then
            ERROR=1
            break
        fi

        break
    done
    break
done
while true
do
    if notistoolchain "gcc"; then
        break
    fi
    while true
    do

        TEMP_CFLAGS="$TEMP_CFLAGS -DTOOLCHAIN_GCC"
        break
    done
    break
done
while true
do
    if notistoolchain "clang"; then
        break
    fi
    while true
    do

        TEMP_CFLAGS="$TEMP_CFLAGS -DTOOLCHAIN_CLANG"
        break
    done
    break
done
while true
do
    if isplatform "gcc" || istoolchain "gcc"; then
        break
    fi
    while true
    do

        TEMP_CFLAGS="$TEMP_CFLAGS -DNOT_TOOLCHAIN_GCC"
        break
    done
    break
done
while true
do
    if isplatform "clang" || istoolchain "clang"; then
        break
    fi
    while true
    do

        TEMP_CFLAGS="$TEMP_CFLAGS -DNOT_TOOLCHAIN_CLANG"
        break
    done
    break
done

# add general dependency flags to flags.mk
echo "# general flags" >> "$TEMP_DIR/flags.mk"
if [ -n "${TEMP_CFLAGS}" ] && [ -n "$lang_c" ]; then
    echo "CFLAGS += $TEMP_CFLAGS" >> "$TEMP_DIR/flags.mk"
fi
if [ -n "${TEMP_CXXFLAGS}" ] && [ -n "$lang_cpp" ]; then
    echo "CXXFLAGS += $TEMP_CXXFLAGS" >> "$TEMP_DIR/flags.mk"
fi
if [ -n "${TEMP_LDFLAGS}" ]; then
    echo "LDFLAGS += $TEMP_LDFLAGS" >> "$TEMP_DIR/flags.mk"
fi

#
# OPTION VALUES
#

#
# TARGETS
#

echo >> "$TEMP_DIR/flags.mk"
echo "configuring global target"
echo "# flags for unnamed target" >> "$TEMP_DIR/flags.mk"
TEMP_CFLAGS=
TEMP_CXXFLAGS=
TEMP_LDFLAGS=

if dependency_error_libxml2; then
    DEPENDENCIES_FAILED="$DEPENDENCIES_FAILED libxml2 "
    ERROR=1
fi
if dependency_error_openssl; then
    DEPENDENCIES_FAILED="$DEPENDENCIES_FAILED openssl "
    ERROR=1
fi

# Features
if [ -n "$FEATURE_PG" ]; then
    # check dependency
    if dependency_error_libpq ; then
        # "auto" features can fail and are just disabled in this case
        if [ "$FEATURE_PG" = "auto" ]; then
            DISABLE_FEATURE_PG=1
        else
            DEPENDENCIES_FAILED="$DEPENDENCIES_FAILED libpq "
            ERROR=1
        fi
    fi
    # check dependency
    if dependency_error_cpp ; then
        # "auto" features can fail and are just disabled in this case
        if [ "$FEATURE_PG" = "auto" ]; then
            DISABLE_FEATURE_PG=1
        else
            DEPENDENCIES_FAILED="$DEPENDENCIES_FAILED cpp "
            ERROR=1
        fi
    fi
    if [ -n "$DISABLE_FEATURE_PG" ]; then
        unset FEATURE_PG
    fi
fi
if [ -n "$FEATURE_PG" ]; then
    :
else
    :
fi


if [ -n "${TEMP_CFLAGS}" ] && [ -n "$lang_c" ]; then
    echo "CFLAGS  += $TEMP_CFLAGS" >> "$TEMP_DIR/flags.mk"
fi
if [ -n "${TEMP_CXXFLAGS}" ] && [ -n "$lang_cpp" ]; then
    echo "CXXFLAGS  += $TEMP_CXXFLAGS" >> "$TEMP_DIR/flags.mk"
fi
if [ "$BUILD_TYPE" = "debug" ]; then
    if [ -n "$lang_c" ]; then
        echo 'CFLAGS += ${DEBUG_CC_FLAGS}' >> "$TEMP_DIR/flags.mk"
    fi
    if [ -n "$lang_cpp" ]; then
        echo 'CXXFLAGS += ${DEBUG_CXX_FLAGS}' >> "$TEMP_DIR/flags.mk"
    fi
fi
if [ "$BUILD_TYPE" = "release" ]; then
    if [ -n "$lang_c" ]; then
        echo 'CFLAGS += ${RELEASE_CC_FLAGS}' >> "$TEMP_DIR/flags.mk"
    fi
    if [ -n "$lang_cpp" ]; then
        echo 'CXXFLAGS += ${RELEASE_CXX_FLAGS}' >> "$TEMP_DIR/flags.mk"
    fi
fi
if [ -n "${TEMP_LDFLAGS}" ]; then
    echo "LDFLAGS += $TEMP_LDFLAGS" >> "$TEMP_DIR/flags.mk"
fi


# final result
if [ $ERROR -ne 0 ]; then
    echo
    echo "Error: Unresolved dependencies"
    echo "$DEPENDENCIES_FAILED"
    abort_configure
fi

echo "configure finished"
echo
echo "Build Config:"
echo "  PREFIX:      $prefix"
echo "  TOOLCHAIN:   $TOOLCHAIN_NAME"
echo "Features:"
if [ -n "$FEATURE_PG" ]; then
echo "  pg: on"
else
echo "  pg: off"
fi
echo

# generate the config.mk file
cat > "$TEMP_DIR/config.mk" << __EOF__
#
# config.mk generated by configure
#

__EOF__
write_toolchain_defaults "$TEMP_DIR/toolchain.mk"
cat "$TEMP_DIR/vars.mk" "$TEMP_DIR/toolchain.mk" "$TEMP_DIR/flags.mk" "$TEMP_DIR/make.mk" > config.mk
rm -Rf "$TEMP_DIR"

mercurial