-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·286 lines (235 loc) · 4.98 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
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
#!/bin/bash
# build.sh - build script for morelia
set -f
set -eu
compiler=gcc
analyzer=clang
lang=python
copt='-O1 -march=native'
# -fanalyzer
cdiag='-Wall -Wextra -Wshadow -Wundef -Wno-unused -Wno-padded -Wno-char-subscripts -Werror'
cxdiag='-Wstack-usage=8192'
ctdiag='-Wstack-usage=32768'
cfmt='-fmax-errors=60 -fno-diagnostics-show-caret -fno-diagnostics-show-option -fno-diagnostics-color -fcompare-debug-second'
#cdbg='-g1 -fsanitize=address,undefined,signed-integer-overflow,bounds -fno-sanitize-recover=all -ftrapv -fstack-protector'
cdbg='-g1 -fno-stack-protector -fno-wrapv -fcf-protection=none -fno-stack-clash-protection -fno-asynchronous-unwind-tables -U_FORTIFY_SOURCE'
#UBSAN_OPTIONS=print_stacktrace=1
#cxtra='-std=c11 -funsigned-char -static -specs /home/joris/lib/musl-gcc.specs'
cxtra='-std=c11 -funsigned-char'
cflags="$copt $cdiag $cfmt $cdbg $cxtra"
lflags="-O1 -fuse-ld=gold $cdbg $cxtra -lm"
# --gc-sections --no-ld-generated-unwind-info -z stack-size=1234
anacdia='--analyze --analyzer-output text -Weverything -Wno-implicit-int-conversion -Wunused -Wno-sign-conversion -Wno-padded -Wno-char-subscripts -Werror=format'
anacfmt='-fno-caret-diagnostics -fno-color-diagnostics -fno-diagnostics-show-option -fno-diagnostics-fixit-info -fno-diagnostics-show-note-include-stack -std=c11 -funsigned-char'
anacflags="$anacdia $anacfmt"
asmcflags='-fverbose-asm -frandon-seed=0'
# egrep -o -h -E 'enum [A-Za-z]+ {' *.h | sort
dryrun=0
always=0
ana=0
map=0
dolgen=0
dosgen=0
vrb=0
valgrind=0
target=''
usage()
{
echo 'usage: build [options] [target]'
echo
echo '-a - analyze'
echo '-g - run generators'
echo '-n - dryrun'
echo '-m - create map file'
echo '-u - unconditional'
echo '-v - verbose'
echo '-vg - valgrind'
echo '-h - help'
echo '-L - build license'
echo
echo 'target - only build given target'
}
error()
{
echo $1
exit 1
}
verbose()
{
if [ $vrb -eq 0 ]; then
echo $1
else
echo $2
fi
}
cc()
{
local src
local tgt
local dep
local mtime
local newer
tgt="$1"
src="$2"
newer=$always
shift
if [ -f "$tgt" ]; then
for dep in "$@"; do
[ -f $dep ] || error "$dep does not exist"
if [ "$dep" -nt "$tgt" ]; then
newer=1
fi
done
else
newer=1
fi
if [ $newer -eq 1 ]; then
verbose "$compiler -c $src" "$compiler -c $cflags $src"
if [ $dryrun -eq 0 ]; then
$compiler -c $cflags $cxdiag $src
fi
fi
if [ "$tgt" = "$target" ]; then
exit 0
fi
}
tc()
{
local src
local tgt
local def
local dep
local mtime
local newer
def="$1"
tgt="$2"
src="$3"
newer=$always
shift 2
if [ -f "$tgt" ]; then
for dep in "$@"; do
if [ -f "$dep" -a "$dep" -nt "$tgt" ]; then
newer=1
fi
done
else
newer=1
fi
if [ $newer -eq 1 ]; then
echo "$def $compiler -c $src"
if [ $dryrun -eq 0 ]; then
$compiler -c $cflags $ctdiag $src
fi
fi
if [ "$tgt" = "$target" ]; then
exit 0
fi
}
ld()
{
local tgt
local dep
local mtime
local newer
tgt="$1"
if [ $ana -eq 1 ]; then
return 0
fi
newer=$always
shift
if [ $newer -eq 0 -a -f "$tgt" ]; then
for dep in "$@"; do
if [ "$dep" = "-lm" ]; then
continue;
fi
if [ -f "$dep" -a "$dep" -nt "$tgt" ]; then
newer=1
fi
done
else
newer=1
fi
if [ $newer -eq 1 ]; then
echo "ld -o $tgt $@"
if [ $dryrun -eq 0 ]; then
$compiler -o $tgt $lflags $@
if [ $map -eq 1 ]; then
nm --line-numbers -S -r --size-sort $tgt > $tgt.map
fi
fi
fi
if [ "$tgt" = "$target" ]; then
exit 0
fi
}
run()
{
local tgt
local dep
local alw
local cmd
local mtime
local newer
local args
tgt="$1"
dep="$2"
alw="$3"
cmd="$4"
args="$5"
uncond=""
newer=$alw
if [ $alw -eq 1 ]; then
uncond="-u"
fi
if [ $ana -eq 1 ]; then
return 0
fi
if [ $newer -eq 0 -a -f "$tgt" ]; then
if [ -f "$dep" -a "$dep" -nt "$tgt" ]; then
echo "$dep > $tgt"
newer=1
fi
if [ -f "$cmd" -a "$cmd" -nt "$tgt" ]; then
echo " $cmd > $tgt"
newer=1
fi
else
newer=1
fi
if [ $newer -eq 1 ]; then
echo "run $cmd $uncond $args"
if [ $dryrun -eq 0 ]; then
$cmd $uncond $args
fi
fi
if [ "$tgt" = "$target" ]; then
exit 0
fi
}
mklic()
{
echo 'objcopy --add-section license=License.txt'
}
while [ $# -ge 1 ]; do
case "$1" in
'-a') ana=1
# always=1
compiler=$analyzer
cflags=$anacflags
cflags_t=$anacflags ;;
'-g') dolgen=1; dosgen=1 ;;
'-h'|'-?') usage ;;
'-l') lang=$2; shift ;;
'-L') mklic ;;
'-n') dryrun=1 ;;
'-m') map=1 ;;
'-u') always=1 ;;
'-v') vrb=1 ;;
'-vg') valgrind=1; cflags="$cflags -DVALGRIND" ;;
*) target="$1" ;;
esac
shift
done
cc morelia.o morelia.c base.h
ld morelia morelia.o
# ~/bin/valgrind -s --redzone-size=4096 --exit-on-first-error=yes --error-exitcode=1 --track-fds=yes --leak-check=no --partial-loads-ok=no --track-origins=yes --malloc-fill=55 $*