test/make/configure.vm

Sun, 17 Sep 2023 12:08:05 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 17 Sep 2023 12:08:05 +0200
branch
configure-evo
changeset 55
d626acd09e9f
parent 54
2b8f40c58944
child 56
d9e8fdfc08f6
permissions
-rw-r--r--

revert to backticks - relates to #283

#!/bin/sh

# runs a command and returns 0 if the command failed
# same as ! cmd but compatible with svr4 sh
failed()
{
    $@
    if [ $? -ne 0 ]; then
        return 0
    else
        return 1
    fi
}

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

# define standard variables
PREFIX=/usr
EPREFIX=/usr
BINDIR=
SBINDIR=
LIBDIR=
LIBEXECDIR=
DATADIR=
SYSCONFDIR=
SHAREDSTATEDIR=
LOCALSTATEDIR=
INCLUDEDIR=
INFODIR=
MANDIR=

# custom variables
#foreach( $var in $vars )
#if( $var.exec )
${var.varName}=`${var.value}`
#else
${var.varName}=${var.value}
#end
#end

# features
#foreach( $feature in $features )
#if( ${feature.isDefault()} )
${feature.getVarName()}=auto
#end
#end

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

# help text
printhelp()
{
    echo "Usage: $0 [OPTIONS]..."
    cat << __EOF__
Installation directories:
  --prefix=PREFIX         path prefix for architecture-independent files
                          [/usr]
  --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]
  --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]

#if( $options.size() > 0 )
Options:
  --debug                 add extra compile flags for debug builds
  --release               add extra compile flags for release builds
#foreach( $opt in $options )
  --${opt.getArgument()}=${opt.getValuesString()}
#end

#end
#if( $features.size() > 0 )
Optional Features:
#foreach( $feature in $features )
#if( $feature.default )
  --disable-${feature.arg}
#else
  --enable-${feature.arg}
#end
#end

#end
__EOF__
}

#
# parse arguments
#
BUILD_TYPE="default"
#set( $D = '$' )
for ARG in "$@"
do
    case "$ARG" in
        "--prefix="*)         PREFIX=${D}{ARG#--prefix=} ;;
        "--exec-prefix="*)    EPREFIX=${D}{ARG#--exec-prefix=} ;;
        "--bindir="*)         BINDIR=${D}{ARG#----bindir=} ;;
        "--sbindir="*)        SBINDIR=${D}{ARG#--sbindir=} ;;
        "--libdir="*)         LIBDIR=${D}{ARG#--libdir=} ;;
        "--libexecdir="*)     LIBEXECDIR=${D}{ARG#--libexecdir=} ;;
        "--datadir="*)        DATADIR=${D}{ARG#--datadir=} ;;
        "--sysconfdir="*)     SYSCONFDIR=${D}{ARG#--sysconfdir=} ;;
        "--sharedstatedir="*) SHAREDSTATEDIR=${D}{ARG#--sharedstatedir=} ;;
        "--localstatedir="*)  LOCALSTATEDIR=${D}{ARG#--localstatedir=} ;;
        "--includedir="*)     INCLUDEDIR=${D}{ARG#--includedir=} ;;
        "--infodir="*)        INFODIR=${D}{ARG#--infodir=} ;;
        "--mandir"*)          MANDIR=${D}{ARG#--mandir} ;;
        "--help"*) printhelp; abort_configure ;;
        "--debug")           BUILD_TYPE="debug" ;;
        "--release")         BUILD_TYPE="release" ;;
    #foreach( $opt in $options )
        "--${opt.getArgument()}="*) ${opt.getVarName()}=${D}{ARG#--${opt.getArgument()}=} ;;
    #end
    #foreach( $feature in $features )
        "--enable-${feature.arg}") ${feature.getVarName()}=on ;;
        "--disable-${feature.arg}") unset ${feature.getVarName()} ;;
    #end
        "-"*) echo "unknown option: $ARG"; abort_configure ;;
    esac
done

## Begin unparsed content. **
#[[
# set dir variables
BINDIR=${BINDIR:-"$EPREFIX/bin"}
SBINDIR=${SBINDIR:-"$EPREFIX/sbin"}
LIBDIR=${LIBDIR:-"$EPREFIX/lib"}
LIBEXECDIR=${LIBEXECDIR:-"$EPREFIX/libexec"}
DATADIR=${DATADIR:-"$PREFIX/share"}
SYSCONFDIR=${SYSCONFDIR:-"$PREFIX/etc"}
SHAREDSTATEDIR=${SHAREDSTATEDIR:-"$PREFIX/com"}
LOCALSTATEDIR=${LOCALSTATEDIR:-"$PREFIX/var"}
INCLUDEDIR=${INCLUDEDIR:-"$PREFIX/include"}
INFODIR=${INFODIR:-"$PREFIX/info"}
MANDIR=${MANDIR:-"$PREFIX/man"}

# Test for availability of pkg-config
PKG_CONFIG=`command -v pkg-config`
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"
fi
if [ "$OS" = "Linux" ]; then
    PLATFORM="linux unix"
