-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake.sh
executable file
·412 lines (396 loc) · 12.7 KB
/
make.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
#!/bin/bash
function usage() {
echo -e "\033[4mcommands and parameters:\033[0m"
echo -e " \033[1mclean\033[0m: cleans all built files (can be chained)"
echo -e " \033[1mhere\033[0m: sets the working directory here (can be chained)"
echo -e " \033[1mgcc\033[0m: sets the compiler to gcc (can be chained)"
echo -e " \033[1msed\033[0m: manipulates patterns in source files (can be chained)"
echo -e " <pattern> [replace]"
echo -e " \033[1mdoc\033[0m: builds the documentation (can be chained)"
echo -e " \033[1mgui\033[0m: builds graphical simulations (platform can be unix or windows)"
echo -e " [-g] <platform> <targets...>"
echo -e " \033[1mbuild\033[0m: builds binaries for given targets, skipping tests"
echo -e " <copts...> <targets...>"
echo -e " \033[1mtest\033[0m: builds binaries and tests for given targets"
echo -e " <copts...> <targets...>"
echo -e " \033[1mrun\033[0m: build and runs a single target"
echo -e " <copts...> <target> <arguments...>"
echo -e " \033[1mall\033[0m: builds all possible targets and documentation"
echo -e " <copts...>"
echo -e "Targets can be substrings demanding builds for all possible expansions."
exit 1
}
if [ "$1" == "" ]; then
usage
fi
asan="--features=asan"
copts=""
targets=""
errored=( )
exitcodes=( )
folders=( `ls */BUILD | sed 's|/BUILD||'` )
function numformat {
n=$1
k=$2
if [ "$n" == "?" ]; then
l=$[k-1]
else
n=$[n+0]
l=${#n}
l=$[k-l]
fi
for ((x=0; x<l; ++x)); do
n=" $n"
done
echo -n "$n"
}
function ramformat {
numformat "$1" 4
echo -n " MB"
}
function addzero {
n=$1
if [ $n -lt 10 ]; then
n=0$n
fi
echo -n $n
}
function timeformat {
if [[ "$1" =~ [0-9][0-9]:[0-9][0-9]:[0-9][0-9] ]]; then
echo -n "$1".00
else
mins=`echo $1 | sed 's|:.*||'`
hour=$[mins/60]
mins=$[mins%60]
secs=`echo $1 | sed 's|^[0-9]*:||'`
echo -n `addzero $hour`:`addzero $mins`:$secs
fi
}
function reporter() {
"$@"
code=$?
if [ $code -gt 0 ]; then
exitcodes=( ${exitcodes[@]} $code )
failcmd="\033[4m$@\033[0m"
errored=( "${errored[@]}" "$failcmd" )
fi
return ${#exitcodes[@]}
}
function quitter() {
code=${#exitcodes[@]}
if [ $code -gt 0 ]; then
echo
echo -e "\033[1mBuild terminated with errors:\033[0m"
for ((i=0; i<code; ++i)); do
echo -e "${errored[i]}: exit with ${exitcodes[i]}"
done
fi
exit $code
}
function mkdoc() {
if [ ! -d doc ]; then
mkdir doc
fi
reporter doxygen Doxyfile
}
function parseopt() {
i=0
while [ "${1:0:1}" == "-" ]; do
if [ "${1:0:2}" == "-O" ]; then
asan="--features=opt"
else
copts="$copts --copt=$1"
fi
i=$[i+1]
shift 1
done
return $i
}
function filter() {
rule="$1"
shift 1
while read -r target; do
build=`echo $target | sed 's|/[^/]*.cpp$|/|'`BUILD
if [ "${target: -4}" == ".cpp" ]; then
name="['\"]`basename $target .cpp`['\"]"
else
name=""
fi
if [ -f $build -a `cat $build | tr -s ' \r\n' ' ' | grep "$rule( name = $name" | wc -l` -gt 0 ]; then
echo -n "$target "
fi
done
echo
}
function finder() {
if [ "$targets" != "" ]; then
return 0
fi
find="$1"
rule="$2"
for pattern in "$find" "$find*" "*$find*"; do
if [ "$targets" == "" ]; then
targets=$(for base in ${folders[@]}; do
echo $base*/$pattern/ $base*/$pattern.cpp $base/*/$pattern.cpp | tr ' ' '\n' | grep -v "*" | filter "$rule"
done)
fi
done
# convert to bazel format
if [ "$targets" != "" ]; then
targets=`echo $targets | tr ' ' '\n' | rev | sed 's|^ppc.||;s|/|:|;s|^:|.../|' | rev | sort | uniq`
fi
}
function builder() {
cmd=$1
shift 1
t=`echo " $@" | sed 's| | //|g'`
echo -e "\033[4mbazel $cmd $copts $asan $t\033[0m"
reporter bazel $cmd $copts $asan $t
}
function powerset() {
first="$1"
if [ "$first" == "" ]; then
echo ""
exit 0
fi
shift 1
rec=`powerset "$@"`
echo -n "$rec"
echo " $rec" | sed "s| | #$first|g"
}
while [ "$1" != "" ]; do
if [ "$1" == "clean" ]; then
shift 1
rm -rf doc
bazel clean
elif [ "$1" == "here" ]; then
shift 1
export TEST_TMPDIR=`pwd`/..
elif [ "$1" == "gcc" ]; then
shift 1
gcc=$(which $(compgen -c gcc- | grep "^gcc-[1-9][0-9]*$" | uniq))
gpp=$(which $(compgen -c g++- | grep "^g++-[1-9][0-9]*$" | uniq))
export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
export CC="$gpp"
export CXX="$gpp"
elif [ "$1" == "sed" ]; then
pattern="$2"
replace=""
videoreplace=`echo -e "\033[7m&\033[0m"`
shift 2
if [ "$1" != "" -a `echo $1 | grep "clean\|here\|gcc\|sed\|doc\|build\|test\|run\|all" | wc -l` -eq 0 ]; then
replace="$1"
videoreplace=`echo -e "\033[31m[-&-]\033[32m{+$replace+}\033[0m"`
shift 1
fi
for folder in ${folders[@]}; do
for f in $folder/*.?pp $folder/*/*.?pp; do
if [ -f "$f" -a `cat "$f" | grep -E "$pattern" | wc -l` -gt 0 ]; then
echo -e "\n==> $f <=="
cat -n $f | grep -E "$pattern" | sed -E "s/$pattern/$videoreplace/g"
fi
done
done | less -r
totn=0
totf=0
for folder in ${folders[@]}; do
for f in $folder/*.?pp $folder/*/*.?pp; do
if [ -f "$f" -a `cat "$f" | grep -E "$pattern" | wc -l` -gt 0 ]; then
n=`cat $f | sed -E "s/$pattern/ß/g" | tr -cd "ß" | tr "ß" "x" | wc -c`
totn=$[totn+n]
totf=$[totf+1]
fi
done
done
echo "$totn occurrences found across $totf files."
if [ "$replace" != "" ]; then
echo -n "Proceed with substitution? (y/N) "
read x
if [ "$x" == "y" ]; then
for folder in ${folders[@]}; do
for f in $folder/*.?pp $folder/*/*.?pp; do
if [ -f "$f" -a `cat "$f" | grep -E "$pattern" | wc -l` -gt 0 ]; then
sed -i "" -E "s/$pattern/$replace/g" $f
fi
done
done
fi
fi
elif [ "$1" == "doc" ]; then
shift 1
mkdoc
elif [ "$1" == "gui" ]; then
git submodule init
git submodule update
shift 1
btype="Release"
if [ "$1" == "-g" ]; then
btype="Debug"
shift 1
fi
platform=$1
if [ "$platform" == windows ]; then
flag=MinGW
elif [ "$platform" == unix ]; then
flag=Unix
else
echo -e "\033[4mUnrecognized platform \"$platform\". Available platforms are:\033[0m"
echo -e " \033[1mwindows unix\033[0m"
exit 1
fi
shift 1
echo -e "\033[4mcmake -S ./ -B ./bin -G \"$flag Makefiles\" -DCMAKE_BUILD_TYPE=$btype -Wno-dev\033[0m"
cmake -S ./ -B ./bin -G "$flag Makefiles" -DCMAKE_BUILD_TYPE=$btype -Wno-dev
echo -e "\033[4mcmake --build ./bin/\033[0m"
cmake --build ./bin/
if [ "$platform" == windows ]; then
cp bin/fcpp/src/libfcpp.dll bin/
fi
for target in "$@"; do
cd bin
./$target | tee ../output/$target.asy
cd ../output
sed -i "" -E "s| \(mean-mean\)||g" $target.asy
asy $target.asy -f pdf
cd ..
done
quitter
elif [ "$1" == "build" ]; then
shift 1
parseopt "$@"
shift $?
alltargets=""
while [ "$1" != "" ]; do
if [ "$1" == "all" ]; then
for folder in ${folders[@]}; do
alltargets="$alltargets $folder/..."
done
else
finder "$1" "\(cc_library\|cc_binary\)"
finder "$1" "cc_test"
if [ "$targets" == "" ]; then
echo -e "\033[1mtarget \"$1\" not found\033[0m"
else
alltargets="$alltargets $targets"
targets=""
fi
fi
shift 1
done
if [ "$alltargets" != "" ]; then
builder build $alltargets
fi
quitter
elif [ "$1" == "test" ]; then
shift 1
parseopt "$@"
shift $?
alltargets=""
while [ "$1" != "" ]; do
if [ "$1" == "all" ]; then
for folder in ${folders[@]}; do
alltargets="$alltargets $folder/..."
done
else
finder "$1" "cc_test"
if [ "$targets" == "" ]; then
echo -e "\033[1mtarget \"$1\" not found\033[0m"
else
alltargets="$alltargets $targets"
targets=""
fi
fi
shift 1
done
if [ "$alltargets" != "" ]; then
builder test $alltargets
fi
quitter
elif [ "$1" == "run" ]; then
shift 1
parseopt "$@"
shift $?
finder "$1" "cc_binary"
if [ "$targets" == "" ]; then
echo -e "\033[1mtarget \"$1\" not found\033[0m"
elif [ `echo $targets | tr ' ' '\n' | wc -l` -ne 1 ]; then
echo -e "\033[1mtarget is not unique\033[0m"
echo $targets | tr ' ' '\n' | sed 's|^|//|'
else
shift 1
plots=( )
while [ `echo "$1" | grep '(' | wc -l` -gt 0 ]; do
plots=( "${plots[@]}" "$1" )
shift 1
done
name=`echo $targets | sed 's|.*:||'`
file="output/raw/$name.txt"
built=`echo bazel-bin/$targets | tr ':' '/'`
builder build $targets
if [ ${#exitcodes[@]} -gt 0 ]; then
quitter
fi
mkdir -p output/raw
$built "$@" > $file & pid=$!
trap ctrl_c INT
function ctrl_c() {
echo -e "\n\033[J"
kill -9 $pid 2>&1
exit 1
}
echo -e "\033[4mRUNNING: CPU TIME RAM (NOW) (AVG) (MAX) FILES LINES\033[0m"
num=0
max=0
sum=0
while true; do
tim=`ps -o time -p $pid | tail -n +2 | tr -d ' \t\n'`
m=`ps -o rss -p $pid | tail -n +2 | tr -d ' \t\n'`
if [ "$m" == "" ]; then break; else mem=$m; fi
num=$[num+1]
sum=$[sum+mem]
mem=$[(mem+511)/1024]
max=$[max > mem ? max : mem]
avg=$[((sum+511)/1024 + num/2)/ num]
fil=`ls output/raw | grep $"$name.*\.txt" | wc -l`
if [ "$fil" -gt 1000 ]; then
row="?"
else
row=`cat output/raw/$name*.txt | grep -v "^#" | wc -l`
fi
echo -e " `timeformat $tim`s `ramformat $mem` `ramformat $avg` `ramformat $max` `numformat $fil 7` `numformat $row 7`\n\033[J"
( cat $file | tail -n 10 | cut -c 1-`tput cols`; echo -e "\n\n\n\n\n\n\n\n\n" ) | head -n 10
echo -en "\033[12A"
sleep 1
done
echo -e "\n\033[J"
if [ `cat $file | wc -c` -eq 0 ]; then
rm $file
fi
if [ `cat "output/raw/$name.txt" | wc -l` -gt 0 ]; then
v=`ls output/$name-*.asy | sed "s|^output/$name-||;s|.asy$||" | sort -n | tail -n 1`
v=$[v+1]
mv output/raw/$name.txt output/$name-$v.asy
cd output
asy $name-$v.asy -f pdf
cd ..
fi
fi
quitter
elif [ "$1" == "all" ]; then
shift 1
parseopt "$@"
shift $?
if [ "$1" != "" ]; then
usage
fi
mkdoc
for folder in ${folders[@]}; do
alltargets="$alltargets $folder/..."
done
builder build $alltargets
builder test $alltargets
quitter
else
usage
fi
done