#!/bin/sh # Automagically generate make rules to compile jar files # specified in the Makefile.aj # # (C) Tapsell-Ferrier Limited 2002 # # This results in a file called: # Makefile # unless there is an argument specified in which case # it results in a file specified in the argument. # # Therefore to produce a Makefile.in you could do this: # # automakejar Makefile.in # TMP=${TEMP:-/tmp/} # Create the generic editor script. SEDSCRIPT=$TMP/automakejar.$$ cat > ${SEDSCRIPT} <<\EOF #-n # A first stage sed script to produce a Makefile from a template. # (C) Tapsell-Ferrier Limited 2002 # # This script would be much easier if GNU Make's target specific variables # cascaded into sub-targets. But they don't as of 3.79.1. /^__JARFILENAME__.jar:.*$/,/^$/{ # Store the dummy target. /^__JARFILENAME__.jar:.*/ { h d } # Make sure the variable definitions appear at the start of lines. s/^ sourcedir=/sourcedir=/ s/^ sourcefiles=/sourcefiles=/ s/^ classpath=/classpath=/ s/^ classesdest=/classesdest=/ s/^ otherfiles=/otherfiles=/ s/^ manifest=/manifest=/ # Mangle the names of the target variables s/sourcedir/__JARFILENAME___jar_sourcedir/ s/sourcefiles/__JARFILENAME___jar_sourcefiles/ s/classpath/__JARFILENAME___jar_classpath/ s/classesdest/__JARFILENAME___jar_classesdest/ s/otherfiles/__JARFILENAME___jar_otherfiles/ s/manifest/__JARFILENAME___jar_manifest/ # Ensure $variables are referenced properly s/\$\([A-Za-z_][A-Za-z_]*\)/$(\1)/g # Change quoted sh syntax to make syntax s/`\(.*\)`/$(shell \1)/ # At the end of the automakejar decleration put the real Make decl. /^$/ { i\ __JARFILENAME___jar_debugclasses=$(__JARFILENAME___jar_sourcefiles:.java=.class)\ __JARFILENAME___jar_classfiles=$(__JARFILENAME___jar_debugclasses:$(__JARFILENAME___jar_sourcedir)%=$(__JARFILENAME___jar_classesdest)%)\ # This ensures that the target that the user put in is inserted. { g p i\ __JARFILENAME__.jar: __JARFILENAME__-init $(__JARFILENAME___jar_classfiles) __JARFILENAME__-compilation $(__JARFILENAME___jar_otherfiles) $(__JARFILENAME___jar_manifest)\ $(JAR) cf$(if $(__JARFILENAME___jar_manifest),m) __JARFILENAME__.jar $(__JARFILENAME___jar_manifest) $(__JARFILENAME___jar_otherfiles) -C $(__JARFILENAME___jar_classesdest) .\ \ .PHONY: __JARFILENAME__-init __JARFILENAME__-compilation\ \ __JARFILENAME__-init:\ echo > filelist\ \ __JARFILENAME__-compilation: $(__JARFILENAME___jar_classesdest)\ $(if $(shell cat filelist),$(JAVAC) $(JAVAC_OPTS) -d $(__JARFILENAME___jar_classesdest) -classpath $(call PATHMK,$(__JARFILENAME___jar_classesdest) $(call PATHMK,$(__JARFILENAME___jar_classpath))) @filelist)\ \ $(__JARFILENAME___jar_classesdest):\ mkdir address@hidden \ $(__JARFILENAME___jar_classesdest)/%.class: $(__JARFILENAME___jar_sourcedir)/%.java\ @echo $? >> filelist\ \ echo___JARFILENAME___jar:\ @echo sourcedir = $(__JARFILENAME___jar_sourcedir)\ @echo sourcefiles = $(__JARFILENAME___jar_sourcefiles)\ @echo classpath = $(call PATHMK,$(__JARFILENAME___jar_classpath))\ @echo classesdest = $(__JARFILENAME___jar_classesdest)\ @echo classfiles = $(__JARFILENAME___jar_classfiles)\ \ ifeq (${OS},Windows_NT)\ PATHMK = $(subst $(space),;,$(1))\ else\ PATHMK = $(subst $(space),:,$(1))\ endif } # Ensure we branch to end of script once template is done. b end } } # Output all other lines as they are. p :end EOF # The file to produce. Makefile=${1:-Makefile} # The name of the template file. MakeTemplate=`dirname $0`/Makefile.aj echo Making $Makefile from $MakeTemplate # The Makefile to be mangled, changes every iteration. MakeSource=${MakeTemplate} # Make a bespoke sed script and then edit the source Makefile. for something in `sed -n 's/^\([A-Za-z0-9_-]*\)\.jar:.*$/\1/p' ${MakeTemplate}` do MakeOutput=${Makefile}.${RANDOM} echo creating the target: $something EDITSCRIPT=${SEDSCRIPT}.edit.$$ sed -n "s/__JARFILENAME__/${something}/g ; p" ${SEDSCRIPT} > ${EDITSCRIPT} sed -n -f ${EDITSCRIPT} ${MakeSource} > ${MakeOutput} if [ "${MakeSource}" != "${MakeTemplate}" ] ; then rm ${MakeSource} fi MakeSource=${MakeOutput} rm $EDITSCRIPT done mv ${MakeOutput} ${Makefile} rm $SEDSCRIPT # end.