#!/bin/sh
# $Id: genLibTargetf77,v 1.8 2001/10/26 22:13:04 m-hirano Exp $
# $RWC_Release: Omni-1.6 $
# $RWC_Copyright:
#  Omni Compiler Software Version 1.5-1.6
#  Copyright (C) 2002 PC Cluster Consortium
#  
#  This software is free software; you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License version
#  2.1 published by the Free Software Foundation.
#  
#  Omni Compiler Software Version 1.0-1.4
#  Copyright (C) 1999, 2000, 2001.
#   Tsukuba Research Center, Real World Computing Partnership, Japan.
#  
#  Please check the Copyright and License information in the files named
#  COPYRIGHT and LICENSE under the top  directory of the Omni Compiler
#  Software release kit.
#  
#  
#  $

safeBasename () {
    last=`echo $1 | awk -F'.' '{ print $NF }'`
    basename $1 .${last}
}

getArgVal () {
    echo $1 | awk -F'=' '{ print $2 }'
}

debugPrint () {
    echo debug: \'$1\' >&2
}

checkNull () {
    if test -z "$1"; then
	return 0
    else
	return 1
    fi
}

checkDefLock () {
    for i in `echo ${lockList}`
    do
	if test "x$i" = "x${defLock}"; then
	    unset i
	    return 1
	fi
    done
    echo "\"${defLock}\" is not in \"${lockList}\". exit" >&2
    return 0
}

checkDefThread () {
    for i in `echo ${threadList}`
    do
	if test "x$i" = "x${defThread}"; then
	    unset i
	    return 1
	fi
    done
    echo "\"${defThread}\" is not in \"${threadList}\". exit" >&2
    return 0
}


genSuffix () {
    lck=$1
    thd=$2
    echo "._${lck}_${thd}_o"
}


getSaneNameOther () {
    type=""
    case "$1" in
	shmem|scash)
	    type=sm;;
	st)
	    type=st;;
	*)
	    type=unknown;;
    esac
    echo $type
}
    
genSuffixOther () {
    type=`getSaneNameOther $1`
    echo "._${type}_o"
}


genObjMacro () {
    lck=$1
    thd=$2
    objs=""
    for k in `echo $srcs`
    do
	bNm=`safeBasename $k`
	sfx=`genSuffix ${lck} ${thd}`
	objs="${objs} ${bNm}${sfx}"
    done
    echo "${lck}_${thd}_OBJS=${objs}"
    echo
    unset k
}


genObjMacroOther () {
    type=$1
    objs=""
    for k in `echo $srcs`
    do
	bNm=`safeBasename $k`
	sfx=`genSuffixOther ${type}`
	objs="${objs} ${bNm}${sfx}"
    done
    echo "${type}_OBJS=${objs}"
    echo
    unset k
}


getLockDefine () {
    case "$1" in
	mutex*)
	    echo "";;
	spin*)
	    echo "-DUSE_SPIN_LOCK";;
	*)
	    echo "";;
    esac
}


getThreadDefine () {
    case "$1" in
	solaris*)
	    echo "-DUSE_SOL_THREAD";;
	sproc*)
	    echo "-DUSE_SPROC";;
	pthread*)
	    echo "-DUSE_PTHREAD";;
	*)
	    echo "-DUSE_PTHREAD";;
    esac
}


getOtherDefine () {
    case "$1" in
	scash)
	    echo "-I${scoreDir}/include -DUSE_SCASH";;
	shmem)
	    echo "-DUSE_UNIX_SHMEM";;
	st)
	    echo "-DUSE_STACKTHREADS";;
	*)
	    true;;
    esac
}


gen_C_O_Rules () {
    sfx=`genSuffix $1 $2`
    lckDef=`getLockDefine $1`
    thdDef=`getThreadDefine $2`

    for l in .c .s
    do
	echo "${l}${sfx}:"
	echo "	@rm -f \$@ \$*.tmp.o"
	echo "	\$(CC) \$(CC_OPT_SWITCH) \$(CPPFLAGS) ${MTSafeDefs} ${lckDef} ${thdDef} -DMT_SAFE_IO -DOMPF77_MTIO -c \$< -o \$*.tmp.o"
	echo "	mv \$*.tmp.o \$@"
	echo
    done
}


gen_C_O_RulesOther () {
    sfx=`genSuffixOther $1`
    def=`getOtherDefine $1`

    if test "x${1}" = "xst"; then
	for l in .c .s
	do
	    echo "${l}${sfx}:"
	    echo "	@rm -f \$@ \$*.tmp.o"
	    echo "	\$(STGCC) \$(CC_OPT_SWITCH) \$(CPPFLAGS) ${MTSafeDefs} ${def} -DMT_SAFE_IO -DOMPF77_MTIO -c \$< -o \$*.tmp.o"
	    echo "	mv \$*.tmp.o \$@"
	    echo
	done
    else
	for l in .c .s
	do
	    echo "${l}${sfx}:"
	    echo "	@rm -f \$@ \$*.tmp.o"
	    echo "	\$(CC) \$(CC_OPT_SWITCH) \$(CPPFLAGS) ${MTSafeDefs} ${def} -DMT_SAFE_IO -DOMPF77_MTIO -c \$< -o \$*.tmp.o"
	    echo "	mv \$*.tmp.o \$@"
	    echo
	done
    fi
}


