| 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] |
| 4 | set -euo pipefail |
| 5 | repoRoot="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 6 | crateDir="$(realpath "${1:-$repoRoot/spikes/gpui-list}")" |
| 7 | binaryName="${2:-}" |
| 8 | export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$crateDir/target}" |
| 9 | export PATH="$HOME/.cargo/bin:$PATH" |
| 10 | export XWIN_ACCEPT_LICENSE=1 |
| 11 | export GPUI_FXC_PATH="$repoRoot/scripts/fxc-wsl.sh" |
| 12 | targetName=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>. |
| 15 | toolShimDir="$(mktemp -d)"; trap 'rm -rf "$toolShimDir"' EXIT |
| 16 | for 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 |
| 20 | done |
| 21 | export PATH="$toolShimDir:$PATH" |
| 22 | |
| 23 | command -v cargo-xwin >/dev/null || cargo install cargo-xwin --locked |
| 24 | rustup 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. |
| 27 | patchedDir="$CARGO_TARGET_DIR/patched/gpui-pre-windows" |
| 28 | sourceDir="$(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)" |
| 30 | stagingDir="$(mktemp -d)/gpui-pre-windows"; cp -r "$sourceDir" "$stagingDir"; chmod -R u+w "$stagingDir" |
| 31 | python3 - "$stagingDir/build.rs" <<'PY' |
| 32 | import re, sys |
| 33 | path = sys.argv[1] |
| 34 | text = open(path).read() |
| 35 | text = text.replace('#[cfg(all(target_os = "windows", not(debug_assertions)))]', '#[cfg(not(debug_assertions))]') |
| 36 | text = 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) |
| 37 | text = 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) |
| 38 | open(path, 'w').write(text) |
| 39 | PY |
| 40 | # A fresh copy gets new mtimes, and Cargo then rebuilds gpui and everything above it. |
| 41 | if ! diff -rq "$stagingDir" "$patchedDir" >/dev/null 2>&1; then |
| 42 | rm -rf "$patchedDir"; mkdir -p "$(dirname "$patchedDir")"; cp -rp "$stagingDir" "$patchedDir" |
| 43 | fi |
| 44 | rm -rf "$(dirname "$stagingDir")" |
| 45 | |
| 46 | # ONNX Runtime (background effects) goes into the exe as an embedded DLL, see tools/onnxruntime-dll. |
| 47 | (cd "$repoRoot/tools/onnxruntime-dll" && RUSTFLAGS="-C target-feature=-crt-static" cargo xwin build --release --target "$targetName") |
| 48 | export CALLING_ONNXRUNTIME_DLL="$CARGO_TARGET_DIR/$targetName/release/onnxruntime.dll" |
| 49 | |
| 50 | cd "$crateDir" |
| 51 | binaryArguments=() |
| 52 | outputDir="$CARGO_TARGET_DIR/$targetName/release" |
| 53 | if [ -n "$binaryName" ] && [ -f "$crateDir/examples/$binaryName.rs" ]; then |
| 54 | binaryArguments=(--example "$binaryName") |
| 55 | outputDir="$outputDir/examples" |
| 56 | elif [ -n "$binaryName" ]; then |
| 57 | binaryArguments=(--bin "$binaryName") |
| 58 | fi |
| 59 | cargo xwin build --release --target "$targetName" "${binaryArguments[@]}" \ |
| 60 | --config "patch.crates-io.gpui-pre-windows.path=\"$patchedDir\"" |
| 61 | ls -la "$outputDir/"*.exe |