Files
excel-search-tool/rust/build.bat
T
2026-09-21 21:04:43 +08:00

130 lines
4.0 KiB
Batchfile

@echo off
setlocal enabledelayedexpansion
rem ============================================================
rem Excel Keyword Search Tool (Rust) - one-click build
rem
rem Usage:
rem build.bat release build, default
rem build.bat debug debug build
rem build.bat test run cargo test
rem
rem Toolchain autodetect order:
rem 1. CherryStudio mise managed cargo/rustup
rem 2. system PATH cargo/rustup
rem If a windows-gnu toolchain + winlibs mingw is available it is
rem used automatically - works without Visual Studio.
rem Otherwise the default msvc target is used - needs VS Build Tools.
rem ASCII only - keep it that way for cmd GBK codepage safety.
rem ============================================================
cd /d "%~dp0"
set "ACTION=%~1"
if "%ACTION%"=="" set "ACTION=release"
echo ============================================================
echo excel-search-rust build script [action: %ACTION%]
echo ============================================================
rem ---------- locate cargo / rustup ----------
set "MISE_HOME=%APPDATA%\CherryStudio\Toolchain\mise\shims"
set "RUSTUP_BIN="
set "CARGO_BIN="
if exist "%MISE_HOME%\rustup.exe" set "RUSTUP_BIN=%MISE_HOME%\rustup.exe"
if exist "%MISE_HOME%\cargo.exe" set "CARGO_BIN=%MISE_HOME%\cargo.exe"
if not defined CARGO_BIN (
for /f "delims=" %%i in ('where cargo 2^>nul') do if not defined CARGO_BIN set "CARGO_BIN=%%i"
)
if not defined RUSTUP_BIN (
for /f "delims=" %%i in ('where rustup 2^>nul') do if not defined RUSTUP_BIN set "RUSTUP_BIN=%%i"
)
if not defined CARGO_BIN (
echo [ERROR] cargo not found.
echo Install Rust from https://rustup.rs , or via CherryStudio
echo managed CLI with the mise recipe core:rust , then retry.
goto :fail
)
echo [info] cargo : %CARGO_BIN%
rem ---------- prefer GNU host toolchain, no VS required ----------
set "USE_GNU="
set "GNU_TC=stable-x86_64-pc-windows-gnu"
rem NOTE: call find by absolute path - GNU find from mingw/Git in PATH would hijack it
if defined RUSTUP_BIN (
"%RUSTUP_BIN%" toolchain list 2>nul | "%WINDIR%\System32\find.exe" "%GNU_TC%" >nul && set "USE_GNU=1"
)
set "MINGW_BIN=%USERPROFILE%\tools\mingw64\bin"
set "HAVE_MINGW="
if exist "%MINGW_BIN%\gcc.exe" set "HAVE_MINGW=1"
if not defined HAVE_MINGW (
for /f "delims=" %%i in ('where gcc 2^>nul') do set "HAVE_MINGW=1"
)
set "CMD_HEAD="
if defined USE_GNU if defined HAVE_MINGW (
if exist "%MINGW_BIN%\gcc.exe" set "PATH=%MINGW_BIN%;%PATH%"
set "CMD_HEAD="%RUSTUP_BIN%" run %GNU_TC% cargo"
echo [info] toolchain: %GNU_TC% gnu, no MSVC required
)
if not defined CMD_HEAD (
set "CMD_HEAD="%CARGO_BIN%""
echo [info] toolchain: default msvc
echo [warn] no gnu toolchain + mingw found. The msvc target needs Visual
echo Studio Build Tools / Windows SDK to link. To build without VS:
echo 1. %RUSTUP_BIN% toolchain install %GNU_TC%
echo 2. unzip winlibs mingw into %USERPROFILE%\tools\mingw64
)
rem ---------- run ----------
echo.
if "%ACTION%"=="test" (
%CMD_HEAD% test
goto :post
)
if "%ACTION%"=="debug" (
%CMD_HEAD% build
goto :post
)
%CMD_HEAD% build --release
:post
set "RC=%ERRORLEVEL%"
echo.
if not "%RC%"=="0" (
echo [ERROR] build failed, exit code %RC%
goto :fail
)
if "%ACTION%"=="test" (
echo [OK] all tests passed
goto :done
)
echo ============================================================
echo [OK] build succeeded. outputs:
if "%ACTION%"=="debug" (
if exist "target\debug\excel-search-rust.exe" echo target\debug\excel-search-rust.exe
if exist "target\debug\excel-search-rust-desktop.exe" echo target\debug\excel-search-rust-desktop.exe
)
if not "%ACTION%"=="debug" (
for %%f in (excel-search-rust.exe excel-search-rust-desktop.exe) do (
if exist "target\release\%%f" (
for %%s in ("target\release\%%f") do echo target\release\%%f - %%~zs bytes
)
)
)
echo ============================================================
:done
echo.
pause
exit /b 0
:fail
echo.
pause
exit /b 1