-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-statics
executable file
·391 lines (320 loc) · 8.48 KB
/
build-statics
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
#!/bin/bash
# Based on http://blog.assarbad.net/20140415/fully-static-build-of-tmux-using-libc-musl-for-linux/
set -e -u
tmp_list=()
cleanup() {
if [[ ${#tmp_list[@]} -ge 1 ]]; then
rm -rf "${tmp_list[@]}"
fi
}
trap cleanup EXIT
build_root="/vol/build"
archives="$build_root/download"
forest="$build_root/forest"
install_dir="$build_root/install"
build_opt_dir="$build_root/build_opt"
muslcc="$install_dir/bin/musl-gcc"
binaries=(
tmux
screen
)
log() {
local line
for line in "$@"; do
printf '%s\n' "$line" 1>&2
done
}
err() {
log "$@"
exit 1
}
write_file() {
local mode=644 OPTIND opt
write_file_update=''
while getopts eLl:m:o: opt; do
case "$opt" in
m ) mode="$OPTARG";;
* ) err "bad write_file usage";;
esac
done
shift $(($OPTIND - 1))
[[ $# -ge 1 ]] || err "write_file - missing path argument"
[[ $# -le 2 ]] || err "write_file - too many arguments"
local path="$1"
shift
# Use temporary to ensure atomic operation on filesystem
local tmp="$(mktemp "$path.XXXXXXXXXX")"
tmp_list+=("$tmp")
cat > "$tmp"
chmod "$mode" "$tmp"
mv -f "$tmp" "$path"
}
get_info() {
local name="$1"
p_name="$name"
p_required=()
p_config_args=()
p_executable=""
p_extra_scripts_writer=""
unset p_archive_name p_archive_url p_checksum
case "$name" in
musl )
p_archive_name="musl.tgz"
p_archive_url="http://www.musl-libc.org/releases/musl-1.1.4.tar.gz"
p_checksum="658c65ad3c3a9b281a96c5281e75720c758d91fcaae35ab987f2fdfb4f88f5cd"
p_config_args=(
--enable-gcc-wrapper
--disable-shared
--prefix="$install_dir"
)
;;
ncurses )
p_archive_name="ncurses.tgz"
p_archive_url="http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.9.tar.gz"
p_checksum="9046298fb440324c9d4135ecea7879ffed8546dd1b58e59430ea07a4633f563b"
p_config_args=(
--without-ada --without-cxx
--without-progs --without-manpages
--disable-db-install --without-tests
--with-default-terminfo-dir=/usr/share/terminfo
--with-terminfo-dirs="/etc/terminfo:/lib/terminfo:/usr/share/terminfo"
--prefix="$install_dir"
CC="$muslcc"
)
;;
libevent2 )
p_archive_name="libevent2.tgz"
p_archive_url="https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz"
p_checksum="22a530a8a5ba1cb9c080cba033206b17dacd21437762155c6d30ee6469f574f5"
p_config_args=(
--enable-static
--disable-shared
--disable-openssl
--prefix="$install_dir"
CC="$muslcc"
)
;;
tmux )
p_archive_name="tmux.tgz"
p_archive_url="http://downloads.sourceforge.net/project/tmux/tmux/tmux-1.9/tmux-1.9a.tar.gz?r=&use_mirror=heanet"
p_checksum="c5e3b22b901cf109b20dab54a4a651f0471abd1f79f6039d79b250d21c2733f5"
p_required=(ncurses libevent2)
p_config_args=(
--enable-static
--prefix="$install_dir"
CC="$muslcc"
CPPFLAGS="-I$install_dir/include -I$install_dir/include/ncurses"
LDFLAGS="-L$install_dir/lib"
#LIBS=-lncurses
)
p_executable="$name"
p_extra_scripts_writer=write_tmux_extra
;;
screen )
p_archive_name="screen.tgz"
p_archive_url="http://ftp.gnu.org/gnu/screen/screen-4.2.1.tar.gz"
p_checksum="5468545047e301d2b3579f9d9ce00466d14a7eec95ce806e3834a3d6b0b9b080"
p_required=(ncurses)
p_config_args=(
--prefix="$install_dir"
--enable-colors256
--disable-socket-dir
CC="$muslcc"
CPPFLAGS="-I$install_dir/include"
LDFLAGS="-L$install_dir/lib"
)
p_executable="$name"
p_extra_scripts_writer=write_screen_extra
;;
* ) err "unknown package name - $name" ;;
esac
}
declare -A recursion_checks
run_with_info() {
local cmd="$1" name
shift
recursion_checks=()
for name in "$@"; do
run_one_with_info "$cmd" "$name"
done
}
run_one_with_info() {
local cmd="$1" name="$2" required
if [[ -n "${recursion_checks["$name"]-}" ]]; then
return
fi
recursion_checks["$name"]=1
run_one_with_info "$cmd" musl
get_info "$name"
if [[ ${#p_required[@]} -ne 0 ]]; then
for required in "${p_required[@]}"; do
run_one_with_info "$cmd" "$required"
done
# get info again as the above recurssive call overwrites the globals
get_info "$name"
fi
"$cmd" "$name"
}
get_checksum() {
local path="$1" sum ignore
read sum ignore < <(sha256sum "$path")
printf %s "$sum"
}
failed_fetch=()
fetch_source() {
local path messages
path="$archives/$p_archive_name"
if [[ -f "$path" && "$(get_checksum "$path")" == "$p_checksum" ]]; then
log "Using already downloaded $p_archive_name"
return
fi
log "Fetching $path from $p_archive_url"
while true; do
log ""
curl -L -o "$path" "$p_archive_url" || break
log ""
if [[ ! -f "$path" ]]; then
log "failed to create $path"
break
fi
if [[ ! -s "$path" ]]; then
log "$path is empty"
break
fi
if [[ "$(get_checksum "$path")" != "$p_checksum" ]]; then
log "checksum mismatch for $path"
break
fi
return
done
failed_fetch+=("$p_name")
}
build_source() {
local dir prefix config_args_str i old_config_args
dir="$forest/$p_name"
config_args_str="configure"
for i in "${p_config_args[@]}"; do
config_args_str+=" $(printf %q "$i")"
done
if [[ -f "$build_opt_dir/$p_name" ]]; then
if [[ "$(< "$build_opt_dir/$p_name")" == "$config_args_str" ]]; then
log "$p_name is already build"
return 0
fi
fi
log "unpacking $p_name"
rm -rf "$dir"
mkdir "$dir"
tar --strip-components=1 -C "$dir" -xzf "$archives/$p_archive_name"
log "building $p_name"
(cd "$dir" && ./configure "${p_config_args[@]}" && make && make install) 1>&2
printf '%s\n' "$config_args_str" > "$build_opt_dir/$p_name"
log "$p_name is successfully built"
}
write_command_with_service() {
local name="$1" service_args="$2" suffix="$3" cmd_args="$4" path
shift
args="$*"
path="$install_dir/bin/$name-$suffix"
log "Creating $path"
sed -e "s/%NAME%/$name/g" \
-e "s/%SERVICE_ARGS%/$service_args/g" \
-e "s/%CMD_ARGS%/$cmd_args/g" <<'EOF' \
| write_file -m 755 "$path"
#!/bin/sh
set -e -u
get_service_state() {
service_state="$(systemctl show --property=ActiveState "$service")"
service_state="${service_state#*=}"
}
service="%NAME%-$USER"
dir="$(dirname "$0")"
get_service_state
if test active != "$service_state"; then
if test inactive = "$service_state" -o failed = "$service_state"; then
sudo systemd-run --uid $UID --unit "$service" --service-type=forking \
"$dir/%NAME%" %SERVICE_ARGS% 2>/dev/null
fi
i=0
while test $i -lt 20; do
get_service_state
test activating = "$service_state" || break
sleep 0.05
i=$(($i + 1))
done
fi
exec "$dir/%NAME%" %CMD_ARGS%
EOF
printf '%s\n' "$path"
}
write_screen_extra() {
write_command_with_service screen "-d -m" resume "-D -r"
}
write_tmux_extra() {
write_command_with_service tmux "new-session -d" attach attach
}
print_ready_executables() {
local name path script
for name in "${binaries[@]}"; do
get_info "$name"
path="$install_dir/bin/$p_executable"
if [[ -x "$path" ]]; then
printf '%s\n' "$path"
if [[ -n "$p_extra_scripts_writer" ]]; then
$p_extra_scripts_writer "$install_dir/bin"
fi
fi
done
}
run_setup() {
local dir
for dir in "$archives" "$forest" "$install_dir" "$build_opt_dir" ; do
mkdir -p "$dir"
done
}
if [[ "$UID" == 0 ]]; then
if [[ ! -d "$build_root" ]]; then
tmp="$(mktemp -d "$build_root.XXXXXXXXXX")"
tmp_list+=("$tmp")
chown user:user "$tmp"
chmod 770 "$tmp"
mv "$tmp" "$build_root"
fi
built_binaries="$(sudo -u user -H "$0" "$@")"
if [[ -n "$built_binaries" ]]; then
xargs install -t /vol/bin <<< "$built_binaries"
fi
exit
fi
build_all=""
usage_error=""
bad_usage() {
log "$@"
usage_error=1
}
while getopts as opt; do
case "$opt" in
a ) build_all=1;;
* ) usage_error=1 ;;
esac
done
shift $(($OPTIND - 1))
if [[ -n "$build_all" ]]; then
[[ $# -eq 0 ]] || bad_usage "unexpected argument"
else
[[ $# -ne 0 ]] || bad_usage "list of binaried to build must be given"
fi
[[ -z "$usage_error" ]] || err "Try $0 -h for usage"
run_setup
build_list=()
if [[ -n "$build_all" ]]; then
build_list+=("${binaries[@]}")
else
build_list+=("$@")
fi
run_with_info fetch_source "${build_list[@]}"
if [[ ${#failed_fetch[@]} -ne 0 ]]; then
err "" "Failed to download:" "${failed_fetch[@]}"
fi
run_with_info build_source "${build_list[@]}"
print_ready_executables