genLibRules () {
    lck=$1
    thd=$2
    sfx=`genSuffix ${lck} ${thd}`
    tgt="libf2c_${lck}_${thd}.a"
    dep="${lck}_${thd}_OBJS"

    echo "${tgt}:	\$(${dep}) \$(COM_OBJS)"
    echo "	@rm -f \$@"
    echo "	\$(AR) ${tgt} *${sfx} \$(COM_OBJS)"
    echo "	\$(RANLIB) \$@"
    echo
}


genLibRulesOther () {
    type=$1
    sfx=`genSuffixOther ${type}`
    on=`getSaneNameOther ${type}`
    tgt="libf2c_${on}.a"
    dep="${type}_OBJS"

    echo "${tgt}:	\$(${dep}) \$(COM_OBJS)"
    echo "	@rm -f \$@"
    echo "	\$(AR) ${tgt} *${sfx} \$(COM_OBJS)"
    echo "	\$(RANLIB) \$@"
    echo
}


genTargetMacro () {
    target=""
    suffixs=""
    for i in `echo ${lockList}`
    do
	for j in `echo ${threadList}`
	do
	    if test "X${ignoreCombo}" != "X${j} ${i}"; then
		genObjMacro ${i} ${j}
		tgt="libf2c_${i}_${j}.a"
		target="${target} ${tgt}"
		sfx=`genSuffix ${i} ${j}`
		suffixs="${suffixs} ${sfx}"
		gen_C_O_Rules ${i} ${j}
		genLibRules ${i} ${j}
	    fi
	done
    done

    if test $useScash -eq 1; then
	genObjMacroOther scash
	on=`getSaneNameOther scash`
	tgt=libf2c_${on}.a
	target="${target} ${tgt}"
	sfx=`genSuffixOther scash`
	suffixs="${suffixs} ${sfx}"
	gen_C_O_RulesOther scash
	genLibRulesOther scash
    fi
    if test $useShmem -eq 1; then
	genObjMacroOther shmem
	on=`getSaneNameOther shmem`
	tgt=libf2c_${on}.a
	target="${target} ${tgt}"
	sfx=`genSuffixOther shmem`
	suffixs="${suffixs} ${sfx}"
	gen_C_O_RulesOther shmem
	genLibRulesOther shmem
    fi
    if test $useStackThread -eq 1; then
	genObjMacroOther st
	on=`getSaneNameOther st`
	tgt=libf2c_${on}.a
	target="${target} ${tgt}"
	sfx=`genSuffixOther st`
	suffixs="${suffixs} ${sfx}"
	gen_C_O_RulesOther st
	genLibRulesOther st
    fi

    echo ".SUFFIXES:	.c .h .o .a .s ${suffixs}"
    echo
    echo "DEFAULT_LIB=libf2c_${defLock}_${defThread}.a"
    echo
    echo "LIB_TARGET=${target}"
    echo "doAllLib:	\$(LIB_TARGET)"
    echo
}


genCppMTSafeDef () {
    defs=""
    case "${os}" in
	irix)
	    defs="-D_SGI_MP_SOURCE -D_SGI_REENTRANT_FUNCTIONS";;
	aix)
	    defs="-D_THREAD_SAFE";;
	solaris|linux|freebsd|darwin)
	    defs="-D_REENTRANT";;
	*)
	    true;;
    esac
    MTSafeDefs=$defs
}


MTSafeDefs=""
lockList=""
threadList=""
defLock=""
defThread=""
ignoreCombo=""
os=""
cpu=""

useShmem=0
useScash=0
useStackThread=0
scoreDir="/opt/score"

while case "$1" in
    --os=*)
	os=`getArgVal "$1"`
	true;;
    --cpu=*)
	cpu=`getArgVal "$1"`
	true;;
    --locks=*)
	lockList=`getArgVal "$1"`
	true;;
    --threads=*)
	threadList=`getArgVal "$1"`
	true;;
    --def_lock=*)
	defLock=`getArgVal "$1"`
	true;;
    --def_thread=*)
	defThread=`getArgVal "$1"`
	true;;
    --ignore_combo=*)
	ignoreCombo=`getArgVal "$1"`
	true;;
    --use-scash)
	useScash=1
	true;;
    --use-shmem)
	useShmem=1
	true;;
    --use-StackThread)
	useStackThread=1
	true;;
    --score_dir=*)
	scoreDir=`getArgVal "$1"`
	true;;
    -*)
	echo "$0: unknown option \"$1\"" >&2
	exit 1;;
    *)
	false;;
    esac
do
    shift
done

if test ${useShmem} -eq 1 -a ${useScash} -eq 1; then
    echo "shmem and scash are exclusive. exit." >&2
    exit 1
fi

checkNull "${lockList}"
if test $? -eq 0; then
    echo "lock list null. exit." >&2
    exit 1
fi

checkNull "${threadList}"
if test $? -eq 0; then
    echo "thread list null. exit." >&2
    exit 1
fi

checkNull "${defLock}"
if test $? -eq 0; then
    echo "default lock null. exit." >&2
    exit 1
fi

checkNull "${defThread}"
if test $? -eq 0; then
    echo "default thread null. exit." >&2
    exit 1
fi

checkDefLock
if test $? -eq 0; then
    exit 1
fi

checkDefThread
if test $? -eq 0; then
    exit 1
fi

genCppMTSafeDef

srcs="$*"

genTargetMacro

exit 0
