-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·286 lines (249 loc) · 8.65 KB
/
build.sh
File metadata and controls
executable file
·286 lines (249 loc) · 8.65 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
#!/bin/bash
set -euo pipefail
# ### HELP_START ###
# ==============================================================================
# gkNextRenderer Build Script v2.2 (Linux/macOS)
# Wraps CMake Presets for a streamlined build workflow.
# Supports incremental configuration (skips configure if already configured).
#
# Usage:
# ./build.sh [options] [-- <cmake_args>...]
#
# Options:
# --preset <name> CMake configure preset (e.g., default-linux) [REQUIRED]
# --clean Clean build directory before building.
# --reconfigure Force CMake reconfiguration even if already configured.
# --android Switch to Android Gradle build.
# --help, -h Show this help message.
#
# Examples:
# ./build.sh --preset full-linux
# ./build.sh --preset default-linux -- -DENABLE_AVIF=ON
# ./build.sh --preset default-linux --clean
# ./build.sh --preset default-linux --reconfigure
# ./build.sh --preset default-mingw
# ==============================================================================
# ### HELP_END ###
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$SCRIPT_DIR"
# Timing
TOTAL_START=$(date +%s)
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
log() { printf "${GREEN}[build] %s${NC}\n" "$*"; }
warn() { printf "${YELLOW}[build] Warning: %s${NC}\n" "$*" >&2; }
err() { printf "${RED}[build] Error: %s${NC}\n" "$*" >&2; exit 1; }
print_err() { printf "${RED}[build] Error: %s${NC}\n" "$*" >&2; }
has_pkg_config_module() {
local module="$1"
pkg-config --exists "$module" >/dev/null 2>&1
}
show_linux_dependency_hint_and_exit() {
local missing=("$@")
print_err "Missing required Linux desktop development packages: ${missing[*]}"
case "$(uname -s)" in
Linux*)
if [ -f /etc/arch-release ] || [ -f /etc/manjaro-release ] || [ -f /etc/endeavouros-release ]; then
printf "${YELLOW}[build] Install them on Arch/Steam Deck with:${NC}\n" >&2
printf " sudo pacman -S --needed libxrandr wayland-protocols libxkbcommon\n" >&2
elif command -v apt-get >/dev/null 2>&1; then
printf "${YELLOW}[build] Install them on Debian/Ubuntu with:${NC}\n" >&2
printf " sudo apt install libxrandr-dev wayland-protocols libxkbcommon-dev\n" >&2
else
printf "${YELLOW}[build] Install packages that provide these pkg-config modules:${NC}\n" >&2
printf " xrandr wayland-protocols xkbcommon\n" >&2
fi
;;
esac
exit 1
}
ensure_linux_desktop_packages() {
local preset="$1"
local missing=()
if [[ "$preset" != *linux* && "$preset" != *mingw* ]]; then
return 0
fi
if [ "$(uname -s)" != "Linux" ]; then
return 0
fi
has_pkg_config_module xrandr || missing+=("xrandr")
has_pkg_config_module wayland-protocols || missing+=("wayland-protocols")
has_pkg_config_module xkbcommon || missing+=("xkbcommon")
if [ ${#missing[@]} -gt 0 ]; then
show_linux_dependency_hint_and_exit "${missing[@]}"
fi
}
ensure_slangc() {
local preset="$1"
local fetch_script="$PROJECT_ROOT/tools/fetch_slang_linux.sh"
if [[ "$preset" != *linux* && "$preset" != *macos* ]]; then
return 0
fi
if command -v slangc >/dev/null 2>&1; then
return 0
fi
if find "$PROJECT_ROOT/external" -maxdepth 2 -type f -name slangc >/dev/null 2>&1; then
return 0
fi
if [ "$(uname -s)" = "Linux" ] && [ -x "$fetch_script" ]; then
log "slangc not found. Fetching project-managed Slang toolchain..."
"$fetch_script"
return 0
fi
warn "slangc not found in PATH or under external/. CMake configure may fail."
}
# ==============================================================================
# Helper Functions
# ==============================================================================
detect_default_preset() {
case "$(uname -s)" in
Darwin*)
if [ "$(uname -m)" = "arm64" ]; then echo "default-macos-arm64"; else echo "default-macos-x64"; fi ;;
Linux*) echo "default-linux" ;;
MINGW*|MSYS*) echo "default-mingw" ;;
*) echo "unknown" ;;
esac
}
ensure_vcpkg() {
if [ ! -f "$PROJECT_ROOT/.vcpkg/scripts/buildsystems/vcpkg.cmake" ]; then
log "vcpkg toolchain not found. Bootstrapping..."
"$PROJECT_ROOT/scripts/vcpkg.sh"
fi
}
show_help() {
sed -n '/### HELP_START ###/,/### HELP_END ###/p' "$0" | \
grep -v '### HELP_START ###' | grep -v '### HELP_END ###' | \
cut -c3- # Remove leading '# '
exit 0
}
list_presets_and_exit() {
print_err "$1"
log "Available configure presets:"
cmake --list-presets=configure | sed 's/^/ /'
exit 1
}
# ==============================================================================
# Main Logic
# ==============================================================================
CONFIGURE_PRESET=""
CLEAN=0
RECONFIGURE=0
TARGET_ANDROID=0
declare -a CMAKE_ARGS=()
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--clean) CLEAN=1; shift ;;
--reconfigure) RECONFIGURE=1; shift ;;
--android) TARGET_ANDROID=1; shift ;;
--preset)
if [[ -z "$2" || "$2" == --* ]]; then
list_presets_and_exit "--preset option requires a value."
fi
CONFIGURE_PRESET="$2"
shift 2
;;
--preset=*) CONFIGURE_PRESET="${1#*=}"; shift ;;
--help|-h) show_help ;;
--) shift; CMAKE_ARGS+=("$@"); break ;;
*) CMAKE_ARGS+=("$1"); shift ;;
esac
done
# Android Build
if [ "$TARGET_ANDROID" -eq 1 ]; then
log "Building for Android..."
cd "$PROJECT_ROOT/android"
./gradlew build
exit $?
fi
# Native Build
if [ -z "$CONFIGURE_PRESET" ]; then
list_presets_and_exit "No preset specified. You must explicitly specify a preset."
fi
ensure_vcpkg
ensure_linux_desktop_packages "$CONFIGURE_PRESET"
ensure_slangc "$CONFIGURE_PRESET"
BUILD_DIR="$PROJECT_ROOT/out/build/$CONFIGURE_PRESET"
CACHE_FILE="$BUILD_DIR/CMakeCache.txt"
if [ "$CLEAN" -eq 1 ]; then
log "Cleaning build for preset: $CONFIGURE_PRESET..."
rm -rf "$BUILD_DIR"
fi
# Check if reconfiguration is needed
NEEDS_CONFIGURE=0
if [ "$RECONFIGURE" -eq 1 ] || [ "$CLEAN" -eq 1 ] || [ ! -f "$CACHE_FILE" ]; then
NEEDS_CONFIGURE=1
fi
# Check if any -D arguments are passed (requires reconfigure)
if [ ${#CMAKE_ARGS[@]} -gt 0 ]; then
for arg in "${CMAKE_ARGS[@]}"; do
if [[ "$arg" == -D* ]]; then
NEEDS_CONFIGURE=1
break
fi
done
fi
config_time=0
if [ "$NEEDS_CONFIGURE" -eq 1 ]; then
if [ ${#CMAKE_ARGS[@]} -eq 0 ]; then
log "Configuring preset: $CONFIGURE_PRESET"
else
log "Configuring preset: $CONFIGURE_PRESET with extra args: ${CMAKE_ARGS[*]}"
fi
config_start=$(date +%s)
if [ ${#CMAKE_ARGS[@]} -eq 0 ]; then
cmake --preset "$CONFIGURE_PRESET"
else
cmake --preset "$CONFIGURE_PRESET" "${CMAKE_ARGS[@]}"
fi
config_time=$(( $(date +%s) - config_start ))
else
log "Skipping configure (already configured). Use --reconfigure to force."
fi
# Derive build preset name from configure preset name
# In current CMakePresets.json, build presets map 1:1 to configure presets
BUILD_PRESET="$CONFIGURE_PRESET"
log "Building with preset: $BUILD_PRESET"
build_start=$(date +%s)
# Pass only build-specific args to the build command
declare -a build_args=()
is_target_next=0
if [ ${#CMAKE_ARGS[@]} -gt 0 ]; then
for arg in "${CMAKE_ARGS[@]}"; do
if [ $is_target_next -eq 1 ]; then
build_args+=("$arg")
is_target_next=0
continue
fi
if [[ "$arg" == "--target" ]]; then
build_args+=("$arg")
is_target_next=1
elif [[ "$arg" == --* ]]; then
# Heuristic: pass common build tool arguments
if [[ "$arg" == "--config" || "$arg" == "-j" || "$arg" == "--verbose" ]]; then
build_args+=("$arg")
# if the arg is like --config=Release
if [[ "$arg" != *=* ]]; then
is_target_next=1
fi
fi
fi
done
fi
if [ ${#build_args[@]} -eq 0 ]; then
cmake --build --preset "$BUILD_PRESET"
else
cmake --build --preset "$BUILD_PRESET" "${build_args[@]}"
fi
build_time=$(( $(date +%s) - build_start ))
TOTAL_TIME=$(( $(date +%s) - TOTAL_START ))
log "--------------------------------------------------"
log "Build Finished!"
log " Preset: $CONFIGURE_PRESET"
log " Configure: ${config_time}s"
log " Build: ${build_time}s"
log " Total: ${TOTAL_TIME}s"
log "--------------------------------------------------"