fi
if [ "$OS" = "FreeBSD" ]; then
    PLATFORM="freebsd bsd unix"
fi
if [ "$OS" = "Darwin" ]; then
    PLATFORM="macos osx bsd unix"
fi
if echo "$OS" | grep -i "MINGW" > /dev/null; then
    PLATFORM="windows mingw"
fi
PLATFORM=${PLATFORM:-"unix"}

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

isplatform()
{
    for p in $PLATFORM
    do
        if [ "$p" = "$1" ]; then
            return 0
        fi
    done
    return 1
}
]]#
## End of unparsed content **

# generate config.mk
cat > "$TEMP_DIR/vars.mk" << __EOF__
PREFIX=$PREFIX
EPREFIX=$EPREFIX
BINDIR=$BINDIR
SBINDIR=$SBINDIR
LIBDIR=$LIBDIR
LIBEXECDIR=$LIBEXECDIR
DATADIR=$DATADIR
SYSCONFDIR=$SYSCONFDIR
SHAREDSTATEDIR=$SHAREDSTATEDIR
LOCALSTATEDIR=$LOCALSTATEDIR
INCLUDEDIR=$INCLUDEDIR
INFODIR=$INFODIR
MANDIR=$MANDIR
#foreach( $var in $vars )
${var.varName}=$${var.varName}
#end
__EOF__
cat > "$TEMP_DIR/config.mk" << __EOF__
#
# config.mk generated by configure
#
__EOF__
sort -u "$TEMP_DIR/vars.mk" >> "$TEMP_DIR/config.mk"


# toolchain detection
# this will insert make vars to config.mk
. make/toolchain.sh
if [ -n "$TOOLCHAIN_ERROR" ]; then
    echo "Toolchain error: $TOOLCHAIN_ERROR"
    echo "Abort."
    abort_configure
fi

#
# DEPENDENCIES
#

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

#foreach( $dependency in $namedDependencies )
dependency_${dependency.name}()
{
    printf "checking for ${dependency.name}... "
    #foreach( $sub in $dependency.getSubdependencies() )
    # dependency $sub.getFullName()
    while true
    do
        #if( $sub.platform )
        if failed isplatform "${sub.platform}"; then
            break
        fi
        #end
        #foreach( $np in $sub.getNotList() )
        if isplatform "${np}"; then
            break
        fi
        #end
        #if( $sub.pkgconfig.size() > 0 )
        if [ -z "$PKG_CONFIG" ]; then
            break
        fi
        #end
        #foreach( $test in $sub.tests )
        if failed $test > /dev/null ; then
            break
        fi
        #end
        #foreach( $pkg in $sub.pkgconfig )
        if failed $pkg.getPkgConfigTestQuery() ; then
            break
        fi
        TEMP_CFLAGS="$TEMP_CFLAGS `"$PKG_CONFIG" --cflags $pkg.name`"
        TEMP_LDFLAGS="$TEMP_LDFLAGS `"$PKG_CONFIG" --libs $pkg.name`"
        #end
        #foreach( $flags in $sub.flags )
        #if( $flags.exec )
        if tmp_flags=`$flags.value` ; then
            TEMP_$flags.varName="$TEMP_$flags.varName $tmp_flags"
        else
            break
        fi
        #else
        TEMP_$flags.varName="$TEMP_$flags.varName $flags.value"
        #end
        #end
        #if ( $sub.make.length() > 0 )
        cat >> $TEMP_DIR/make.mk << __EOF__
# Dependency: $dependency.name
$sub.make
__EOF__
        #end
        echo yes
        return 0
    done

    #end
    echo no
    return 1
}
#end

DEPENDENCIES_FAILED=
ERROR=0
#if( $dependencies.size() > 0 )
# unnamed dependencies
TEMP_CFLAGS=
TEMP_LDFLAGS=
#foreach( $dependency in $dependencies )
while true
do
    #if( $dependency.platform )
    if failed isplatform "${dependency.platform}"; then
        break
    fi
    #end
    #foreach( $np in $dependency.getNotList() )
    if isplatform "${np}"; then
        break
    fi
    #end
    while true
    do
        #if( $dependency.pkgconfig.size() > 0 )
        if [ -z "$PKG_CONFIG" ]; then
            ERROR=1
            break
        fi
        #end
        #foreach( $pkg in $dependency.pkgconfig )
        printf "checking for pkg-config package $pkg.name... "
        if failed $pkg.getPkgConfigTestQuery() ; then
            echo no
            ERROR=1
            break
        fi
        echo yes
        TEMP_CFLAGS="$TEMP_CFLAGS `"$PKG_CONFIG" --cflags $pkg.name`"
        TEMP_LDFLAGS="$TEMP_LDFLAGS `"$PKG_CONFIG" --libs $pkg.name`"
        #end

        #foreach( $flags in $dependency.flags )
        #if( $flags.exec )
        $flags.value > /dev/null
        if tmp_flags=`$flags.value` ; then
            TEMP_$flags.varName="$TEMP_$flags.varName $tmp_flags"
        else
            ERROR=1
            break
        fi
        #else
        TEMP_$flags.varName="$TEMP_$flags.varName $flags.value"
        #end
        #end
        #if ( $dependency.make.length() > 0 )
        cat >> "$TEMP_DIR/make.mk" << __EOF__
