-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcbuild.sh
More file actions
executable file
·476 lines (408 loc) · 17 KB
/
cbuild.sh
File metadata and controls
executable file
·476 lines (408 loc) · 17 KB
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
#!/bin/sh
# TODO: It really doesn't make sense to use `dbin` here if we (I) plan to support BSDs too.
# The only way to build the tooling right now in BSDs is to manually gather and compress all dependencies
# To then build each target (appbundle-runtime (universal), pelf)
#
# TODO: What to do about pelfCreator in non-Linux systems?
#
[ "$DEBUG" = "1" ] && set -x
OPWD="$PWD"
BASE="$(dirname "$(realpath "$0")")"
TEMP_DIR="/tmp/pelf_build_$(date +%s)"
# Change to BASE directory if not already there
if [ "$OPWD" != "$BASE" ]; then
echo "Changing to $BASE"
cd "$BASE" || exit 1
fi
trap 'cd "$OPWD"; [ -d "$TEMP_DIR" ] && rm -rf "$TEMP_DIR"' EXIT
# -Dbin-related-envs-------------------------------#
export DBIN_INSTALL_DIR="$BASE/binaryDependencies" #
export DBIN_NOCONFIG="1" #
# -Dependency-Revision-Tracking--------------------#
DWFS_VER="0.13.0" #
# -------------------------------------------------#
if [ "$(uname -m)" = "aarch64" ]; then
export GOARCH="arm64" # Weird things happen when it is not set, I think my GH action has this env already set.
fi
# Logging functions
log() {
printf "\033[34m->\033[m %s\n" "$*"
}
log_warning() {
printf "\033[33m->\033[m %s\n" "$*"
}
log_error() {
printf "\033[31m->\033[m %s\n" "$*"
exit 1
}
# Utility functions
unnappear() {
"$@" >/dev/null 2>&1
}
available() {
unnappear which "$1" || return 1
}
require() {
available "$1" || log_error "[$1] is not installed. Please ensure the command is available [$1] and try again."
}
fetchFromGithub() {
_repo="$1"
_tag="$2"
_fileName="$3"
_outputPath="$4"
log "Fetching $_fileName from $_repo:$_tag"
if [ "$_tag" = "latest" ]; then
_downloadUrl="https://github.com/$_repo/releases/latest/download/$_fileName"
else
_downloadUrl="https://github.com/$_repo/releases/download/$_tag/$_fileName"
fi
if curl -fsSL --retry 2 --retry-delay 3 --max-time 120 \
"$_downloadUrl" -o "$_outputPath" 2>/dev/null && validateDownload "$_outputPath" "$_fileName"; then
return 0
fi
rm -f "$_outputPath"
log_warning "Direct download failed, trying API method"
if [ "$_tag" = "latest" ]; then
_apiUrl="https://api.github.com/repos/$_repo/releases/latest"
else
_apiUrl="https://api.github.com/repos/$_repo/releases/tags/$_tag"
fi
_tempJson="/tmp/release_info_$$.json"
if ! curl -fsSL -H "Accept: application/vnd.github.v3+json" \
"$_apiUrl" -o "$_tempJson" 2>/dev/null; then
rm -f "$_tempJson"
log_error "Failed to fetch release info for $_repo:$_tag"
fi
if available "jq"; then
_downloadUrl=$(jq -r ".assets[] | select(.name == \"$_fileName\") | .browser_download_url" "$_tempJson" 2>/dev/null)
else
_downloadUrl=$(grep -o "\"browser_download_url\":\"[^\"]*$_fileName\"" "$_tempJson" | cut -d'"' -f4)
fi
rm -f "$_tempJson"
[ -z "$_downloadUrl" ] || [ "$_downloadUrl" = "null" ] && log_error "Asset '$_fileName' not found in $_repo:$_tag"
if ! curl -fsSL --retry 3 --retry-delay 5 --max-time 300 \
"$_downloadUrl" -o "$_outputPath" || ! validateDownload "$_outputPath" "$_fileName"; then
log_error "Failed to download $_fileName"
fi
}
validateDownload() {
_file="$1"
_name="$2"
[ -f "$_file" ] && [ -s "$_file" ] || { log_error "Downloaded file $_name is empty or doesn't exist"; return 1; }
file "$_file" 2>/dev/null | grep -q "HTML\|text" && { log_error "Downloaded HTML error page instead of $_name"; return 1; }
return 0
}
checkElf() {
_file="$1"
# Check if file exists
[ -f "$_file" ] || { log_error "File $_file does not exist"; return 1; }
if available "xxd"; then
magic=$(xxd -p -l 4 "$_file" 2>/dev/null)
elif available "hexdump"; then
magic=$(hexdump -ve '/1 "%02x"' -n 4 "$_file" 2>/dev/null)
else
log_error "Neither xxd nor hexdump is available to check ELF header"
return 1
fi
# Check if the first 4 bytes match ELF magic number (7F454C46)
[ "$magic" = "7f454c46" ] && return 0
log_error "$_file is not an ELF file. Do clean and re-run the target to re-download"
return 1
}
build_appbundle_runtime() {
log "Building appbundle-runtime variants"
if [ "$(basename "$(uname -o)")" = "Linux" ]; then
log "Preparing appbundle-runtime binary dependencies"
export DBIN_INSTALL_DIR="$BASE/appbundle-runtime/binaryDependencies"
mkdir -p "$DBIN_INSTALL_DIR"
# Fetch required tools using curl and dbin
fetchFromGithub "mhx/dwarfs" "v$DWFS_VER" "dwarfs-fuse-extract-$DWFS_VER-$(basename "$(uname -o)")-$(uname -m).upx" "$DBIN_INSTALL_DIR/dwarfs"
checkElf "$DBIN_INSTALL_DIR/dwarfs"
chmod +x "$DBIN_INSTALL_DIR/dwarfs"
fetchFromGithub "VHSgunzo/squashfuse-static" "latest" "squashfuse_ll-musl-mimalloc-$(uname -m)" "$DBIN_INSTALL_DIR/squashfuse"
checkElf "$DBIN_INSTALL_DIR/squashfuse"
chmod +x "$DBIN_INSTALL_DIR/squashfuse"
dbin add squashfs-tools/unsquashfs
# UPX the unsquashfs binary
if available "upx"; then
log "Compressing unsquashfs for appbundle-runtime"
checkElf "$DBIN_INSTALL_DIR/unsquashfs"
upx "$DBIN_INSTALL_DIR/unsquashfs" || log_error "Unable to compress unsquashfs"
else
log_warning "upx not available. The unsquashfs binary will be unnecessarily large"
fi
chmod +x "$DBIN_INSTALL_DIR"/*
# Build dwarfs version
log "Building dwarfs appbundle-runtime"
go build --tags dwarfs -o "$BASE/binaryDependencies/appbundle-runtime_dwarfs" ./appbundle-runtime || log_error "Unable to build appbundle-runtime_dwarfs"
# Build squashfs version
log "Building squashfs appbundle-runtime"
go build --tags squashfs -o "$BASE/binaryDependencies/appbundle-runtime_squashfs" ./appbundle-runtime || log_error "Unable to build appbundle-runtime_squashfs"
available "strip" && strip "$BASE/binaryDependencies/appbundle-runtime_dwarfs" "$BASE/binaryDependencies/appbundle-runtime_squashfs"
else
# Build standard version
log "Building universal appbundle-runtime"
go build --tags noEmbed -o "$BASE/binaryDependencies/appbundle-runtime" ./appbundle-runtime || log_error "Unable to build appbundle-runtime"
available "strip" && strip "$BASE/binaryDependencies/appbundle-runtime"
fi
if ! available "strip"; then
log_warning "strip not available. The binaries will be unnecessarily large"
fi
}
build_pelf() {
if [ -f "./pelf.go" ]; then
build_appbundle_runtime
export DBIN_INSTALL_DIR="$BASE/binaryDependencies"
mkdir -p "$DBIN_INSTALL_DIR"
[ "$NO_REMOTE" != "1" ] && handle_dependencies
log "Creating binaryDependencies.tar.zst for pelf"
tar -C binaryDependencies -c . | zstd -T0 -19 -fo binaryDependencies.tar.zst
rm -f ./pelf
export CGO_ENABLED=0
export GOFLAGS="-ldflags=-static-pie -ldflags=-s -ldflags=-w"
go build -o ./pelf || log_error "Unable to build ./pelf"
if available "upx"; then
log "Compressing ./pelf tool"
checkElf "./pelf"
upx ./pelf || log_error "unable to compress ./pelf"
rm -f ./pelf.upx
else
log_warning "upx not available. The resulting binary will be unnecessarily large"
fi
else
log_error "./pelf.go not found."
fi
}
build_pelfCreator() {
log "Building pelfCreator"
# Create temporary build directory
mkdir -p "$TEMP_DIR/binaryDependencies"
# Copy only the necessary dependencies to temp dir
log "Preparing dependencies for pelfCreator"
cp "$BASE/pelf" "$TEMP_DIR/binaryDependencies/pelf" || log_error "Unable to move pelf to the binaryDependencies of pelfCreator"
checkElf "$TEMP_DIR/binaryDependencies/pelf"
# Get the unionfs and bwrap binaries
mkdir -p "$TEMP_DIR/binaryDependencies"
DBIN_INSTALL_DIR="$TEMP_DIR/binaryDependencies" dbin add unionfs-fuse3/unionfs bwrap
checkElf "$TEMP_DIR/binaryDependencies/unionfs"
checkElf "$TEMP_DIR/binaryDependencies/bwrap"
# Copy AppRun assets
if [ -d "$BASE/assets" ]; then
cp "$BASE/assets/AppRun"* "$BASE/assets/LAUNCH"* "$TEMP_DIR/binaryDependencies/" 2>/dev/null || log_warning "AppRun assets not found"
else
log_warning "assets directory not found, AppRun files might be missing"
fi
cat <<'EOF' > "$TEMP_DIR/binaryDependencies/pkgadd.sh"
#!/bin/sh
fakeroot apk \
--allow-untrusted \
--no-interactive \
--no-cache \
--initdb add \
$@ || true
# Check if each package in $@ is installed
for pkg in "$@"; do
if ! fakeroot apk info | grep -q "^${pkg}$" >/dev/null 2>&1; then
echo "error: Package $pkg not installed" >&2
exit 1
fi
done
# Get version of the first package ($1)
if [ -n "$1" ]; then
version=$(fakeroot apk info "$1" 2>/dev/null | head -n 1 | cut -d' ' -f1 | cut -d'-' -f2-)
if [ -n "$version" ]; then
# Blue color for NOTE using ANSI escape codes
printf "\033[34mNOTICE\033[0m: using %s's version as the AppBundle's version: [%s]\n" "$1" "$version"
else
echo "error: could not retrieve version for $1" >&2
exit 1
fi
fi
EOF
chmod +x "$TEMP_DIR/binaryDependencies/pkgadd.sh"
if [ ! -f "$TEMP_DIR/binaryDependencies/rootfs.tar.zst" ]; then
log "Downloading rootfs"
RELEASE_NAME="AlpineLinux_edge-$(uname -m).tar.xz"
fetchFromGithub "xplshn/filesystems" "latest" "$RELEASE_NAME" "$TEMP_DIR/binaryDependencies/$RELEASE_NAME"
cd "$TEMP_DIR/binaryDependencies" || log_error "Failed to change to temp directory"
ln -sfT "$RELEASE_NAME" "rootfs.tar.${RELEASE_NAME##*.}"
fi
if [ ! -f "$TEMP_DIR/binaryDependencies/sharun" ]; then
log "Downloading sharun-$(uname -m)-aio"
fetchFromGithub "VHSgunzo/sharun" "latest" "sharun-$(uname -m)-aio" "$TEMP_DIR/binaryDependencies/sharun"
checkElf "$TEMP_DIR/binaryDependencies/sharun"
chmod +x "$TEMP_DIR/binaryDependencies/sharun"
fi
unnappear rm -rf "$BASE/cmd/pelfCreator/binaryDependencies"
mv "$TEMP_DIR/binaryDependencies" "$BASE/cmd/pelfCreator/binaryDependencies" || log_error "Unable to move binaryDependencies from temp to pelfCreator"
# Create archive of binaryDependencies
log "Creating binaryDependencies.tar.zst for pelfCreator"
tar -C "$BASE/cmd/pelfCreator/binaryDependencies" -c . | zstd -T0 -19 -fo "$BASE/cmd/pelfCreator/binaryDependencies.tar.zst"
log "Building pelfCreator"
cd "$BASE/cmd/pelfCreator" || log_error "Unable to change directory to ./cmd/pelfCreator"
go build || log_error "Unable to build pelfCreator"
if available "upx"; then
log "Compressing ./pelfCreator tool"
checkElf "./pelfCreator"
upx ./pelfCreator || log_error "unable to compress ./pelfCreator"
rm -f ./pelfCreator.upx
else
log_warning "upx not available. The resulting binary will be unnecessarily large"
fi
cd "$BASE" || log_error "Unable to go back to $BASE"
# Clean up temporary directory
rm -rf "$TEMP_DIR"
}
build_pelfCreator_extensions() {
log "Building pelfCreator extensions"
if [ ! -d "$BASE/cmd/pelfCreator/binaryDependencies" ]; then
log_error "pelfCreator must be built first. Run './cbuild.sh pelfCreator' before building extensions"
fi
mkdir -p "$TEMP_DIR/binaryDependencies"
# Copy existing dependencies (excluding rootfs)
log "Copying dependencies from pelfCreator binaryDependencies (excluding rootfs)"
for file in "$BASE/cmd/pelfCreator/binaryDependencies"/*; do
[ -f "$file" ] || continue
filename=$(basename "$file")
case "$filename" in
AppRun* | LAUNCH* | *.tar* | pkgadd.sh )
log "Skipping $filename"
;;
*)
cp "$file" "$TEMP_DIR/binaryDependencies/"
checkElf "$TEMP_DIR/binaryDependencies/$filename"
;;
esac
done
# Build ArchLinux extension
log "Creating pelfCreator ArchLinux extension"
cat <<'EOF' > "$TEMP_DIR/binaryDependencies/pkgadd.sh"
#!/bin/sh
fakeroot pacman -Sy --noconfirm $@
EOF
chmod +x "$TEMP_DIR/binaryDependencies/pkgadd.sh"
# Download ArchLinux rootfs
if [ ! -f "$TEMP_DIR/binaryDependencies/rootfs.tar.zst" ]; then
log "Downloading ArchLinux rootfs"
RELEASE_NAME="ArchLinux-base_$(uname -m).tar.zst"
fetchFromGithub "xplshn/filesystems" "latest" "$RELEASE_NAME" "$TEMP_DIR/binaryDependencies/$RELEASE_NAME"
cd "$TEMP_DIR/binaryDependencies" || log_error "Failed to change to temp directory"
ln -sfT "$RELEASE_NAME" "rootfs.tar.${RELEASE_NAME##*.}"
cd "$BASE" || log_error "Unable to return to base directory"
fi
# Create the extension archive
log "Creating pelfCreatorExtension_archLinux.tar.zst"
tar -C "$TEMP_DIR/binaryDependencies" -c . | zstd -T0 -19 -fo "$BASE/cmd/pelfCreator/pelfCreatorExtension_archLinux.tar.zst"
# Clean up
rm -rf "$TEMP_DIR"
log "pelfCreator ArchLinux extension created successfully"
}
build_appstream_helper() {
log "Building appstream-helper"
cd "$BASE/cmd/misc/appstream-helper" || log_error "Unable to change directory to ./cmd/misc/appstream-helper"
go build || log_error "Unable to build appstream-helper"
if available "upx"; then
log "Compressing ./appstream-helper tool"
checkElf "./appstream-helper"
upx ./appstream-helper
else
log_warning "upx not available. The resulting binary will be unnecessarily large"
fi
cd "$BASE" || log_error "Unable to go back to $BASE"
}
clean_project() {
log "Starting clean process"
rm -rf ./pelf ./pelf.upx ./binaryDependencies ./binaryDependencies.tar.zst ./appbundle-runtime/binaryDependencies ./cmd/pelfCreator/pelfCreator ./cmd/pelfCreator/binaryDependencies* ./cmd/pelfCreator/*.zst.tar ./cmd/misc/appstream-helper/appstream-helper ./cmd/misc/appstream-helper/appstream-helper.upx
log "Clean process completed"
}
retrieve_executable() {
readlink -f ./pelf
readlink -f ./cmd/pelfCreator/pelfCreator
readlink -f ./cmd/misc/appstream-helper/appstream-helper
readlink -f ./cmd/pelfCreator/binaryDependencies_archLinux.tar.zst
}
handle_dependencies() {
mkdir -p "$DBIN_INSTALL_DIR"
DEPS="bintools/objcopy
squashfs-tools/mksquashfs
squashfs-tools/unsquashfs" #squashfuse/squashfuse_ll
unnappear rm "$DBIN_INSTALL_DIR/dwarfs-tools"
fetchFromGithub "mhx/dwarfs" "v$DWFS_VER" "dwarfs-universal-$DWFS_VER-Linux-$(uname -m)" "$DBIN_INSTALL_DIR/dwarfs-tools"
checkElf "$DBIN_INSTALL_DIR/dwarfs-tools"
chmod +x "$DBIN_INSTALL_DIR/dwarfs-tools"
fetchFromGithub "VHSgunzo/squashfuse-static" "latest" "squashfuse_ll-musl-mimalloc-$(uname -m)" "$DBIN_INSTALL_DIR/squashfuse_ll"
checkElf "$DBIN_INSTALL_DIR/squashfuse_ll"
chmod +x "$DBIN_INSTALL_DIR/squashfuse_ll"
log "Installing dependencies..."
# shellcheck disable=SC2086
dbin add $DEPS
for dep in $DEPS; do
dep_name=$(echo "$dep" | cut -d'/' -f2)
checkElf "$DBIN_INSTALL_DIR/$dep_name"
done
cd "$DBIN_INSTALL_DIR" && {
log "Linking dependencies"
[ -f ./dwarfs-tools ] && [ ! -h ./dwarfs-tools ] && {
mv ./dwarfs-tools ./dwarfs
ln -sfT dwarfs mkdwarfs
}
ln -sfT dwarfs dwarfsextract
upx mksquashfs mkdwarfs objcopy
[ -f ./squashfuse_ll ] && [ ! -h ./squashfuse_ll ] && mv ./squashfuse_ll ./squashfuse
ln -sfT squashfuse squashfuse_ll
}
unnappear rm ./*.upx
cd "$BASE" || log_error "Unable to go back to $BASE"
}
update_dependencies() {
dbin update
}
# Main case statement for actions
case "$1" in
"" | "build")
require go
log "Starting build process for targets: pelf, pelfCreator, appstream-helper"
build_pelf
build_pelfCreator
build_appstream_helper
;;
"pelf")
require go
log "Starting build process for target: pelf"
build_pelf
;;
"appbundle-runtime")
require go
log "Starting build process for target: appbundle-runtime"
build_appbundle_runtime
;;
"pelfCreator")
require go
log "Starting build process for target: pelfCreator"
build_pelf
build_pelfCreator
;;
"pelfCreator_extensions")
log "Starting build process for target: pelfCreator_extensions"
build_pelfCreator_extensions
# TODO: Add moar
;;
"appstream-helper")
require go
log "Starting build process for target: appstream-helper"
build_appstream_helper
;;
"clean")
clean_project
;;
"retrieve")
retrieve_executable
;;
"update-deps")
update_dependencies
;;
*)
log_warning "Usage: $0 {build|pelf|appbundle-runtime|pelfCreator|pelfCreator_extensions|appstream-helper|clean|retrieve|update-deps}"
exit 1
;;
esac