-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautobuild
executable file
·476 lines (375 loc) · 12.8 KB
/
autobuild
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/bin/bash
cd "$(dirname "$0")" ; here="$(pwd)"
source "$here/common"
echo 'Updating repo...'
cd "$repo" || die "unable to cd to repository at $repo"
if [ $fetch = 1 ] ; then
git fetch --tags || die "git fetch failed"
fi
git remote prune origin &> /dev/null || die "failed to prune repository"
# Initialize the output directory.
mkdir "$logs" &> /dev/null
mkdir "$bin" &> /dev/null
mkdir "$logs/branches" &> /dev/null
mkdir "$logs/commits" &> /dev/null
mkdir "$bin/branches" &> /dev/null
mkdir "$bin/commits" &> /dev/null
add_replace_rule() {
local two="$(printf '%s' "$2" | escape_sed)"
replace_rules+="s/$(printf '/mnt/overflow%s' "$1" | escape_sed)/$two/g;"
replace_rules+="s/$(printf 'Z:%s' "$1" | tr '/' '\\' | escape_sed)/$two/g;"
replace_rules+="s/$(printf 'z:%s' "$1" | tr '/' '\\' | escape_sed)/$two/g;"
replace_rules+="s/$(printf 'Z:%s' "$1" | sed 's:/:\\\\:g' | escape_sed)/$two/g;"
replace_rules+="s/$(printf 'z:%s' "$1" | sed 's:/:\\\\:g' | escape_sed)/$two/g;"
replace_rules+="s/$(printf '%s' "$1" | escape_sed)/$two/g;"
replace_rules+="s/$(printf 'mnt/overflow%s' "$1" | escape_sed)/$two/g;"
replace_rules+="s/$(printf '%s' "${1#/}" | escape_sed)/$two/g;"
}
# Shorten long paths and make them independent of where arxbuild is installed.
process_paths() {
local replace_rules=''
add_replace_rule "$build" "Build"
add_replace_rule "$repo" "$project_name"
add_replace_rule "$toolchains" "Toolchains"
add_replace_rule "$deps/msvc/drive_c" "C:"
add_replace_rule "$deps" "Deps"
add_replace_rule "$here/" ""
exec sed -e "${replace_rules[@]}"
}
escape() {
printf '%s' "$1" | sed "s:[^a-zA-Z0-9/_.,=+\\-]:\\\\&:g"
}
escape_args() {
local prefix=''
for arg in "$@" ; do
printf "$prefix"
escape "$arg"
prefix=' '
done
}
msvc_started=0
set_branch_commit() {
local branch_dst="$1"
local buildid="$2"
local commit="$3"
local bin_branch="$branch_bindst/$buildid"
local bin_commit="$commit_bindst/$buildid"
if [ -e "$bin_commit" ] \
&& ( [ ! -e "$bin_branch" ] \
|| [ "$(readlink -f "$bin_branch")" != "$(readlink -f "$bin_commit")" ] \
) ; then
rm "$bin_branch" &> /dev/null
ln -s --relative "$bin_commit" "$bin_branch"
touch "$branch_bindst"
fi
local ref_branch="$branch_dst/$buildid.ref"
local old_branch="$branch_dst/$buildid.old"
if [ -e "$ref_branch" ] ; then
local old_commit="$(cat "$ref_branch")"
[ "$old_commit" = "$commit" ] && return # nothing changed
mv "$ref_branch" "$old_branch"
fi
echo "$commit" > "$ref_branch"
touch "$branch_dst"
}
do_build() {
# Clear the build directory.
local _build="$build"
local build="$_build/$buildid"
[ -d "$build" ] || mkdir "$build" || die "${esc}[32m[$buildid]${esc}[0m Could not create build directory!"
find "$build" -mindepth 1 -delete
# Clear the build directory.
local work="$build/work"
mkdir "$work" || die "${esc}[32m[$buildid]${esc}[0m Could not create work directory!"
# Final log file locations.
local stage="$build/stage"
mkdir "$stage" || die "${esc}[32m[$buildid]${esc}[0m Could not create stage directory!"
local dst_raw="$stage/$buildid-raw.txt"
local dst_config="$stage/$buildid-config.txt"
local dst_options="$stage/$buildid-options.txt"
local dst_time="$stage/$buildid-time.txt"
cd "$work"
export CFLAGS="$cflags"
export CXXFLAGS="$cxxflags"
export LDFLAGS="$ldflags"
local _make_command="$(eval escape_args "${make_command[$compiler]:-"make $makeopts"}")"
echo "${esc}[32m[$buildid]${esc}[0m Configuring..."
local _config_command="$config --no-warn-unused-cli $(escape "$repo/$path")"
_config_command+=" -DCMAKE_INSTALL_PREFIX=/"
_config_command="$(eval escape_args "$_config_command")"
if [ ! "${config##*#}" = '' ] ; then
_config_command+=" $options"
fi
rm -f "$dst_options"
[ -z "$cflags" ] || echo "export CFLAGS=\"$(eval escape_args $cflags)\"" >> "$dst_options"
[ -z "$cxxflags" ] || echo "export CXXFLAGS=\"$(eval escape_args $cxxflags)\"" >> "$dst_options"
[ -z "$ldflags" ] || echo "export LDFLAGS=\"$(eval escape_args $ldflags)\"" >> "$dst_options"
echo "$_config_command" | process_paths >> "$dst_options"
echo "$_make_command" | process_paths >> "$dst_options"
eval "_config_cmd=(
$_config_command
)"
if [ "${_config_cmd[0]}" = 'cmake' ] || [[ "${_config_cmd[0]}" == *'/cmake' ]]
then "${_config_cmd[0]}" --version > "$dst_config"
else touch "$dst_config"
fi
eval "$_config_command" 2>&1 | process_paths >> "$dst_config"
if [ ! ${PIPESTATUS[0]} = 0 ] ; then
echo "${esc}[32m[$buildid]${esc}[0m ${esc}[31mConfigure failed!${esc}[0m"
echo -e "\nConfigure.h:0:0: error: Configure error, see config log.\n\n" > "$dst_raw"
else
echo "${esc}[32m[$buildid]${esc}[0m Building..."
if [ -f "compile_commands.json" ] && [[ ! $_make_command == 'make '* ]] ; then
# TODO find a way to generate files but don't run compile commands
eval "make $makeopts &> /dev/null"
fi
{
time "$toolchains/run.cpp" --tty sh -c "exec $_make_command" \
| process_paths > "$dst_raw"
} &> "$dst_time"
if [ "$(grep -P "make\\: \\*\\*\\* \\[[a-z]*\\] Interrupt" "$dst_raw")" ] ; then
die "${esc}[32m[$buildid]${esc}[0m Build cancelled!"
fi
# Save the resulting binaries.
rm -rf "$commit_bindst/$buildid" &> /dev/null
mkdir "$commit_bindst/$buildid" &> /dev/null
if [ "$_make_command" = 'make' ] || [ "$_make_command" = 'make -k' ] ; then
DESTDIR="$commit_bindst/$buildid/" make -k install &> /dev/null
fi
fi
local results="$build/results"
if [ -d "$results" ] ; then
result_dirs=( "$results"/* )
if [ ${#result_dirs[@]} = 1 ] && [ -d "${result_dirs[0]}" ] ; then
mv "${result_dirs[0]}" "$stage/$buildid-results"
elif [ ${#result_dirs[@]} -gt 0 ] ; then
mv "$results" "$stage/$buildid-results"
fi
fi
if [ -f "$stage/$buildid-results/index.html" ] ; then
sed -i 's!^.*<th>\(User\|Working Directory\):</th>.*$!!' \
"$stage/$buildid-results/index.html"
for f in $(find "$stage/$buildid-results" -type f) ; do
cp "$f" "$build/tmpfile"
cat "$build/tmpfile" | process_paths > "$f"
done
fi
# Record build for branch
touch "$branch_dst"
set_branch_commit "$branch_dst" "$buildid" "$commit"
# Record build for commit
touch "$commit_dst"
[ -e "$ref_commit" ] && rm "$ref_commit"
rmbuilddir "$commit_dst/$buildid"
mv "$stage"/* "$commit_dst/"
echo "$commit" > "$ref_commit"
[ -e "$force_commit" ] && rm "$force_commit"
rm -r "$build"
echo "${esc}[1;32m[$buildid]${esc}[0m Done."
}
build_done() {
_builds_done=$(($_builds_done + ${1:-1}))
_builds_running=1
[ $jobs -gt 1 ] && _builds_running="${#pids[@]}"
printf '\033]2;%s%s%s\007' "Building $project branch $branch: " \
"$_builds_done/$_builds_total done, " \
"$_builds_running/$(($_builds_total - $_builds_done)) running" > /dev/tty
}
build_wait() {
while true ; do
for pid in "${!pids[@]}"; do
if ! kill -0 "$pid" &> /dev/null ; then
build_done
unset pids[$pid]
break 2
fi
done
sleep 0.1
done
# Update statistics and indices.
"$here/buildstats" "$project" &> /dev/null
}
build() {
local compiler="$1"
buildid="${compiler}"
local options="$default_cmake_options"
local config="${config_command[$compiler]}"
if [ ! -z "${cmake_options[$compiler]}" ] ; then
[ ! -z "$options" ] && local options="$options "
local options="$options${cmake_options[$compiler]}"
fi
local cflags="${compiler_options[$compiler]}"
local cxxflags="${compiler_options[$compiler]}"
local ldflags="${linker_options[$compiler]}"
local prefixes="${prefix_paths[$compiler]}"
if [ ! -z "${c_compiler_options[$compiler]}" ] ; then
[ -z "$cflags" ] || cflags="$cflags "
cflags="$cflags${c_compiler_options[$compiler]}"
fi
if [ ! -z "${cxx_compiler_options[$compiler]}" ] ; then
[ -z "$cxxflags" ] || cxxflags="$cxxflags "
cxxflags="$cxxflags${cxx_compiler_options[$compiler]}"
fi
local disabled=''
in_array "$compiler" "${disabled_configs[@]}" && disabled="$compiler"
for arg in "${@:2}" ; do
local buildid="${buildid}_$arg"
if [ ! -z "${config_command[$arg]}" ] ; then
[ -z "$config" ] || die '$config_command can only be set once!'
config="${config_command[$arg]}"
fi
if [ ! -z "${cmake_options[$arg]}" ] ; then
[ -z "$options" ] || options="$options "
options="$options${cmake_options[$arg]}"
fi
if [ ! -z "${compiler_options[$arg]}" ] ; then
[ -z "$cflags" ] || cflags="$cflags "
cflags="$cflags${compiler_options[$arg]}"
fi
if [ ! -z "${c_compiler_options[$arg]}" ] ; then
[ -z "$cflags" ] || cflags="$cflags "
cflags="$cflags${c_compiler_options[$arg]}"
fi
if [ ! -z "${compiler_options[$arg]}" ] ; then
[ -z "$cxxflags" ] || cxxflags="$cxxflags "
cxxflags="$cxxflags${compiler_options[$arg]}"
fi
if [ ! -z "${cxx_compiler_options[$arg]}" ] ; then
[ -z "$cxxflags" ] || cxxflags="$cxxflags "
cxxflags="$cxxflags${cxx_compiler_options[$arg]}"
fi
if [ ! -z "${linker_options[$arg]}" ] ; then
[ -z "$ldflags" ] || ldflags="$ldflags "
ldflags="$ldflags${linker_options[$arg]}"
fi
if [ ! -z "${prefix_paths[$arg]}" ] ; then
[ -z "$prefixes" ] || prefixes="$prefixes;"
prefixes="$prefixes${prefix_paths[$arg]}"
fi
if in_array "$arg" "${disabled_configs[@]}" ; then
[ -z "$disabled" ] || disabled="$disabled, "
disabled="$disabled$arg"
fi
done
[ -z "$config" ] && config='cmake'
if [ "$prefixes" != "" ] ; then
options="$options \"-DCMAKE_PREFIX_PATH=$prefixes\""
fi
local ref_commit="$commit_dst/$buildid.ref"
local force_commit="$commit_dst/$buildid.force"
if [ -e "$ref_commit" ] && [ ! -e "$force_commit" ] ; then
set_branch_commit "$branch_dst" "$buildid" "$commit"
build_done
return
fi
if [ ! -z "$disabled" ] ; then
echo "${esc}[32m[$buildid]${esc}[0m Ignoring ($disabled disabled)"
build_done
return
fi
# Wait for jobs to complete
if [ $jobs -gt 1 ] && [ ! ${#pids[@]} -lt $jobs ] ; then
build_wait
fi
case "$compiler" in msvc-*) if [ $msvc_started = 0 ] ; then
echo "${esc}[36mStarting MSVC environment${esc}[0m"
"$toolchains/msvcrun" --start
msvc_started=1
fi ; esac
if [ $jobs -gt 1 ] ; then
do_build &
local pid=$!
pids[$pid]=1
build_done 0
else
do_build
build_done
"$here/buildstats" "$project" &> /dev/null
fi
}
stop_builds() {
trap - SIGHUP SIGINT SIGTERM
echo 'Interrupted!'
kill 0
exit 1
}
build_branch() {
branch="$1"
[ ! -z "$selected_branch" ] && [ ! "$branch" = "$selected_branch" ] && return
# Abort if the branch is empty
if [ ! "$branch" ] ; then return ; fi
cd "$repo"
git reset --hard &> /dev/null
git checkout "origin/$branch" &> /dev/null \
|| git checkout "tags/$branch" &> /dev/null \
|| die "error checking out branch $branch"
commit=`cat "$repo/.git/HEAD"`
# Abort if the commit is empty
if [ ! "$commit" ] ; then return ; fi
echo "Checked out branch ${esc}[36m'$branch'${esc}[0m at commit ${esc}[34m$commit${esc}[0m"
# Clear the build directory.
local _build="$build"
local build="$_build/$branch"
[ -d "$build" ] || mkdir "$build" || die "Could not create branch build directory!"
find "$build" -mindepth 1 -delete
# Prepare the output directories.
branch_dst="$logs/branches/$branch"
commit_dst="$logs/commits/$commit"
mkdir "$branch_dst" &> /dev/null
mkdir "$commit_dst" &> /dev/null
branch_bindst="$bin/branches/$branch"
commit_bindst="$bin/commits/$commit"
mkdir "$branch_bindst" &> /dev/null
mkdir "$commit_bindst" &> /dev/null
echo "file://$branch_dst/index.html"
# Build with different configurations.
unset pids
declare -gA pids
[ $jobs -gt 1 ] && trap "stop_builds" SIGHUP SIGINT SIGTERM
_builds_total="${#build_types[@]}"
_builds_done=0
build_done 0
for build_type in "${build_types[@]}" ; do
local args;
parse_build_type "$build_type" 'args'
build "${args[@]}"
done
if [ $jobs -gt 1 ] ; then
while [ ! ${#pids[@]} = 0 ] ; do
build_wait
done
wait # just to be sure
fi
[ $jobs -gt 1 ] && trap - SIGHUP SIGINT SIGTERM
}
# Clear the build directory.
build="$build/$project"
[ -d "$build" ] || mkdir "$build" || die "Could not create project build directory!"
find "$build" -mindepth 1 -delete
if [ $build_latest_tag = 1 ] ; then
latest_tag="$(
git --git-dir="$repo/.git" describe --tags $(git --git-dir="$repo/.git" rev-list --tags --max-count=1)
)"
[ -z "$latest_tag" ] || build_branch "$latest_tag"
fi
# Build all remote branches.
for f in `dir -d $repo/.git/logs/refs/remotes/origin/* 2> /dev/null` ; do
branch="${f##*/}"
case "$branch" in
*.*-branch) ;;
*)
if ! in_array "$branch" "${allowed_branches[@]}" ; then
# Ignore this branch
continue
fi
;;
esac
build_branch "$branch"
done
if [ $msvc_started = 1 ] ; then
echo "${esc}[36mStopping MSVC environment${esc}[0m"
"$toolchains/msvcrun" --stop
msvc_started=0
fi
printf '\033]2;%s\007' "Done building $project" > /dev/tty
echo "Done, logs saved to $logs"