-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·346 lines (308 loc) · 10.2 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
#!/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
: > "$_TMP/.builtpkgs"
rm -rf "$_TMP"/iphoneports-sdk*
error() {
printf '\033[1;31mError:\033[0m %s\n' "$1"
[ "$2" != "noexit" ] && { 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; do
if ! command -v "$dep" > /dev/null; then
error "Missing dependency: $dep"
fi
done
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 "$_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
fi
rm -rf "$_SDK"
)
printf '%s\n' "$1" >> "$_TMP/.builtpkgs"
}
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"
[ -z "$dryrun" ] && mv "$_SDK.$dep" "$_SDK"
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() {
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)
depcheck
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
rm -rf "$pkg/pkg" "$pkg/src" &
done
wait
for pkg in $pkglist; do
build "$pkg" || error "Failed to build package: $pkg"
cp -f "$pkgdir/$pkg"/*.deb "$bsroot/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 "$bsroot/debs/$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
"$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"
build "$pkg" || error "Failed to build package: $pkg"
cp -f "$pkgdir/$pkg"/*.deb "$bsroot/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 '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)
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
--target - Specify a target (default: $defaulttarget)
--no-tmp - Do not use /tmp for anything, use the current directory instead
"
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" &
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"