#!/bin/sh # Release builds into dist/. Named targets may be given; without any, everything this machine can # produce is built. set -eu cd -- "$(dirname -- "$0")" usage() { cat <<'EOF' usage: ./build.sh [target ...] Builds release binaries into dist/. With no target, everything this machine can produce is built. all every target below linux64amd x86_64 Linux linux64arm arm64 Linux needs aarch64-linux-gnu-gcc mac64amd x86_64 macOS only on a Mac mac64arm arm64 macOS only on a Mac win64amd x86_64 Windows mingw on Linux, MSVC on Windows win64arm arm64 Windows MSVC on Windows, LLVM-MinGW elsewhere rustup target add aarch64-unknown-linux-gnu # enable one of them EOF } host=$(rustc -vV | sed -n 's/^host: //p' | tr -d '\r') if [ -z "$host" ]; then echo "error: cannot read the host target from rustc" >&2 exit 1 fi os= other= win_env= case "$host" in x86_64-unknown-linux-gnu) os=linux other=aarch64 ;; aarch64-unknown-linux-gnu) os=linux other=x86_64 ;; x86_64-apple-darwin) os=macos other=aarch64 ;; aarch64-apple-darwin) os=macos other=x86_64 ;; x86_64-pc-windows-msvc) os=windows other=aarch64 win_env=msvc ;; x86_64-pc-windows-gnu) os=windows win_env=gnu ;; aarch64-pc-windows-msvc) os=windows other=x86_64 win_env=msvc ;; *) echo "error: unsupported host target: $host" >&2 exit 1 ;; esac if command -v rustup >/dev/null 2>&1; then installed_targets=$(rustup target list --installed 2>/dev/null | tr -d '\r' || true) else installed_targets= fi installed() { [ -n "$installed_targets" ] && printf '%s\n' "$installed_targets" | grep -qx "$1" } linker_for() { case "$1" in aarch64-unknown-linux-gnu) echo aarch64-linux-gnu-gcc ;; x86_64-unknown-linux-gnu) echo x86_64-linux-gnu-gcc ;; x86_64-pc-windows-gnu) echo x86_64-w64-mingw32-gcc ;; aarch64-pc-windows-gnullvm) echo aarch64-w64-mingw32-clang ;; *) echo "" ;; esac } native_triple() { case "$os" in linux) echo "$1-unknown-linux-gnu" ;; macos) echo "$1-apple-darwin" ;; windows) if [ "$win_env" = msvc ]; then echo "$1-pc-windows-msvc" else echo "$1-pc-windows-gnu" fi ;; esac } win_amd() { case "$host" in *-windows-msvc) echo x86_64-pc-windows-msvc ;; *) echo x86_64-pc-windows-gnu ;; esac } win_arm() { case "$host" in *-windows-msvc) echo aarch64-pc-windows-msvc ;; *) echo aarch64-pc-windows-gnullvm ;; esac } alias_triple() { case "$1" in linux64amd) echo x86_64-unknown-linux-gnu ;; linux64arm) echo aarch64-unknown-linux-gnu ;; mac64amd) echo x86_64-apple-darwin ;; mac64arm) echo aarch64-apple-darwin ;; win64amd) win_amd ;; win64arm) win_arm ;; *) return 1 ;; esac } targets= add_target() { case " $targets " in *" $1 "*) ;; *) targets="$targets $1" ;; esac } add_all() { add_target "$host" case "$os" in linux) if [ -n "$other" ]; then add_target "$(native_triple "$other")" fi add_target "$(win_amd)" add_target "$(win_arm)" ;; macos) if [ -n "$other" ]; then add_target "$(native_triple "$other")" fi ;; windows) add_target "$(win_amd)" add_target "$(win_arm)" ;; esac } for arg in "$@"; do case "$arg" in -h | --help) usage exit 0 ;; -*) echo "error: unknown option: $arg" >&2 usage >&2 exit 2 ;; all) add_all ;; *) if triple=$(alias_triple "$arg"); then add_target "$triple" else echo "error: unknown target: $arg" >&2 usage >&2 exit 2 fi ;; esac done if [ -z "$targets" ]; then add_all fi build() { target=$1 ext= case "$target" in *windows*) ext=.exe ;; esac case "$target" in *-apple-darwin) if [ "$os" != macos ]; then echo "skip $target: macOS builds run on a Mac" return 0 fi ;; esac linker= if [ "$target" != "$host" ]; then if ! installed "$target"; then echo "skip $target: rustup target add $target" return 0 fi linker=$(linker_for "$target") if [ -n "$linker" ] && ! command -v "$linker" >/dev/null 2>&1; then echo "skip $target: $linker is missing" return 0 fi fi echo "building $target" if [ "$target" = "$host" ]; then cargo build --release -p hamster-pet from=target/release/hamster-pet$ext else if [ -n "$linker" ]; then upper=$(printf '%s' "$target" | tr 'a-z-' 'A-Z_') env "CARGO_TARGET_${upper}_LINKER=$linker" \ cargo build --release -p hamster-pet --target "$target" else cargo build --release -p hamster-pet --target "$target" fi from=target/$target/release/hamster-pet$ext fi out=dist/hamster-pet-$target$ext cp "$from" "$out" echo " -> $out" } mkdir -p dist for target in $targets; do build "$target" done if [ ! -f assets/animations/manifest.json ]; then echo "warning: assets/animations/manifest.json is missing; run the converter first" >&2 fi if [ "$os" = macos ]; then echo "note: the macOS window backend is not implemented yet (docs/PLAN.md, M5)" >&2 fi echo "done: binaries in dist/ (run them from the project root so assets/ and config.json are found)"