$dependency.make
__EOF__
        #end
        break
    done
    break
done
#end

# add unnamed dependency flags to config.mk
echo >> "$TEMP_DIR/config.mk"
if [ -n "${TEMP_CFLAGS}" ]; then
    echo "CFLAGS += $TEMP_CFLAGS" >> "$TEMP_DIR/config.mk"
fi
if [ -n "${TEMP_LDFLAGS}" ]; then
    echo "LDFLAGS += $TEMP_LDFLAGS" >> "$TEMP_DIR/config.mk"
fi
#end

#
# OPTION VALUES
#
#foreach( $opt in $options )
#foreach( $val in $opt.values )
${val.func}()
{
    VERR=0
    #foreach( $dep in $val.dependencies )
    if failed dependency_$dep ; then
        VERR=1
    fi
    #end
    if [ $VERR -ne 0 ]; then
        return 1
    fi
    #foreach( $def in $val.defines )
        TEMP_CFLAGS="$TEMP_CFLAGS ${def.toFlags()}"
    #end
    #if( $val.hasMake() )
    cat >> "$TEMP_DIR/make.mk" << __EOF__
$val.make
__EOF__
    #end
    return 0
}
#end
#end

#
# TARGETS
#

#foreach( $target in $targets )
#if ( $target.name )
# Target: $target.name
#else
# Target
#end
TEMP_CFLAGS=
TEMP_LDFLAGS=

#foreach( $dependency in $target.dependencies )
if failed dependency_$dependency; then
    DEPENDENCIES_FAILED="$DEPENDENCIES_FAILED ${dependency} "
    ERROR=1
fi
#end

# Features
#foreach( $feature in $target.features )
if [ -n "$${feature.getVarName()}" ]; then
#foreach( $dependency in $feature.dependencies )
    # check dependency
    if failed dependency_$dependency ; then
        # "auto" features can fail and are just disabled in this case
        if [ "$${feature.getVarName()}" = "auto" ]; then
            unset ${feature.getVarName()}
		else
            DEPENDENCIES_FAILED="$DEPENDENCIES_FAILED ${dependency} "
            ERROR=1
        fi
    fi
#end
fi
#end

#foreach( $opt in $target.options )
# Option: --${opt.argument}
if [ -z ${D}${opt.getVarName()} ]; then
    SAVED_ERROR="$ERROR"
    SAVED_DEPENDENCIES_FAILED="$DEPENDENCIES_FAILED"
    ERROR=1
    while true
    do
        #foreach( $optdef in $opt.defaults )
        #if( $optdef.platform )
        if isplatform "$optdef.platform"; then
        #end
        if $optdef.func ; then
            echo "  ${opt.argument}: ${optdef.valueName}" >> "$TEMP_DIR/options"
            ERROR=0
            break
        fi
        #if( $optdef.platform )
        fi
        #end
        #end
        break
    done
    if [ $ERROR -ne 0 ]; then
        SAVED_ERROR=1
    fi
    ERROR="$SAVED_ERROR"
    DEPENDENCIES_FAILED="$SAVED_DEPENDENCIES_FAILED"
else
    if false; then
        false
    #foreach( $optval in $opt.values )
    elif [ "${D}${opt.getVarName()}" = "${optval.value}" ]; then
        echo "  ${opt.argument}: ${D}${opt.getVarName()}" >> $TEMP_DIR/options
        if failed $optval.func ; then
            ERROR=1
        fi
    #end
    fi
fi
#end

echo >> "$TEMP_DIR/config.mk"
if [ -n "${TEMP_CFLAGS}" ]; then
    echo "${target.getCFlags()}  += $TEMP_CFLAGS" >> "$TEMP_DIR/config.mk"
fi
if [ "$BUILD_TYPE" = "debug" ]; then
    echo '${target.getCFlags()} += ${DEBUG_FLAGS}' >> "$TEMP_DIR/config.mk"
fi
if [ "$BUILD_TYPE" = "release" ]; then
    echo '${target.getCFlags()} += ${RELEASE_FLAGS}' >> "$TEMP_DIR/config.mk"
fi
if [ -n "${TEMP_LDFLAGS}" ]; then
    echo "${target.getLDFlags()} += $TEMP_LDFLAGS" >> "$TEMP_DIR/config.mk"
fi

#end
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"
#if ( $options.size() > 0 )
echo "Options:"
cat "$TEMP_DIR/options"
#end
#if ( $features.size() > 0 )
echo "Features:"
#foreach( $feature in $features )
if [ -n "$${feature.getVarName()}" ]; then
echo "  $feature.name: on"
else
echo "  $feature.name: off"
fi
#end
#end
echo
cat "$TEMP_DIR/config.mk" "$TEMP_DIR/make.mk" > config.mk
rm -Rf "$TEMP_DIR"

mercurial