-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_container.sh
executable file
·96 lines (86 loc) · 2.64 KB
/
build_container.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
#!/usr/bin/env bash
set -o errexit -o pipefail -o noclobber -o nounset
PWD="$(pwd)"
PROJECT_DIR_NAME="PSX-Minecraft"
# Ensure we work from the project base dir to avoid
# weird mounting behaviour when running container
case "$(basename "$PWD")" in
"$PROJECT_DIR_NAME") ;;
*)
echo "[ERROR] This script must be run from the $PROJECT_DIR_NAME directory, not $PWD"
exit 1
;;
esac
# Ignore errexit with `&& true`
getopt --test > /dev/null && true
if [[ $? -ne 4 ]]; then
echo '[ERROR] getopt invocation failed.'
exit 1
fi
function printHelp() {
echo "Usage: ./build_container.sh [<options>]"
echo "Options:"
echo " -h | --help Print this help message"
echo " -r | --rebuild Clean the build directory and initialise CMake again (default: false)"
echo " -o | --output=<dir> Set the directory to use as the build output (default: ./build)"
echo " -i | --image=<image:tag> Specify which image to use when building (default: psxmc:latest)"
}
# Note that options with a ':' require an argument
LONGOPTS=help,rebuild,output:,image:
OPTIONS=hro:i:
# 1. Temporarily store output to be able to check for errors
# 2. Activate quoting/enhanced mode (e.g. by writing out “--options”)
# 3. Pass arguments only via -- "$@" to separate them correctly
# 4. If getopt fails, it complains itself to stdout
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@") || exit 2
# Read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
rebuild=0
outDir="./build"
image="psxmc:latest"
# Handle options in order and nicely split until we see --
while true; do
case "$1" in
-h|--help)
printHelp
exit 1
;;
-r|--rebuild)
rebuild=1
shift
;;
-o|--output)
outDir="$2"
shift 2
;;
-i|--image)
image="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "[ERROR] Unknown option encountered: $1"
exit 3
;;
esac
done
# Construct the command to run in the container,
# only invoking a cmake build recreation when the
# flag is set
COMMAND="python3 scripts/asset_bundler.py"
if [ $rebuild -eq 0 ]; then
COMMAND="$COMMAND && cmake --build $outDir"
else
rm -r "$outDir"
echo "Removed build directory at: $outDir"
COMMAND="$COMMAND && cmake --preset=default . && cmake --build $outDir"
fi
docker run -it \
--rm \
-v "$(pwd):/tmp/PSX-Minecraft" \
-w "/tmp/PSX-Minecraft" \
"$image" \
/bin/bash -c "$COMMAND"