/
build.sh
executable file
·217 lines (200 loc) · 4.66 KB
/
build.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
#!/bin/bash -e
shopt -s nullglob
unalias -a
to_array() {
IFS=\ read -r -a "$1" <<< "${!1}"
}
clean_build() {
$fresh_build || return 0
fresh_build=false
if test -d build; then
pushd build >/dev/null
for file in *.f90 *.mod *.o; do
rm "$file"
done
popd >/dev/null
fi
}
compile() {
$build_flag || return 0
clean_build
to_array std_flags
to_array pic_flags
to_array optim_flags
to_array debug_flags
srcdir=$rootdir/$1
if test ! -d "$srcdir"; then
echo Error: $srcdir does not exist
exit 1
fi
pushd "$buildir" >/dev/null
flags=("${std_flags[@]}")
if $pic_build; then
flags+=("${pic_flags[@]}")
fi
if $debug_build; then
flags+=("${debug_flags[@]}")
else
flags+=("${optim_flags[@]}")
fi
while IFS= read -r srcfile; do
prefix=${srcfile%.*}
objfile=$prefix.o
if ! test -e "$objfile" \
|| ! test -e "$srcfile" \
|| ! diff -q "$srcfile" "$srcdir/$srcfile" >/dev/null
then
echo Compiling $prefix...
if test "$srcdir" != "$PWD"; then
cp -f "$srcdir/$srcfile" "$srcfile"
fi
"$F90" "${flags[@]}" -c "$srcfile" -o "$objfile"
fi
obj_files+=("$objfile")
done < <(grep -v ^# "$srcdir/source_files")
popd >/dev/null
if test -f "$srcdir/f2py_files"; then
while IFS= read -r f2pyfile; do
f2py_files+=("$f2pyfile")
done < <(grep -v ^# "$srcdir/f2py_files")
fi
}
make_prog() {
executable=$buildir/$1
$build_flag || return 0
if test -z "$1"; then
echo Error: name is empty
exit 1
fi
echo Linking program...
pushd "$buildir" > /dev/null
"$F90" -o "$1" "${obj_files[@]}" -llapack
popd > /dev/null
}
make_lib() {
$build_flag || return 0
if test -z "$1"; then
echo Error: name is empty
exit 1
fi
echo Linking dynamic library...
pushd "$buildir" >/dev/null
"$F90" -shared -o "$1.so" "${obj_files[@]}" -llapack
popd >/dev/null
}
make_pyext() {
$build_flag || return 0
if test -z "$1"; then
echo Error: name is empty
exit 1
fi
if ! type "$F2PY" &>/dev/null; then
echo Error: F2PY executable not found
exit 1
fi
pushd "$buildir" >/dev/null
echo Linking extension module...
"$F2PY" -h "$1.pyf" -m "$1" --overwrite-signature "${f2py_files[@]}" --quiet
"$F2PY" -c "$1.pyf" --fcompiler=gnu95 --link-lapack "${obj_files[@]}" --quiet
popd >/dev/null
}
runtests() {
$test_flag || return 0
if test -z "$executable"; then
echo Error: executable is not set
exit 1
fi
testset=$1
shift
for file in "$testdir/$testset"/*.out; do
testname=$(basename "$file" .out)
echo -n "Running test $testset/$testname... "
# "$executable" -stdin xyz -stdout xyz -test -stats "$@" < "$testdir/$testset/$testname.xyz" 2>&1 > "$file"
if diff -bB <("$executable" -stdin xyz -stdout xyz -test -stats "$@" < "$testdir/$testset/$testname.xyz" 2>&1) "$file"; then
echo ok
else
echo failed
fi
done
}
rootdir=$(dirname "$(readlink -e "$0")")
if test ! -e ./build.env; then
echo Error: build.env does not exist
exit 1
elif test ! -f ./build.env; then
echo Error: build.env does exist but is not a file
exit 1
fi
buildir=$rootdir/build
testdir=$rootdir/tests
if test ! -e "$buildir"; then
mkdir "$buildir"
elif test ! -d "$buildir"; then
echo Error: $buildir does exist but is not a directory
exit 1
fi
# Set environment
while IFS= read -r line; do
var=${line%%=*}
value=${line#*=}
declare -- "$var"="$value"
done < <(grep -v -e^# -e^$ ./build.env)
build_flag=true
fresh_build=true
debug_build=false
test_flag=true
while getopts ":dqt" opt; do
case $opt in
d)
build_flag=true
debug_build=true
test_flag=false
;;
q)
build_flag=true
fresh_build=false
test_flag=false
;;
t)
build_flag=false
test_flag=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
if test $# -gt 0; then
target=$1
else
target=prog
fi
case $target in
prog)
# Build program
pic_build=false
compile molalignlib
compile molalign
make_prog molalign
# Run tests
runtests jcim.2c01187/0.05 -rec 5 -sort -fast -tol 0.17
runtests jcim.2c01187/0.1 -rec 5 -sort -fast -tol 0.35
runtests jcim.2c01187/0.2 -rec 5 -sort -fast -tol 0.69
;;
lib)
# Build dynamic library
pic_build=true
compile molalignlib
make_lib molalignlib
;;
pyext)
# Build python extension module
pic_build=true
compile molalignlib
make_pyext molalignlibext
;;
*)
echo Unknown target $target
esac