-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.sh
executable file
·479 lines (398 loc) · 12.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
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
477
478
479
#!/bin/sh
printf "\nMURCB - muOS RetroArch Core Builder\n"
# Show 'build.sh' USAGE options
USAGE() {
echo "Usage: $0 [options]"
echo ""
echo "Options:"
echo " -a, --all Build all cores"
echo " -c, --core [cores] Build specific cores (e.g., -c dosbox-pure sameboy)"
echo " -p, --purge Purge cores directory before building"
echo " -u, --update Combine all core archives into a single update archive"
echo ""
echo "Notes:"
echo " - Either -a, -c, or -u is required, but NOT together"
echo " - If -p is used, it MUST be the first argument"
echo " - The -u switch must have a storage pointer (e.g., -u mmc)"
echo ""
echo "Examples:"
echo " $0 -a"
echo " $0 -c dosbox-pure sameboy"
echo " $0 -p -a"
echo " $0 -p -c dosbox-pure sameboy"
echo " $0 -u mmc"
echo ""
exit 1
}
# Initialise all options to 0
PURGE=0
BUILD_ALL=0
BUILD_CORES=""
OPTION_SPECIFIED=0
UPDATE=0
STORAGE_POINTER=x
# If argument '-p' or '--purge' provided, set PURGE=1
if [ "$#" -gt 0 ]; then
case "$1" in
-p | --purge)
PURGE=1
shift
;;
esac
fi
# If no argument(s) provided show USAGE
if [ "$#" -eq 0 ]; then
USAGE
fi
# Cechk for remaining arguments and set appropriate options
while [ "$#" -gt 0 ]; do
case "$1" in
-a | --all)
[ "$OPTION_SPECIFIED" -ne 0 ] && USAGE
BUILD_ALL=1
OPTION_SPECIFIED=1
shift
;;
-c | --core)
[ "$OPTION_SPECIFIED" -ne 0 ] && USAGE
OPTION_SPECIFIED=1
shift
if [ "$#" -eq 0 ]; then
printf "Error: Missing cores\n\n" >&2
USAGE
fi
BUILD_CORES="$*"
break
;;
-u | --update)
[ "$OPTION_SPECIFIED" -ne 0 ] && USAGE
OPTION_SPECIFIED=1
shift
if [ "$#" -eq 0 ]; then
printf "Error: Missing storage pointer\n\n" >&2
USAGE
fi
STORAGE_POINTER="$1"
shift
[ -z "$STORAGE_POINTER" ] && {
printf "Error: Invalid storage pointer\n"
exit 1
}
UPDATE=1
;;
*)
printf "Error: Unknown option '%s'\n" "$1" >&2
USAGE
;;
esac
done
# Confirm a valid argument was provided, else show USAGE
[ "$OPTION_SPECIFIED" -eq 0 ] && USAGE
# Initialise directory variables
BASE_DIR=$(pwd)
CORE_CONFIG="core.json"
BUILD_DIR="$BASE_DIR/build"
CORES_DIR="$BASE_DIR/cores"
PATCH_DIR="$BASE_DIR/patch"
# Create an update zip containing all cores
UPDATE_ZIP() {
UPDATE_ARCHIVE="muOS-RetroArch-Core_Update-$(date +"%Y-%m-%d_%H-%M").zip"
TEMP_DIR="$(mktemp -d)"
CORE_FOLDER="$TEMP_DIR/mnt/$STORAGE_POINTER/MUOS/core"
if [ -z "$(ls "$BUILD_DIR"/*.zip 2>/dev/null)" ]; then
printf "No ZIP files found in '%s'\n" "$BUILD_DIR" >&2
rmdir "$TEMP_DIR"
exit 1
fi
mkdir -p "$CORE_FOLDER"
printf "Extracting all ZIP files from '%s' into '%s'\n" "$BUILD_DIR" "$CORE_FOLDER"
for ZIP_FILE in "$BUILD_DIR"/*.zip; do
printf "Unpacking '%s'...\n" "$(basename "$ZIP_FILE")"
unzip -q "$ZIP_FILE" -d "$CORE_FOLDER" || {
printf "Failed to unpack '%s'\n" "$(basename "$ZIP_FILE")" >&2
rm -rf "$TEMP_DIR"
exit 1
}
done
printf "Creating consolidated update archive: %s\n" "$UPDATE_ARCHIVE"
(cd "$TEMP_DIR" && zip -q -r "$BASE_DIR/$UPDATE_ARCHIVE" .) || {
printf "Failed to create update archive\n" >&2
rm -rf "$TEMP_DIR"
exit 1
}
rm -rf "$TEMP_DIR"
printf "Update archive created successfully: %s\n" "$BASE_DIR/$UPDATE_ARCHIVE"
exit 0
}
[ "$UPDATE" -eq 1 ] && UPDATE_ZIP
for CMD in aarch64-linux-objcopy aarch64-linux-strip file git jq make patch pv readelf zip; do
if ! command -v "$CMD" >/dev/null 2>&1; then
printf "Error: Missing required command '%s'\n" "$CMD" >&2
exit 1
fi
done
# Create required directories
mkdir -p "$BUILD_DIR"
mkdir -p "$CORES_DIR"
trap 'printf "\nAn error occurred. Returning to base directory.\n"; cd "$BASE_DIR"; exit 1' INT TERM
RETURN_TO_BASE() {
cd "$BASE_DIR" || {
printf "Failed to return to base directory\n" >&2
exit 1
}
}
RUN_COMMANDS() {
printf "\nRunning '%s' commands\n" "$1"
CMD_LIST=$(echo "$2" | jq -r '.[]')
# Run through the list of given commands in the array and use an EOF to run them outside of this subshell
while IFS= read -r CMD; do
CMD=$(eval "echo \"$CMD\"")
# Skip "Running" message for commands starting with 'printf'
if ! echo "$CMD" | grep -qE '^printf'; then
printf "Running: %s\n" "$CMD"
fi
eval "$CMD" || {
printf "Command Failed: %s\n" "$CMD" >&2
return 1
}
done <<EOF
$CMD_LIST
EOF
}
APPLY_PATCHES() {
NAME="$1"
CORE_DIR="$2"
if [ -d "$PATCH_DIR/$NAME" ]; then
printf "Applying patches from '%s' to '%s'\n" "$PATCH_DIR/$NAME" "$CORE_DIR"
for PATCH in "$PATCH_DIR/$NAME"/*.patch; do
[ -e "$PATCH" ] || continue
printf "Applying patch: %s\n" "$PATCH"
patch -d "$CORE_DIR" -p1 <"$PATCH" || {
printf "Failed to apply patch: %s\n" "$PATCH" >&2
return 1
}
done
printf "\n"
fi
}
# Get specific core names or process all cores given as arguments
if [ "$BUILD_ALL" -eq 0 ]; then
CORES="$BUILD_CORES"
else
CORES=$(jq -r 'keys[]' "$CORE_CONFIG")
fi
# Load the cache file
CACHE_FILE="$BASE_DIR/cache.json"
if [ ! -f "$CACHE_FILE" ]; then
echo "{}" > "$CACHE_FILE"
fi
for NAME in $CORES; do
printf "\n-------------------------------------------------------------------------\n"
MODULE=$(jq -c --arg name "$NAME" '.[$name]' "$CORE_CONFIG")
if [ -z "$MODULE" ] || [ "$MODULE" = "null" ]; then
printf "Core '%s' not found in '%s'\n" "$NAME" "$CORE_CONFIG" >&2
continue
fi
# Required keys
DIR=$(echo "$MODULE" | jq -r '.directory')
OUTPUT=$(echo "$MODULE" | jq -r '.output')
SOURCE=$(echo "$MODULE" | jq -r '.source')
SYMBOLS=$(echo "$MODULE" | jq -r '.symbols')
# Make keys
MAKE_FILE=$(echo "$MODULE" | jq -r '.make.file')
MAKE_ARGS=$(echo "$MODULE" | jq -r '.make.args')
MAKE_TARGET=$(echo "$MODULE" | jq -r '.make.target')
# Verify required keys
if [ -z "$DIR" ] || [ -z "$OUTPUT" ] || [ -z "$SOURCE" ] || [ -z "$MAKE_FILE" ] || [ -z "$SYMBOLS" ]; then
printf "Missing required configuration keys for '%s' in '%s'\n" "$NAME" "$CORE_CONFIG" >&2
continue
fi
# Optional branch
BRANCH=$(echo "$MODULE" | jq -r '.branch // ""')
# Optional keys
PRE_MAKE=$(echo "$MODULE" | jq -c '.commands["pre-make"] // []')
POST_MAKE=$(echo "$MODULE" | jq -c '.commands["post-make"] // []')
CORE_DIR="$CORES_DIR/$DIR"
printf "Processing: %s\n\n" "$NAME"
# Get cached hash
CACHED_HASH=$(jq -r --arg name "$NAME" '.[$name] // ""' "$CACHE_FILE")
# Get remote hash before cloning
if [ -n "$BRANCH" ]; then
REMOTE_HASH=$(git ls-remote "$SOURCE" "refs/heads/$BRANCH" | cut -c 1-7)
else
REMOTE_HASH=$(git ls-remote "$SOURCE" HEAD | cut -c 1-7)
fi
if [ -z "$REMOTE_HASH" ]; then
printf "Failed to get remote hash for '%s'\n" "$NAME" >&2
continue
fi
printf "Remote hash: %s\n" "$REMOTE_HASH"
printf "Cached hash: %s\n" "$CACHED_HASH"
if [ "$CACHED_HASH" = "$REMOTE_HASH" ] && [ "$PURGE" -eq 0 ]; then
printf "Core '%s' is up to date (hash: %s). Skipping build.\n" "$NAME" "$REMOTE_HASH"
continue
fi
if [ "$PURGE" -eq 1 ]; then
printf "Purging core '%s' directory\n" "$DIR"
rm -rf "$CORE_DIR"
fi
BEEN_CLONED=0
if [ ! -d "$CORE_DIR" ]; then
printf "Core '%s' not found\n\n" "$DIR" "$SOURCE"
GC_CMD="git clone --progress --quiet --recurse-submodules -j$(nproc)"
[ -n "$BRANCH" ] && GC_CMD="$GC_CMD -b $BRANCH"
GC_CMD="$GC_CMD $SOURCE $CORE_DIR"
eval "$GC_CMD" || {
printf "Failed to clone %s\n" "$SOURCE" >&2
continue
}
# Enter the directory and update submodules
cd "$CORE_DIR" || {
printf "Failed to enter directory %s\n" "$CORE_DIR" >&2
continue
}
# Update all submodules recursively
git submodule update --init --recursive || {
printf "Failed to update submodules for %s\n" "$SOURCE" >&2
cd - > /dev/null # Return to previous directory
continue
}
# Return to previous directory
cd - > /dev/null
printf "\n"
BEEN_CLONED=1
fi
APPLY_PATCHES "$NAME" "$CORE_DIR" || {
printf "Failed to apply patches for %s\n" "$NAME" >&2
continue
}
cd "$CORE_DIR" || {
printf "Failed to enter directory %s\n" "$CORE_DIR" >&2
continue
}
if [ $BEEN_CLONED -eq 0 ]; then
printf "Pulling latest changes for '%s'\n" "$NAME"
git pull --quiet --recurse-submodules -j8 || {
printf "Failed to pull latest changes for '%s'\n" "$NAME" >&2
RETURN_TO_BASE
continue
}
fi
# Verify local hash matches remote hash after clone/pull
LOCAL_HASH=$(git rev-parse --short HEAD | cut -c 1-7)
if [ "$LOCAL_HASH" != "$REMOTE_HASH" ]; then
printf "Warning: Local hash (%s) doesn't match remote hash (%s)\n" "$LOCAL_HASH" "$REMOTE_HASH" >&2
RETURN_TO_BASE
continue
fi
if [ "$PRE_MAKE" != "[]" ]; then
if ! RUN_COMMANDS "pre-make" "$PRE_MAKE"; then
printf "Pre-make commands failed for %s\n" "$NAME" >&2
RETURN_TO_BASE
continue
fi
fi
printf "Make Structure:"
printf "\n\tFILE:\t%s" "$MAKE_FILE"
printf "\n\tARGS:\t%s" "$MAKE_ARGS"
printf "\n\tTARGET: %s\n" "$MAKE_TARGET"
printf "\nBuilding '%s' (%s) ..." "$NAME" "$OUTPUT"
(while :; do
printf '.'
sleep 1
done) | pv -q -L 10 -N "Building $NAME" &
PV_PID=$!
trap 'kill $PV_PID 2>/dev/null' EXIT
MAKE_CMD="make -j$(nproc)"
[ -n "$MAKE_FILE" ] && MAKE_CMD="$MAKE_CMD -f $MAKE_FILE"
[ -n "$MAKE_ARGS" ] && MAKE_CMD="$MAKE_CMD $MAKE_ARGS"
[ -n "$MAKE_TARGET" ] && MAKE_CMD="$MAKE_CMD $MAKE_TARGET"
# Run the command
if $MAKE_CMD >/dev/null 2>&1; then
kill $PV_PID
wait $PV_PID 2>/dev/null
printf "\nBuild completed successfully for '%s'\n" "$NAME"
# Update cache with new hash
jq --arg name "$NAME" --arg hash "$REMOTE_HASH" '.[$name] = $hash' "$CACHE_FILE" > "$CACHE_FILE.tmp" && mv "$CACHE_FILE.tmp" "$CACHE_FILE"
else
kill $PV_PID
wait $PV_PID 2>/dev/null
printf "\nBuild failed for '%s' using '%s'\n" "$NAME" "$MAKE_FILE" >&2
RETURN_TO_BASE
continue
fi
if [ "$POST_MAKE" != "[]" ]; then
if ! RUN_COMMANDS "post-make" "$POST_MAKE"; then
printf "Post-make commands failed for '%s'\n" "$NAME" >&2
RETURN_TO_BASE
continue
fi
fi
if [ "$SYMBOLS" -eq 0 ]; then
# Check if the output is not stripped already
if file "$OUTPUT" | grep -q 'not stripped'; then
aarch64-linux-strip -sx "$OUTPUT"
printf "\nStripped debug symbols"
fi
# Check if the BuildID section is present
if readelf -S "$OUTPUT" | grep -Fq '.note.gnu.build-id'; then
aarch64-linux-objcopy --remove-section=.note.gnu.build-id "$OUTPUT"
printf "\nRemoved BuildID section"
fi
fi
printf "\nFile Information: %s\n" "$(file -b "$OUTPUT")"
printf "\nMoving '%s' to '%s'\n" "$OUTPUT" "$BUILD_DIR"
mv "$OUTPUT" "$BUILD_DIR" || {
printf "Failed to move '%s' for '%s' to '%s'\n" "$OUTPUT" "$NAME" "$BUILD_DIR" >&2
RETURN_TO_BASE
continue
}
printf "\nIndexing and compressing '%s'\n" "$OUTPUT"
cd "$BUILD_DIR" || {
printf "Failed to enter directory %s\n" "$BUILD_DIR" >&2
continue
}
INDEX=$(printf "%s %08x %s" "$(date +%Y-%m-%d)" "$(cksum "$OUTPUT" | awk '{print $1}')" "$OUTPUT.zip")
[ -f "$OUTPUT.zip" ] && rm -f "$OUTPUT.zip"
zip -q "$OUTPUT.zip" "$OUTPUT"
rm "$OUTPUT"
ESCAPED_OUTPUT_ZIP=$(printf "%s" "$OUTPUT.zip" | sed 's/[\\/&]/\\&/g')
if [ -f .index-extended ]; then
sed "/$ESCAPED_OUTPUT_ZIP/d" .index-extended >.index-extended.tmp
mv .index-extended.tmp .index-extended
else
touch .index-extended
fi
echo "$INDEX" >>.index-extended
if [ -f .index ]; then
sed "/$ESCAPED_OUTPUT_ZIP/d" .index >.index.tmp
mv .index.tmp .index
else
touch .index
fi
echo "$OUTPUT.zip" >>.index
sort -k3 .index-extended -o .index-extended
sort .index -o .index
if [ "$PURGE" -eq 1 ]; then
printf "\nPurging core directory: %s\n" "$CORE_DIR"
rm -rf "$CORE_DIR"
else
printf "Cleaning build environment for '%s'\n" "$NAME"
make clean >/dev/null 2>&1 || {
printf "Clean failed or not required\n" >&2
}
fi
RETURN_TO_BASE
done
(
printf "<!DOCTYPE html>\n<html>\n<head>\n<title>MURCB - muOS RetroArch Core Builder</title>\n</head>\n<body>\n"
printf "<pre style='font-size:2rem;margin-top:-5px;margin-bottom:-15px;'>MURCB - muOS RetroArch Core Builder</pre>\n"
printf "<pre style='font-size:1rem;'>Currently only <span style='font-weight:800'>"
printf "aarch64"
printf "</span> builds for now!</pre>\n"
printf "<hr>\n<pre>\n"
[ -f "$BUILD_DIR/.index-extended" ] && cat "$BUILD_DIR/.index-extended" || printf "No cores available!\n"
printf "</pre>\n</body>\n</html>\n"
) >"$BUILD_DIR/index.html"
printf "\n-------------------------------------------------------------------------\n"
printf "All successful core builds are in '%s'\n\n" "$BUILD_DIR"