-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopjcBash
366 lines (305 loc) · 10.3 KB
/
popjcBash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/bin/bash
#############################################################################################
# POP-Java Compiler v1.0
# Author : Clement Valentin
# Creation date : 07/02/2010
# Description : This script is a part of the POP-Java compiler.
# Files given to this script will be filtered and parsed by the POPJParser. An intermediate
# Java code will be generated. This intermediate code is compiled by the standard Java
# Compiler
# -------------------------------------------------------------------------------------------
# IMPORTANT : Please, keep this log up to date
#
# Update log
# Date Author Description
# 07/02/2010 clementval First version of this script
# 07/06/2010 clementval Add POP-C++ additional information xml file generation
# 07/14/2010 clementval Add the possibility to give external JAR or compiled class files
# 07/20/2010 clementval Change the extension of accepted file (.pjava)
#############################################################################################
################################################################
# Define variable to hold the different options of the compiler
################################################################
NOCLEAN=
POPC=
POPC_INFOFILE=
VERBOSE=
FILE=
JAR=
GENERATE=
PARTIAL=
################################################################
# Define the different location of executable and JAR used in
# the compiler
################################################################
if [ -z $POPJAVA_LOCATION ]
then
POPJAVA_LOCATION=/usr/local/popj
fi
POPJAVA_PARSER_PATH=$POPJAVA_LOCATION/JarFile/popjparser.jar
POPJAVA_PARSER=POPJParser
POPJAVA_JAR=$POPJAVA_LOCATION/JarFile/popjava.jar
#Temporary files
GENERATED_FILE="additional-infos.xml"
################################################################
# Define the function to print the program usage
################################################################
usage()
{
cat << EOF
POP-Java Compiler v1.0
This program is used to compile a POP-Java program
Usage: popjc <options> <source files>
OPTIONS:
-h, --help Show this message
-n, --noclean Do not clean the intermediate Java file generated by the POP-Java parser
-p, --popcpp <xml_file> Compile a POP-Java parallel class for POP-C++ usage (Need XML additional informations file)
-j, --jar <filename> Create a JAR archive with the given name (Need the JAR file name)
-v, --verbose Verbose mode
-c, --classpath <files> Include JAR or compiled Java class to the compilation process. Files must be separated by a semicolon ":"
OPTIONS FOR POP-C++ INTEROPERABILITY:
-x, --xmlpopcpp <files> Generate a canvas of the POP-C++ XML additional informations file for the given Java files. This option must be used alone
-g, --generate <pjava> Generate the POP-C++ partial implementation to use the given POP-Java parclass in a POP-C++ application (NOT IMPLEMENTED YET)
EOF
}
#####################################################
# Handle long options (--option)
#####################################################
OPTIONSLINE=
for i in $*
do
if [[ $i == --* ]]
then
if [ "$i" != "--help" ] && [ "$i" != "--noclean" ] && [ "$i" != "--popcpp" ] && [ "$i" != "--generate" ] && [ "$i" != "--jar" ] && [ "$i" != "--verbose" ] && [ "$i" != "--classpath" ] && [ "$i" != "--xmlpopcpp" ]
then
echo "$i is not a valid option"
usage
exit
fi
i=$(echo "$i" | cut -c 2-3)
fi
OPTIONSLINE=$OPTIONSLINE" "$i
done
#####################################################
# Handle the options with getopts
#####################################################
while getopts “hp:vg:j:c:nx” OPTION $OPTIONSLINE
do
case $OPTION in
h)
usage
exit 0
;;
n)
NOCLEAN=1
;;
p)
POPC=1
POPC_INFOFILE=$OPTARG
;;
v)
VERBOSE=1
;;
j)
JAR=$OPTARG
;;
x)
GENERATE=1
;;
g)
PARTIAL=1
echo "This options is not implemented yet. Compilation aborted !"
exit 0
;;
c)
ADD_CLASSPATH=$OPTARG
;;
?)
usage
exit 1
;;
esac
done
shift $(($OPTIND - 1))
########################################################
# Generate XML base file for POP-C++ special generation
########################################################
if [ $GENERATE ] && [ !$NOCLEAN ] && [ !$POPC ] && [ !$JAR ]
then
FILE=$@
echo $FILE
if [ ! -z "$FILE" ]
then
[ $VERBOSE ] && echo "Generating XML caneva file : $GENERATED_FILE"
echo "<popjparser-infos>" > $GENERATED_FILE
for file in $FILE
do
echo "<popc-parclass file=\"$file\" name=\"\" classuid=\"\" hasDestructor=\"true\"/>" >> $GENERATED_FILE
done
echo "</popjparser-infos>" >> $GENERATED_FILE
[ $VERBOSE ] && echo "XML caneva file generated successfully"
exit 0
else
usage
exit 1
fi
fi
########################################################
# Parsing, generation and compilation start from here
########################################################
#Recover the files on the command lines
FILE=$@
#Print usage and exit if no file is given
if [ -z "$FILE" ]
then
usage
exit 1
fi
#If jar is specified, clean the .class files at the end
if [ -z $JAR ]
then
NOCLEANCLASS=1
fi
[ $VERBOSE ] && echo "Analazing files $FILE"
# Variables used to hold different kind of files
PARCLASSES=
MAINFILE=
STDFILES=
CLASSPATHADD=
#Filter files (Parclasses, main class and standard classes)
for i in $FILE
do
if [ ${i##*.} == "jar" ] || [ ${i##*.} == "class" ]
then
CLASSPATHADD=$CLASSPATHADD":"$i
else
#Check if the file is a Java file and if it exists
if [ ${i##*.} == "pjava" ] && [ -f $i ]
then
ISPARCLASS=$(cat $i | grep "public parclass")
ISMAIN=$(cat $i | grep "public static void main")
if [ "$ISPARCLASS" ]
then
PARCLASSES=$PARCLASSES" "$i
elif [ "$ISMAIN" ]
then
if [ -z $MAINFILE ]
then
MAINFILE=$i
else
echo "Your application has more than one main. Compilation aborted !"
exit
fi
else
echo "File $i is not a parclass or a main class but has the pjava extension"
exit 1
fi
else
if [ ${i##*.} == "java" ]
then
STDFILES=$STDFILES" "$i
else
[ $VERBOSE ] && echo "File $i is not a java file or does not exist. This file will be discarded."
fi
fi
fi
done
#Abort compilation if the application does not contains parclasses
if [ -z "$PARCLASSES" ] && [ -z "$MAINFILE" ]
then
echo "You application does not contain any parclasses or main class. Compilation aborted !"
exit
fi
############################################
# Create parclasse list for argument
############################################
PARLIST=
for P in $PARCLASSES
do
if [ -z $PARLIST ]
then
PARLIST=$P
else
PARLIST=$PARLIST:$P
fi
done
############################################
# Parse each parclass
############################################
CLASS_FILES_OUT=
COMPILE_FILES=
[ $VERBOSE ] && echo "Parse and generate Java code for parclasses ..."
for P in $PARCLASSES
do
FILENAME=${P%.*}".java"
CLASSFILENAME=${P%.*}".class"
if [ $POPC ]
then
[ $VERBOSE ] && echo "java -cp $POPJAVA_PARSER_PATH $POPJAVA_PARSER -file=$P -parclasses=$PARLIST -popcinfos=$POPC_INFOFILE > $FILENAME"
java -cp $POPJAVA_PARSER_PATH $POPJAVA_PARSER -file=$P -parclasses=$PARLIST -popcinfos=$POPC_INFOFILE > $FILENAME
else
[ $VERBOSE ] && echo "java -cp $POPJAVA_PARSER_PATH $POPJAVA_PARSER -file=$P -parclasses=$PARLIST > $FILENAME"
java -cp $POPJAVA_PARSER_PATH $POPJAVA_PARSER -file=$P -parclasses=$PARLIST > $FILENAME
fi
if [ $? -ne 0 ]
then
echo "Paring error in the file $P"
exit 1
fi
COMPILE_FILES=$COMPILE_FILES" "$FILENAME
CLASS_FILES_OUT=$CLASS_FILES_OUT" "$CLASSFILENAME
done
############################################
# Parse the main class
############################################
if [ ! -z $MAINFILE ]
then
FILENAME=${MAINFILE%.*}".java"
CLASSFILENAME=${MAINFILE%.*}".class"
[ $VERBOSE ] && echo "Parse and generate Java code for the main class ..."
[ $VERBOSE ] && echo "java -cp $POPJAVA_PARSER_PATH $POPJAVA_PARSER -file=$MAINFILE -m -parclasses=$PARLIST > $FILENAME"
java -cp $POPJAVA_PARSER_PATH $POPJAVA_PARSER -file=$MAINFILE -m -parclasses=$PARLIST > $FILENAME
if [ $? -ne 0 ]
then
echo "Paring error in the file $MAINFILE"
rm $FILENAME
exit
fi
COMPILE_FILES=$COMPILE_FILES" "$FILENAME
CLASS_FILES_OUT=$CLASS_FILES_OUT" "$CLASSFILENAME
fi
############################################
# Compilation of the generated code
############################################
#Compile with the standard Java compiler. The POP-Java library must be given in the classpath
[ $VERBOSE ] && echo "Compiling ..."
[ $VERBOSE ] && echo "javac -cp .:$ADD_CLASSPATH:$POPJAVA_JAR$CLASSPATHADD $COMPILE_FILES $STDFILES"
javac -cp ".:$ADD_CLASSPATH:$POPJAVA_JAR$CLASSPATHADD" $COMPILE_FILES $STDFILES
#Check is the compilation return any error code
if [ $? -ne 0 ]
then
echo "Compiling error occured (error n$?)! Process is aborted"
fi
###############################################################
# Remove temporary file if the option noclean is not specified
###############################################################
if [ -z $NOCLEAN ]
then
[ $VERBOSE ] && echo "Removing temporary files : "$COMPILE_FILES
rm $COMPILE_FILES
fi
############################################
# JAR Creation
############################################
if [ $JAR ]
then
[ $VERBOSE ] && echo "Creating JAR archive (file name : $JAR)"
jar cf $JAR $CLASS_FILES_OUT
rm -rf $CLASS_FILES_OUT
fi
#if [ $? -ne 0 ]
#then
# echo "An error occured during the JAR creation process"
#else
# rm *.class
#fi