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:-}"
8export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$crateDir/target}"
9export PATH="$HOME/.cargo/bin:$PATH"
10export XWIN_ACCEPT_LICENSE=1
11export GPUI_FXC_PATH="$repoRoot/scripts/fxc-wsl.sh"
12targetName=x86_64-pc-windows-msvc
13
14# embed-resource looks for plain `llvm-rc`, cc-rs (sqlite) for plain `llvm-lib`; Debian/Ubuntu only ship <tool>-<ver>.
15toolShimDir="$(mktemp -d)"; trap 'rm -rf "$toolShimDir"' EXIT
16for tool in llvm-rc llvm-lib; do
17 for candidate in /usr/lib/llvm-*/bin/$tool /usr/bin/$tool-*; do
18 [ -x "$candidate" ] && ln -sf "$candidate" "$toolShimDir/$tool" && break
19 done
20done
21export PATH="$toolShimDir:$PATH"
22
23command -v cargo-xwin >/dev/null || cargo install cargo-xwin --locked
24rustup target add "$targetName"
25
26# gpui-pre-windows/build.rs gates shader compilation on the HOST os. Patch a copy to gate on the target.
27patchedDir="$CARGO_TARGET_DIR/patched/gpui-pre-windows"
28sourceDir="$(cd "$crateDir" && cargo metadata --format-version 1 --filter-platform "$targetName" |
29 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)"
30stagingDir="$(mktemp -d)/gpui-pre-windows"; cp -r "$sourceDir" "$stagingDir"; chmod -R u+w "$stagingDir"
31python3 - "$stagingDir/build.rs" <<'PY'
32import re, sys
33path = sys.argv[1]
34text = open(path).read()
35text = text.replace('#[cfg(all(target_os = "windows", not(debug_assertions)))]', '#[cfg(not(debug_assertions))]')
36text = 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)
37text = 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)
38open(path, 'w').write(text)
39PY
40for patchFile in "$repoRoot"/scripts/gpui-windows-patches/*.patch; do
41 patch --directory="$stagingDir" --strip=1 --forward --batch --no-backup-if-mismatch --quiet < "$patchFile"
42done
43# A fresh copy gets new mtimes, and Cargo then rebuilds gpui and everything above it.
44if ! diff -rq "$stagingDir" "$patchedDir" >/dev/null 2>&1; then
45 rm -rf "$patchedDir"; mkdir -p "$(dirname "$patchedDir")"; cp -rp "$stagingDir" "$patchedDir"
46fi
47rm -rf "$(dirname "$stagingDir")"
48
49# ONNX Runtime (background effects) goes into the exe as an embedded DLL, see tools/onnxruntime-dll.
50(cd "$repoRoot/tools/onnxruntime-dll" && RUSTFLAGS="-C target-feature=-crt-static" cargo xwin build --release --target "$targetName")
51export CALLING_ONNXRUNTIME_DLL="$CARGO_TARGET_DIR/$targetName/release/onnxruntime.dll"
52
53cd "$crateDir"
54binaryArguments=()
55outputDir="$CARGO_TARGET_DIR/$targetName/release"
56if [ -n "$binaryName" ] && [ -f "$crateDir/examples/$binaryName.rs" ]; then
57 binaryArguments=(--example "$binaryName")
58 outputDir="$outputDir/examples"
59elif [ -n "$binaryName" ]; then
60 binaryArguments=(--bin "$binaryName")
61fi
62cargo xwin build --release --target "$targetName" "${binaryArguments[@]}" \
63 --config "patch.crates-io.gpui-pre-windows.path=\"$patchedDir\""
64ls -la "$outputDir/"*.exe