BlueGit

ict rusty-teams

Files

rusty-teams / scripts / build-windows.sh

build-windows.sh

build-windows.sh 3.5 KiB Download Raw
1#!/usr/bin/env bash
2# Cross-build a GPUI crate to a Windows .exe from WSL/Linux. No admin, no Visual Studio.
3# Usage: scripts/build-windows.sh [crate-dir, default spikes/gpui-list] [binary-or-example-name, default: the crate's only binary]
4set -euo pipefail
5repoRoot="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
6crateDir="$(realpath "${1:-$repoRoot/spikes/gpui-list}")"
7binaryName="${2:-}"
8# One target dir for every worktree and crate (scripts/build-cache.sh): dependencies, libwebrtc and the patched gpui are built once.
9export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/rusty-teams/target}"
10export PATH="$HOME/.cargo/bin:$PATH"
11export XWIN_ACCEPT_LICENSE=1
12export GPUI_FXC_PATH="$repoRoot/scripts/fxc-wsl.sh"
13targetName=x86_64-pc-windows-msvc
14
15# embed-resource looks for plain `llvm-rc`, cc-rs (sqlite) for plain `llvm-lib`; Debian/Ubuntu only ship <tool>-<ver>.
16toolShimDir="$(mktemp -d)"; trap 'rm -rf "$toolShimDir"' EXIT
17for tool in llvm-rc llvm-lib; do
18 for candidate in /usr/lib/llvm-*/bin/$tool /usr/bin/$tool-*; do
19 [ -x "$candidate" ] && ln -sf "$candidate" "$toolShimDir/$tool" && break
20 done
21done
22export PATH="$toolShimDir:$PATH"
23
24command -v cargo-xwin >/dev/null || cargo install cargo-xwin --locked
25rustup target add "$targetName"
26
27# gpui-pre-windows/build.rs gates shader compilation on the HOST os. Patch a copy to gate on the target.
28patchedDir="$CARGO_TARGET_DIR/patched/gpui-pre-windows"
29sourceDir="$(cd "$crateDir" && cargo metadata --format-version 1 --filter-platform "$targetName" |
30 python3 -c 'import json,sys; print(next(p["manifest_path"] for p in json.load(sys.stdin)["packages"] if p["name"]=="gpui-pre-windows"))' | xargs dirname)"
31stagingDir="$(mktemp -d)/gpui-pre-windows"; cp -r "$sourceDir" "$stagingDir"; chmod -R u+w "$stagingDir"
32python3 - "$stagingDir/build.rs" <<'PY'
33import re, sys
34path = sys.argv[1]
35text = open(path).read()
36text = text.replace('#[cfg(all(target_os = "windows", not(debug_assertions)))]', '#[cfg(not(debug_assertions))]')
37text = re.sub(r'fn main\(\) \{.*?\n\}\n', 'fn main() {\n #[cfg(not(debug_assertions))]\n if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") {\n compile_shaders();\n }\n}\n', text, count=1, flags=re.S)
38text = re.sub(r'(pub fn find_latest_windows_sdk_binary\(.*?\) -> Result<[^{]*\{).*?(\n /// You can set)', r'\1\n let _ = binary;\n Ok(None)\n }\n\2', text, count=1, flags=re.S)
39open(path, 'w').write(text)
40PY
41# A fresh copy gets new mtimes, and Cargo then rebuilds gpui and everything above it.
42if ! diff -rq "$stagingDir" "$patchedDir" >/dev/null 2>&1; then
43 rm -rf "$patchedDir"; mkdir -p "$(dirname "$patchedDir")"; cp -rp "$stagingDir" "$patchedDir"
44fi
45rm -rf "$(dirname "$stagingDir")"
46
47# ONNX Runtime (background effects) goes into the exe as an embedded DLL, see tools/onnxruntime-dll.
48(cd "$repoRoot/tools/onnxruntime-dll" && RUSTFLAGS="-C target-feature=-crt-static" cargo xwin build --release --target "$targetName")
49export CALLING_ONNXRUNTIME_DLL="$CARGO_TARGET_DIR/$targetName/release/onnxruntime.dll"
50
51cd "$crateDir"
52binaryArguments=()
53outputDir="$CARGO_TARGET_DIR/$targetName/release"
54if [ -n "$binaryName" ] && [ -f "$crateDir/examples/$binaryName.rs" ]; then
55 binaryArguments=(--example "$binaryName")
56 outputDir="$outputDir/examples"
57elif [ -n "$binaryName" ]; then
58 binaryArguments=(--bin "$binaryName")
59fi
60cargo xwin build --release --target "$targetName" "${binaryArguments[@]}" \
61 --config "patch.crates-io.gpui-pre-windows.path=\"$patchedDir\""
62ls -la "$outputDir/"*.exe