-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·461 lines (414 loc) · 14 KB
/
build.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
#!/bin/sh
defaulttarget='armv6-apple-darwin10'
[ "${0%/*}" = "$0" ] && bsroot="." || bsroot="${0%/*}"
cd "$bsroot" || exit 1
bsroot="$PWD"
if ! command -v "$EDITOR" >/dev/null 2>&1; then
for editor in nvim vim vi micro nano; do
if command -v $editor >/dev/null 2>&1; then
EDITOR=$editor
break
fi
done
command -v "$EDITOR" >/dev/null 2>&1 || EDITOR=none
fi
if [ -z "$1" ]; then
if [ -f "$bsroot/.args.txt" ]; then
while IFS= read -r line; do
set -- "$@" "$line"
done < "$bsroot/.args.txt"
fi
fi
if [ -f "$bsroot/pkglock" ]; then
lockpid="$(cat "$bsroot/pkglock")"
if kill -0 "$lockpid" 2> /dev/null; then
printf '%s\n' "Waiting for PID $lockpid to finish..."
while kill -0 "$lockpid" 2> /dev/null; do
sleep 1
done
fi
fi
printf '%s' "$$" > "$bsroot/pkglock"
pkgdir="$bsroot/pkgs"
export TERM="xterm-256color"
export _ENT="$bsroot/entitlements.xml"
case "$*" in
*--no-tmpfs*) export _TMP="$bsroot" ;;
*) export _TMP="/tmp" ;;
esac
case "$*" in
*--target=*) _TARGET="$*" ; _TARGET="${_TARGET#*--target=}" ; export _TARGET="${_TARGET%% *}" ;;
*) export _TARGET="$defaulttarget" ;;
esac
case "$*" in
*-j*)
_JOBS="$*" ; _JOBS="${_JOBS#*-j}" ; _JOBS="${_JOBS%% *}"
case $_JOBS in
''|*[!0-9]*) unset _JOBS ;;
*) export _JOBS ;;
esac
;;
esac
if [ -z "$_JOBS" ]; then
if command -v nproc > /dev/null; then
cpus=$(nproc)
elif sysctl -n hw.ncpu > /dev/null 2>&1; then
cpus=$(sysctl -n hw.ncpu)
else
cpus=1
fi
_JOBS=$((cpus * 2 / 3))
[ "$_JOBS" = 0 ] && _JOBS=1
export _JOBS
fi
case $_TARGET in
arm64*|aarch64*)
command -v "$_TARGET-sdkpath" > /dev/null || error "Missing dependency: $_TARGET-sdkpath"
if [ -d "$("$_TARGET-sdkpath")/System/Library/Frameworks/MobileCoreServices.framework" ]; then
export _DPKGARCH=iphoneos-arm64
else
export _DPKGARCH=darwin-arm64
fi
;;
arm*) export _DPKGARCH=iphoneos-arm ;;
x86_64*) export _DPKGARCH=darwin-amd64 ;;
i386*) export _DPKGARCH=darwin-i386 ;;
ppc64*|powerpc64*) export _DPKGARCH=darwin-ppc64 ;;
ppc*|powerpc*) export _DPKGARCH=darwin-powerpc ;;
esac
: > "$_TMP/.builtpkgs"
rm -rf "$_TMP"/iphoneports-sdk*
error() {
printf '\033[1;31mError:\033[0m %s\n' "$1"
rm -f "$bsroot/pkglock"
exit 1
}
depcheck() {
for dep in "$_TARGET-gcc" "$_TARGET-g++" "$_TARGET-cc" "$_TARGET-c++" "$_TARGET-strip" "$_TARGET-sdkpath" ldid dpkg-deb patch fakeroot automake autoreconf m4 yacc ctags tar gzip bzip2 xz zstd ninja sed pgrep meson cmake; do
if ! command -v "$dep" > /dev/null; then
error "Missing dependency: $dep"
fi
done
case $_TARGET in
x86_64*|i386-*)
if ! command -v nasm > /dev/null; then
error "Missing dependency: nasm"
fi
;;
esac
if command -v gmake > /dev/null; then
_MAKE="gmake"
elif command -v make > /dev/null; then
_make_version="$(make --version)"
case "$_make_version" in
*GNU*) _MAKE="make" ;;
*) error "Missing dependency: GNU make" ;;
esac
else
error "Missing dependency: GNU make"
fi
if command -v gtar > /dev/null; then
gtar="gtar"
elif command -v make > /dev/null; then
_tar_version="$(tar --version)"
case "$_tar_version" in
*GNU*) gtar="tar" ;;
*) error "Missing dependency: GNU tar" ;;
esac
else
error "Missing dependency: GNU tar"
fi
if command -v "$_TARGET-otool" > /dev/null; then
_OTOOL="$_TARGET-otool"
elif command -v otool > /dev/null; then
_OTOOL="otool"
elif command -v llvm-otool > /dev/null; then
_OTOOL="llvm-otool"
else
error "Missing dependency: otool"
fi
if command -v "$_TARGET-install_name_tool" > /dev/null; then
_INSTALLNAMETOOL="$_TARGET-install_name_tool"
elif command -v install_name_tool > /dev/null; then
_INSTALLNAMETOOL="install_name_tool"
elif command -v llvm-install-name-tool > /dev/null; then
_INSTALLNAMETOOL="llvm-install-name-tool"
else
error "Missing dependency: install_name_tool"
fi
sdk="$("$_TARGET-sdkpath")"
export _MAKE _OTOOL _INSTALLNAMETOOL
}
build() {
if hasbeenbuilt "$1" "$2"; then
return 0
fi
(
export _PKGROOT="$pkgdir/$1"
cd "$_PKGROOT" || error "Failed to cd to package directory: $1"
includedeps
if [ -n "$dryrun" ]; then
printf '%s\n' "Building $1"
else
[ -f fetch.sh ] && ./fetch.sh
applypatches
printf '%s\n' "Building $1"
./build.sh || fail=1
fi
rm -rf "$_SDK"
[ -z "$fail" ] || return 2
)
case $? in
1) exit 1 ;;
2) return 1 ;;
esac
[ -n "$dryrun" ] && printf '%s\n' "$1" >> "$_TMP/.builtpkgs"
return 0
}
hasbeenbuilt() {
if [ -n "$dryrun" ]; then
while IFS= read -r pkg; do
if [ "$pkg" = "$1" ]; then
return 0
fi
done < "$_TMP/.builtpkgs"
elif [ -d "$pkgdir/$1/pkg" ]; then
return 0
fi
return 1
}
applypatches() {
if [ -d patches ]; then
for patch in patches/*; do
printf '%s\n' "Applying patch ${patch##*/}"
patch -p0 < "$patch"
done
fi
}
includedeps() {
if [ -z "$dryrun" ]; then
if [ -d "$sdk" ]; then
export _SDK="$_TMP/iphoneports-sdk"
mkdir -p "$_SDK"
cp -a "$sdk"/* "$_SDK"
else
error "SDK not found"
fi
fi
if [ -f dependencies.txt ]; then
while IFS= read -r dep; do
if [ -d "$pkgdir/$dep" ]; then
if ! hasbeenbuilt "$dep"; then
printf '%s\n' "Building dependency $dep"
[ -z "$dryrun" ] && mv "$_SDK" "$_SDK.$dep"
build "$dep" || fail=1
if [ -z "$dryrun" ]; then
if [ -n "$fail" ]; then
rm -rf "$_SDK.$dep"
error "Failed to build package: $dep"
else
mv "$_SDK.$dep" "$_SDK"
fi
fi
fi
printf '%s\n' "Including dependency $dep"
[ -z "$dryrun" ] && cp -a "$pkgdir/$dep/pkg/"* "$_SDK"
else
error "Dependency not found: $dep"
fi
done < dependencies.txt
fi
if [ -z "$dryrun" ]; then
if [ -d sdk ]; then
cp -a sdk/* "$_SDK"
fi
fi
}
sysroot() {
[ -d "$pkgdir/$1/pkg" ] || build "$1" || error "Failed to build package: $1"
if [ -f "$pkgdir/$1/dependencies.txt" ]; then
while IFS= read -r dep; do
sysroot "$dep"
done < "$pkgdir/$1/dependencies.txt"
fi
cp -a "$pkgdir/$1"/pkg/* sysroot
}
main() {
case "$1" in
all|all-noclean)
depcheck
kind=$1
shift
for pkg in "$pkgdir"/*; do
unset dontbuild
for exclude in "$@"; do
[ "$pkg" = "$exclude" ] && dontbuild=1
done
[ -n "$dontbuild" ] && continue
if [ -z "$pkglist" ]; then
pkglist="${pkg##*/}"
else
pkglist="$pkglist ${pkg##*/}"
fi
if [ "$kind" != "all-noclean" ]; then
rm -rf "$pkg/pkg" "$pkg/src" "$pkg"/*.deb &
fi
done
wait
for pkg in $pkglist; do
[ "$kind" = "all-noclean" ] && hasbeenbuilt "$pkg" && continue
build "$pkg" || error "Failed to build package: $pkg"
cp -f "$pkgdir/$pkg"/*.deb debs 2> /dev/null
done
;;
clean)
[ -z "$2" ] && error "No package specified"
shift
for pkg in "$@"; do
[ -d "$pkgdir/$pkg" ] || error "Package not found: $pkg"
rm -rf "$pkgdir/$pkg/pkg" "$pkgdir/$pkg/src" "$pkgdir/$pkg"/*.deb
done
;;
cleanall)
rm -rf "$pkgdir"/*/pkg "$pkgdir"/*/src "$pkgdir"/*/*.deb "$bsroot"/debs/*.deb "$_TMP"/iphoneports-sdk* "$_TMP/.builtpkgs"
;;
dryrun)
dryrun=1
if [ -z "$2" ]; then
for pkg in "$pkgdir"/*; do
build "${pkg##*/}" || error "Failed to build package: ${pkg##*/}"
done
else
shift
for pkg in "$@"; do
[ -d "$pkgdir/$pkg" ] || error "Package not found: $pkg"
done
for pkg in "$@"; do
build "$pkg" || error "Failed to build package: $pkg"
done
fi
;;
abibreak)
[ -z "$2" ] && error "You must specify a package"
[ "$EDITOR" = "none" ] && error "No suitable text editor found"
depcheck
[ -d "$pkgdir/$2" ] || error "Package not found: $2"
for pkg in "$pkgdir"/*; do
if [ -f "$pkg/dependencies.txt" ]; then
while IFS= read -r dep; do
if [ "$dep" = "$2" ]; then
if [ -z "$deppkgs" ]; then
deppkgs="${pkg##*/}"
else
deppkgs="$deppkgs ${pkg##*/}"
fi
[ -f "$pkg/DEBIAN/control" ] && "$EDITOR" "$pkg/DEBIAN/control"
fi
done < "$pkg/dependencies.txt"
fi
done
rm -rf "$pkgdir/$2/pkg" "$pkgdir/$2/src"
build "$2" || error "Failed to build package: $2"
cp -f "$pkgdir/$2"/*.deb "$bsroot/debs" 2> /dev/null
for pkg in $deppkgs; do
rm -rf "$pkgdir/$pkg/pkg" "$pkgdir/$pkg/src" "$pkgdir/$pkg"/*.deb
build "$pkg" || error "Failed to build package: $pkg"
cp -f "$pkgdir/$pkg"/*.deb debs 2> /dev/null
done
;;
sysroot)
[ -z "$2" ] && error "You must specify a package"
depcheck
shift
for pkg in "$@"; do
[ -d "$pkgdir/$pkg" ] || error "Package not found: $pkg"
done
for pkg in "$@"; do
build "$pkg" || error "Failed to build package: $pkg"
done
rm -rf sysroot
mkdir sysroot
printf 'Building sysroot...\n'
for pkg in "$@"; do
sysroot "$pkg"
done
rm -rf sysroot/DEBIAN
printf 'Making tarball...\n'
(
cd sysroot || exit 1
"$gtar" --owner 0 --group 0 -czf sysroot.tar.gz ./*
) || error "Failed to build sysroot tarball"
printf 'Done!\n'
;;
bootstrap)
depcheck
pkgs='base bzip2 ca-certificates coreutils dash diffutils dpkg gmp grep gtar gzip less libmd libressl ncurses pcre2 readline sed xz zlib'
for pkg in $pkgs; do
build "$pkg" || error "Failed to build package: $pkg"
done
rm -rf sysroot
mkdir sysroot
printf 'Building sysroot...\n'
for pkg in $pkgs; do
sysroot "$pkg"
cp -f "$pkgdir/$pkg"/*.deb debs 2> /dev/null
done
rm -rf sysroot/DEBIAN
printf 'Making tarballs...\n'
(
cd sysroot || exit 1
"$gtar" --owner 0 --group 0 -czf bootstrap.tar.gz ./*
) || error "Failed to build bootstrap tarball"
(
cd debs || exit 1
"$gtar" --owner 0 --group 0 -czf ../sysroot/debs.tar.gz ./*
) || error "Failed to build debs tarball"
printf 'Done!\n'
;;
-*)
shift
main "$@"
;;
'')
printf '%s' "\
Usage: build.sh [options] <command>
<pkg> [pkgs...] - Build specified packages
build <pkg> [pkgs...] - Build specified packages
all [pkgs...] - Build all packages (except those specified)
all-noclean [pkgs...] - Same as all but doesn't cleanall first
clean <pkg> [pkgs...] - Clean a single package
cleanall - Clean all packages
dryrun [pkgs...] - Pretend to build all packages, for debugging
abibreak <pkg> - ABI break helper, opens all the control files for packages
that depend on <pkg> and then rebuilds them
sysroot <pkg> [pkgs...] - Copy specified package's files and dependencies to sysroot directory
Useful for installing packages in environments without dpkg
bootstrap - Make a sysroot with base, dpkg, and all their dependencies
--target - Specify a target (default: $defaulttarget)
--no-tmp - Do not use /tmp for anything, use the current directory instead
-jN - Set the number of jobs passed to programs like make and ninja
"
exit 1
;;
*)
if [ "$1" = "build" ]; then
shift
fi
[ -z "$1" ] && error "You must specify a package"
depcheck
for pkg in "$@"; do
[ -d "$pkgdir/$pkg" ] || error "Package not found: $pkg"
done
for pkg in "$@"; do
rm -rf "$pkgdir/$pkg/pkg" "$pkgdir/$pkg/src" "$pkgdir/$pkg"/*.deb &
done
wait
for pkg in "$@"; do
build "$pkg" || error "Failed to build package: $pkg"
cp -f "$pkgdir/$pkg"/*.deb "$bsroot/debs" 2> /dev/null
done
;;
esac
}
main "$@"
rm -f "$bsroot/pkglock"