#!/bin/sh

# mkfrags -- create Makefile fragments for Squeak sources
# 
# @copyright@

# Author: Ian.Piumarta@INRIA.Fr
# 
# Last edited: 2000-11-08 14:25:32 by piumarta on emilia.rd.wdi.disney.com

# We assume the following source structure:
# 
#	src/generated/*.[ch]	= portable, generated VM files + internal plugins
#	src/generated/Foo/	= portable, generated external plugin "Foo"
#	src/Bar/		= portable, hand-written external plugin "Bar"
#	src/unix/*.[ch]		= unix-specific VM support files
#	src/unix/Baz/		= unix-specific external plugin "Baz"

# definitions written to the head fragment:
# 
#   PLUGINS      = list of external plugin names
#   UNX          = list of unix .o files
#   GEN          = list of generated .o files (excluding interp.o)
# 
# rules written to the tail fragment:
# 
#   PLUGIN.la :          list of .lo for PLUGIN
#	$(LINK) -module -rpath $(sqlibdir) -o $@ $^
# 
#   plgfile.lo :	{gen,unx}dir/plgfile.c
#	$(LTCOMPILE) -c -o $@ $<
# 
#   unxfile.o : 	 unxdir/unxfile.c
#   genfile.o : 	 gendir/genfile.c
#	$(COMPILE) -c -o $@ $<
# 
# NOTE: this script should work everywhere, as should the Makefile
#       fragments that it generates.

set -e
#set -vx

if test $# -ne 7; then
  echo "usage: $0 topdir head-frag tail-frag ac_n ac_c plugin-lib-prefix jit" >&2
  exit 1
fi

topdir=$1
mh_frag=$2
mt_frag=$3
ac_n=$4
ac_c=$5
lib=$6
jit=$7

srcdir=$topdir/src
gendir=$srcdir/generated
unxdir=$srcdir/unix
utldir=$unxdir/util

module="-module"

nlsp='tr \012\015 \040\040'
spnl='tr \040\040 \012\012'

report()   { while test $# -gt 0; do echo "$1 = {`eval echo '$'$1`}" >&2; shift; done; }
reportin() { for i in `eval echo '$'$2`; do report $1${i}; done; }
append()   { var=$1; shift; val="`eval echo '$'$var` $*"; eval "${var}=\"${val}\""; }
remove()   { eval $1='"'`eval echo '$'$1 | $spnl | fgrep -v $2 | $nlsp`'"'; }
progress() { echo ${ac_n} ".${ac_c}"; }

#report() { :; }
#reportin() { :; }

GEN=`echo $gendir/*.c | $spnl | fgrep -v interp.c | sed 's,.*/,,g;s,\.c$,,g' | $nlsp`
UNX=`echo $unxdir/*.c | $spnl | sed 's,.*/,,g;s,\.c$,,g' | $nlsp`

### tail fragment #1: blanket rules for .c.o and .c.lo

echo  > $mt_frag '### generated files'

for gen in ${GEN}; do
  echo
  echo "${gen}.o : ${gendir}/${gen}.c"
  echo "	\$(COMPILE) -c -o ${gen}.o ${gendir}/${gen}.c"
done >> $mt_frag

echo >> $mt_frag
echo >> $mt_frag '### unix files'

progress

for unx in ${UNX}; do
  echo
  echo "${unx}.o : ${unxdir}/${unx}.c"
  echo "	\$(COMPILE) -c -o ${unx}.o ${unxdir}/${unx}.c"
done >> $mt_frag

echo >> $mt_frag
echo >> $mt_frag '### plugins'

progress

PLUGINS=""

for dir in `find ${srcdir} -type d -name '[A-Z]*' -print`; do
  plg=`basename ${dir}`
  if test "$plg" != "SqueakCompiler" -o "$jit" = "yes"; then
    append PLUGINS ${lib}${plg}
    echo >> $mt_frag
    echo >> $mt_frag "# ${lib}${plg}"
    if test -f ${dir}/Makefile; then
      # the plugin knows how to compile itself
      test ! -d ${plg} && mkdir ${plg}
      ( cd ${plg}; test ! -f Makefile && ln -s ../${dir}/Makefile .; )
	echo >> $mt_frag
	echo >> $mt_frag "${plg}.la : .force"
	echo >> $mt_frag "	( cd ${plg}; \$(MAKE) sqlibdir=\"\$(sqlibdir)\"; )"
    else
      # compile the plugin in the main build dir
      LOS=""
      for dep in `echo ${dir}/*.c | $spnl | sed 's,.*/,,g;s,\.c$,,g'`; do
	append LOS ${dep}.lo
	echo >> $mt_frag
	echo >> $mt_frag "${dep}.lo : ${dir}/${dep}.c"
	echo >> $mt_frag "	\$(LTCOMPILE) -I${dir} -c -o ${dep}.lo ${dir}/${dep}.c"
      done
      echo >> $mt_frag
      echo >> $mt_frag "$lib$plg.la : ${LOS}"
      echo >> $mt_frag "	\$(LINK) $module -rpath \$(sqlibdir) -o $lib$plg.la ${LOS}"
    fi
  fi
done

progress

### head fragment

echo  > $mh_frag       "PLUGINS=	" $PLUGINS
echo >> $mh_frag       "UNX=		" $UNX
echo >> $mh_frag       "GEN=		" $GEN

echo
