-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.sh
executable file
·255 lines (230 loc) · 6.62 KB
/
lib.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
PWD0=${PWD0:-$PWD}
LIBSH_FILE=${BASH_SOURCE[0]}
SCRIPT_FILE=${BASH_SOURCE[$(( ${#BASH_SOURCE[@]} - 1 ))]}
PROJECT=$(dirname $(realpath "$LIBSH_FILE"))
BUILD_DIR_GENERIC=$PROJECT/build
DOWNLOAD_DIR=$BUILD_DIR_GENERIC/download
HOST_ARCH=$(uname -m); HOST_ARCH=${HOST_ARCH/arm64/aarch64}
HOST_SYS=$(uname -s) # e.g. linux, macos
case "$HOST_SYS" in
Darwin) HOST_SYS=macos ;;
*) HOST_SYS=$(awk '{print tolower($0)}' <<< "$HOST_SYS") ;;
esac
_err() { echo "$0:" "$@" >&2; exit 1; }
_warn() { echo "$0: warning:" "$@" >&2; }
_relpath() { # <path>
case "$1" in
"$PWD0/"*) echo "${1##$PWD0/}" ;;
"$PWD0") echo "." ;;
# "$HOME/"*) echo "~${1:${#HOME}}" ;;
*) echo "$1" ;;
esac
}
_pushd() {
local wd="$PWD"
pushd "$1" >/dev/null
[ "$PWD" = "$wd" -o "$PWD" = "$PWD0" ] || echo "cd $(_relpath "$PWD")"
}
_popd() {
local wd="$PWD"
popd >/dev/null
[ "$PWD" = "$wd" -o "$PWD" = "$PWD0" ] || echo "cd $(_relpath "$PWD")"
}
_array_join() { # <gluechar> <element> ...
local IFS="$1"; shift; echo "$*"
}
_array_println() { # <prefix> <element> ...
local s
local prefix=$1;shift
for s in "$@"; do echo "$prefix$s"; done
}
_array_println_slashesc() { # <prefix> <element> ...
local prefix=$1;shift
local args=( "$@" )
for ((i = 0; i < $#; i++ )); do
if [ $(( i + 1 )) -lt $# ]; then
echo "$prefix${args[i]} \\"
else
echo "$prefix${args[i]}"
fi
done
}
_copy() { # <arg to cp> ...
printf "copy"
local past=
for arg in "$@"; do case "$arg" in
-*) [ -z "$past" ] || printf " %s" "$(_relpath "$arg")";;
--) past=1 ;;
*) printf " %s" "$(_relpath "$arg")" ;;
esac; done
printf "\n"
cp -R "$@"
}
_cpd() {
echo "copy $(($#-1)) files to $(_relpath "${@: -1}")/"
mkdir -p "${@: -1}"
cp -r "$@"
}
_symlink() { # <linkfile-to-create> <target>
if [ -L "$1" -a "$(readlink "$1")" = "$2" ]; then
return 0
fi
echo "symlink $(_relpath "$1") -> $(_relpath "$2")"
[ ! -e "$1" ] || [ -L "$1" ] || _err "$(_relpath "$1") exists (not a link)"
rm -f "$1"
ln -fs "$2" "$1"
}
_hascmd() { # <cmd>
command -v "$1" >/dev/null || return 1
}
_needcmd() {
while [ $# -gt 0 ]; do
_hascmd "$1" || _err "$1 not found in PATH"
shift
done
}
_sha256() { # [<file>]
sha256sum "$@" | cut -d' ' -f1
}
_sha_verify() { # <file> [<sha256> | <sha512>]
local file=$1
local expect=$2
local actual=
echo "verifying checksum of $(_relpath "$file")"
case "${#expect}" in
128) kind=512; actual=$(sha512sum "$file" | cut -d' ' -f1) ;;
64) kind=256; actual=$(sha256sum "$file" | cut -d' ' -f1) ;;
*) _err "checksum $expect has incorrect length (not sha256 nor sha512)" ;;
esac
if [ "$actual" != "$expect" ]; then
echo "$file: SHA-$kind sum mismatch:" >&2
echo " actual: $actual" >&2
echo " expected: $expect" >&2
return 1
fi
}
_download_nocache() { # <url> [<sha256>|<sha512> [<outfile>]]
local url=$1
local checksum=${2:-}
local outfile="${3:-$DOWNLOAD_DIR/$(basename "$url")}"
rm -f "$outfile"
mkdir -p "$(dirname "$outfile")"
echo "$(_relpath "$outfile"): fetch $url"
command -v curl >/dev/null &&
curl -L '-#' --fail -o "$outfile" "$url" ||
wget -O "$outfile" "$url"
[ -z "$checksum" ] || _sha_verify "$outfile" "$checksum"
}
_download() { # <url> [<sha256>|<sha512> [<outfile>]]
local url=$1
local checksum=${2:-}
local outfile="${3:-$DOWNLOAD_DIR/$(basename "$url")}"
if [ -f "$outfile" ]; then
[ -z "$checksum" ] && return 0
_sha_verify "$outfile" "$checksum" && return 0
fi
_download_nocache "$url" "$checksum" "$outfile"
}
_extract_tar() { # <file> <outdir>
[ $# -eq 2 ] || _err "_extract_tar"
local tarfile=$1
local outdir=$2
[ -e "$tarfile" ] || _err "$tarfile not found"
local extract_dir="${outdir%/}-extract-$(basename "$tarfile")"
rm -rf "$extract_dir"
mkdir -p "$extract_dir"
echo "extract $(basename "$tarfile") -> $(_relpath "$outdir")"
if ! XZ_OPT='-T0' tar -C "$extract_dir" -xf "$tarfile"; then
rm -rf "$extract_dir"
return 1
fi
rm -rf "$outdir"
mkdir -p "$(dirname "$outdir")"
mv -f "$extract_dir"/* "$outdir"
rm -rf "$extract_dir"
}
_create_tar_xz_from_dir() { # <srcdir> <dstfile>
command -v tar >/dev/null || _err "can't find \"tar\" in PATH"
local archive=$2
[[ "$archive" == *".tar.xz" ]] || _err "$archive doesn't end with .tar.xz"
case "$archive" in
/*) ;;
*) archive="$PWD/$archive" ;;
esac
mkdir -p "$(dirname "$archive")"
local srcdir="$(realpath "$1")"
if command -v xz >/dev/null; then
tar -C "$(dirname "$srcdir")" -c "$(basename "$srcdir")" \
| xz -9 -f -T0 -v > "$archive"
else
XZ_OPT='-9 -T0' \
tar -C "$(dirname "$srcdir")" -cJpf "$archive" "$(basename "$srcdir")"
fi
}
_download_and_extract_tar() { # <url> <outdir> [<sha256>|<sha512> [<tarfile>]]
local url=$1
local outdir=$2
local checksum=${3:-}
local tarfile="${4:-$DOWNLOAD_DIR/$(basename "$url")}"
_download "$url" "$checksum" "$tarfile"
_extract_tar "$tarfile" "$outdir"
}
_co_targets() { # -> "arch,sys,sysver,intsize,ptrsize,llvm_triple" ...
awk '$1 ~ /^TARGET\(/' "$PROJECT/src/targets.h" |
sed -E 's/[ "]//g' |
sed -E 's/^TARGET\((.+)\)$/\1/'
}
_version_gte() { # <v> <minv>
local v1 v2 v3 min_v1 min_v2 min_v3
IFS=. read -r v1 v2 v3 <<< "$1"
IFS=. read -r min_v1 min_v2 min_v3 <<< "$2"
[ -n "$min_v2" ] || min_v2=0
[ -n "$min_v3" ] || min_v3=0
[ -n "$v2" ] || v2=$min_v2
[ -n "$v3" ] || v3=$min_v3
# echo "v $v1 $v2 $v3"
# echo "min $min_v1 $min_v2 $min_v3"
if [ "$v1" -lt "$min_v1" ]; then return 1; fi
if [ "$v1" -gt "$min_v1" ]; then return 0; fi
if [ "$v2" -lt "$min_v2" ]; then return 1; fi
if [ "$v2" -gt "$min_v2" ]; then return 0; fi
if [ "$v3" -lt "$min_v3" ]; then return 1; fi
if [ "$v3" -gt "$min_v3" ]; then return 0; fi
}
_abspath() { # <path>
local path="${1%/}" # "%/" = shave off any trailing "/"
case "$path" in
"") echo "/" ;;
/*) echo "$path" ;;
./*) echo "$PWD/${path:2}" ;;
*) echo "$PWD/$path" ;;
esac
}
_relative_path() { # <basedir> <subject>
local basedir="$(_abspath "$1")"
local subject="$(_abspath "$2")"
case "$subject" in
"$basedir") echo "."; return 0 ;;
"$basedir/"*) echo "${subject:$(( ${#basedir} + 1 ))}"; return 0 ;;
esac
# TODO: calculate "../" path
echo "$subject"
}
_create_tool_symlinks() { # <coexe> [<dir>]
local coexe="$(realpath "$1" 2>/dev/null || _abspath "$1")"
local dir="$(realpath "${2:-"$(dirname "$coexe")"}")"
local target="$(_relative_path "$dir" "$coexe")"
for name in \
cc \
c++ \
ar \
as \
ranlib \
ld.lld \
ld64.lld \
wasm-ld \
lld-link \
;do
_symlink "$dir/$name" "$target"
done
}