-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbash_utils
More file actions
executable file
·558 lines (457 loc) · 14.3 KB
/
bash_utils
File metadata and controls
executable file
·558 lines (457 loc) · 14.3 KB
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
#!/bin/bash
################################################################################
#
# Installation script for cocosim dependencies (lustrec, zustre, kind2).
#
# Author: Maxime Arthaud <maxime.arthaud@nasa.gov>
#
# Copyright (c) 2019 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
################################################################################
function init_colors() {
if (( use_colors )); then
coff="\033[0m"
cbold="\033[1m"
cred="\033[31m"
cgreen="\033[32m"
cyellow="\033[33m"
cblue="\033[34m"
cpurple="\033[35m"
ccyan="\033[36m"
cwhite="\033[37m"
else
coff=""
cbold=""
cred=""
cgreen=""
cyellow=""
cblue=""
cpurple=""
ccyan=""
cwhite=""
fi
}
#####################
# General functions #
#####################
function usage() {
echo "usage: $progname [OPTION]..."
echo ""
echo "Build and install cocosim dependencies without root access."
echo ""
echo "Defaults for the options are specified in brackets."
echo ""
echo "Configuration:"
echo " --prefix=PREFIX install files in PREFIX"
echo " --builddir=BUILDDIR configure for building on BUILDDIR [$build_dir]"
echo ""
echo "Optional arguments:"
echo " -h, --help display this help and exit"
echo " -v, --verbose make this script more verbose"
echo " -f, --force force"
echo " --no-colors disable colors"
echo " --jobs=N allow N jobs at once [$njobs]"
echo " --lustrec-url=URL lustrec git repository url [$lustrec_url]"
echo " --lustrec-branch=BR lustrec branch [$lustrec_branch]"
echo " --zustre-url=URL zustre git repository url [$zustre_url]"
echo " --zustre-branch=BR zustre branch [$zustre_branch]"
echo " --kind2-url=URL kind2 git repository url [$kind2_url]"
echo " --kind2-branch=BR kind2 branch [$kind2_branch]"
}
function short_help() {
echo "Try '$progname -h' for more information." >&2
}
function error() {
echo "$progname: error: $1" >&2
}
# Split command line arguments, i.e:
# -ab -> -a -b
# --foo=bar -> --foo bar
#
# Split arguments are stored in the ARGS array
#
# Parameters:
# $1,$2,$3,...,$n: arguments to split
function explode_args() {
unset ARGS
local arg=$1 key value
while [[ $arg ]]; do
[[ $arg = "--" ]] && ARGS+=("$@") && break
# Short options
if [[ ${arg:0:1} = "-" && ${arg:1:1} != "-" ]]; then
ARGS+=("-${arg:1:1}")
(( ${#arg} > 2 )) && arg="-${arg:2}" || { shift; arg=$1; }
# Long options
elif [[ ${arg:0:2} = "--" ]]; then
# Split argument at '=':
# e.g --foo=bar -> key=--foo, value=bar
key=${arg%%=*}; value=${arg#*=}
ARGS+=("$key")
[[ "$key" != "$value" ]] && ARGS+=("$value")
shift; arg=$1
else
ARGS+=("$arg"); shift; arg=$1
fi
done
}
# Return a concatenation of strings separated by a given separator, i.e:
# , a b c -> a,b,c
#
# Parameters:
# $1: separator
# $2,$2,$4,...,$n: arguments to join
function join() {
local sep=$1; shift
echo -n "$1"; shift
printf "%s" "${@/#/$sep}"
}
# Check if a command exists
function command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Return the absolute path of the given filename
function abs_path() {
local arg=${1/#\~/$HOME}
local dirname=$(dirname "$arg") basename=$(basename "$arg")
while [[ ! -d "$dirname" ]]; do
basename="$(basename "$dirname")/$basename"
dirname=$(dirname "$dirname")
done
pushd . >/dev/null
cd "$dirname"
dirname=$(pwd)
popd >/dev/null
if [[ "$dirname" = "/" ]]; then
echo -n "/$basename"
else
echo -n "$dirname/$basename"
fi
}
# Find a pattern within the standard input
#
# If found, return 0 and print the given captured group
# Otherwise, return 1
#
# Parameters:
# $1: regular expression
# $2: parenthesis group number to capture
function match() {
while read -r line; do
if [[ "$line" =~ $1 ]]; then
echo -n "${BASH_REMATCH[$2]}"
return 0
fi
done
return 1
}
# Return the name of all binaries in the PATH matching a regular expression
function glob_binaries() {
local IFS=:
find $PATH \
-maxdepth 1 \
-regex ".*/$1" \
-exec basename {} \; 2>/dev/null | sort -u
}
########################
# Download and extract #
########################
# Download a file from an URL.
#
# The file is stored under $download_dir and is named after the remote file
function download() {
local url=$1 filename=$(basename "$1")
cd "$download_dir"
if [[ -f "$filename" ]]; then
progress "Using already downloaded $filename from $url"
else
progress "Downloading $url"
# Note: do not try to capture curl/wget output because it contains
# the progress bar
$download_agent "$url" || {
rm -f "$filename"; error "Error while fetching $filename"; exit 6;
}
fi
}
# Download a file from an URL.
#
# Parameters:
# $1: url
# $2: new name for the archive
function download_rename() {
local url=$1 filename=$2
cd "$download_dir"
if [[ -f "$filename" ]]; then
progress "Using already downloaded $filename from $url"
else
progress "Downloading $url"
# Note: do not try to capture curl/wget output because it contains
# the progress bar
$download_agent "$url" || {
rm -f "$(basename "$1")"; error "Error while fetching $filename"; exit 6;
}
mv "$(basename "$1")" "$filename"
fi
}
# Extract an archive and move the root directory
#
# Parameters:
# $1: path to the archive
# $2: destination path
function extract() {
local archive_path=$1 destination_path=$2
local archive_filename=$(basename "$archive_path")
local archive_directory=$(dirname "$archive_path")
progress "Extracting $archive_filename"
cd "$archive_directory"
run_log_debug tar xf "$archive_filename" || {
error "Error while extracting $archive_filename"; exit 7;
}
root_directory=$archive_filename
for ext in .gz .xz .bz2 .tar; do
root_directory=${root_directory%$ext}
done
[[ -d "$root_directory" ]] || assert_failed "$root_directory does not exist"
rm -rf "$destination_path"
mv "$root_directory" "$destination_path"
}
# Download an archive from an URL and extract it at the given location
#
# Parameters:
# $1: URL
# $2: destination path
function download_extract() {
local url=$1 destination_path=$2
download "$url"
extract "$download_dir/$(basename "$url")" "$destination_path"
}
# Clone a git repository in the download directory, and copy it at the given
# location
#
# Parameters:
# $1: URL
# $2: branch name (e.g, master)
# $3: destination path
function git_clone() {
local url=$1 branch=$2 destination_path=$3 dirname=$(basename "$3")
# download
cd "$download_dir"
if [[ -d "$dirname" ]]; then
progress "Using already downloaded $dirname from $url branch $branch"
cd "$dirname"
git pull
git checkout $branch
else
progress "Downloading $url branch $branch"
if (( verbose == 0 )); then
run_log_quiet git clone --depth 1 --branch "$branch" "$url" "$dirname"
else
git clone --depth 1 --branch "$branch" "$url" "$dirname"
fi || {
rm -rf "$dirname"; error "Error while fetching $dirname"; exit 6;
}
fi
# copy
rm -rf "$destination_path"
cp -r "$download_dir/$dirname" "$destination_path"
}
#####################
# Version detection #
#####################
version_regex='[0-9]+(\.[0-9a-z\-]+)*'
# Parse and detect the version of cmake, given the command name
function cmake_parse_version() {
local version_output=$("$1" --version 2>&1)
debug "'$1 --version':\n$version_output"
echo "$version_output" | match "^cmake version ($version_regex)$" 1
}
# Parse and detect the version of m4, given the command name
function m4_parse_version() {
local version_output=$("$1" --version 2>&1)
debug "'$1 --version':\n$version_output"
echo "$version_output" | match "^((GNU M4)|(m4 \(GNU M4\))) ($version_regex)$" 4
}
# Parse and detect the version of python, given the command name
function python_parse_version() {
local version_output=$("$1" --version 2>&1)
debug "'$1 --version':\n$version_output"
echo "$version_output" | match "^Python ($version_regex)$" 1
}
# Check if a python version satisfies cocosim requirements
function python_satisfies_requirements() {
local version=$1
#Check that the version that was found is greater than or equal to the required version.
[ "$(printf '%s\n' "$version" "$python_required_version" | sort -V | head -n1)" = "$python_required_version" ]
}
# Parse and detect the version of autoconf, given the command name
function autoconf_parse_version() {
local version_output=$("$1" --version 2>&1)
debug "'$1 --version':\n$version_output"
echo "$version_output" | match "^autoconf ((GNU Autoconf)) ($version_regex)$" 4
}
# Parse and detect the version of ocaml, given the command name
function ocaml_parse_version() {
local version_output=$("$1" -version 2>&1)
debug "'$1 -version':\n$version_output"
echo "$version_output" | match "^The ((OCaml)|(Objective Caml)) toplevel, version ($version_regex)$" 4
}
# Parse and detect the version of ocamlbuild, given the command name
function ocamlbuild_parse_version() {
local version_output=$("$1" -version 2>&1)
debug "'$1 -version':\n$version_output"
echo "$version_output" | match "^ocamlbuild ($version_regex)$" 1
}
# Parse and detect the version of menhir, given the command name
function menhir_parse_version() {
local version_output=$("$1" --version 2>&1)
debug "'$1 --version':\n$version_output"
echo "$version_output" | match "^menhir, version ($version_regex)$" 1
}
# Parse and detect the version of spacer, given the command name
function spacer_parse_version() {
local version_output=$("$1" --version 2>&1)
debug "'$1 --version':\n$version_output"
echo "$version_output" | match "^Z3 version ($version_regex).*$" 1
}
# Parse and detect the version of lustrec, given the command name
function lustrec_parse_version() {
local version_output=$("$1" -version 2>&1)
debug "'$1 -version':\n$version_output"
echo "$version_output" | match "^Lustrec compiler, version ($version_regex)(\-[ \n\t]*[0-9]+)? \(.+\)$" 1
}
# Parse and detect the version of kind2, given the command name
function kind2_parse_version() {
local version_output=$("$1" --version 2>&1)
debug "'$1 --version':\n$version_output"
echo "$version_output" | match "^kind2 ([a-zA-Z0-9\.\-]+)?$" 1
#echo "$version_output" | match "^kind2 v($version_regex)(\-[0-9]+)?(\-[a-zA-Z0-9]+)?$" 1
}
######################
# Output and logging #
######################
function strip_colors() {
echo -en "$1" | sed -E 's#'$(echo -en '\x1B')'\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]##g'
}
function log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$1] $(strip_colors "$2")" >> "$log_file"
}
function error_msg() {
echo -e "${cbold}${cred}==> Error: $1${coff}"
log error "$1"
}
function warning() {
echo -e "${cbold}${cyellow}==> Warning: $1${coff}"
log warning "$1"
}
function success() {
echo -e "${cbold}${cgreen}==> ${coff}${cbold}$1${coff}"
log success "$1"
}
function progress() {
echo -e "${cbold}${cblue}==> ${coff}${cbold}$1${coff}"
log progress "$1"
}
function info() {
echo -e "$1${coff}"
log info "$1"
}
function debug() {
if (( verbose >= 1 )); then
echo -e "$1${coff}" >&2
fi
log debug "$1"
}
function assert_failed() {
error_msg "Assertion failed: $1";
exit 3;
}
function error_parse_version() {
error_msg "Unable to detect the version of '$1'";
#exit 5;
}
function error_patch() {
error_msg "Error while patching $1. see $log_file_orig for more details."
#exit 8
}
function error_configure() {
if (( verbose == 0 )); then
error_msg "Error while configuring $1. see $log_file_orig for more details."
else
error_msg "Error while configuring $1."
fi
# exit 9
}
function error_make() {
if (( verbose == 0 )); then
error_msg "Error while building $1. see $log_file_orig for more details."
else
error_msg "Error while building $1."
fi
# exit 10
}
function error_install() {
if (( verbose == 0 )); then
error_msg "Error while installing $1. see $log_file_orig for more details."
else
error_msg "Error while installing $1."
fi
# exit 11
}
function error_test() {
if (( verbose == 0 )); then
error_msg "Error while testing $1. see $log_file_orig for more details."
else
error_msg "Error while testing $1."
fi
#exit 12
}
# log everything, display everything
function run_log_debug() {
"$@" 2>&1 | tee -a "$log_file" >&2
return ${PIPESTATUS[0]}
}
# log everything, only display stderr
function run_log_verbose() {
{ "$@" >> "$log_file" 2>&3; } 3>&1 | tee -a "$log_file" >&2
return ${PIPESTATUS[0]}
}
# log everything, do not display anything
function run_log_quiet() {
"$@" >> "$log_file" 2>&1
}
function progress_run() {
progress "$*"
if (( verbose >= 2 )); then
run_log_debug "$@"
elif (( verbose == 1 )); then
run_log_verbose "$@"
else
run_log_quiet "$@"
fi
}
######################
# Activation scripts #
######################
function prepend_path() {
PATH="$1:$PATH"
act_prepend_path+=("$1")
}
function prepend_manpath() {
export MANPATH="$1${MANPATH:+:$MANPATH}"
act_prepend_manpath+=("$1")
}
function prepend_infopath() {
export INFOPATH="$1${INFOPATH:+:$INFOPATH}"
act_prepend_infopath+=("$1")
}
function prepend_python_path() {
export PYTHONPATH="$1${PYTHONPATH:+:$PYTHONPATH}"
act_prepend_python_path+=("$1")
}
function generate_env_path_line() {
local var_name=$1; shift
if (( $# > 0 )); then
echo "export ${var_name}=\"$(join ':' "$@")\${${var_name}:+:\$${var_name}}\""
fi
}