#!/bin/sh

# mkfrags -- create Makefile fragments for Squeak sources
# 
#   Copyright (C) 1996 1997 1998 1999 2000 2001 Ian Piumarta and individual
#      authors/contributors listed elsewhere in this file.
#   All rights reserved.
#   
#   This file is part of Unix Squeak.
# 
#   This file is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#   FITNESS FOR A PARTICULAR PURPOSE.
#   
#   You may use and/or distribute this file ONLY as part of Squeak, under
#   the terms of the Squeak License as described in `LICENSE' in the base of
#   this distribution, subject to the following restrictions:
# 
#   1. The origin of this software must not be misrepresented; you must not
#      claim that you wrote the original software.  If you use this software
#      in a product, an acknowledgment to the original author(s) (and any
#      other contributors mentioned herein) in the product documentation
#      would be appreciated but is not required.
# 
#   2. This notice may not be removed or altered in any source distribution.
# 
#   Using or modifying this file for use in any context other than Squeak
#   changes these copyright conditions.  Read the file `COPYING' in the base
#   of the distribution before proceeding with any such use.
# 
#   You are STRONGLY DISCOURAGED from distributing a modified version of
#   this file under its original name without permission.  If you must
#   change it, rename it first.

# Author: Ian.Piumarta@INRIA.Fr
# 
# Last edited: 2000-11-27 11:19:12 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}
    test ! -d ${plg} && mkdir ${plg}
    echo >> $mt_frag
    echo >> $mt_frag "# ${lib}${plg}"
    if test -f ${dir}/Makefile; then
      # the plugin knows how to compile itself
      ( 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
      # default rules
      LOS=""
      for dep in `ls ${dir}/*.c 2>/dev/null | $spnl | sed 's,.*/,,g;s,\.c$,,g'`; do
	append LOS ${plg}/${dep}.lo
	echo >> $mt_frag
	echo >> $mt_frag "${plg}/${dep}.lo : ${dir}/${dep}.c"
	echo >> $mt_frag "	\$(LTCOMPILE) -I${dir} -c -o ${plg}/${dep}.lo ${dir}/${dep}.c"
      done
      for dep in `ls ${dir}/*.cc 2>/dev/null | $spnl | sed 's,.*/,,g;s,\.cc$,,g'`; do
	append LOS ${plg}/${dep}.lo
	echo >> $mt_frag
	echo >> $mt_frag "${plg}/${dep}.lo : ${dir}/${dep}.cc"
	echo >> $mt_frag "	\$(LTCOMPILEXX) -I${dir} -c -o ${plg}/${dep}.lo ${dir}/${dep}.cc"
      done
      echo >> $mt_frag
      echo >> $mt_frag "$lib$plg.la : ${LOS}"
      echo >> $mt_frag "	\$(LINKXX) $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
