1 |
#!/bin/sh -f |
2 |
# |
3 |
# Functions used by the mod_tool shell script. |
4 |
# |
5 |
# |
6 |
check_action() { |
7 |
theaction=$1 |
8 |
therc=1 |
9 |
if [ "x$theaction" == "xbuild" ]; then |
10 |
therc=0 |
11 |
fi |
12 |
if [ "x$theaction" == "xclean" ]; then |
13 |
therc=0 |
14 |
fi |
15 |
if [ "x$therc" == "x1" ]; then |
16 |
write_usage |
17 |
exit $therc |
18 |
fi |
19 |
} |
20 |
|
21 |
check_rc_exit() { |
22 |
therc=$1 |
23 |
thecommand=$2 |
24 |
themessage=$3 |
25 |
if [ "x$therc" != "x0" ]; then |
26 |
echo "$themessage" |
27 |
echo " Command:" |
28 |
echo " >>" "$thecommand" |
29 |
echo " Error:" |
30 |
cat ${ERROUT} | awk '{print " >> ",$0}' |
31 |
echo "\"$0\" exiting with return code $rc" |
32 |
exit $rc |
33 |
fi |
34 |
} |
35 |
|
36 |
is_comment_line() { |
37 |
line=$1 |
38 |
if [ "x${line:0:1}" != "x#" ] && [ "x${line:0:1}" != "x" ]; then |
39 |
return 0 |
40 |
else |
41 |
return 1 |
42 |
fi |
43 |
} |
44 |
|
45 |
mod_tool_build() { |
46 |
# Do commands for when called with action=build. |
47 |
|
48 |
# Write module header |
49 |
write_module_header |
50 |
if [ -r ${USE_FILE_LIST} ]; then |
51 |
write_use_blocks |
52 |
fi |
53 |
write_implicit_none |
54 |
|
55 |
# Write explicit interface blocks |
56 |
if [ -r ${IF_FILE_LIST} ]; then |
57 |
write_explicit_interface_blocks |
58 |
fi |
59 |
|
60 |
# Process templated procedure interfaces |
61 |
if [ -r ${PROCTEMPLATE_FILE_LIST} ]; then |
62 |
write_templated_interface_blocks |
63 |
fi |
64 |
|
65 |
# Process templated procedures |
66 |
if [ -r ${PROCTEMPLATE_FILE_LIST} ]; then |
67 |
write_templated_procedures |
68 |
fi |
69 |
|
70 |
# Process non-templated procedures |
71 |
if [ -r ${PROC_FILE_LIST} ]; then |
72 |
write_nontemplated_procedures |
73 |
fi |
74 |
|
75 |
# Write module close |
76 |
write_module_close |
77 |
|
78 |
mkdir -p ${F90_OUT_DIR} |
79 |
mv ${F90_OUT_FILE} ${F90_OUT_DIR}/${MODULE_NAME}.F90 |
80 |
|
81 |
} |
82 |
|
83 |
mod_tool_clean() { |
84 |
if [ -d ${F90_OUT_DIR} ]; then |
85 |
\rm -fr ${F90_OUT_DIR}/${MODULE_NAME}.F90 |
86 |
fi |
87 |
} |
88 |
|
89 |
start_module_procedures() { |
90 |
if [ "x${CONTAINS_IS_WRITTEN}" == "x0" ]; then |
91 |
export CONTAINS_IS_WRITTEN=1 |
92 |
cat >> ${F90_OUT_FILE} <<EOFA |
93 |
CONTAINS |
94 |
EOFA |
95 |
fi |
96 |
} |
97 |
|
98 |
write_explicit_interface_blocks() { |
99 |
while read line ; do |
100 |
is_comment_line $line; rc=$? |
101 |
if [ "x$rc" == "x0" ]; then |
102 |
fname=$line |
103 |
cat $fname >> ${F90_OUT_FILE} |
104 |
fi |
105 |
done |
106 |
} < ${IF_FILE_LIST} |
107 |
|
108 |
write_implicit_none() { |
109 |
cat >> ${F90_OUT_FILE} <<EOFA |
110 |
IMPLICIT NONE |
111 |
EOFA |
112 |
} |
113 |
|
114 |
write_module_header() { |
115 |
# Write module header |
116 |
cat > ${F90_OUT_FILE} <<'EOFA' |
117 |
! $Header: $ |
118 |
! $Id: $ |
119 |
|
120 |
EOFA |
121 |
|
122 |
cat >> ${F90_OUT_FILE} <<EOFA |
123 |
MODULE ${MODULE_NAME} |
124 |
USE ESMF_Mod |
125 |
EOFA |
126 |
} |
127 |
|
128 |
write_nontemplated_procedures(){ |
129 |
# Write out the procedures that are copied verbatim |
130 |
if [ -r ${PROC_FILE_LIST} ]; then |
131 |
{ |
132 |
while read line ; do |
133 |
is_comment_line "${line}"; rc=$? |
134 |
if [ x"$rc" == "x0" ]; then |
135 |
start_module_procedures |
136 |
fname=$line |
137 |
cat $fname >> ${F90_OUT_FILE} |
138 |
fi |
139 |
done |
140 |
} < ${PROC_FILE_LIST} |
141 |
fi |
142 |
} |
143 |
|
144 |
write_templated_interface_blocks(){ |
145 |
# Process templated procedure interfaces |
146 |
# Each templated procedure requires two files named: |
147 |
# o XXXXX.proctemplate.F90 |
148 |
# o XXXXX.ifacetemplate.F90 |
149 |
# and one or more files named |
150 |
# o XXXXX.proctemplate.tNNN |
151 |
# (where NNN is a number in the sequence 001,002,003 etc...) |
152 |
# the "proctemplate" file contains the templated procedure |
153 |
# code. |
154 |
# the "ifacetemplate" file contains the templated entry for |
155 |
# the generic interface blocks. |
156 |
# the "proctemplate.tNNN" files contain definitions of the |
157 |
# the macros that will be used to generate actual source |
158 |
# from the template sources. Each "proctemplate.tNNN" contains |
159 |
# the macros needed for a single template type. |
160 |
# |
161 |
# These two parts go in separate areas of the generated module |
162 |
# file. The "ifacetemplate" file will generate code for a |
163 |
# INTERFACE |
164 |
# END INTERFACE |
165 |
# Fortran 90 section. |
166 |
# The "proctemplate" file contains code for an actual procedure |
167 |
# definition. |
168 |
if [ -r ${PROCTEMPLATE_FILE_LIST} ]; then |
169 |
{ |
170 |
while read line ; do |
171 |
is_comment_line "${line}"; rc=$? |
172 |
if [ x"$rc" == "x0" ]; then |
173 |
# Found a file name. Now generate the output. |
174 |
fname=$line |
175 |
tpref=${line%.F90} |
176 |
templates=${tpref}.t* |
177 |
iftemplatefile=${tpref%.proctemplate}.ifacetemplate.F90 |
178 |
ifnamefile=${tpref%.proctemplate}.ifacename.F90 |
179 |
cat $ifnamefile > foo2.F90 |
180 |
for t in $templates; do |
181 |
# |
182 |
# Interface block |
183 |
# |
184 |
echo " " |
185 |
echo "#" |
186 |
echo "# Applying template $t" |
187 |
echo "# to $iftemplatefile" |
188 |
echo "#" |
189 |
echo " " |
190 |
cp $iftemplatefile tempIN.F90 |
191 |
{ |
192 |
while read line ; do |
193 |
is_comment_line "${line}"; rc=$? |
194 |
if [ x"$rc" == "x0" ]; then |
195 |
key=`echo "${line[0]}" | awk '{print $1}'` |
196 |
keyval=`echo "${line[0]}" | sed s/'[^\"]*\"\(.*\)\" *$/\1/'` |
197 |
# echo Changing occurences of \""${key}"\" to \""${keyval}"\". |
198 |
cat tempIN.F90 | sed s/"${key}"/"${keyval}"/g > tempOUT.F90 |
199 |
cp tempOUT.F90 tempIN.F90 |
200 |
fi |
201 |
done |
202 |
cat tempIN.F90 >> foo2.F90 |
203 |
} <$t |
204 |
done |
205 |
cat << 'EOFA' >> foo2.F90 |
206 |
END INTERFACE |
207 |
EOFA |
208 |
fi |
209 |
done |
210 |
} < ${PROCTEMPLATE_FILE_LIST} |
211 |
fi |
212 |
cat << EOFA >> $F90_OUT_FILE |
213 |
! |
214 |
! Begin interfaces generated from templates |
215 |
EOFA |
216 |
cat foo2.F90 >> $F90_OUT_FILE |
217 |
cat << 'EOFA' >> foo2.F90 |
218 |
EOFA |
219 |
cat << EOFA >> $F90_OUT_FILE |
220 |
! End interfaces generated from templates |
221 |
! |
222 |
EOFA |
223 |
} |
224 |
|
225 |
write_templated_procedures(){ |
226 |
# Process templated procedures |
227 |
# Each templated procedure requires two files named: |
228 |
# o XXXXX.proctemplate.F90 |
229 |
# o XXXXX.ifacetemplate.F90 |
230 |
# and one or more files named |
231 |
# o XXXXX.proctemplate.tNNN |
232 |
# (where NNN is a number in the sequence 001,002,003 etc...) |
233 |
# the "proctemplate" file contains the templated procedure |
234 |
# code. |
235 |
# the "ifacetemplate" file contains the templated entry for |
236 |
# the generic interface blocks. |
237 |
# the "proctemplate.tNNN" files contain definitions of the |
238 |
# the macros that will be used to generate actual source |
239 |
# from the template sources. Each "proctemplate.tNNN" contains |
240 |
# the macros needed for a single template type. |
241 |
# |
242 |
# These two parts go in separate areas of the generated module |
243 |
# file. The "ifacetemplate" file will generate code for a |
244 |
# INTERFACE |
245 |
# END INTERFACE |
246 |
# Fortran 90 section. |
247 |
# The "proctemplate" file contains code for an actual procedure |
248 |
# definition. |
249 |
if [ -r ${PROCTEMPLATE_FILE_LIST} ]; then |
250 |
{ |
251 |
while read line ; do |
252 |
is_comment_line "${line}"; rc=$? |
253 |
if [ x"$rc" == "x0" ]; then |
254 |
# Found a file name. Now generate the output. |
255 |
start_module_procedures |
256 |
fname=$line |
257 |
tpref=${line%.F90} |
258 |
templates=${tpref}.t* |
259 |
echo > foo1.F90 |
260 |
for t in $templates; do |
261 |
echo "#" |
262 |
echo "# Applying template $t" |
263 |
echo "# to $fname" |
264 |
echo "#" |
265 |
cp $fname tempIN.F90 |
266 |
{ |
267 |
while read line ; do |
268 |
is_comment_line "${line}"; rc=$? |
269 |
if [ x"$rc" == "x0" ]; then |
270 |
key=`echo "${line[0]}" | awk '{print $1}'` |
271 |
keyval=`echo "${line[0]}" | sed s/'[^\"]*\"\(.*\)\" *$/\1/'` |
272 |
# echo Changing occurences of \""${key}"\" to \""${keyval}"\". |
273 |
cat tempIN.F90 | sed s/"${key}"/"${keyval}"/g > tempOUT.F90 |
274 |
cp tempOUT.F90 tempIN.F90 |
275 |
fi |
276 |
done |
277 |
cat tempIN.F90 >> foo1.F90 |
278 |
} <$t |
279 |
done |
280 |
fi |
281 |
done |
282 |
} < ${PROCTEMPLATE_FILE_LIST} |
283 |
fi |
284 |
cat << EOFA >> $F90_OUT_FILE |
285 |
! |
286 |
! Begin procedures generated from templates |
287 |
EOFA |
288 |
cat foo1.F90 >> $F90_OUT_FILE |
289 |
cat << 'EOFA' >> foo1.F90 |
290 |
EOFA |
291 |
cat << EOFA >> $F90_OUT_FILE |
292 |
! End procedures generated from templates |
293 |
! |
294 |
EOFA |
295 |
|
296 |
} |
297 |
|
298 |
write_usage() { |
299 |
echo "Usage: $PROG_NAME [build|clean]" |
300 |
} |
301 |
|
302 |
write_use_blocks() { |
303 |
while read line ; do |
304 |
is_comment_line $line; rc=$? |
305 |
if [ "x$rc" == "x0" ]; then |
306 |
fname=$line |
307 |
cat $fname >> ${F90_OUT_FILE} |
308 |
fi |
309 |
done |
310 |
} < ${USE_FILE_LIST} |
311 |
|
312 |
write_module_close() { |
313 |
cat >> ${F90_OUT_FILE} <<EOFA |
314 |
END MODULE |
315 |
EOFA |
316 |
} |
317 |
|