# -*-shell-script-*- # # (c) Sean Seefried 2005 # # A library of useful functions for configure scripts # #-------------------------------------------------------------------- # Function "findprogram": find a program in the path #-------------------------------------------------------------------- findprogram () { if test -f $1; then return 0; fi saveIFS="$IFS" IFS=':' for dir in $PATH; do if test -z "$dir"; then dir=.; fi if test -f $dir/$1; then IFS="$saveIFS" return 0 fi done IFS="$saveIFS" return 1 } #-------------------------------------------------------------------- # Parse command-line arguments #-------------------------------------------------------------------- process_args () { arg="" while : ; do # put optional argument in the $arg variable case "$1" in -*=*) arg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` arg=`eval echo $arg`;; # expand ~ *) arg="";; esac # match on the arguments case "$1" in "") break;; -?|--help) echo "usage:" echo " ./configure [options]" echo "" echo "options: [defaults in brackets]" echo " --help show this information" echo " --with-hc= the haskell compiler [$hc]" echo " --with-hc-pkg= package compiler [$hcpkg]" echo "install options:" echo " --prefix= install directory [$prefix]" exit 1;; -with-hc=*|--with-hc=*) hc="$arg";; -with-hc-pkg=*|--with-hc-pkg=*) hc_pkg="$arg";; esac # process next argument shift done return 0; } echoc() { echo configure: "$@" } #---------------------------------------------------------------------- # Get the version of the haskell compiler #---------------------------------------------------------------------- get_version() { echo "`$hc --version | sed -e 's|[^0-9.]*\([0-9.][0-9.]*\)|\1|'`" } # # Converts a version number of form MAJ.MIN.PATCHLEVEL to an integer # #---------------------------------------------------------------------- # Get the version of the haskell compiler as an integer #---------------------------------------------------------------------- get_version_int() { version=`get_version` MAJ="`echo $version | sed 's|\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.*\([0-9][0-9]*\)*|\1|'`" MIN="`echo $version | sed 's|\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.*\([0-9][0-9]*\)*|\2|'`" PATCH="`echo $version | sed 's|\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.*\([0-9][0-9]*\)*|\3|'`" if test "$PATCH" = "" ; then PATCH=0 fi echo `expr $MAJ \* 10000 + $MIN \* 100 + $PATCH` } #---------------------------------------------------------------------- # Get the OS type #---------------------------------------------------------------------- get_OS() { uname -s } #---------------------------------------------------------------------- # Substitute a value in a file (a la AC_SUBST) # # The first (n-1) arguments given to this command are variables to be # substituted. The last argument is the file in which to # substitute. Call this FILE. The command will look for a file called FILE.in # and produce a substituted file called FILE. # ---------------------------------------------------------------------- subst() { local SED_CMD local n n=$(($#-1)) FILE=${!#} while test $n -gt 0; do echoc Substituting @$1@ in $FILE SED_CMD="$SED_CMD -e 's|@$1@|${!1}|g'" shift n=$((n-1)) done if test -f $FILE.in ; then SED_CMD="$SED_CMD $FILE.in" echo "$SED_CMD" | xargs sed > $FILE fi } #---------------------------------------------------------------------- # Gets a value from .setup-config file # # $1 is the name of the field the value is attached to e.g. buildDir # $2 is the .setup-config file #---------------------------------------------------------------------- get_config_value() { FIELD=$1 SETUP_CONFIG=$2 cat $SETUP_CONFIG | sed "s|.*$FIELD = \"\([^\"]*\)\".*|\1|" } #---------------------------------------------------------------------- # Gets a value from a Cabal file # # $1 is the name of the field the value is attached to e.g. name, # version, executable # # $2 is the .cabal file #---------------------------------------------------------------------- get_cabal_value() { FIELD=$1 CABAL=$2 cat $CABAL | grep -i "$1:" | sed 's|.*:[[:space:]]*\([^[:space:]]*\)|\1|' } #---------------------------------------------------------------------- # Gets the installation directory from a Cabal .setup-config file and # a Cabal .cabal file # $1 is the path to .setup-config and $2 is the path to the .cabal file #---------------------------------------------------------------------- get_install_dir() { SETUP_CONFIG=$1 CABAL=$2 prefix="`cat $SETUP_CONFIG | sed 's|.*prefix = \"\([^\"]*\)\".*|\1|'`" name=`get_cabal_value name $CABAL` ver=`get_cabal_value version $CABAL` echo $prefix/lib/$name-$ver } # # Defaults # prefix=/usr/local hc="ghc" hcpkg="ghc-pkg"