#!/bin/sh # $Id$ # @Id. TOP=`pwd` PREFIX="/usr/local" EXEC="" EXPORTS="TOP PREFIX EXEC" if [ ! -e ./configure.script ]; then echo "configure.script is missing, aborted" exit 1 fi for arg in $@; do case "$arg" in --help|-h) echo "$0 configures stuffs for you" echo "" echo "Usage: $0 [options]" exit 0 ;; --prefix=*) PREFIX=`echo $arg | sed "s/--prefix=//g"` ;; --config=*) . `echo $arg | sed "s/--config=//g"` ;; *) echo "Invalid option: $arg" exit 1 ;; esac done echo "$0 is generating config.mk/config.h/Makefile" DEBUG () { if ${DODEBUG:-false}; then echo "DEBUG: $1" > /dev/stderr fi } PARSE () { i=1 OPTIONAL=false LABEL="" FAILLABEL="" RUNIF=true _JOB="" OPLEN=`echo -n "$OP" | wc -m` DEBUG "--- BEGIN Parsing operator: $OP ---" while [ $i -le $OPLEN ]; do if [ $i -gt 1 ]; then _CHAR=`echo $OP | cut -c $i` _EXEC=false # Arguments required if [ "x$_JOB" = "x" ]; then : elif [ "$_JOB" = ":" ]; then DEBUG "Will define label: $_CHAR" LABEL="$_CHAR" _EXEC=true elif [ "$_JOB" = ";" ]; then DEBUG "Will define label if failed: $_CHAR" FAILLABEL="$_CHAR" _EXEC=true elif [ "$_JOB" = "@" ]; then DEBUG "Will run if label $_CHAR is defined" RUNIF=`GET_LABEL $_CHAR` if $RUNIF; then DEBUG "Running, label $_CHAR is defined" else DEBUG "Not running, label $_CHAR is not defined" fi _EXEC=true fi if [ "x$_JOB" = "x" ]; then _JOB="$_CHAR" DEBUG "Job token: $_JOB" fi # No arguments required if [ "$_JOB" = "?" ]; then DEBUG "Optional job specified" OPTIONAL=true _JOB="" fi if $_EXEC; then _JOB="" fi fi i=`expr $i + 1` done DEBUG "--- END Parsing operator: $OP ---" } SET_LABEL () { eval LABEL_$1=$2 } GET_LABEL () { RESULT=`eval echo \\\$LABEL_$1` if [ "x$RESULT" = "x" ]; then echo "false" else echo $RESULT fi } TEST_C () { OLDPWD=`pwd` cd /tmp DEBUG "--- BEGIN C compiler check: $1 ---" echo "int main(int argc, char** argv){return 0;}" >> $$.c $1 -c -o $$.o $$.c >/dev/null 2>&1 RET=$? if [ "$RET" = "0" ]; then rm -f $$.o DEBUG "Seems to compile fine" else DEBUG "Compilation failure" fi rm -f $$.c cd $OLDPWD DEBUG "--- END C compiler check: $1 ---" return $RET } COMPILE_C () { OLDPWD=`pwd` cd /tmp DEBUG "--- BEGIN C program check ---" printf "$1" > $$.c $CC $2 -c -o $$.o $$.c >/dev/null 2>&1 RET=$? if [ "$RET" = "0" ]; then rm -f $$.o DEBUG "Seems to compile fine" else DEBUG "Compilation failure" fi rm -f $$.c cd $OLDPWD DEBUG "--- END C program check ---" return $RET } PROBE () { DEBUG "--- BEGIN Probing ---" for i in $@; do OLDIFS="$IFS" IFS=":" for p in $PATH; do if [ -f $p/$i ]; then DEBUG "$p/$i exists" IFS="$OLDIFS" echo $p/$i DEBUG "--- END Probing ---" return 0 else DEBUG "$p/$i does not exist" fi done IFS="$OLDIFS" done DEBUG "--- END Probing ---" return 1 } PROBE_C () { DEBUG "--- BEGIN Probing C ---" for i in $@; do OLDIFS="$IFS" IFS=":" for p in $PATH; do if [ -f $p/$i ]; then DEBUG "$p/$i exists" IFS="$OLDIFS" if TEST_C $p/$i; then IFS=":" echo $p/$i DEBUG "--- END Probing ---" return 0 fi IFS=":" else DEBUG "$p/$i does not exist" fi done IFS="$OLDIFS" done DEBUG "--- END Probing C ---" return 1 } AFTER () { if [ ! "x$LABEL" = "x" ]; then if [ "$1" = "0" ]; then SET_LABEL $LABEL true else SET_LABEL $LABEL false fi fi if [ ! "x$FAILLABEL" = "x" ]; then if [ "$1" = "0" ]; then SET_LABEL $FAILLABEL false else SET_LABEL $FAILLABEL true fi fi } echo "# This file was auto-generated" > config.mk echo "/* This file was auto-generated */" > config.h while read a; do OP=`echo $a | cut -d" " -f1` LEN=`echo -n $OP | wc -m` LEN=`expr $LEN + 2` ARG=`echo $a | cut -c$LEN-` DEBUG "--- BEGIN Line $a ---" DEBUG "Operator: $OP" DEBUG "Argument: $ARG" case "$OP" in "#") ;; f*) PARSE if $RUNIF; then if [ "$ARG" = "ansi" ]; then echo -n "Checking the flag for ANSI... " COMPILE_C "int main(int argc, char** argv){}" "-std=c89" if [ "$RET" = "0" ]; then ANSI="-std=c89" else COMPILE_C "int main(int argc, char** argv){}" "-ansi" if [ "$RET" = "0" ]; then ANSI="-ansi" fi fi if [ "x$ANSI" = "x" ]; then echo "(none?)" if $OPTIONAL; then : else echo "Cannot find a flag for C89!" > /dev/stderr exit 1 fi else echo "$ANSI" fi else echo "Not supported" > /dev/stderr exit 1 fi fi ;; s*) PARSE if $RUNIF; then SYMBOL=`echo $ARG | cut -d"@" -f1` HEADER=`echo $ARG | cut -d"@" -f2` echo -n "Checking if $SYMBOL exists in $HEADER... " COMPILE_C "#include <$HEADER>\nint main(int argc, char** argv){(void)$SYMBOL;return 0;}" RET=$? if [ "$RET" = "0" ]; then echo "yes" else echo "no" if $OPTIONAL; then : else echo "Symbol not found!" > /dev/stderr exit 1 fi fi AFTER $RET fi ;; d*) PARSE if $RUNIF; then echo "#define $ARG 1" >> config.h fi ;; h*) PARSE if $RUNIF; then echo -n "Checking if $ARG exists... " COMPILE_C "#include <$ARG>" RET=$? if [ "$RET" = "0" ]; then echo "yes" else echo "no" if $OPTIONAL; then : else echo "Header not found!" > /dev/stderr exit 1 fi fi AFTER $RET fi ;; p*) PARSE if $RUNIF; then if [ "x$ARG" = "xc" ]; then echo -n "Probing C compiler... " if [ "x$CC" = "x" ]; then CC=`PROBE_C gcc clang cc` fi if [ "x$CC" = "x" ]; then echo "not found" if $OPTIONAL; then : else echo "C compiler not found!" > /dev/stderr exit 1 fi AFTER 1 else echo "$CC" AFTER 0 fi EXPORTS="$EXPORTS CC" elif [ "x$ARG" = "xar" ]; then echo -n "Probing archiver... " if [ "x$AR" = "x" ]; then AR=`PROBE gar ar` fi if [ "x$AR" = "x" ]; then echo "not found" if $OPTIONAL; then : else echo "Archiver not found!" > /dev/stderr exit 1 fi AFTER 1 else echo "$AR" AFTER 0 fi EXPORTS="$EXPORTS AR" elif [ "x$ARG" = "xp" ]; then echo -n "Probing Pascal compiler... " if [ "x$PC" = "x" ]; then PC=`PROBE fpc` fi if [ "x$PC" = "x" ]; then echo "not found" if $OPTIONAL; then : else echo "Pascal compiler not found!" > /dev/stderr exit 1 fi AFTER 1 else echo "$PC" AFTER 0 fi EXPORTS="$EXPORTS PC PCFLAGS" elif [ "x$ARG" = "xr" ]; then echo -n "Probing ranlib... " if [ "x$RANLIB" = "x" ]; then RANLIB=`PROBE granlib ranlib` fi if [ "x$RANLIB" = "x" ]; then echo "not found" if $OPTIONAL; then : else echo "Ranlib not found!" > /dev/stderr exit 1 fi AFTER 1 else echo "$RANLIB" AFTER 0 fi EXPORTS="$EXPORTS RANLIB" else echo "Not supported" > /dev/stderr exit 1 fi fi ;; m*) PARSE if $RUNIF; then echo "$ARG" fi AFTER 0 ;; o*) PARSE if $RUNIF; then echo "Creating $ARG" if [ -f $ARG.in ]; then cp $ARG.in /tmp/$$.1 for i in $EXPORTS; do eval "data=\$$i" data="`echo "$data" | sed -E "s/%/\\\\\%/g"`" sed "s%@$i@%$data%g" /tmp/$$.1 > /tmp/$$.2 mv /tmp/$$.2 /tmp/$$.1 done mv /tmp/$$.1 $ARG AFTER 0 else if $OPTIONAL; then AFTER 1 else echo "Cannot find $ARG.in!" > /dev/stderr exit 1 fi fi fi ;; r*) PARSE if $RUNIF; then eval $ARG AFTER $? fi ;; esac DEBUG "--- END Line $a ---" done < configure.script for i in $EXPORTS; do eval "data=\$$i" echo "$i=$data" >> config.mk done