forked from kelchuan/snac-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-functions.sh
461 lines (410 loc) · 12.1 KB
/
build-functions.sh
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/bin/sh
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##
## Copyright (C), 2003, Victorian Partnership for Advanced Computing (VPAC) Ltd, 110 Victoria Street, Melbourne, 3053, Australia.
##
## Authors:
## Stevan M. Quenette, Senior Software Engineer, VPAC. ([email protected])
## Patrick D. Sunter, Software Engineer, VPAC. ([email protected])
## Luke J. Hodkinson, Computational Engineer, VPAC. ([email protected])
## Siew-Ching Tan, Software Engineer, VPAC. ([email protected])
## Alan H. Lo, Computational Engineer, VPAC. ([email protected])
##
## This library is free software; you can redistribute it and/or
## modify it under the terms of the GNU Lesser General Public
## License as published by the Free Software Foundation; either
## version 2.1 of the License, or (at your option) any later version.
##
## This library 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. See the GNU
## Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public
## License along with this library; if not, write to the Free Software
## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
##
## $Id: Makefile.def 2338 2004-11-19 04:51:11Z SteveQuenette $
##
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Set up the which command (used for all tests)
if test "${WHICH}x" = "x"; then
WHICH="/usr/bin/which"
if test ! -x "${WHICH}"; then
echo "Error: default \"which\" command/default of \"$WHICH\" not found/executable. Set the WHICH variable."
exit 1
fi
export WHICH
fi
#Function to see if a "which" command failed to find a binary (needs to handle Os X where Stderr goes to Stdout
# for which command)
whichFailed() {
if test "${1}x" = "x"; then return 0; fi
#For Max OsX which implementation
if test `echo $1 | cut -f 1 -d' '` = "no"; then return 0; fi
if test `echo $1 | cut -f 3 -d' '` = "aliased"; then return 0; fi
#For OSF1 which implementation at ANU
if test `echo $1 | cut -f 2 -d' '` = "Cannot"; then return 0; fi
return 1;
}
#Set up the tr command (used for check functions below)
if test "${TR}x" = "x"; then
TR=`${WHICH} tr 2> /dev/null`
if whichFailed "${TR}"; then
TR="/bin/tr"
fi
if test ! -x "${TR}"; then
echo "Error: default \"tr\" command of \"$TR\" not found/executable. Set the TR variable."
exit 1
fi
export TR
else
if whichFailed `${WHICH} ${TR} 2> /dev/null`; then
echo "Error: given \"tr\" command of \"$TR\" not found/executable. Correct the TR variable."
exit 1
fi
fi
#Functions to check validity
assertValidExecutable ()
{
if test ! -x "${1}"; then
upperCommand=`echo ${2} | tr '[:lower:]' '[:upper:]'`
echo "Error: $2 binary \"$1\" not found in path, and defaults were not found/executable."
printf "Set the $upperCommand "
if test "${3}x" != "x"; then
printf "(or $3)"
fi
printf " environment variable/s, or modify your path appropriately.\n"
exitWithErrorIfNotOptional
fi
}
warnValidExecutable ()
{
resolveVariable theExec ${1}
if test ! -x "${theExec}"; then
upperCommand=`echo ${2} | tr '[:lower:]' '[:upper:]'`
echo "Warning: $2 binary \"$theExec\" not found in path, and defaults were not found/executable."
printf "If you require this package, check it is installed, built, your path is correct, and that the $upperCommand "
if test "${4}x" != "x"; then
printf "(or $4)"
fi
printf " environment variable(s) are set.\n"
return 1
fi
return 0
}
assertValidExecutableInput ()
{
testResult=`${WHICH} "$1" 2> /dev/null`
if whichFailed "$testResult"; then
upperCommand=`echo ${2} | tr '[:lower:]' '[:upper:]'`
echo "Error: Given name/path to $2 command of \"$1\" not found or not valid."
printf "Correct the $upperCommand "
if test "${3}x" != "x"; then
printf "(or $3)"
fi
printf " environment variable/s, or modify your path appropriately.\n"
exitWithErrorIfNotOptional
fi
}
warnValidExecutableInput ()
{
testResult=`${WHICH} "$1" 2> /dev/null`
if whichFailed "$testResult"; then
upperCommand=`echo ${2} | tr '[:lower:]' '[:upper:]'`
echo "Warning: Given name/path to $2 command of \"$1\" not found or not valid."
printf "If needed, correct the $upperCommand "
if test "${3}x" != "x"; then
printf "(or $3)"
fi
printf " environment variable/s, or modify your path appropriately.\n"
return 1
fi
return 0
}
#Functions to check validity
assertValidFile ()
{
if test ! -r "${1}"; then
echo "Error: File/dir \"$1\" not found, meaning package $2 is not installed, not build, or some variables are wrong."
printf "Check the package is installed, built, and that the $3 "
if test "${4}x" != "x"; then
printf "(or $4)"
fi
printf " environment variable/s are set correctly.\n"
exitWithErrorIfNotOptional
fi
}
#Functions to check validity
warnIfNotValidFile ()
{
if test ! -r "${1}"; then
echo "Warning: File/dir \"$1\" not found, meaning package $2 is not installed, not build, or some environment variables are incorrect."
printf "If you require this package, check it is installed, built, and that the $3 "
if test "${4}x" != "x"; then
printf "(or $4)"
fi
printf " environment variable/s are set correctly.\n"
fi
}
assertValidDirectory ()
{
if test ! -d "${1}"; then
echo "Error: Directory \"$1\" not found, meaning package $2 is not installed, not build, or some variables are wrong."
printf "Check the package is installed, built, and that the $3 "
if test "${4}x" != "x"; then
printf "(or $4)"
fi
printf " environment variable/s are set correctly.\n"
exitWithErrorIfNotOptional
fi
}
# Sets value in Makefile.system with value from environment or from default
setValueWithDefault() {
echo "ifndef $1" >> Makefile.system
eval "INPUTVALUE=\"\$$1\""
if test "${INPUTVALUE}x" = "x" ; then
echo " $1=$2" >> Makefile.system
eval "$1=\"$2\"" 2> /dev/null &&
export ${1}
else
echo " $1=${INPUTVALUE}" >> Makefile.system
fi
echo "endif" >> Makefile.system
}
# Sets value in Makefile.system
setValue() {
echo "$1=$2" >> Makefile.system
eval "$1=\"$2\"" 2> /dev/null &&
export ${1}
}
makePathAbsolute () {
eval "INPUTVALUE=\"\$$1\""
if test "${INPUTVALUE}" = "x" ; then
eval "${1}=`pwd`"
else
eval "${1}=`cd ${INPUTVALUE} ; pwd`"
fi
export ${1}
}
appendVariableIfReadable() {
if test -r ${2} ; then
echo "${1}+=${3}" >> Makefile.system
eval "$1=\"\$$1\"\ \"$3\"" 2> /dev/null &&
export ${1}
fi
}
getValueFromMakefile_System() {
MAKEFILE_SYSTEM=Makefile.system
until test -r ${MAKEFILE_SYSTEM} ; do
MAKEFILE_SYSTEM=../${MAKEFILE_SYSTEM}
done
eval "INPUTVALUE=\"\$$1\""
if test "${INPUTVALUE}x" = "x" ; then
# The "head" filter is a bit of a hack due to a known problem of MPI info appearing in the Makefile.system
# files twice at the moment. Once that's fixed, it really should be changed into a firewall such that
# only one result comes back.
FOUND_VALUE=`grep "${1}=" ${MAKEFILE_SYSTEM} | head -n 1 | cut -d'=' -f2`
eval "${1}=\"${FOUND_VALUE}\"" 2> /dev/null
fi
}
getVariableFromMakefile() {
theVariable=$1
theMakefile=$2
echotemp="./echoMakefile"
cat <<EOF > ${echotemp}
ifdef TARGET_MAKEFILE
include \$(TARGET_MAKEFILE)
endif
echo-var:
@echo \$(\$(VAR))
EOF
theValue=`make echo-var -f ${echotemp} TARGET_MAKEFILE=${theMakefile} VAR=${theVariable}`
rm ${echotemp}
eval "${theVariable}=\"${theValue}\"" 2> /dev/null
}
resolveVariable() {
theVar=$1
resolvedResult=$2
while test "`echo $resolvedResult | grep '[\$]'`x" != "x"
do
eval resolvedResult=$resolvedResult
done
eval $theVar=$resolvedResult
}
### For compiling & running code tests in configure
cleanConftest() {
CONFTEST_DIR=$1
${MAKE} -C ${CONFTEST_DIR} clean > /dev/null 2>&1
return $?
}
compileConftest() {
CONFTEST_DIR=$1
cleanConftest ${CONFTEST_DIR}
echo " Compiling config test ${CONFTEST_DIR} ..."
${MAKE} -C ${CONFTEST_DIR} > /dev/null 2>&1
if ! test -x ${CONFTEST_DIR}/conftest; then
echo " ...Compile failed"
return 1
fi
return 0;
}
runConftest() {
CONFTEST_DIR=$1
RESULT_VAR=$2
echo " Running config test ${CONFTEST_DIR} ..."
if ! test -x ${CONFTEST_DIR}/conftest; then
EXIT_VALUE=1
else
RESULT_TEXT=`${CONFTEST_DIR}/conftest`
EXIT_VALUE=$?
eval "${RESULT_VAR}=$RESULT_TEXT"
return ${EXIT_VALUE}
fi
if ! test "${EXIT_VALUE}" = "0"; then
echo " ...Run failed"
return ${EXIT_VALUE}
fi
return 0
}
DoCompileConftest() {
CONFTEST_DIR=$1
if ! compileConftest ${CONFTEST_DIR}; then
return 1
fi
cleanConftest ${CONFTEST_DIR}
return 0
}
DoCriticalCompileConftest() {
if ! DoCompileConftest $1; then
echo "Critical compile conftest failed, -> exiting configure process."
exit 1
fi
}
DoConftest() {
CONFTEST_DIR=$1
RESULT_VAR=$2
if ! compileConftest ${CONFTEST_DIR}; then
return 1
fi
if ! runConftest ${CONFTEST_DIR} ${RESULT_VAR}; then
return 1
fi
cleanConftest ${CONFTEST_DIR}
return 0
}
DoCritialConftest() {
if ! DoConftest $1 $2; then
echo "Critical compile conftest failed, -> exiting configure process."
exit 1
fi
}
parsePackageConfigOptions() {
# Unset all possible options for package configs
unset PACKAGE_OPTIONAL
while test $# -gt 0; do
case $1 in
--optional)
export PACKAGE_OPTIONAL=1;
;;
# *)
# echo "Unknown argument $1 for package config"
# ;;
esac
shift
done
}
exitWithErrorIfNotOptional() {
if test "${PACKAGE_OPTIONAL}x" = "x"; then
exit 1
fi
}
exitWithErrorIfNotOptionalWithMessage() {
if test "${PACKAGE_OPTIONAL}x" = "x"; then
echo " *** Error: Configuration has failed because $i installation $2 is invalid"
exit 1
else
echo " ($1 will be unavailable)"
fi
}
dumpPkgConfigFile() {
dumpdir=$1
if ! test -d "${dumpdir}"; then
mkdir -p ${dumpdir}
fi
INC_VAR=`packages="PETSC MPI XML MATH DL HYPRE" make INCLUDES-echo | cut -f 2 -d =`
LIB_VAR=`packages="PETSC MPI XML MATH DL HYPRE" make EXTERNAL_LIBS-echo | cut -f 2 -d = | sed 's/\-Xlinker \-rpath \-Xlinker /\-Wl,\-rpath,/g'`
echo " Creating $dumpdir/stgermain.pc"
cat <<EOF > $dumpdir/stgermain.pc
prefix=${EXPORT_DIR}
exec_prefix=${EXPORT_DIR}/bin
libdir=${EXPORT_DIR}/lib
includedir=${EXPORT_DIR}/include
Name: StGermain
Description: The StGermain Framework
Version: 0
Requires:
Libs: -L${EXPORT_DIR}/lib -lStGermain ${LIB_VAR}
Cflags: ${INC_VAR}
EOF
}
markAsBuilding() {
echo " $1 Marked as a project building built"
cat <<EOF > ./create-env.tmp
BUILDING_$1=1
EOF
. ./create-env.tmp
rm create-env.tmp
}
executeProgramWithTimeOut() {
# Get my process id
myid=$$
# remove leading '-'
# on macs, bash is listed as '-bash' which grep does not like as an argument
myname=`echo $0 | sed 's/^\-//'`
if ! test $# -eq 2; then
echo "Usage:"
echo "./exec.sh (timeout) (program)"
return
fi
timeout=$1
if test $timeout -lt 0; then
echo "timeout must be greater than 0 seconds"
return
fi
commandline=$2
program=`echo $commandline | cut -f 1 -d ' '`
# Run the test as a background process
echo "Process id $myid Running program '$commandline'"
$commandline &
elapsedtime="0"
while [ $elapsedtime -lt $timeout ]
do
sleep 1
# Check alive
programid=`ps xo pid,ppid,command 2> /dev/null | grep $program | grep -v grep | grep -v $myname | grep $myid | sed 's/^[ ]*//' | cut -f 1 -d ' '`
if test "${programid}x" != "x"; then
elapsedtime=$[$elapsedtime+1]
else
# exit loop
finishtime=$[$elapsedtime+1]
elapsedtime=$timeout
fi
done
# Search for whether the test program has finished
# Explaination:
# - Get processes
# - Get matches of the of the program name
# -> excluding grep
# -> excluding this script (arguments)
# - Which has this script id in it (the parent id)
# - trim leading white space (mac ps adds this)
# - Take the id value
programid=`ps xo pid,ppid,command 2> /dev/null | grep $program | grep -v grep | grep -v $myname | grep $myid | sed 's/^[ ]*//' | cut -f 1 -d ' '`
if test "${programid}x" != "x"; then
echo "Process has not finished in $timeout seconds...killing"
kill -9 $programid > /dev/null 2>&1
else
echo "Process finished on its own in $finishtime seconds"
fi
}