-
Notifications
You must be signed in to change notification settings - Fork 68
/
run.sh
executable file
·589 lines (540 loc) · 17.4 KB
/
run.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#!/usr/bin/env bash
set -o errexit
set -o errtrace
set -o pipefail
set -o nounset
readonly BINTRAY_API="https://api.bintray.com/content"
readonly DEFAULT_STON_CONFIG="smalltalk.ston"
readonly GITHUB_REPO_URL="https://github.com/hpi-swa/smalltalkCI"
# Indicates whether any secondary action was chosen that makes the execution
# of the actual build process optional. See ensure_ston_config_exists.
first_action=""
################################################################################
# Locate $SMALLTALK_CI_HOME and load helpers.
################################################################################
initialize() {
local resolved_path
# Set up traps, otherwise fail if OS is not supported
case "$(uname -s)" in
"Linux"|"Darwin"|"CYGWIN_NT-"*|"MINGW64_NT-"*|"MSYS_NT-"*)
trap handle_exit EXIT
trap handle_error ERR
trap handle_interrupt INT
;;
*)
echo "Unsupported platform '$(uname -s)'." 1>&2
exit 1
;;
esac
if [[ "$@" = *--self-test* ]]; then
# Unset all `SMALLTALK_CI_*` environment variables for self testing
for var in ${!SMALLTALK_CI_@}; do
unset "${var}"
done
fi
if [[ -z "${SMALLTALK_CI_HOME:-}" ]]; then
# Try to determine absolute path to smalltalkCI
export SMALLTALK_CI_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [[ ! -f "${SMALLTALK_CI_HOME}/run.sh" ]]; then
# Try to resolve symlink
case "$(uname -s)" in
"Linux")
resolved_path="$(readlink -f "${BASH_SOURCE[0]}")" || true
;;
"Darwin")
resolved_path="$(readlink "${BASH_SOURCE[0]}")" || true
;;
esac
SMALLTALK_CI_HOME=$(dirname "${resolved_path}")
fi
if [[ ! -f "${SMALLTALK_CI_HOME}/run.sh" ]]; then
echo "Unable to locate smalltalkCI directory." 1>&2
exit 1
fi
# Load environment variables
source "${SMALLTALK_CI_HOME}/env_vars"
fi
if [[ ! -f "${SMALLTALK_CI_HOME}/run.sh" ]]; then
echo "smalltalkCI could not be initialized." 1>&2
exit 1
fi
# Load helpers
source "${SMALLTALK_CI_HOME}/helpers.sh"
smalltalk_ci_start_time=$(timer_nanoseconds)
}
################################################################################
# Exit handler.
################################################################################
handle_exit() {
local error_code=$?
report_build_metrics "${error_code}"
exit "${error_code}"
}
################################################################################
# Print error information and exit.
################################################################################
handle_error() {
local error_code=$?
local num_crash_lines=250
local i
if is_file "${SMALLTALK_CI_BUILD}/crash.dmp"; then
print_notice "Found a crash.dmp. Here are the first ${num_crash_lines} lines:"
head -n ${num_crash_lines} "${SMALLTALK_CI_BUILD}/crash.dmp"
fi
printf "\n"
print_notice "Error with status code ${error_code}:"
i=0
while caller $i;
do ((i++));
done
printf "===================================================================\n"
print_config
exit "${error_code}"
}
################################################################################
# Print notice on interrupt and exit.
################################################################################
handle_interrupt() {
print_notice $'\nsmalltalkCI has been interrupted. Exiting...'
exit 1
}
################################################################################
# Ensure $config_ston is an existing STON file.
# Locals:
# config_ston
# Arguments:
# Custom project home path
################################################################################
ensure_ston_config_exists() {
local custom_ston=$1
# STON provided as cmd line parameter can override $config_ston
if ! is_empty "${custom_ston}"; then
if [[ ${custom_ston: -5} != ".ston" ]] || ! is_file "${custom_ston}"; then
print_error_and_exit "User-provided configuration is not a STON-file or \
could not be found at '${custom_ston}'."
fi
config_ston="${custom_ston}"
# Expand path if $config_ston does not start with / or ~
if ! [[ "${config_ston:0:1}" =~ (\/|\~) ]]; then
config_ston="$(pwd)/${config_ston}"
fi
return
fi
if is_travis_build; then
if is_not_empty "${TRAVIS_SMALLTALK_CONFIG:-}"; then
# If the variable is a list (ruby array like ["a", "b", "c"]) extract the first value
# This is a workaround for https://github.com/hpi-swa/smalltalkCI/issues/448
first_config="$(echo ${TRAVIS_SMALLTALK_CONFIG//[\[\]]} | awk -F',' '{print $1}')"
config_ston="${TRAVIS_BUILD_DIR}/${first_config}"
else
locate_ston_config
fi
elif is_file "${config_ston}"; then
# Make sure $config_ston does not start with ./
config_ston="${config_ston#./}"
# Expand path if $config_ston does not start with / or ~
if ! [[ "${config_ston:0:1}" =~ (\/|\~) ]]; then
config_ston="$(pwd)/${config_ston}"
fi
else
locate_ston_config
fi
# Resolve absolute path if necessary
if [[ "${config_ston:0:1}" != "/" ]]; then
case "$(uname -s)" in
"Linux")
config_ston="$(readlink -f "${config_ston}")" || true
;;
"Darwin")
config_ston="$(readlink "${config_ston}")" || true
;;
esac
fi
if ! is_empty "${first_action}"; then
if ! is_file "${config_ston}"; then
exit
fi
read -p "Continue with build progress? (y/N): " user_input
[[ "${user_input}" != "y" ]] && exit 0
elif ! is_file "${config_ston}"; then
print_error_and_exit "STON configuration could not be found at \
'${config_ston}'."
fi
}
################################################################################
# Allow STON config filename to start with a dot.
# Locals:
# config_ston
# Globals:
# TRAVIS_BUILD_DIR
################################################################################
locate_ston_config() {
local project_home
if is_travis_build; then
project_home="${TRAVIS_BUILD_DIR}"
else
project_home="$(pwd)"
fi
if is_file "${project_home}/${DEFAULT_STON_CONFIG}"; then
config_ston="${project_home}/${DEFAULT_STON_CONFIG}"
elif is_file "${project_home}/.${DEFAULT_STON_CONFIG}"; then
config_ston="${project_home}/.${DEFAULT_STON_CONFIG}"
else
if ! is_empty "${first_action}"; then
exit
fi
print_error_and_exit "No STON file named '.${DEFAULT_STON_CONFIG}' found \
in ${project_home}."
fi
}
################################################################################
# Select Smalltalk image interactively if not already selected.
# Locals:
# config_smalltalk
################################################################################
select_smalltalk() {
local images="Squeak64-trunk Squeak64-6.0 Squeak64-5.3 Squeak64-5.2 Squeak64-5.1
Squeak32-trunk Squeak32-6.0 Squeak32-5.3 Squeak32-5.2 Squeak32-5.1 Squeak32-5.0
Squeak32-4.6 Squeak32-4.5
Pharo64-stable Pharo64-alpha Pharo64-13 Pharo64-12 Pharo64-11 Pharo64-10 Pharo64-9.0 Pharo64-8.0 Pharo64-7.0 Pharo64-6.1 Pharo64-6.0
Pharo32-stable Pharo32-alpha Pharo32-13 Pharo32-12 Pharo32-9.0 Pharo32-8.0 Pharo32-7.0 Pharo32-6.0 Pharo32-5.0
Pharo32-4.0 Pharo32-3.0
GemStone64-3.6.5 GemStone64-3.6.0 GemStone64-3.5.8 GemStone64-3.5.3
GToolkit64-release
Moose64-trunk Moose64-12 Moose64-11 Moose64-10 Moose64-9.0 Moose64-8.0"
if is_not_empty "${config_smalltalk}"; then
return
fi
if is_travis_build || is_appveyor_build; then
config_smalltalk="${TRAVIS_SMALLTALK_VERSION:-${SMALLTALK}}"
return
fi
# Ask user to choose an image if one has not been selected yet
if is_empty "${config_smalltalk}"; then
PS3="Choose Smalltalk image: "
set -o posix # fixes SIGINT during select
select selection in $images; do
case "${selection}" in
Squeak*|Pharo*|GemStone*|GToolkit*|Moose*)
config_smalltalk="${selection}"
break
;;
*)
print_error_and_exit "No Smalltalk image selected."
;;
esac
done
set +o posix
fi
}
################################################################################
# Validate options and exit with '1' if an option is invalid.
# Locals:
# config_smalltalk
################################################################################
validate_configuration() {
if is_empty "${config_smalltalk}"; then
print_error_and_exit "Smalltalk image is not defined."
fi
if [[ "${config_smalltalk}" != *"64-"* ]] && \
[[ "${config_smalltalk}" != *"32-"* ]]; then
print_notice 'Please consider explicitly specifying image architecture
(Example: use `Squeak64-trunk` instead of `Squeak-trunk`).'
fi
if is_empty "${config_ston}"; then
print_error_and_exit "No STON file found."
elif ! is_file "${config_ston}"; then
print_error_and_exit "STON file at '${config_ston}' does not exist."
fi
}
################################################################################
# Set options that depend on the context, i.e., the input values and selections
# performed so far.
################################################################################
set_context_options() {
if [[ "${config_force_cache}" = "true" ]]; then
print_info "Forcing cache use"
config_overwrite_cache="false"
else
case "${config_smalltalk}" in
*-alpha | *-trunk)
print_info "Forcing image update for in-development version"
config_overwrite_cache="true"
;;
*)
;;
esac
fi
}
################################################################################
# Handle user-defined options.
# Locals:
# config_clean
# config_debug
# config_headless
# config_overwrite_cache
# config_smalltalk
# config_verbose
# Arguments:
# All positional parameters
################################################################################
parse_options() {
local positional=()
while [[ $# -gt 0 ]]
do
case "$1" in
--clean)
config_clean="true"
shift
;;
-d | --debug)
config_debug="true"
shift
;;
--gs-*)
# Reserved namespace for GemStone options
shift
;;
-h | --help)
print_help
exit 0
;;
--headful | --headfull)
config_headless="false"
shift
;;
--image)
config_image="${2:-}"
if ! is_file "${config_image}"; then
print_error_and_exit "${config_image} does not exist"
fi
shift 2
;;
--force-cache)
config_force_cache="true"
shift
;;
--no-color)
config_colorful="false"
shift
;;
--no-tracking)
config_tracking="false"
shift
;;
--overwrite-cache)
config_overwrite_cache="true"
shift
;;
--print-env)
print_env
exit 0
;;
-s | --smalltalk)
config_smalltalk="${2:-}"
if is_empty "${config_smalltalk}"; then
print_error_and_exit "-s | --smalltalk option requires an argument \
(e.g. 'smalltalkci -s Squeak64-trunk')."
fi
shift 2
;;
-v | --verbose)
config_verbose="true"
shift
;;
--vm)
config_vm="${2:-}"
if ! is_file "${config_vm}"; then
print_error_and_exit "${config_vm} does not exist"
fi
shift 2
;;
-- | --self-test)
shift
;;
-*)
print_error_and_exit "Unknown option: $1"
;;
*)
positional+=("$1")
shift
;;
esac
done
if [[ "${#positional[@]}" -gt 1 ]]; then
print_error_and_exit "Too many positional arguments: '${positional[*]:-}'"
else
config_first_arg_or_empty="${positional:-}"
fi
}
################################################################################
# Make sure all required folders exist, create build folder and symlink project.
# Locals:
# config_ston
# Globals:
# SMALLTALK_CI_CACHE
# SMALLTALK_CI_BUILD_BASE
# SMALLTALK_CI_BUILD
################################################################################
prepare_folders() {
print_info "Preparing folders..."
is_dir "${SMALLTALK_CI_CACHE}" || mkdir "${SMALLTALK_CI_CACHE}"
is_dir "${SMALLTALK_CI_BUILD_BASE}" || mkdir "${SMALLTALK_CI_BUILD_BASE}"
# Create folder for this build
if is_dir "${SMALLTALK_CI_BUILD}"; then
print_info "Build folder already exists at ${SMALLTALK_CI_BUILD}."
else
mkdir "${SMALLTALK_CI_BUILD}"
fi
}
################################################################################
# Set up build environment.
################################################################################
prepare_environment() {
add_env_vars
if is_linux_build && is_sudo_enabled; then
raise_rtprio_limit
fi
}
################################################################################
# Add environment variables for in-image use (with `SCIII_` prefix).
################################################################################
add_env_vars() {
export SCIII_SMALLTALK="${config_smalltalk}"
export SCIII_BUILD="$(resolve_path "${SMALLTALK_CI_BUILD}")"
export SCIII_COLORFUL="${config_colorful}"
export SCIII_DEBUG="${config_debug}"
}
################################################################################
# Raise RTPRIO of current bash for OpenSmalltalk VMs with threaded heartbeat.
################################################################################
raise_rtprio_limit() {
if ! program_exists "gcc"; then
print_info "Unable to raise real-time priority: gcc is not available."
return
fi
fold_start set_rtprio_limit "Raising real-time priority for OpenSmalltalk VMs with threaded heartbeat..."
pushd $(mktemp -d) > /dev/null
gcc -o "set_rtprio_limit" "${SMALLTALK_CI_HOME}/utils/set_rtprio_limit.c"
chmod +x "./set_rtprio_limit"
sudo "./set_rtprio_limit" $$ || true
popd > /dev/null
fold_end set_rtprio_limit
}
################################################################################
# Run cleanup if requested by user.
# Locals:
# config_clean
################################################################################
check_clean_up() {
local user_input
if [[ "${config_clean}" != "true" ]]; then
return 0
fi
print_info "cache at '${SMALLTALK_CI_CACHE}'."
print_info "builds at '${SMALLTALK_CI_BUILD_BASE}'."
if is_dir "${SMALLTALK_CI_CACHE}" || \
is_dir "${SMALLTALK_CI_BUILD_BASE}"; then
read -p "Are you sure you want to clear builds and cache? (y/N): " user_input
if [[ "${user_input}" = "y" ]]; then
clean_up
fi
else
print_notice "Nothing to clean up."
fi
first_action="check_cleanup"
}
################################################################################
# Remove all builds and clear cache.
# Globals:
# SMALLTALK_CI_CACHE
# SMALLTALK_CI_BUILD_BASE
################################################################################
clean_up() {
print_info "Cleaning up..."
print_error "Removing the following directories:"
if is_dir "${SMALLTALK_CI_CACHE}"; then
print_info " ${SMALLTALK_CI_CACHE}"
rm -rf "${SMALLTALK_CI_CACHE}"
fi
if is_dir "${SMALLTALK_CI_BUILD_BASE}"; then
print_info " ${SMALLTALK_CI_BUILD_BASE}"
# Make sure read-only files (e.g. some GemStone files) can be removed
chmod -fR +w "${SMALLTALK_CI_BUILD_BASE}"
rm -rf "${SMALLTALK_CI_BUILD_BASE}"
fi
print_info "Done."
}
################################################################################
# Load platform-specific package and run the build.
# Locals:
# config_smalltalk
################################################################################
run() {
case "${config_smalltalk}" in
Squeak*)
print_info "Starting Squeak build..."
source "${SMALLTALK_CI_HOME}/squeak/run.sh"
;;
Pharo*|Moose*)
print_info "Starting Pharo build..."
source "${SMALLTALK_CI_HOME}/pharo/run.sh"
;;
GemStone*)
print_info "Starting GemStone build..."
source "${SMALLTALK_CI_HOME}/gemstone/run.sh"
;;
GToolkit*)
print_info "Starting GToolkit build..."
source "${SMALLTALK_CI_HOME}/gtoolkit/run.sh"
;;
*)
print_error_and_exit "Unknown Smalltalk image '${config_smalltalk}'."
;;
esac
if debug_enabled; then
fold_start display_config "Current configuration"
print_config
fold_end display_config
fi
run_build "$@"
}
################################################################################
# Main entry point. Exit with build status code.
# Arguments:
# All positional parameters
################################################################################
main() {
local config_clean="false"
local config_colorful="true"
local config_debug="false"
local config_first_arg_or_empty=""
local config_force_cache="false"
export config_headless="true"
local config_image=""
export config_overwrite_cache="false"
export config_smalltalk=""
local config_ston=""
export config_tracking="true"
local config_verbose="false"
local config_vm=""
local config_vm_dir
initialize "$@"
parse_options "$@"
[[ "${config_verbose}" = "true" ]] && set -o xtrace
check_clean_up
ensure_ston_config_exists "${config_first_arg_or_empty}"
select_smalltalk
validate_configuration
set_context_options
config_vm_dir="${SMALLTALK_CI_VMS}/${config_smalltalk}"
prepare_folders
export_coveralls_data
prepare_environment
run "$@"
finalize
}
# Run main if script is not being tested
if [[ "$(basename -- "$0")" != *"test"* ]]; then
main "$@"
fi