在 Windows 上建置
在 Windows 上建置 Bun
請使用 PowerShell 7 (pwsh.exe),而不是預設的 powershell.exe。如果遇到問題,請在我們的 Discord 上的 #contributing 頻道 中提問。
前置條件
啟用指令碼執行
預設情況下,禁止執行未經驗證的指令碼。
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
系統相依
Bun v1.1 或更高版本。建置過程使用 Bun 執行其自身的程式碼生成器。
irm bun.sh/install.ps1 | iex
Visual Studio,並安裝“使用 C++ 的桌面開發”工作負載。安裝過程中,如果尚未安裝適用於 Windows 的 Git,也請一併安裝。
使用圖形化嚮導安裝 Visual Studio,或通過 WinGet 安裝:
winget install "Visual Studio Community 2022" --override "--add Microsoft.VisualStudio.Workload.NativeDesktop Microsoft.VisualStudio.Component.Git " -s msstore
安裝 Visual Studio 後,需要安裝以下工具:
- LLVM 23.1.1
- Go
- Rust(通過 rustup)
- NASM
- Perl
- Ruby
- Node.js
使用 Scoop 來安裝這些剩餘工具。
irm https://get.scoop.sh | iex
scoop install nodejs-lts go rustup nasm ruby perl ccache
# scoop seems to be buggy if you install llvm and the rest at the same time
scoop install llvm@23.1.1
對於 Windows ARM64,請直接從 GitHub releases 下載 LLVM 23.1.1:
# Download and install LLVM for ARM64
Invoke-WebRequest -Uri "https://github.com/llvm/llvm-project/releases/download/llvmorg-23.1.1/LLVM-23.1.1-woa64.msi" -OutFile "$env:TEMP\LLVM-23.1.1-woa64.msi"
Start-Process msiexec.exe -ArgumentList "/i `"$env:TEMP\LLVM-23.1.1-woa64.msi`" /qn" -Wait
若要在本地建置 WebKit(可選,僅限 x64),請安裝以下軟體包:
scoop install make cygwin python
從現在開始,請使用已載入 .\scripts\vs-shell.ps1 的 PowerShell 終端。執行以下命令載入該指令碼:
.\scripts\vs-shell.ps1
要進行驗證,請檢查 mt.exe 等僅限 MSVC 的命令:
Get-Command mt
建置
bun run build
成功建置後,會將 bun-debug.exe 寫入 build/debug 資料夾。
.\build\debug\bun-debug.exe --revision
將此資料夾新增到 $Env:PATH:開啟“開始”選單,輸入“Path”,然後使用環境變數選單,將 C:\.....\bun\build\debug 新增到使用者環境變數 PATH 中。然後重新啟動編輯器(如果仍未生效,請登出後重新登入)。
額外路徑
- 建置會將 WebKit 解壓到
$Env:BUN_INSTALL\build-cache\webkit-<version>-debug(ARM64 上為webkit-<version>-arm64-debug);BUN_INSTALL預設為~\.bun。設定$Env:BUN_BUILD_CACHE_DIR可將建置快取放到其他位置。
測試
使用 bun-debug test <path> 或包裝指令碼 bun run test <path> 執行測試套件。bun run test 命令會在單獨的 bun-debug.exe 例項中執行每個測試檔案,因此測試執行器崩潰不會導致整個套件停止。
# 使用報告器執行整個測試套件
# package.json 指令碼 "test" 預設使用 "build/debug/bun-debug.exe"
bun run test
# 執行單個測試檔案:
bun-debug test node\fs
bun-debug test "C:\bun\test\js\bun\resolve\import-meta.test.js"
故障排查
.rc 檔案建置失敗
llvm-rc.exe 很奇怪;不要使用它。請改用 rc.exe:確保你處於 Visual Studio 開發人員終端中,並檢查 rc /?,確認它是 Microsoft Resource Compiler。
輸出寫入失敗 ‘bun-debug.exe’: 許可權被拒絕
bun-debug.exe 處於開啟狀態時無法覆蓋它。你可能有一個正在執行的例項,也可能是在 VS Code 偵錯程式中執行。
從 Linux 交叉編譯
你也可以在 Linux 主機上建置 Windows 二進位制檔案(x64 和 arm64)。建置過程使用主機 LLVM 的 clang-cl、lld-link、llvm-lib 和 llvm-rc(這些工具包含在每個 LLVM 發行版中)。對於標頭檔案和匯入庫,建置還會使用 MSVC CRT/STL 和 Windows SDK 的“xwin splat”。
前置條件
- 與原生建置相同版本的 LLVM(
scripts/build/ci-images/spec.ts中的pins.llvm),並確保clang-cl、lld-link、llvm-lib和llvm-rc可用。在 Debian/Ubuntu 上,apt.llvm.org軟體包提供這些工具。 nasm(僅 Windows x64 建置需要;BoringSSL 的 x64 彙編採用 NASM 語法)。- Windows 目標的 Rust std(
rust-toolchain.toml中列出了這些目標;如果缺少,可執行rustup target add x86_64-pc-windows-msvc aarch64-pc-windows-msvc)。 - Windows sysroot:一個 xwin splat,其中包含 MSVC CRT、Windows SDK 和 ATL,目錄結構與 Visual Studio 安裝目錄相同。下載這些元件即表示接受 Microsoft 對這些元件的許可條款。
cargo install xwin # 或下載一個釋出版二進位制檔案
xwin --accept-license --arch x86_64,aarch64 --sdk-version 10.0.26100 --crt-version 14.44.17.14 --include-atl splat \
--use-winsysroot-style --preserve-ms-arch-notation --include-debug-libs \
--output /opt/winsysroot
# clang-cl/lld-link 將 SDK 路徑視為 "Include"/"Lib";
# splat 會用小寫寫入這些路徑,所以需要把兩種拼寫都做別名(需要與
# splat 相同的許可權——當目錄可寫時,configure 會自行建立這些別名)。
ln -s include "/opt/winsysroot/Windows Kits/10/Include"
ln -s lib "/opt/winsysroot/Windows Kits/10/Lib"
建置過程會自動在 /opt/winsysroot(或 /opt/xwin)查詢 sysroot。如果 sysroot 位於其他位置,請設定 WINDOWS_SYSROOT=<path> 或傳入 --winsysroot=<path>。使用使用者可寫的路徑,也可以讓 configure 自動管理這些別名。每次交叉建置開始時,configure 都會驗證 splat。CI 代理會將相同的 splat 預置到其映象中(使用 scripts/build/ci-images/spec.ts 中的 windowsSysroot 工具);如果代理上沒有該工具,建置過程會在 configure 時將其下載到快取目錄。
建置
# Debug 建置
bun run build --profile=windows-x64
bun run build --profile=windows-arm64
# Release 建置
bun run build --profile=windows-x64-release
bun run build --profile=windows-arm64-release
輸出檔案位於 build/debug-windows-x64/bun-debug.exe、build/release-windows-aarch64/bun-profile.exe + bun.exe 等路徑中。等效的原始引數為:bun run build --os=windows --arch=aarch64。
建置過程不會在主機上執行交叉編譯出的執行檔(會跳過 --revision 冒煙測試),因此請在 Windows 機器或 Wine 中測試這些檔案。
LTO
x64 release 交叉建置使用 ThinLTO,並進行跨語言(Rust↔C++)LTO,與所有 release 建置一樣(加上 --lto=off 可加快重新連結):
- Bun 的 C/C++ 使用
-flto=thin進行編譯 - rustc 會生成 LLVM bitcode(
-Clinker-plugin-lto) bun-webkit-windows-amd64-ltoThinLTO 預建置版本是被連結的 WebKit- 所有內容都使用 LLVM 的
lld-link進行連結,它會讀取兩種編譯器的 bitcode:建置要求 clang 和 rustc 使用相同的主版本
arm64 不支援 LTO,因為沒有 -lto WebKit 預建置版本:LLVM 的 CodeView 發射器在 LTO 程式碼生成期間無法處理 ARM64 NEON tuple 暫存器。