#!/bin/sh

# $TSUKUBA_Release: Omni Compiler Version 0.9.0 $
# $TSUKUBA_Copyright:
#  Copyright (C) 2010-2014 University of Tsukuba, 
#  	      2012-2014  University of Tsukuba and Riken AICS
#  
#  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.
#  
#  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.
#  
#  * The specification of XcalableMP has been designed by the XcalableMP
#    Specification Working Group (http://www.xcalablemp.org/).
#  
#  * The development of this software was partially supported by "Seamless and
#    Highly-productive Parallel Programming Environment for
#    High-performance computing" project funded by Ministry of Education,
#    Culture, Sports, Science and Technology, Japan.
#  $
# $Id$

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"
}


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
}


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
}


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} -c \$< -o \$*.tmp.o"
	echo "	mv \$*.tmp.o \$@"
	echo
    done
}


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

    echo "${tgt}:	\$(${dep})"
    echo "	@rm -f \$@"
    echo "	\$(AR) ${tgt} *${sfx}"
    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="libogr_${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

    echo ".SUFFIXES:	.c .h .o .a .s ${suffixs}"
    echo
    echo "DEFAULT_LIB=libogr_${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|netbsd)
	    defs="-D_REENTRANT";;
	*)
	    true;;
    esac
    MTSafeDefs=$defs
}


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

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;;
    -*)
	echo "$0: unknown option \"$1\"" >&2
	exit 1;;
    *)
	false;;
    esac
do
    shift
done

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
