Files
larp/linux/build.sh

152 lines
4.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
SCRIPT_PATH="$SCRIPT_DIR/$(basename "${BASH_SOURCE[0]}")"
# shellcheck source=/dev/null
source "$SCRIPT_DIR/common.sh"
REPO_ROOT="$(larp_repo_root)"
TOOLCHAIN_DEFAULT="$(larp_toolchain_channel "$REPO_ROOT")"
PROFILE="release"
FEATURES=()
INSTALL_TOOLCHAIN=0
CHECK_DEPS=0
CARGO_ARGS=()
usage() {
cat <<'EOF'
Usage: linux/build.sh [options] [-- cargo-args...]
Cross-compile larp.exe (x86_64-pc-windows-msvc) and shaders on Linux.
Options:
--diag Build with profile 'diag' and '--features diagnostics'
--tracy Build with profile 'tracy' and '--features tracy'
--install-toolchain Install missing rustup toolchain and MSVC target automatically
--check-deps Verify required build tools and Rust target, then exit
-h, --help Show this help message
EOF
}
ORIG_ARGS=("$@")
while [[ $# -gt 0 ]]; do
case "$1" in
--diag)
PROFILE="diag"
FEATURES+=(diagnostics)
shift
;;
--tracy)
PROFILE="tracy"
FEATURES+=(tracy)
shift
;;
--install-toolchain)
INSTALL_TOOLCHAIN=1
shift
;;
--check-deps)
CHECK_DEPS=1
shift
;;
-h|--help)
usage
exit 0
;;
--)
shift
CARGO_ARGS+=("$@")
break
;;
*)
CARGO_ARGS+=("$1")
shift
;;
esac
done
missing_tools=()
for cmd in cargo-xwin clang-cl lld-link dxc; do
if ! command -v "$cmd" >/dev/null 2>&1; then
missing_tools+=("$cmd")
fi
done
if [[ "${#missing_tools[@]}" -gt 0 ]]; then
if [[ "$CHECK_DEPS" -eq 0 ]]; then
larp_reexec_in_nix "$REPO_ROOT" "$SCRIPT_PATH" "${ORIG_ARGS[@]}" || true
fi
printf 'error: missing required build tool(s): %s\n\n' "${missing_tools[*]}" >&2
cat >&2 <<EOF
Install them or enter the Nix development shell:
NixOS / Nix: nix develop "path:$REPO_ROOT"
Arch Linux: pacman -S clang lld llvm cargo-xwin directx-shader-compiler
Fedora: dnf install clang lld llvm cargo-xwin directx-shader-compiler
Debian/Ubuntu: apt install clang lld llvm # plus cargo install cargo-xwin and DXC release binary
EOF
exit 1
fi
export RUSTUP_TOOLCHAIN="${RUSTUP_TOOLCHAIN:-$TOOLCHAIN_DEFAULT}"
export RUSTFLAGS="${RUSTFLAGS:--C target-cpu=x86-64-v3}"
if ! command -v rustup >/dev/null 2>&1; then
printf 'error: rustup is required to manage the %s Windows MSVC target.\n' "$RUSTUP_TOOLCHAIN" >&2
exit 1
fi
need_toolchain_install=0
if ! rustup toolchain list 2>/dev/null | grep -qE "^${RUSTUP_TOOLCHAIN}(-| |$)"; then
need_toolchain_install=1
elif ! rustup target list --installed --toolchain "$RUSTUP_TOOLCHAIN" 2>/dev/null | grep -qx 'x86_64-pc-windows-msvc'; then
need_toolchain_install=1
fi
if [[ "$need_toolchain_install" -eq 1 ]]; then
if [[ "$INSTALL_TOOLCHAIN" -eq 1 ]]; then
printf 'Installing Rust toolchain %s with target x86_64-pc-windows-msvc...\n' "$RUSTUP_TOOLCHAIN"
rustup toolchain install "$RUSTUP_TOOLCHAIN" --profile minimal --target x86_64-pc-windows-msvc
else
printf 'error: Rust toolchain %s with target x86_64-pc-windows-msvc is not installed.\n' "$RUSTUP_TOOLCHAIN" >&2
printf 'Run: rustup toolchain install %s --profile minimal --target x86_64-pc-windows-msvc\n' "$RUSTUP_TOOLCHAIN" >&2
printf 'Or re-run with: %s --install-toolchain\n' "$SCRIPT_PATH" >&2
exit 1
fi
fi
if [[ "$CHECK_DEPS" -eq 1 ]]; then
printf 'OK: cargo-xwin, clang-cl, lld-link, dxc, and Rust %s (x86_64-pc-windows-msvc) are ready.\n' "$RUSTUP_TOOLCHAIN"
exit 0
fi
build_cmd=(
cargo xwin build
--profile "$PROFILE"
-p larp
--target x86_64-pc-windows-msvc
--xwin-arch x86_64
)
if [[ "${#FEATURES[@]}" -gt 0 ]]; then
feature_csv="$(IFS=,; printf '%s' "${FEATURES[*]}")"
build_cmd+=(--features "$feature_csv")
fi
build_cmd+=("${CARGO_ARGS[@]}")
cd "$REPO_ROOT"
"${build_cmd[@]}"
profile_dir="$PROFILE"
exe_path="$REPO_ROOT/target/x86_64-pc-windows-msvc/$profile_dir/larp.exe"
dxil_count="$(find "$REPO_ROOT/target/shader-replace" -maxdepth 1 -type f -name '*.dxil' 2>/dev/null | wc -l)"
if [[ ! -f "$exe_path" ]]; then
printf 'error: build finished but %s was not produced.\n' "$exe_path" >&2
exit 1
fi
printf 'Built: %s (%s compiled replacement shaders in target/shader-replace)\n' "$exe_path" "$dxil_count"