test/make/configure.vm

Sun, 03 Sep 2023 12:24:39 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Sep 2023 12:24:39 +0200
changeset 35
57ea620a54ac
parent 34
4486849e6a6c
child 37
ef09f80e911e
permissions
-rw-r--r--

fix several unquoted variable uses

#!/bin/sh

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

#if ( ! $project.hasVar("PREFIX") )
PREFIX=/usr
#end
#if ( ! $project.hasVar("EPREFIX") )
EPREFIX=$PREFIX
#end

#if ( ! $project.hasVar("BINDIR") )
BINDIR=
#end
#if ( ! $project.hasVar("SBINDIR") )
SBINDIR=
#end
#if ( ! $project.hasVar("LIBDIR") )
LIBDIR=
#end
#if ( ! $project.hasVar("LIBEXECDIR") )
LIBEXECDIR=
#end
#if ( ! $project.hasVar("DATADIR") )
DATADIR=
#end
#if ( ! $project.hasVar("SYSCONFDIR") )
SYSCONFDIR=
#end
#if ( ! $project.hasVar("SHAREDSTATEDIR") )
SHAREDSTATEDIR=
#end
#if ( ! $project.hasVar("LOCALSTATEDIR") )
LOCALSTATEDIR=
#end
#if ( ! $project.hasVar("INCLUDEDIR") )
INCLUDEDIR=
#end
#if ( ! $project.hasVar("INFODIR") )
INFODIR=
#end
#if ( ! $project.hasVar("MANDIR") )
MANDIR=
#end

OS=`uname -s`
OS_VERSION=`uname -r`

TEMP_DIR=".tmp-`uname -n`"
mkdir -p "$TEMP_DIR"
if [ $? -ne 0 ]; then
    echo "Cannot create tmp dir"
    echo "Abort"
fi
touch "$TEMP_DIR/options"
touch "$TEMP_DIR/features"

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

# 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:
#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
#
#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; rm -Rf "$TEMP_DIR"; exit 1 ;;
    #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"; rm -Rf "$TEMP_DIR"; exit 1 ;;
    esac
done

# set dir variables
if [ -z "$BINDIR" ]; then
    BINDIR=$EPREFIX/bin
fi
if [ -z "$SBINDIR" ]; then
    SBINDIR=$EPREFIX/sbin
fi
if [ -z "$LIBDIR" ]; then
    LIBDIR=$EPREFIX/lib
fi
if [ -z "$LIBEXEC" ]; then
    LIBEXECDIR=$EPREFIX/libexec
fi
if [ -z "$DATADIR" ]; then
    DATADIR=$PREFIX/share
fi
if [ -z "$SYSCONFDIR" ]; then
    SYSCONFDIR=$PREFIX/etc
fi
if [ -z "$SHAREDSTATEDIR" ]; then
    SHAREDSTATEDIR=$PREFIX/com
fi
if [ -z "$LOCALSTATEDIR" ]; then
    LOCALSTATEDIR=$PREFIX/var
fi
if [ -z "$INCLUDEDIR" ]; then
    INCLUDEDIR=$PREFIX/include
fi
if [ -z "$INFODIR" ]; then
    INFODIR=$PREFIX/info
fi
if [ -z "$MANDIR" ]; then
    MANDIR=$PREFIX/man
fi

which pkg-config > /dev/null
if [ $? -eq 0 ]; then
    PKG_CONFIG=pkg-config
else
    PKG_CONFIG=false
fi

# Simple uname based platform detection
# $PLATFORM is used for platform dependent dependency selection
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
echo "$OS" | grep -i "MINGW" > /dev/null
if [ $? -eq 0 ]; then
    PLATFORM="windows mingw"
fi

if [ -z "$PLATFORM" ]; then
    PLATFORM="unix"
fi

for p in $PLATFORM
do
    PLATFORM_NAME=$p
    break
done
echo $PLATFORM_NAME

isplatform()
{
    for p in $PLATFORM
    do
        if [ "$p" = "$1" ]; then
            return 0
        fi
    done
    return 1
}
isnotplatform()
{
    for p in $PLATFORM
    do
        if [ "$p" = "$1" ]; then
            return 1
        fi
    done
    return 0
}

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

# general vars
#foreach( $var in $vars )
${var.name}=$${var.name}
#end

#if ( ! $project.hasVar("PREFIX") )
PREFIX=$PREFIX
#end
#if ( ! $project.hasVar("EPREFIX") )
EPREFIX=$EPREFIX
#end

#if ( ! $project.hasVar("BINDIR") )
BINDIR=$BINDIR
#end
#if ( ! $project.hasVar("SBINDIR") )
SBINDIR=$SBINDIR
#end
#if ( ! $project.hasVar("LIBDIR") )
LIBDIR=$LIBDIR
#end
#if ( ! $project.hasVar("LIBEXECDIR") )
LIBEXECDIR=$LIBEXECDIR
#end
#if ( ! $project.hasVar("DATADIR") )
DATADIR=$DATADIR
#end
#if ( ! $project.hasVar("SYSCONFDIR") )
SYSCONFDIR=$SYSCONFDIR
#end
#if ( ! $project.hasVar("SHAREDSTATEDIR") )
SHAREDSTATEDIR=$SHAREDSTATEDIR
#end
#if ( ! $project.hasVar("LOCALSTATEDIR") )
LOCALSTATEDIR=$LOCALSTATEDIR
#end
#if ( ! $project.hasVar("INCLUDEDIR") )
INCLUDEDIR=$INCLUDEDIR
#end
#if ( ! $project.hasVar("INFODIR") )
INFODIR=$INFODIR
#end
#if ( ! $project.hasVar("MANDIR") )
MANDIR=$MANDIR
#end

