/
install.sh
executable file
·601 lines (500 loc) · 14.5 KB
/
install.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
590
591
592
593
594
595
596
597
598
599
600
601
#!/usr/bin/env bash
#
# Copyright: Copyright 2015 Martin Nowak.
# License: Boost License 1.0 (www.boost.org/LICENSE_1_0.txt)
# Authors: Martin Nowak
_() {
set -ueo pipefail
# ------------------------------------------------------------------------------
log() {
if [ "$verbosity" -gt 0 ]; then
echo "${@//$HOME/\~}"
fi
}
logV() {
if [ "$verbosity" -gt 1 ]; then
log "$@";
fi
}
logE() {
log "$@" >&2
}
fatal() {
logE "$@"
exit 1
}
curl() {
: ${CURL_USER_AGENT:="installer/install.sh $(command curl --version | head -n 1)"}
if [ "$verbosity" -gt 0 ]; then
command curl -f#L --retry 3 -A "$CURL_USER_AGENT" "$@"
else
command curl -fsSL --retry 3 -A "$CURL_USER_AGENT" "$@"
fi
}
# ------------------------------------------------------------------------------
command=
compiler=
verbosity=1
path=~/dlang
case $(uname -s) in
Darwin) os=osx;;
Linux) os=linux;;
FreeBSD) os=freebsd;;
*)
fatal "Unsupported OS $(uname -s)"
;;
esac
case $(uname -m) in
x86_64|amd64) arch=x86_64; model=64;;
i*86) arch=x86; model=32;;
*)
fatal "Unsupported Arch $(uname -m)"
;;
esac
check_tools() {
while [[ $# > 0 ]]; do
if ! command -v $1 &>/dev/null; then
fatal "Required tool $1 not found, please install it."
fi
shift
done
}
# ------------------------------------------------------------------------------
mkdir -p "$path"
TMP_ROOT=$(mktemp -d "$path/.installer_tmp_XXXXXX")
mkdtemp() {
mktemp -d "$TMP_ROOT/XXXXXX"
}
cleanup() {
rm -rf "$TMP_ROOT";
}
trap cleanup EXIT
# ------------------------------------------------------------------------------
usage() {
log 'Usage
install.sh [<command>] [<args>]
Commands
install Install a D compiler (default command)
uninstall Remove an installed D compiler
list List all installed D compilers
update Update this dlang script
Options
-h --help Show this help
-p --path Install location (default ~/dlang)
-v --verbose Verbose output
Run "install.sh <command> --help to get help for a specific command.
'
}
command_help() {
local _compiler='Compiler
dmd|gdc|ldc latest version of a compiler
dmd|gdc|ldc-<version> specific version of a compiler (e.g. dmd-2.069.0, ldc-0.16.1-beta2)
dmd-beta latest dmd beta
dmd-nightly latest dmd nightly
dmd-2015-11-22 specific dmd nightly
'
case $1 in
install)
log 'Usage
install.sh install <compiler>
Description
Download and install a D compiler.
Options
-a --activate Only print the path to the activate script
Examples
install.sh install dmd
install.sh dmd
install.sh install dmd-2.069.0
install.sh install ldc-0.16.1
'
log "$_compiler"
;;
uninstall)
log 'Usage
install.sh uninstall <compiler>
Description
Uninstall a D compiler.
Examples
install.sh uninstall dmd
install.sh uninstall dmd-2.069.0
install.sh uninstall ldc-0.16.1
'
log "$_compiler"
;;
list)
log 'Usage
install.sh list
Description
List all installed D compilers.
'
;;
update)
log 'Usage
install.sh update
Description
Update the dlang installer itself.
'
esac
}
# ------------------------------------------------------------------------------
parse_args() {
local _help=
local _activate=
while [[ $# > 0 ]]; do
case "$1" in
-h | --help)
_help=1
;;
-p | --path)
if [ -z "${2:-}" ]; then
fatal '-p|--path must be followed by a path.';
fi
path="$2"
;;
-v | --verbose)
verbosity=2
;;
-a | --activate)
_activate=1
;;
use | install | uninstall | list | update)
command=$1
;;
remove)
command=uninstall
;;
dmd | dmd-* | gdc | gdc-* | ldc | ldc-*)
compiler=$1
;;
esac
shift
done
if [ -n "$_help" ]; then
[ -z "$command" ] && usage || command_help $command
exit 0
fi
if [ -n "$_activate" ]; then
if [ "${command:-install}" == "install" ]; then
verbosity=0
else
[ -z "$command" ] && usage || command_help $command
exit 1
fi
fi
}
# ------------------------------------------------------------------------------
# run_command command [compiler]
run_command() {
case $1 in
install)
check_tools curl
if [ ! -f "$path/install.sh" ]; then
install_dlang_installer
fi
if [ -z "${2:-}" ]; then
fatal "Missing compiler argument for $1 command.";
fi
if [ -d "$path/$2" ]; then
log "$2 already installed";
else
install_compiler $2
fi
install_dub
write_env_vars $2
if [ $(basename $SHELL) = fish ]; then
local suffix=.fish
fi
if [ "$verbosity" -eq 0 ]; then
echo "$path/$2/activate${suffix:-}"
else
log "
Run \`source $path/$2/activate${suffix:-}\` in your shell to use $2.
This will setup PATH, LIBRARY_PATH, LD_LIBRARY_PATH, DMD, DC, and PS1 accordingly.
Run \`deactivate\` later on to restore your environment."
fi
;;
uninstall)
if [ -z "${2:-}" ]; then
fatal "Missing compiler argument for $1 command.";
fi
uninstall_compiler $2
;;
list)
if [ -n "${2:-}" ]; then
log "Ignoring compiler argument '$2' for list command.";
fi
list_compilers
;;
update)
install_dlang_installer
;;
esac
}
install_dlang_installer() {
mkdir -p "$path"
local url=https://dlang.org/install.sh
logV "Downloading $url"
curl -sS "$url" -o "$path/install.sh"
chmod +x "$path/install.sh"
log "The latest version of this script was installed as $path/install.sh.
It can be used it to install further D compilers.
Run \`$path/install.sh --help\` for usage information.
"
}
resolve_latest() {
case $compiler in
dmd)
local url=http://ftp.digitalmars.com/LATEST
logV "Determing latest dmd version ($url)."
compiler="dmd-$(curl -sS $url)"
;;
dmd-beta)
local url=http://ftp.digitalmars.com/LATEST_BETA
logV "Determing latest dmd-beta version ($url)."
compiler="dmd-$(curl -sS $url)"
;;
dmd-nightly)
local url=http://nightlies.dlang.org/LATEST_NIGHTLY
logV "Determing latest dmd-nightly version ($url)."
compiler="dmd-$(curl -sS $url)"
;;
ldc)
local url=https://ldc-developers.github.io/LATEST
logV "Determing latest ldc version ($url)."
compiler="ldc-$(curl -sS $url)"
;;
gdc)
local url=http://gdcproject.org/downloads/LATEST
logV "Determing latest gdc version ($url)."
compiler="gdc-$(curl -sS $url)"
;;
esac
}
install_compiler() {
# dmd-2.065, dmd-2.068.0, dmd-2.068.1-b1
if [[ $1 =~ ^dmd-2\.([0-9]{3})(\.[0-9])?(-.*)?$ ]]; then
local basename="dmd.2.${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"
local ver="2.${BASH_REMATCH[1]}"
if [[ $ver > "2.064z" ]]; then
basename="$basename.$os"
ver="$ver${BASH_REMATCH[2]}"
if [ $os = freebsd ]; then
basename="$basename-$model"
fi
fi
if [[ $ver > "2.068.0z" ]]; then
local arch="tar.xz"
else
local arch="zip"
fi
if [ -n "${BASH_REMATCH[3]}" ]; then # pre-release
local url="http://downloads.dlang.org/pre-releases/2.x/$ver/$basename.$arch"
else
local url="http://downloads.dlang.org/releases/2.x/$ver/$basename.$arch"
fi
download_and_unpack "$url" "$path/$1" "$url.sig"
# dmd-2015-11-20
elif [[ $1 =~ ^dmd-[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
local basename="dmd.master.$os"
if [ $os = freebsd ]; then
basename="$basename-$model"
fi
local url="http://nightlies.dlang.org/$1/$basename.tar.xz"
download_and_unpack "$url" "$path/$1" "$url.sig"
# ldc-0.12.1 or ldc-0.15.0-alpha1
elif [[ $1 =~ ^ldc-([0-9]+\.[0-9]+\.[0-9]+(-.*)?)$ ]]; then
local ver=${BASH_REMATCH[1]}
local url="https://github.com/ldc-developers/ldc/releases/download/v$ver/ldc2-$ver-$os-$arch.tar.xz"
if [ $os != linux ] && [ $os != osx ]; then
fatal "no ldc binaries available for $os"
fi
download_and_unpack "$url" "$path/$1"
# gdc-4.8.2, gdc-4.9.0-alpha1, gdc-5.2, or gdc-5.2-alpha1
elif [[ $1 =~ ^gdc-([0-9]+\.[0-9]+(\.[0-9]+)?(-.*)?)$ ]]; then
local name=${BASH_REMATCH[0]}
if [ $os != linux ]; then
fatal "no gdc binaries available for $os"
fi
case $arch in
x86_64) local triplet=x86_64-linux-gnu;;
x86) local triplet=i686-linux-gnu;;
esac
local url="http://gdcproject.org/downloads/binaries/$triplet/$name.tar.xz"
download_and_unpack "$url" "$path/$1"
url=https://raw.githubusercontent.com/D-Programming-GDC/GDMD/master/dmd-script
log "Downloading gdmd $url"
curl "$url" -o "$path/$1/bin/gdmd"
chmod +x "$path/$1/bin/gdmd"
else
fatal "Unknown compiler '$1'"
fi
}
find_gpg() {
if command -v gpg2 &>/dev/null; then
echo gpg2
elif command -v gpg &>/dev/null; then
echo gpg
else
echo "Warning: No gpg tool found to verify downloads." >&2
echo x
fi
}
# url, path, [verify]
download_and_unpack() {
local tmp=$(mkdtemp)
local name="$(basename $1)"
check_tools curl
if [[ $name =~ \.tar\.xz$ ]]; then
check_tools tar xz
else
check_tools unzip
fi
log "Downloading and unpacking $1"
curl "$1" -o "$tmp/$name"
if [ ! -z ${3:-} ]; then
verify "$3" "$tmp/$name"
fi
if [[ $name =~ \.tar\.xz$ ]]; then
tar --strip-components=1 -C "$tmp" -Jxf "$tmp/$name"
else
unzip -q -d "$tmp" "$tmp/$name"
mv "$tmp/dmd2"/* "$tmp/"
rmdir "$tmp/dmd2"
fi
rm "$tmp/$name"
mv "$tmp" "$2"
}
verify() {
: ${GPG:=$(find_gpg)}
if [ $GPG = x ]; then
return
fi
if [ ! -f "$path/d-keyring.gpg" ]; then
curl -sS https://dlang.org/d-keyring.gpg -o "$path/d-keyring.gpg"
fi
if ! $GPG -q --verify --keyring "$path/d-keyring.gpg" --no-default-keyring <(curl -sS "$1") "$2" 2>/dev/null; then
fatal "Invalid signature $1"
fi
}
write_env_vars() {
case $1 in
dmd*)
[ $os = osx ] && local suffix= || local suffix=$model
local binpath=$os/bin$suffix
local libpath=$os/lib$suffix
local dc=dmd
local dmd=dmd
;;
ldc*)
local binpath=bin
local libpath=lib
local dc=ldc2
local dmd=ldmd2
;;
gdc*)
local binpath=bin
local libpath=lib;
local dc=gdc
local dmd=gdmd
;;
esac
logV "Writing environment variables to $path/$1/activate"
cat > "$path/$1/activate" <<EOF
deactivate() {
export PATH="\$_OLD_D_PATH"
export LIBRARY_PATH="\$_OLD_D_LIBRARY_PATH"
export LD_LIBRARY_PATH="\$_OLD_D_LD_LIBRARY_PATH"
export PS1="\$_OLD_D_PS1"
unset _OLD_D_PATH
unset _OLD_D_LIBRARY_PATH
unset _OLD_D_LD_LIBRARY_PATH
unset _OLD_D_PS1
unset DMD
unset DC
unset -f deactivate
}
_OLD_D_PATH="\$PATH"
_OLD_D_LIBRARY_PATH="\$LIBRARY_PATH"
_OLD_D_LD_LIBRARY_PATH="\$LD_LIBRARY_PATH"
_OLD_D_PS1="\$PS1"
export PATH="$path/dub:$path/$1/$binpath:\$PATH"
export LIBRARY_PATH="$path/$1/$libpath:\$LIBRARY_PATH"
export LD_LIBRARY_PATH="$path/$1/$libpath:\$LD_LIBRARY_PATH"
export DMD=$dmd
export DC=$dc
export PS1="($1)\$PS1"
EOF
logV "Writing environment variables to $path/$1/activate.fish"
cat > "$path/$1/activate.fish" <<EOF
function deactivate
set -gx PATH \$_OLD_D_PATH
set -gx LIBRARY_PATH \$_OLD_D_LIBRARY_PATH
set -gx LD_LIBRARY_PATH \$_OLD_D_LD_LIBRARY_PATH
functions -e fish_prompt
functions -c _old_d_fish_prompt fish_prompt
functions -e _old_d_fish_prompt
set -e _OLD_D_PATH
set -e _OLD_D_LIBRARY_PATH
set -e _OLD_D_LD_LIBRARY_PATH
set -e DMD
set -e DC
functions -e deactivate
end
set -g _OLD_D_PATH \$PATH
set -g _OLD_D_LIBRARY_PATH \$LIBRARY_PATH
set -g _OLD_D_LD_LIBRARY_PATH \$LD_LIBRARY_PATH
set -g _OLD_D_PS1 \$PS1
set -gx PATH "$path/dub" "$path/$1/$binpath" \$PATH
set -gx LIBRARY_PATH "$path/$1/$libpath" \$LIBRARY_PATH
set -gx LD_LIBRARY_PATH "$path/$1/$libpath" \$LD_LIBRARY_PATH
set -gx DMD $dmd
set -gx DC $dc
functions -c fish_prompt _old_d_fish_prompt
function fish_prompt
printf '($1)'
_old_d_fish_prompt
end
EOF
}
uninstall_compiler() {
if [ ! -d "$path/$1" ]; then
fatal "$1 is not installed in $path"
fi
log "Removing $path/$1"
rm -rf "$path/$1"
}
list_compilers() {
check_tools egrep
if [ -d "$path" ]; then
ls "$path" | egrep -v '^dub|^install\.sh|^d-keyring\.gpg'
fi
}
install_dub() {
if [ $os != linux ] && [ $os != osx ]; then
log "no dub binaries available for $os"
return
fi
local url=http://code.dlang.org/download/LATEST
logV "Determing latest dub version ($url)."
dub="dub-$(curl -sS $url)"
if [ -d "$path/$dub" ]; then
log "$dub already installed"
return
fi
local tmp=$(mkdtemp)
local url="http://code.dlang.org/files/$dub-$os-$arch.tar.gz"
log "Downloading and unpacking $url"
curl "$url" | tar -C "$tmp" -zxf -
logV "Removing old dub versions"
rm -rf "$path/dub" "$path/dub-*"
mv "$tmp" "$path/$dub"
logV "Linking $path/dub -> $path/$dub"
ln -s $dub "$path/dub"
}
# ------------------------------------------------------------------------------
[ $# -eq 0 ] && usage && exit 1
parse_args "$@"
resolve_latest $compiler
run_command ${command:-install} $compiler
}
_ "$@"