|
| 1 | +#!/bin/sh |
| 2 | +#################################################################### |
| 3 | +# |
| 4 | +# Copyright (C) 2006 Andrew Tomazos <andrew@tomazos.com> |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or |
| 7 | +# modify it under the terms of the GNU General Public License |
| 8 | +# as published by the Free Software Foundation; either version 2 |
| 9 | +# of the License, or (at your option) any later version. |
| 10 | +# |
| 11 | +#################################################################### |
| 12 | +# |
| 13 | +# jjrun 1.0d1 |
| 14 | +# |
| 15 | +# - Build and execute a JavaCC .jj file in one command |
| 16 | +# |
| 17 | +# - Requires javacc, java, make and sh |
| 18 | +# |
| 19 | +# - TODO: If you use something other than SimpleCharStream you |
| 20 | +# will need to modify the OBJECTS variable below |
| 21 | +# |
| 22 | +# - TODO: Only tested on GNU/Linux |
| 23 | +# |
| 24 | +#################################################################### |
| 25 | + |
| 26 | +# $PROGFILE: This programs file path |
| 27 | +PROGFILE=$0 |
| 28 | + |
| 29 | +# $VERSION: This programs version |
| 30 | +VERSION=1.0d1 |
| 31 | + |
| 32 | +# $PROGNAME: This programs name |
| 33 | +PROGNAME=`basename $0` |
| 34 | + |
| 35 | +# If project not specified output usage and exit |
| 36 | +if [ ! $1 ] |
| 37 | +then |
| 38 | + echo "USAGE: $PROGNAME <jjfile>" |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +# $PROJECT: The jj file to run |
| 43 | +PROJECT=`basename $1 .jj` |
| 44 | + |
| 45 | +# $MAKEFILE: An automatically generated makefile |
| 46 | +MAKEFILE=$PROJECT.mk |
| 47 | + |
| 48 | +# $FORCE: forces the regeneration of the makefile |
| 49 | +FORCE=$2 |
| 50 | + |
| 51 | + |
| 52 | +# If the specified project does not exist than exit |
| 53 | +if [ ! -f $PROJECT.jj ] |
| 54 | +then |
| 55 | + echo ERROR: $PROGNAME: File not found $PROJECT.jj |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +# If forcing makefile regeneration than delete the makefile |
| 60 | +if [ $FORCE ] |
| 61 | +then |
| 62 | + rm $MAKEFILE |
| 63 | +fi |
| 64 | + |
| 65 | +# If the makefile does not exist than generate it |
| 66 | +if [ ! -f $MAKEFILE ] |
| 67 | +then |
| 68 | + echo $PROGNAME: Generating $MAKEFILE... |
| 69 | + |
| 70 | +#### START jjrun_makefile_header_here_doc |
| 71 | +cat > $MAKEFILE <<jjrun_makefile_header_here_doc |
| 72 | +# Automatically generated makefile for $PROJECT by $PROGNAME $VERSION |
| 73 | +
|
| 74 | +# Copyright (C) 2006 Andrew Tomazos <andrew@tomazos.com> |
| 75 | +# |
| 76 | +# This program is free software; you can redistribute it and/or |
| 77 | +# modify it under the terms of the GNU General Public License |
| 78 | +# as published by the Free Software Foundation; either version 2 |
| 79 | +# of the License, or (at your option) any later version. |
| 80 | +
|
| 81 | +# ${PROJECT}: Name of the project |
| 82 | +PROJECT=$PROJECT |
| 83 | +
|
| 84 | +# ${MAKEFILE}: Filename of this makefile |
| 85 | +MAKEFILE=$MAKEFILE |
| 86 | +
|
| 87 | +# ${PROGFILE}: Filepath of $PROGNAME |
| 88 | +PROGFILE=$PROGFILE |
| 89 | +
|
| 90 | +jjrun_makefile_header_here_doc |
| 91 | +#### END jjrun_makefile_header_here_doc |
| 92 | + |
| 93 | +#### START jjrun_makefile_main_here_doc |
| 94 | +cat >> $MAKEFILE <<'jjrun_makefile_main_here_doc' |
| 95 | +
|
| 96 | +# ${JJ}: The original .jj file |
| 97 | +JJ=${PROJECT}.jj |
| 98 | +
|
| 99 | +# ${AUTOGENS}: The automatically generated classed of javacc |
| 100 | +AUTOGENS= \ |
| 101 | + ${PROJECT} \ |
| 102 | + ${PROJECT}Constants \ |
| 103 | + ${PROJECT}TokenManager |
| 104 | +
|
| 105 | +# ${BOILERS}: The boilerplate classes generated by javacc |
| 106 | +BOILERS= \ |
| 107 | + SimpleCharStream \ |
| 108 | + ParseException \ |
| 109 | + Token \ |
| 110 | + TokenMgrError |
| 111 | +
|
| 112 | +# ${OBJECTS}: The names of the classes output by javacc |
| 113 | +OBJECTS=${AUTOGENS} ${BOILERS} |
| 114 | +
|
| 115 | +# ${BOILERSOURCES}: The boilerplater sources files output by javacc |
| 116 | +BOILERSOURCES=$(addsuffix .java, ${BOILERS}) |
| 117 | +
|
| 118 | +# ${SOURCES}: The source files output by javacc |
| 119 | +SOURCES=$(addsuffix .java, ${OBJECTS}) |
| 120 | +
|
| 121 | +# ${CLASSES}: The class files compiled with javac from the output of javacc |
| 122 | +CLASSES=$(addsuffix .class, ${OBJECTS}) |
| 123 | +
|
| 124 | +# ${MANIFEST}: filename of the manifest for the final jar file |
| 125 | +MANIFEST=${PROJECT}.mf |
| 126 | +
|
| 127 | +# ${JAR}: filename of the final jar file |
| 128 | +JAR=${PROJECT}.jar |
| 129 | +
|
| 130 | +# make build rule to make JAR |
| 131 | +build: ${JAR} |
| 132 | +
|
| 133 | +# make ${JAR}: build the jar file |
| 134 | +${JAR}: ${SOURCES} ${MANIFEST} |
| 135 | + @echo ${MAKEFILE}: Building ${JAR}... |
| 136 | + javac ${SOURCES} |
| 137 | + jar cvfm ${JAR} ${MANIFEST} ${CLASSES} |
| 138 | + rm ${CLASSES} |
| 139 | +
|
| 140 | +# make clean: remove all generated jar files |
| 141 | +clean: |
| 142 | + @echo ${MAKEFILE}: Cleaning ${PROJECT}... |
| 143 | + @-rm ${JAR} ${SOURCES} ${MANIFEST} |
| 144 | +
|
| 145 | +# make ${SOURCES}: compile the jj file into java sources |
| 146 | +${SOURCES}: ${JJ} |
| 147 | + @echo ${MAKEFILE}: Compiling ${JJ}... |
| 148 | + @javacc ${JJ} |
| 149 | + touch -c ${BOILERSOURCES} |
| 150 | +
|
| 151 | +# make ${MANIFEST}: rule to make |
| 152 | +${MANIFEST}: |
| 153 | + @echo ${MAKEFILE}: Creating manifest... |
| 154 | + @echo Manifest-Version: 1.0 > ${MANIFEST} |
| 155 | + @echo Main-Class: ${PROJECT} >> ${MANIFEST} |
| 156 | +
|
| 157 | +${MAKEFILE}: ${PROGFILE} |
| 158 | + @echo ${MAKEFILE}: Regenerating ${MAKEFILE} |
| 159 | + @${PROGFILE} ${PROJECT} FORCE |
| 160 | +
|
| 161 | +jjrun_makefile_main_here_doc |
| 162 | +# END jjrun_makefile_main_here_doc |
| 163 | + |
| 164 | +fi |
| 165 | + |
| 166 | +# If this was a forced regeneration than stop here |
| 167 | +if [ $FORCE ] |
| 168 | +then |
| 169 | + exit 0 |
| 170 | +fi |
| 171 | + |
| 172 | +# Execute the makefile to rebuild the jar file if necessary |
| 173 | +echo $PROGNAME: Executing $MAKEFILE... |
| 174 | +make -f $MAKEFILE |
| 175 | + |
| 176 | +# Execute the jar file |
| 177 | +echo $PROGNAME: Executing $PROJECT.jar... |
| 178 | +java -jar $PROJECT.jar |
| 179 | + |
0 commit comments