__EOF__

echo > "$TEMP_DIR/make.mk"

ENV_CFLAGS=$CFLAGS
ENV_LDFLAGS=$LDFLAGS
ENV_CXXFLAGS=$CXXFLAGS

# Toolchain detection
# this will insert make vars to config.mk
. make/toolchain.sh

# add user specified flags to config.mk
echo >> "$TEMP_DIR/config.mk"
if [ -n "${ENV_CFLAGS}" ]; then
    echo "CFLAGS += $ENV_CFLAGS" >> "$TEMP_DIR/config.mk"
fi
if [ -n "${ENV_CXXFLAGS}" ]; then
    echo "CXXFLAGS += $ENV_CXXFLAGS" >> "$TEMP_DIR/config.mk"
fi
if [ -n "${ENV_LDFLAGS}" ]; then
    echo "LDFLAGS += $ENV_LDFLAGS" >> "$TEMP_DIR/config.mk"
fi

#
# DEPENDENCIES
#

#foreach( $dependency in $namedDependencies )
dependency_${dependency.name}()
{
    printf "checking for ${dependency.name}... "
    #foreach( $sub in $dependency.getSubdependencies() )
    # dependency $sub.name $sub.getPlatformString()
    while true
    do
        #if( $sub.platform )
        if isnotplatform "${sub.platform}"; then
            break
        fi
        #end
        #foreach( $not in $sub.getNotList() )
        if isplatform "${not}"; then
            break
        fi
        #end
        #if( $sub.pkgconfig.size() > 0 )
        if [ -z "$PKG_CONFIG" ]; then
            break
        fi
        #end
        #foreach( $test in $sub.tests )
        $test > /dev/null
        if [ $? -ne 0 ]; then
        break
        fi
        #end
        #foreach( $pkg in $sub.pkgconfig )
        $PKG_CONFIG $pkg.getPkgConfigParam()
        if [ $? -ne 0 ] ; then
            break
        fi
        TEMP_CFLAGS="$TEMP_CFLAGS `$PKG_CONFIG --cflags $pkg.getPkgConfigParam()`"
        TEMP_LDFLAGS="$TEMP_LDFLAGS `$PKG_CONFIG --libs $pkg.getPkgConfigParam()`"
        #end
        #foreach( $flags in $sub.flags )
        #if( $flags.exec )
        $flags.value > /dev/null
        if [ $? -eq 0 ]; then
            TEMP_$flags.varName="$TEMP_$flags.varName `$flags.value`"
        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 )
# general dependencies
TEMP_CFLAGS=
TEMP_LDFLAGS=
#foreach( $dependency in $dependencies )
while true
do
    #if( $dependency.platform )
    if isnotplatform "${dependency.platform}"; then
        break
    fi
    #end
    #foreach( $not in $dependency.getNotList() )
    if isplatform "${not}"; 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.getPkgConfigParam()... "
        $PKG_CONFIG $pkg.getPkgConfigParam()
        if [ $? -ne 0 ]; then
            echo no
            ERROR=1
            break
        fi
        echo yes
        TEMP_CFLAGS="$TEMP_CFLAGS `$PKG_CONFIG --cflags $pkg.getPkgConfigParam()`"
        TEMP_LDFLAGS="$TEMP_LDFLAGS `$PKG_CONFIG --libs $pkg.getPkgConfigParam()`"
        #end

        #foreach( $flags in $dependency.flags )
        #if( $flags.exec )
        $flags.value > /dev/null
        if [ $? -ne 0 ]; then
            TEMP_$flags.varName="$TEMP_$flags.varName `$flags.value`"
        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 general 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_CXXFLAGS}" ]; then
    echo "CXXFLAGS += $TEMP_CXXFLAGS" >> $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 )
    dependency_$dep
    if [ $? -ne 0 ]; 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
#
TEMP_CFLAGS=
TEMP_CXXFLAGS=
TEMP_LDFLAGS=

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

#foreach( $dependency in $target.dependencies )
dependency_$dependency
if [ $? -ne 0 ]; 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
    dependency_$dependency
    if [ $? -ne 0 ]; then
        # "auto" features can fail and are just disabled in this case
        if [ "$${feature.getVarName()}" != "auto" ]; then
            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=0
    while true
    do
        #foreach( $optdef in $opt.defaults )
        #if( $optdef.platform )
        if isplatform "$optdef.platform"; then
        #end
        $optdef.func
        if [ $? -eq 0 ]; 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
        $optval.func
        if [ $? -ne 0 ]; 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 [ -n "${TEMP_CXXFLAGS}" ]; then
    echo "${target.getCXXFlags()} += $TEMP_CXXFLAGS" >> "$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"
    rm -Rf "$TEMP_DIR"
    exit 1
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
echo
cat "$TEMP_DIR/config.mk" "$TEMP_DIR/make.mk" > config.mk
rm -Rf "$TEMP_DIR"

mercurial