diff --git a/.gitattributes b/.gitattributes index 441bdfe1eb..472a0b2d1f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -5,3 +5,8 @@ # with CRLF line endings: it fails on the first line. Windows checkouts default # to core.autocrlf=true, so keep these LF whatever the platform. *.sh text eol=lf + +# Batch files are read by cmd.exe, which tracks a byte offset into the file to +# resume after `call :label`. With LF endings that offset can land wrong and the +# label lookup fails, so keep these CRLF whatever the platform. +*.bat text eol=crlf diff --git a/.github/workflows/build_all.yml b/.github/workflows/build_all.yml index fe4f10e81b..d991bedca6 100644 --- a/.github/workflows/build_all.yml +++ b/.github/workflows/build_all.yml @@ -33,6 +33,8 @@ on: - 'build_linux.sh' - 'build_release_vs.bat' - 'build_release_vs2022.bat' + - 'build_win.bat' + - 'scripts/test_build_win.ps1' - 'build_release_macos.sh' - 'scripts/build_preset_cache.*' - 'scripts/flatpak/**' @@ -56,6 +58,22 @@ concurrency: jobs: + # build_win.bat ships a test suite. Run it before the Windows builds. + check_build_script: + name: Windows build script tests + runs-on: windows-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + with: + lfs: 'false' + + # Windows PowerShell rather than pwsh: the suite drives build_win.bat + # through cmd, and the two differ in how they quote native arguments. + - name: Run the build script test suite + shell: powershell + run: .\scripts\test_build_win.ps1 + build_linux: strategy: fail-fast: false @@ -83,8 +101,9 @@ jobs: include: ${{ fromJSON(vars.SELF_HOSTED && '[{"arch":"x64","os":"orca-win-server","compiler":"clang"}]' || '[{"arch":"x64","os":"windows-latest","compiler":"clang"},{"arch":"arm64","os":"windows-11-arm","compiler":"clang"}]') }} + needs: check_build_script # Don't run scheduled builds on forks: - if: ${{ !cancelled() && (github.event_name != 'schedule' || github.repository == 'OrcaSlicer/OrcaSlicer') }} + if: ${{ !cancelled() && needs.check_build_script.result == 'success' && (github.event_name != 'schedule' || github.repository == 'OrcaSlicer/OrcaSlicer') }} uses: ./.github/workflows/build_check_cache.yml with: os: ${{ matrix.os }} diff --git a/.gitignore b/.gitignore index cdcd1c90b4..e36d0dfa49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ Build Build.bat /build*/ +/out/ CMakeLists.txt.user CMakeUserPresets.json **/CMakeLists.txt.autosave diff --git a/build_win.bat b/build_win.bat new file mode 100644 index 0000000000..926468687b --- /dev/null +++ b/build_win.bat @@ -0,0 +1,1268 @@ +@echo off + +REM OrcaSlicer build script for Windows. Run with -h for the options. + +REM =========================================================================== +REM Script setup +REM =========================================================================== + +setlocal enableDelayedExpansion + +REM A manually set errorlevel shadows the real one, and cmd then stops +REM updating it after each command. Clear it, inside the scope above so +REM the caller keeps whatever it had. +set errorlevel= + +REM Everything here is relative to the repository root: deps/, build/ and +REM the 7z in tools/. Work from there whatever directory this was called +REM from. setlocal above restores the caller directory on exit. +cd /d "%~dp0" +set "WP=%CD%" +set "script_name=%~nx0" + +set argdefn=0 + +REM Both macros stop on a non-zero errorlevel; error_check also says so. +REM +REM error_check jumps to :die rather than calling exit /b where it stands. +REM Every use of it is inside a parenthesised block, and an exit /b there +REM ends the script but leaves the process exit code at zero, so a failed +REM build reported success. goto unwinds the block first. +set "repeat_error=if not ^!errorlevel^! == 0 exit /b ^!errorlevel^!" +set "error_check=if not ^!errorlevel^! == 0 (set rc=^!errorlevel^!& goto :die)" + +set VSWHERE="%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" + +REM =========================================================================== +REM Command line options +REM =========================================================================== + +call :add_section "Actions" +call :add_arg build_deps bool d deps "Download and build the dependencies, needed before -s" +call :add_arg build_slicer bool s slicer "Build OrcaSlicer" +call :add_arg build_tests bool "" tests "Build the unit tests" +call :add_arg run_tests bool "" run-tests "Build the unit tests and run them" +call :add_arg pack_deps bool p pack "Bundle the built dependencies into a zip file" +call :add_arg install_deps bool u install-deps "Install or update CMake, Perl and Git with WinGet" +call :add_arg install_vs string "" install-vs "Also install Visual Studio: buildtools or ide" +call :add_arg kill_jobs bool k kill-jobs "Kill any running build and compiler processes" +call :add_arg print_help bool h help "Print this help message" + +call :add_section "Build configuration" +call :add_arg config string "" config "release, debug, relwithdebinfo or minsizerel (default: release)" +call :add_arg target_arch string "" arch "x64 or arm64 (default: the host architecture)" +call :add_arg slicer_asan bool a asan "Build the slicer with ASAN enabled" + +call :add_section "Toolchain" +call :add_arg use_clang_cl bool l clang-cl "Use clang-cl as the compiler" +call :add_arg use_msvc bool "" msvc "Use cl as the compiler (default)" +call :add_arg use_ninja bool x ninja "Use the Ninja Multi-Config generator" +call :add_arg use_msbuild bool "" msbuild "Use the Visual Studio generator (default)" +call :add_arg vs_version string "" vs "Visual Studio release: 2019, 2022 or 2026 (default: autodetect)" +call :add_arg clang_path string "" clang-path "Path to clang-cl.exe, requires -x (default: the one from Visual Studio)" + +call :add_section "How much gets rebuilt" +call :add_arg slicer_target string "" slicer-target "Build one slicer target instead of all, e.g. libslic3r" +call :add_arg deps_target string t deps-target "Build one dependency instead of all, e.g. dep_Boost" +call :add_arg no_configure bool "" no-configure "Build the existing tree without configuring" +call :add_arg no_gettext bool "" no-gettext "Skip regenerating the translations" +call :add_arg install_slicer bool i install "Install into the build tree's OrcaSlicer folder" +call :add_arg jobs string j jobs "Limit the build to N parallel jobs" +call :add_arg clean bool c clean "Remove the trees this run builds, deps with -d, slicer with -s" + +call :add_section "Paths and extra arguments" +call :add_arg deps_dir string "" deps-dir "Dependency tree to build in or use, instead of the one named for this build" +call :add_arg slicer_dir string "" build-dir "Slicer build directory, instead of the one named for this build" +call :add_arg deps_args rawstring "" deps-args "Extra arguments for the deps configure, quoted" +call :add_arg slicer_args rawstring "" slicer-args "Extra arguments for the slicer configure, quoted" + +call :add_section "Diagnostics" +call :add_arg verbose bool v verbose "Show the compiler command lines" +call :add_arg dry_run bool D dry-run "Print the commands instead of running them" + +REM =========================================================================== +REM Lookup tables +REM =========================================================================== + +REM Known build configurations: the CMake build type, the directory to build +REM in, and the dependency tree to build against. Adding one means adding all +REM three entries in its group. The name is matched case-insensitively because +REM batch variable names are, so --config Debug finds cfg_type_debug. +REM +REM Release, RelWithDebInfo and MinSizeRel all link the /MD dependencies, so +REM they share one tree rather than each paying for its own. Debug needs /MDd +REM and cannot. deps/CMakeLists.txt makes the same split: only Debug turns on +REM DEP_DEBUG, while RelWithDebInfo just adds debug info to a release build. +set "cfg_type_release=Release" +set "cfg_dir_release=build" +set "cfg_dep_release=build" + +set "cfg_type_debug=Debug" +set "cfg_dir_debug=build-dbg" +set "cfg_dep_debug=build-dbg" + +set "cfg_type_relwithdebinfo=RelWithDebInfo" +set "cfg_dir_relwithdebinfo=build-dbginfo" +set "cfg_dep_relwithdebinfo=build" + +set "cfg_type_minsizerel=MinSizeRel" +set "cfg_dir_minsizerel=build-minsize" +set "cfg_dep_minsizerel=build" + +set "cfg_default=release" + +REM Known Visual Studio releases: the generator name, the suffix in the +REM WinGet id, and the major version vswhere and msbuild report. Adding a +REM release means adding all three entries in its group. +set "vs_gen_2019=Visual Studio 16 2019" +set "vs_winget_2019=.2019" +set "vs_year_16=2019" + +set "vs_gen_2022=Visual Studio 17 2022" +set "vs_winget_2022=.2022" +set "vs_year_17=2022" + +REM 2026 is the unversioned WinGet id: there is no Microsoft.VisualStudio.2026. +set "vs_gen_2026=Visual Studio 18 2026" +set "vs_winget_2026=" +set "vs_year_18=2026" + +set "vs_default=2026" + +REM What --install-vs asks WinGet for. +set "vs_edition_buildtools=BuildTools" +set "vs_edition_ide=Community" + +REM =========================================================================== +REM Command line handling +REM =========================================================================== + +call :handle_args %* +%error_check% + +if "%debugscript%" == "ON" ( + set /A range_end = %argdefn% - 1 + for /L %%i in (0, 1, !range_end!) do ( + call :echo_var !argdefs[%%i].VARIABLE_NAME! + ) +) + +if "%~1" == "" ( + set print_help=ON + goto :before_print_help +) + +set "all_args=%*" +if "%all_args:"=%" == "" ( + set print_help=ON +) + +:before_print_help + +if "%print_help%" == "ON" ( + call :print_help_msg + exit /b 0 +) + +REM Say so before the first + line scrolls past. +if "%dry_run%" == "ON" echo Dry run: printing commands without running them. + +if "%kill_jobs%" == "ON" ( + echo Stopping build processes. + call :kill_image MSBuild.exe + call :kill_image ninja.exe + call :kill_image cl.exe + call :kill_image clang-cl.exe + exit /b 0 +) + +REM Neither test option can happen without building the slicer, so asking for +REM one asks for that, unless another action was already named. +if "%build_deps%%build_slicer%%pack_deps%%install_deps%%install_vs%" == "" ( + if "%build_tests%" == "ON" set "build_slicer=ON" + if "%run_tests%" == "ON" set "build_slicer=ON" +) + +REM Options like --config or -j only shape a build. Without one of the actions +REM there is nothing for them to shape, so say so rather than resolving a whole +REM build and reporting it took no time. The block above may have added one. +if "%build_deps%%build_slicer%%pack_deps%%install_deps%%install_vs%" == "" ( + echo Nothing to do. Pick an action: -d, -s, -p or -u. Run -h for the full list. + exit /b 1 +) + +REM =========================================================================== +REM Visual Studio and target architecture +REM =========================================================================== + +REM Asking for Visual Studio is asking to install prerequisites. Resolve the +REM edition here rather than testing whether the name is defined: `if defined` +REM stops at the first space, so "ide " would pass and then expand to nothing. +set "vs_edition=" +if not "%install_vs%" == "" ( + set "install_deps=ON" + set "vs_edition=!vs_edition_%install_vs%!" + if "!vs_edition!" == "" ( + echo Unknown Visual Studio edition "%install_vs%". Known editions: buildtools, ide. + exit /b 1 + ) +) +REM Autodetection overwrites vs_version, so snapshot whether it was pinned. +set "vs_pinned=%vs_version%" + +if not "%vs_version%" == "" if "%use_ninja%" == "ON" ( + echo --vs and --ninja select different generators. + exit /b 1 +) +REM install(TARGETS OrcaSlicer) carries no OPTIONAL, so installing a tree +REM whose executable was never built fails. Say so before the build starts. +if "%build_slicer%" == "ON" if "%install_slicer%" == "ON" if not "%slicer_target%" == "" if /I not "%slicer_target%" == "OrcaSlicer" ( + echo --install needs the executable, but --slicer-target names "%slicer_target%". + exit /b 1 +) +if not "%vs_version%" == "" if "!vs_gen_%vs_version%!" == "" ( + echo Unknown Visual Studio release "%vs_version%". Known releases: 2019, 2022, 2026. + exit /b 1 +) + +call :autodetect_vs +%error_check% + +REM autodetect leaves this empty when it finds nothing usable. +if "%vs_version%" == "" set "vs_version=%vs_default%" + +REM Default to the host CPU. PROCESSOR_ARCHITEW6432 covers a 32-bit shell on +REM a 64-bit OS, where PROCESSOR_ARCHITECTURE reads x86. +set arch=x64 +if /I "%PROCESSOR_ARCHITECTURE%" == "ARM64" set arch=ARM64 +if /I "%PROCESSOR_ARCHITEW6432%" == "ARM64" set arch=ARM64 +if not "%target_arch%" == "" ( + if /I "%target_arch%" == "arm64" ( + set arch=ARM64 + ) else ( + if /I "%target_arch%" == "x64" ( + set arch=x64 + ) else ( + echo Unknown architecture "%target_arch%". Expected x64 or arm64. + exit /b 1 + ) + ) +) + +REM =========================================================================== +REM Installing prerequisites +REM =========================================================================== + +if "%install_deps%" == "ON" ( + where winget >nul 2>nul + if not !errorlevel! == 0 ( + echo WinGet was not found + exit /b 1 + ) + REM Keep going after one failure so the rest still get installed, then + REM name the ones that did not rather than claiming they all did. + set "install_failed=" + set "winget_args=-e --source=winget" + if not "%install_vs%" == "" ( + set "vs_year=!vs_winget_%vs_version%!" + set "ide_component_flag=" + if /I "%install_vs%" == "ide" set "ide_component_flag=Microsoft.VisualStudio.Component.VC.CoreIde" + REM Two components: the clang-cl compiler itself, and the MSBuild + REM toolset that lets the Visual Studio generator drive it. One + REM install covers both x64 and ARM64. + set "clang_cl_flag=" + if "%use_clang_cl%" == "ON" set "clang_cl_flag=Microsoft.VisualStudio.Component.VC.Llvm.Clang Microsoft.VisualStudio.Component.VC.Llvm.ClangToolset" + REM The x64 tools build the host tooling either way; targeting ARM64 + REM needs its own toolset on top of them. + set "arm64_tools_flag=" + if /I "%arch%" == "ARM64" set "arm64_tools_flag=Microsoft.VisualStudio.Component.VC.Tools.ARM64" + call :print_and_run winget install !winget_args! --id=Microsoft.VisualStudio!vs_year!.!vs_edition! --force --custom "--add !ide_component_flag! Microsoft.VisualStudio.Component.VC.Tools.x86.x64 !arm64_tools_flag! Microsoft.VisualStudio.Component.VC.CMake.Project Microsoft.VisualStudio.Component.Windows11SDK.22621 !clang_cl_flag!" + call :note_failed "Visual Studio" !errorlevel! + ) + + REM CMake 4 dropped pre-3.5 policy support and ships incomplete ASM_ARMASM + REM linker modules, which breaks Boost.Context on ARM64. CI pins the same way. + set "cmake_version_flag=" + if /I "%arch%" == "ARM64" set "cmake_version_flag=--version 3.31.8" + call :print_and_run winget install !winget_args! --id=Kitware.CMake !cmake_version_flag! + call :note_failed CMake !errorlevel! + call :print_and_run winget install !winget_args! --id=StrawberryPerl.StrawberryPerl + call :note_failed Perl !errorlevel! + call :print_and_run winget install !winget_args! --id=Git.Git + call :note_failed Git !errorlevel! + + if defined install_failed ( + set "die_reason=Failed to install:!install_failed!" + set rc=1 + goto :die + ) + + REM Flags to repeat back in the deferred build command. + set "next_flags=" + if "%use_clang_cl%" == "ON" set "next_flags=!next_flags! -l" + if "%use_ninja%" == "ON" set "next_flags=!next_flags! -x" + if not "%target_arch%" == "" set "next_flags=!next_flags! --arch %target_arch%" + + REM Name the build that was deferred, not a fuller one. + set "next_actions=" + if "%build_deps%" == "ON" set "next_actions=!next_actions!d" + if "%build_slicer%" == "ON" set "next_actions=!next_actions!s" + if "%pack_deps%" == "ON" set "next_actions=!next_actions!p" + if "!next_actions!" == "" set "next_actions=ds" + + echo. + echo ------------------------------------------------------------- + REM A dry run installed nothing, so do not report that it did. + if "%dry_run%" == "ON" ( + echo Dry run: nothing was installed. + ) else ( + echo Installed the prerequisites. + ) + echo. + echo Next + REM The new PATH cannot reach this shell, so the build runs in the next one. + echo Restart this shell so the new PATH takes effect, then + echo build_win.bat -!next_actions!!next_flags! + echo ------------------------------------------------------------- + exit /b 0 +) + +REM =========================================================================== +REM Resolving the build +REM =========================================================================== + +REM A value ending in a backslash escapes the closing quote when it is +REM spliced into a command line: -B "D:\tree\" reaches cmake as D:\tree" +REM and swallows the argument after it. No path here needs one. +call :trim_slash deps_dir +call :trim_slash slicer_dir +call :trim_slash clang_path + +REM Naming a specific clang-cl is naming clang-cl. Doing it before the +REM conflict check below means --clang-path with --msvc is caught there. +if not "%clang_path%" == "" set "use_clang_cl=ON" +if not "%clang_path%" == "" if not exist "%clang_path%" ( + echo No clang-cl at "%clang_path%". + exit /b 1 +) +REM A trailing backslash matches directories only, and a directory would +REM otherwise reach CMake as the compiler. +if not "%clang_path%" == "" if exist "%clang_path%\" ( + echo "%clang_path%" is a directory. Name clang-cl.exe itself. + exit /b 1 +) + +REM Naming a default explicitly is fine; naming both sides is not. +if "%use_clang_cl%" == "ON" if "%use_msvc%" == "ON" ( + echo --clang-cl and --msvc select different compilers. + exit /b 1 +) +if "%use_ninja%" == "ON" if "%use_msbuild%" == "ON" ( + echo --ninja and --msbuild select different generators. + exit /b 1 +) + +set "generator=!vs_gen_%vs_version%!" +if "%use_ninja%" == "ON" ( + set "generator=Ninja Multi-Config" + call :setup_dev_env + %error_check% + set "using_ninja=ON" +) + +if "%using_ninja%" == "ON" ( + if "%use_clang_cl%" == "ON" ( + REM Bare, so it resolves from the PATH the dev shell just set up, which + REM is the clang shipped with Visual Studio. --clang-path names another. + set "clang_exe=clang-cl.exe" + if not "%clang_path%" == "" set "clang_exe="%clang_path%"" + set "gen_args=-DCMAKE_C_COMPILER=!clang_exe! -DCMAKE_CXX_COMPILER=!clang_exe!" + ) +) else ( + set "gen_args=-A !arch!" + if "%use_clang_cl%" == "ON" ( + set "gen_args=!gen_args! -T ClangCL" + ) +) + +if not "%clang_path%" == "" if not "%using_ninja%" == "ON" ( + echo --clang-path needs the Ninja generator; add -x. The Visual Studio + echo generator builds clang-cl through its own ClangCL toolset. + exit /b 1 +) + +REM Ninja prints [123/456] by default, which says nothing about how far +REM along it is or how long is left. %p is a percentage, %w and %W are +REM elapsed and remaining, but those two arrived in ninja 1.12 and an +REM unknown placeholder is fatal rather than a warning, so an older ninja +REM would abort the build instead of degrading. Ask which one is on PATH. +REM Keep %p first inside the brackets: VS Code reads the first digits% +REM it finds there to drive its progress bar. +if "%using_ninja%" == "ON" if not defined NINJA_STATUS ( + set "nj_major=0" + set "nj_minor=0" + for /f "tokens=1,2 delims=." %%a in ('ninja --version 2^>nul') do ( + set "nj_major=%%a" + set "nj_minor=%%b" + ) + set "NINJA_STATUS=[%%s/%%t %%p :: %%e] " + if !nj_major! GTR 1 set "NINJA_STATUS=[%%f/%%t %%p :: %%w / %%W] " + if !nj_major! EQU 1 if !nj_minor! GEQ 12 set "NINJA_STATUS=[%%f/%%t %%p :: %%w / %%W] " +) +if "%verbose%" == "ON" if defined NINJA_STATUS echo Ninja progress format: !NINJA_STATUS! + +if "%deps_target%" == "" ( + set "deps_target=deps" +) + +REM Only the builds need CMake. -p zips an existing tree with 7z. +if "%build_deps%%build_slicer%" == "" goto :cmake_ready + +cmake --version >nul 2>nul +if not !errorlevel! == 0 ( + echo CMake was not found. Have you installed the system dependencies? + exit /b 1 +) + +REM Strawberry Perl ships a c/bin full of GNU tools, and the top-level +REM CMakeLists refuses to configure when it precedes CMake on PATH. Put a +REM real CMake first, skipping any hit from Strawberry's own cmake.exe, or +REM this pins exactly the order it is meant to undo. The findstr needle +REM must not end in a backslash, which escapes the quote and matches nothing. +set "cmake_bin=" +for /f "delims=" %%i in ('where cmake 2^>nul') do ( + if not defined cmake_bin ( + echo %%~dpi| findstr /i /c:"\Strawberry\c\bin" >nul || set "cmake_bin=%%~dpi" + ) +) +if defined cmake_bin set "PATH=%cmake_bin%;%PATH%" +:cmake_ready + +if "%config%" == "" set "config=%cfg_default%" + +REM Judge what the lookup returned rather than whether the name is defined. +REM "release " passed `if defined` and then expanded to nothing, leaving an +REM empty build directory for --clean to remove. +set "build_type=!cfg_type_%config%!" +set "tree_name=!cfg_dir_%config%!" +set "dep_name=!cfg_dep_%config%!" +set "bad_config=" +if "!build_type!" == "" set "bad_config=ON" +if "!tree_name!" == "" set "bad_config=ON" +if "!dep_name!" == "" set "bad_config=ON" +if defined bad_config ( + echo Unknown configuration "%config%". Known configurations: release, debug, relwithdebinfo, minsizerel. + exit /b 1 +) + +REM A tree is only good for the configuration, compiler and architecture it +REM was made with. Change one underneath it and CMake resets its cache and +REM carries on, leaving ExternalProject stamps from the old toolchain and +REM sub-builds that fail scattershot on things that build fine from scratch. +REM So name the tree for all three. MSVC x64 keeps the historical names. +if "%use_clang_cl%" == "ON" set "tree_name=!tree_name!-clang" +if /I "%arch%" == "ARM64" set "tree_name=!tree_name!-arm64" +if "%use_clang_cl%" == "ON" set "dep_name=!dep_name!-clang" +if /I "%arch%" == "ARM64" set "dep_name=!dep_name!-arm64" + +set "build_dir=!tree_name!" +if not "%slicer_dir%" == "" set "build_dir=%slicer_dir%" + +REM Resolve it once. --build-dir may arrive absolute, relative, or with +REM forward slashes, and anything that prints the directory has to show a +REM real path rather than one glued onto the repository root. +for %%p in ("!build_dir!") do set "build_full=%%~fp" +echo Configuration: %build_type%, %arch% + +set "SIG_FLAG=" +if defined ORCA_UPDATER_SIG_KEY set "SIG_FLAG=-DORCA_UPDATER_SIG_KEY=%ORCA_UPDATER_SIG_KEY%" + +set "TESTS_FLAG=-DBUILD_TESTS=OFF" +if "%build_tests%" == "ON" set "TESTS_FLAG=-DBUILD_TESTS=ON" +if "%run_tests%" == "ON" set "TESTS_FLAG=-DBUILD_TESTS=ON" + +set "SLICER_TARGET_FLAG=" +if not "%slicer_target%" == "" set "SLICER_TARGET_FLAG=--target %slicer_target%" + +set "DEP_TREE=deps/!dep_name!" +set "DEP_TREE_PACK=%WP%\deps\!dep_name!" +set "DEP_TREE_FLAG=" +if not "%deps_dir%" == "" ( + set "DEP_TREE=%deps_dir%" + set "DEP_TREE_PACK=%deps_dir%" + set "DEP_TREE_FLAG=-DDEP_BUILD_DIR="%deps_dir%"" +) + +REM CMakeLists derives DEP_BUILD_DIR from the build directory NAME, which is +REM wrong whenever the build tree and the dependency tree are named +REM differently, as they are for every configuration that shares the release +REM dependencies. Name it outright once the two stop matching. +if "!DEP_TREE_FLAG!" == "" if not "%build_dir%" == "!dep_name!" ( + set "DEP_TREE_FLAG=-DDEP_BUILD_DIR="%WP%\deps\!dep_name!"" +) + +set "VERBOSE_FLAG=" +if "%verbose%" == "ON" set "VERBOSE_FLAG=--verbose" + +REM The two generators count differently. +REM +REM Ninja counts compilers, so -j is exact. The deps superbuild reads +REM CMAKE_BUILD_PARALLEL_LEVEL for the sub-builds it drives. +REM +REM MSBuild turns -j into /m, which counts projects, not compilers: /MP still +REM runs one cl per core inside each one, so -j 1 would not give one +REM compiler. CL_MPCount sets the /MP degree instead, and MSBuild reads +REM properties from the environment, so nested deps builds inherit it. /m +REM stays at its default of one project at a time. +set "JOBS_FLAG=" +if "%jobs%" == "" goto :jobs_ready +echo %jobs%| findstr /r /c:"^[1-9][0-9]*$" >nul +if errorlevel 1 ( + echo Invalid --jobs value "%jobs%". Expected a positive integer. + exit /b 1 +) +if "%using_ninja%" == "ON" ( + set "JOBS_FLAG=-j %jobs%" + set "CMAKE_BUILD_PARALLEL_LEVEL=%jobs%" +) else ( + set "CL_MPCount=%jobs%" +) +echo Parallel jobs: %jobs% +:jobs_ready + +REM The flags that reproduce this run, for the commands the summary suggests. +REM Kept in three parts because they are not all relevant everywhere: a deps +REM retry has no use for --build-dir, and the bundle line names its own tree. +set "recall_tc=" +if "%use_clang_cl%" == "ON" set "recall_tc=!recall_tc! -l" +if "%using_ninja%" == "ON" set "recall_tc=!recall_tc! -x" +if /I not "%config%" == "%cfg_default%" set "recall_tc=!recall_tc! --config %config%" +if not "%target_arch%" == "" set "recall_tc=!recall_tc! --arch %target_arch%" +if not "%vs_pinned%" == "" set "recall_tc=!recall_tc! --vs %vs_pinned%" +if not "%clang_path%" == "" set "recall_tc=!recall_tc! --clang-path "%clang_path%"" + +set "recall_i=" +if "%install_slicer%" == "ON" set "recall_i= -i" +set "recall_dd=" +if not "%deps_dir%" == "" set "recall_dd= --deps-dir "%deps_dir%"" +set "recall_bd=" +if not "%slicer_dir%" == "" set "recall_bd= --build-dir "%slicer_dir%"" +set "recall=!recall_tc!!recall_i!!recall_dd!!recall_bd!" + +REM =========================================================================== +REM Running the build +REM =========================================================================== + +REM CMake 4 refuses a pre-3.5 policy version; several deps still ask for one. +set CMAKE_POLICY_VERSION_MINIMUM=3.5 + +set _START_TIME=%TIME% + +if "%build_deps%" == "ON" ( + REM Which stage is running, so a failure can name it and suggest a + REM retry scoped to it rather than to the whole run. + set "stage=d" + echo Building the dependencies... + + if "%clean%" == "ON" ( + call :clean_tree "!DEP_TREE!" + %error_check% + ) + + if not "%no_configure%" == "ON" ( + call :print_and_run cmake -S deps -B "!DEP_TREE!" -G "%generator%" %gen_args% -DCMAKE_BUILD_TYPE=%build_type% !deps_args! %ORCA_DEPS_CMAKE_ARGS% + %error_check% + ) + + call :print_and_run cmake --build "!DEP_TREE!" --config %build_type% --target %deps_target% %JOBS_FLAG% %VERBOSE_FLAG% + %error_check% +) + +if "%pack_deps%" == "ON" ( + set "stage=p" + setlocal ENABLEDELAYEDEXPANSION + call :print_and_run cd /d "!DEP_TREE_PACK!" + %error_check% + REM date /t prints in the machine locale and its field order varies by + REM region, which is how the inherited parse produced YYYYDDMM. Ask for + REM an unambiguous stamp instead. powershell.exe lives under + REM System32\WindowsPowerShell rather than System32, so a trimmed PATH + REM cannot find it and the stamp comes back empty. + set "ps=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" + set "build_date=" + for /f %%d in ('!ps! -NoProfile -Command "Get-Date -Format yyyyMMdd"') do set "build_date=%%d" + if "!build_date!" == "" ( + set "die_reason=Could not read the date from !ps!." + set rc=1 + goto :die + ) + + REM A bundle is only good for what built it, so it carries the same parts + REM as the dependency tree. Release x64 on cl keeps the plain name. + set "dep_flavour=!cfg_dep_%config%!" + set "dep_flavour=!dep_flavour:build=!" + set "dep_variant=%arch%" + if "%use_clang_cl%" == "ON" set "dep_variant=!dep_variant!-clang" + set "dep_variant=!dep_variant!!dep_flavour!" + echo Packing the dependencies: OrcaSlicer_dep_win-!dep_variant!_!build_date!.zip + + REM tools\7z.exe loads its codecs from a 7z.dll, and the repo does not + REM carry one, so it can only archive on a machine that has 7-Zip + REM installed. Windows has shipped bsdtar in System32 since 10 1803 and + REM it writes an ordinary deflate zip, so fall back to that rather than + REM failing on a machine that has everything it needs. + set "zipper=" + if exist "%WP%\tools\7z.dll" set "zipper=%WP%/tools/7z.exe a" + if not defined zipper if exist "%SystemRoot%\System32\tar.exe" set "zipper=%SystemRoot%\System32\tar.exe -a -c -f" + if not defined zipper ( + set "die_reason=No archiver. tools\7z.exe needs a 7z.dll, and this Windows has no System32\tar.exe." + set "die_hint=Install 7-Zip, or copy its 7z.dll into tools\." + set rc=1 + goto :die + ) + + call :print_and_run !zipper! OrcaSlicer_dep_win-!dep_variant!_!build_date!.zip OrcaSlicer_dep + %error_check% + REM endlocal is about to discard the name, so carry the path out with it. + for %%z in ("!DEP_TREE_PACK!\OrcaSlicer_dep_win-!dep_variant!_!build_date!.zip") do endlocal & set "bundle=%%~fz" +) + +if "%build_slicer%" == "ON" ( + set "stage=s" + echo Building OrcaSlicer... + + if "%clean%" == "ON" ( + call :clean_tree "%build_dir%" + %error_check% + ) + + if "%slicer_asan%" == "ON" ( + set "slicer_args=!slicer_args! -DSLIC3R_ASAN=ON" + ) + + REM Configuring against a tree that was never built fails deep inside + REM package resolution. Name it here instead. Skipped when -d is about to + REM build it in this same run, and under a dry run, which configures + REM nothing and must not depend on what happens to be on the machine. + if not "%dry_run%" == "ON" if not "%no_configure%" == "ON" if not "%build_deps%" == "ON" if not exist "!DEP_TREE!\OrcaSlicer_dep\usr\local\" ( + for %%p in ("!DEP_TREE!") do set "die_reason=Dependencies not found at %%~fp" + set "die_hint=Build them with build_win.bat -d!recall!." + REM Only worth suggesting to someone who did not name a tree. + if "%deps_dir%" == "" set "die_hint=Build them with build_win.bat -d!recall!, or point --deps-dir at an existing tree." + set rc=1 + goto :die + ) + + if not "%no_configure%" == "ON" ( + call :print_and_run cmake -B "%build_dir%" -G "%generator%" %gen_args% -DORCA_TOOLS=ON %SIG_FLAG% %TESTS_FLAG% %DEP_TREE_FLAG% -DCMAKE_BUILD_TYPE=%build_type% !slicer_args! %ORCA_SLICER_CMAKE_ARGS% + %error_check% + ) + + call :print_and_run cmake --build "%build_dir%" --config %build_type% %SLICER_TARGET_FLAG% %JOBS_FLAG% %VERBOSE_FLAG% + %error_check% + + if "%run_tests%" == "ON" ( + call :print_and_run ctest --test-dir "%build_dir%/tests" -C %build_type% --output-on-failure + %error_check% + ) + + if not "%no_gettext%" == "ON" ( + call :print_and_run call scripts/run_gettext.bat + %error_check% + ) + + if "%install_slicer%" == "ON" ( + call :print_and_run cmake --build "%build_dir%" --target install --config %build_type% + %error_check% + ) +) + +REM Elapsed wall clock. The 1%%a-100 trick strips a leading zero, which set /A +REM would otherwise read as octal, and a negative total means the build ran +REM past midnight. +for /f "tokens=1-3 delims=:.," %%a in ("%_START_TIME: =0%") do set /a "_start_s=(1%%a-100)*3600+(1%%b-100)*60+(1%%c-100)" +for /f "tokens=1-3 delims=:.," %%a in ("%TIME: =0%") do set /a "_end_s=(1%%a-100)*3600+(1%%b-100)*60+(1%%c-100)" +set /a "_elapsed=_end_s - _start_s" +if %_elapsed% lss 0 set /a "_elapsed+=86400" +set /a "_hours=_elapsed / 3600" +set /a "_remainder=_elapsed - _hours * 3600" +set /a "_mins=_remainder / 60" +set /a "_secs=_remainder - _mins * 60" +call :summary +exit /b 0 + +REM Reached only by error_check, from the top level, where exit /b works. +REM The hash block is what CMakeLists already uses for a build that cannot +REM continue, so it means the same thing here. +:die +echo. +echo ############################################################# +REM The paths that set die_reason had no command fail, so last_cmd there +REM names one that succeeded. +if defined die_reason echo !die_reason! +if not defined die_reason if defined last_cmd echo Failed: !last_cmd! +if defined die_hint echo !die_hint! +echo Exit code %rc%. +REM -v only makes the build verbose, so it has nothing to offer a configure +REM that failed before any compiler ran. +set "failed_configure=" +if "!last_cmd:~0,9!" == "cmake -B " set "failed_configure=ON" +if "!last_cmd:~0,9!" == "cmake -S " set "failed_configure=ON" +REM Scoped to the stage that failed. Offering -c for the whole run would +REM discard a dependency tree that was not at fault, and neither flag does +REM anything for a failed pack. A named reason already carries its own advice. +if "!stage!" == "d" set "recall=!recall_tc!!recall_dd!" +if not defined die_reason if defined stage if not "!stage!" == "p" ( + echo. + echo Try + if not defined failed_configure echo build_win.bat -!stage!!recall! -v show the failing compiler command line + echo build_win.bat -!stage!!recall! -c discard that tree and configure from scratch +) +echo ############################################################# +exit /b %rc% + +REM =========================================================================== +REM Function definitions +REM =========================================================================== + +REM summary - what was produced, and what to do with it next. A dry run says +REM so rather than claiming the files exist, but every line below that is +REM worked out the same way in either run. +:summary + for %%p in ("!DEP_TREE!") do set "dep_full=%%~fp" + REM The binary only leaves the build tree when it is installed. + set "slicer_exe=%build_dir%\src\%build_type%\orca-slicer.exe" + if "%install_slicer%" == "ON" set "slicer_exe=%build_dir%\OrcaSlicer\orca-slicer.exe" + for %%p in ("!slicer_exe!") do set "slicer_full=%%~fp" + REM Naming a target builds it and its dependencies, not its dependents, + REM so only a full build or the executable's own target relinks. + set "linked=ON" + if not "%slicer_target%" == "" set "linked=" + if /I "%slicer_target%" == "OrcaSlicer" set "linked=ON" + + echo. + echo ------------------------------------------------------------- + if "%dry_run%" == "ON" ( + echo Dry run: nothing was built. A real run would report: + ) else ( + echo Build completed in %_hours%h %_mins%m %_secs%s + ) + + if "%build_deps%" == "ON" echo Dependencies !dep_full! + if "%build_slicer%" == "ON" if "%linked%" == "ON" echo OrcaSlicer !slicer_full! + if "%build_slicer%" == "ON" if not "%linked%" == "ON" echo Target %slicer_target% + if "%build_slicer%" == "ON" if not "%using_ninja%" == "ON" echo Solution %build_full%\OrcaSlicer.sln + if "%pack_deps%" == "ON" if defined bundle echo Bundle !bundle! + + echo. + echo Next + if "%build_slicer%" == "ON" ( + if "%linked%" == "ON" echo Run it !slicer_exe! + if not "%using_ninja%" == "ON" echo Open in Visual Studio %build_dir%\OrcaSlicer.sln + if "%linked%" == "ON" echo Rebuild after edits build_win.bat -s!recall! --no-configure + if not "%linked%" == "ON" echo Relink the binary build_win.bat -s!recall! --no-configure + if "%linked%" == "ON" if "%using_ninja%" == "ON" echo Rebuild one target build_win.bat -s!recall! --no-configure --slicer-target libslic3r + if "%run_tests%" == "ON" echo Re-run the tests ctest --test-dir %build_dir%/tests -C %build_type% --output-on-failure + ) else ( + if "%build_deps%" == "ON" echo Build the slicer build_win.bat -s!recall! + ) + if "%pack_deps%" == "ON" echo Share the bundle unzip it elsewhere, then build_win.bat -s!recall_tc! --deps-dir ^ + echo ------------------------------------------------------------- + exit /b 0 + +:debug_msg + if "%debugscript%" == "ON" echo %* + exit /b 0 + +REM get_str_len -> length in %ret% +:get_str_len + setlocal + set "in=%~1" + for /L %%i in (0, 1, 100) do ( + if "!in:~%%i,1!" == "" ( + set out=%%i + goto :break_str_len + ) + ) + + echo error in get_str_len: string is too long + endlocal + exit /b 1 + + :break_str_len + endlocal & set ret=%out% + exit /b 0 + +:print_help_msg + setlocal + + REM Measure the widest flag column. Section headers have no flags and + REM must not widen it. + set flags= + set max_len=0 + set /A range_end = %argdefn% - 1 + for /L %%i in (0, 1, %range_end%) do ( + if "!argdefs[%%i].TYPE!" == "section" ( + set "flags[%%i]=" + ) else ( + set str_placeholder= + if not "!argdefs[%%i].TYPE!" == "bool" ( + set "str_placeholder= " + ) + + REM The placeholder goes on the long form only. Repeating it on + REM the short one says nothing extra and costs eight columns. + set "flag_str=" + if not "!argdefs[%%i].SHORT_FLAG!" == "" ( + set "flag_str=-!argdefs[%%i].SHORT_FLAG!" + ) + + if not "!argdefs[%%i].LONG_FLAG!" == "" ( + if "!flag_str!" == "" ( + REM No short flag: pad so the long one lines up with the others. + set "flag_str= " + ) else ( + set "flag_str=!flag_str!, " + ) + set "flag_str=!flag_str!--!argdefs[%%i].LONG_FLAG!!str_placeholder!" + ) else ( + set "flag_str=!flag_str!!str_placeholder!" + ) + set "flag_str=!flag_str! " + set "flags[%%i]=!flag_str!" + call :get_str_len "!flag_str!" + if !ret! GTR !max_len! ( + set max_len=!ret! + ) + ) + ) + + set padding= + for /L %%i in (0, 1, %max_len%) do set "padding=!padding! " + + echo Builds OrcaSlicer and its dependencies on Windows. + echo. + echo Usage: %script_name% [options] + set /A range_end = %argdefn% - 1 + for /L %%i in (0, 1, !range_end!) do ( + if "!argdefs[%%i].TYPE!" == "section" ( + echo. + echo !argdefs[%%i].HELP_TEXT!: + ) else ( + set "flag=!flags[%%i]!%padding%" + echo !flag:~0,%max_len%!!argdefs[%%i].HELP_TEXT! + ) + ) + echo. + echo Examples: + echo %script_name% --install-vs ide Set up a new machine, then restart the shell + echo %script_name% -ds Build the dependencies, then the slicer + echo %script_name% -s -l -x Rebuild the slicer with clang-cl and Ninja + echo %script_name% -s --no-configure -j 8 Rebuild quickly while iterating + echo %script_name% -s --slicer-target glad Compile one target to check the toolchain + echo %script_name% -l -x --run-tests Test that toolchain's build, not the default one + echo. + echo Environment: + echo ORCA_DEPS_CMAKE_ARGS Extra arguments for the deps configure + echo ORCA_SLICER_CMAKE_ARGS Extra arguments for the slicer configure + echo ORCA_UPDATER_SIG_KEY Update signing key baked into the slicer + echo git_commit_hash Revision to stamp, so a commit does not rebuild everything + echo NINJA_STATUS Ninja progress format, if you want your own + echo debugscript Set to ON to trace this script + echo. + echo set ORCA_SLICER_CMAKE_ARGS=-DSLIC3R_PCH=OFF -DSLIC3R_MSVC_PDB=OFF + echo $env:ORCA_SLICER_CMAKE_ARGS = '-DSLIC3R_PCH=OFF' (PowerShell) + echo. + echo --deps-args and --slicer-args cannot carry a value with spaces; use + echo these instead. Neither form supports a value containing an ampersand. + endlocal + exit /b 0 + +REM add_section +:add_section + set argdefs[%argdefn%].VARIABLE_NAME= + set argdefs[%argdefn%].TYPE=section + set argdefs[%argdefn%].SHORT_FLAG= + set argdefs[%argdefn%].LONG_FLAG= + set argdefs[%argdefn%].HELP_TEXT=%~1 + set /A argdefn+=1 + exit /b 0 + +REM add_arg <variable> <type:bool,string,rawstring> <short> <long> <help> +:add_arg + set argdefs[%argdefn%].VARIABLE_NAME=%~1 + set argdefs[%argdefn%].TYPE=%~2 + set argdefs[%argdefn%].SHORT_FLAG=%~3 + set argdefs[%argdefn%].LONG_FLAG=%~4 + set argdefs[%argdefn%].HELP_TEXT=%~5 + + REM Start every option unset, so "defined" means the user gave it. + set %~1= + + if "%debugscript%" == "ON" ( + echo add_arg VARIABLE_NAME: !argdefs[%argdefn%].VARIABLE_NAME! + echo add_arg TYPE: !argdefs[%argdefn%].TYPE! + echo add_arg SHORT_FLAG: !argdefs[%argdefn%].SHORT_FLAG! + echo add_arg LONG_FLAG: !argdefs[%argdefn%].LONG_FLAG! + echo add_arg HELP_TEXT: !argdefs[%argdefn%].HELP_TEXT! + ) + + set /A argdefn+=1 + + exit /b 0 + +REM find_arg <type:short,long> <flag> -> index in %ret% +:find_arg + setlocal + call :debug_msg starting function: find_arg "%~1" "%~2" + + set type= + if /I "%~1" == "short" set type=SHORT + if /I "%~1" == "long" set type=LONG + if not defined type ( + endlocal + set ret= + exit /b 1 + ) + + call :debug_msg find_arg type=%type% + set /A range_end = %argdefn% - 1 + for /L %%i in (0, 1, %range_end%) do ( + if "!argdefs[%%i].%type%_FLAG!" == "%~2" ( + set idx=%%i + goto :find_arg_cont + ) + ) + + echo Error in find_arg: Failed to find arg "%~2" + call :print_help_msg + + endlocal + set ret= + exit /b 1 + + :find_arg_cont + call :debug_msg find_arg: found at %idx% + endlocal & ( + set ret=%idx% + ) + exit /b 0 + +REM set_arg <index> [<value>] +:set_arg + call :debug_msg starting function: set_arg "%~1" "%~2" + + if "%~1" == "" ( + echo Error in set_arg: no index provided + exit /b 1 + ) + + setlocal + if /I "!argdefs[%~1].TYPE!" == "bool" ( + call :debug_msg set_arg: setting bool type to ON + set val=ON + ) else ( + set "val=%~2" + set quote_char=" + REM Delayed expansion throughout: %val% here would be the value from the + REM previous call, since the whole block is parsed before it runs. + if "!val:~0,1!" == "!quote_char!" ( + if "!val:~-1,1!" == "!quote_char!" ( + set "val=!val:~1,-1!" + ) + ) + + call :debug_msg set_arg: setting string type to %~2 + ) + set var_name=!argdefs[%~1].VARIABLE_NAME! + + endlocal & ( + REM Set variable in parent scope + set "%var_name%=%val%" + + REM Add variable to finalize command + set "finalize_cmd=%finalize_cmd% & set "%var_name%=%val%"" + ) + + exit /b 0 + +REM get_arg_type <index> -> type in %ret% +:get_arg_type + call :debug_msg starting function get_arg_type "%~1" + setlocal + set type=!argdefs[%~1].TYPE! + endlocal & set ret=%type% + exit /b 0 + +REM echo_var <variable> +:echo_var + echo %~1=!%~1! + exit /b 0 + +:autodetect_vs + REM Nothing to detect once the release is pinned, or under Ninja. + if not "%vs_version%" == "" exit /b 0 + if "%use_ninja%" == "ON" exit /b 0 + + setlocal + + %VSWHERE% -nologo >nul 2>nul + if not !errorlevel! == 0 ( + REM vswhere is not in its usual place; try msbuild instead. + goto :msbuild_check + ) + + echo Detecting Visual Studio version using vswhere... + for /f "tokens=1 delims=." %%i in ('%VSWHERE% -nologo -products * -latest -property catalog_productDisplayVersion') do ( + set "VS_MAJOR=%%i" + goto :version_found + ) + + :msbuild_check + where msbuild >nul 2>nul + if not !errorlevel! == 0 ( + REM No msbuild either; leave the release empty and let the default apply. + endlocal + exit /b 0 + ) + + echo Detecting Visual Studio version using msbuild... + + REM The version line varies by release, so try two patterns for it. + set VS_MAJOR= + for /f "tokens=*" %%i in ('msbuild -version 2^>^&1 ^| findstr /r "^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"') do ( + for /f "tokens=1 delims=." %%a in ("%%i") do set VS_MAJOR=%%a + set MSBUILD_OUTPUT=%%i + goto :version_found + ) + + REM The same pattern unanchored, for releases that print a banner first. + if "%VS_MAJOR%"=="" ( + for /f "tokens=*" %%i in ('msbuild -version 2^>^&1 ^| findstr /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"') do ( + for /f "tokens=1 delims=." %%a in ("%%i") do set VS_MAJOR=%%a + set MSBUILD_OUTPUT=%%i + goto :version_found + ) + ) + + :version_found + set "detected=!vs_year_%VS_MAJOR%!" + if not defined detected ( + echo Error: Unsupported Visual Studio major version: %VS_MAJOR% + endlocal + exit /b 1 + ) + echo Detected Visual Studio %VS_MAJOR% ^(!detected!^) + endlocal & set "vs_version=%detected%" + + exit /b 0 + +:setup_dev_env + REM A dry run runs nothing, and VsDevCmd costs seconds. + if "%dry_run%" == "ON" exit /b 0 + set "dev_arch=x64" + if /I "!arch!" == "ARM64" set "dev_arch=arm64" + %VSWHERE% -nologo >nul 2>nul + if not !errorlevel! == 0 ( + REM vswhere is not in its usual place; use the environment as it stands. + exit /b 0 + ) + + for /f "tokens=*" %%i in ('%VSWHERE% -nologo -products * -latest -property resolvedInstallationPath') do ( + set "VS_PATH=%%i" + goto :vs_path_found + ) + exit /b 0 + + :vs_path_found + call "%VS_PATH%\Common7\Tools\VsDevCmd.bat" -arch=!dev_arch! >nul 2>nul + set "VS_PATH=" + + exit /b 0 + +REM clean_tree <path> - remove a build tree, refusing anything that is not +REM one. Nothing here should ever fire; it is a floor under a bug that +REM produced a path far shorter than it looks. +:clean_tree + setlocal + set "target=%~1" + if not defined target ( + echo Refusing to clean: no directory to remove. + exit /b 1 + ) + + REM Resolve first, because "." and "deps/.." are shorter than they read. + for %%p in ("!target!") do set "full=%%~fp" + if "!full:~1!" == ":\" ( + echo Refusing to clean "!full!": that is a drive root. + exit /b 1 + ) + if /I "!full!" == "%WP%" ( + echo Refusing to clean "!full!": that is the repository itself. + exit /b 1 + ) + + call :print_and_run rmdir /S /Q "!full!" + exit /b !errorlevel! + +REM note_failed <name> <code> - record a failed install. Two winget codes +REM mean the tool is already there, which is what -u is for: +REM -1978335135 (0x8A150061) winget declined: a version is already there +REM -1978335189 (0x8A15002B) installed and already the newest version +:note_failed + if "%~2" == "0" exit /b 0 + if "%~2" == "-1978335135" exit /b 0 + if "%~2" == "-1978335189" exit /b 0 + set "install_failed=%install_failed% %~1" + exit /b 0 + +REM trim_slash <variable> - drop a trailing backslash from a path value. +REM A drive root keeps its own: "C:\" is not the same place as "C:". +:trim_slash + set "trim_value=!%~1!" + if not defined trim_value exit /b 0 + if not "!trim_value:~-1!" == "\" exit /b 0 + if "!trim_value:~-2!" == ":\" exit /b 0 + set "%~1=!trim_value:~0,-1!" + exit /b 0 + +REM kill_image <name> - stop every process of one image. The count is printed +REM first because taskkill can take a while and says nothing until it returns. +REM Never fails, so one stubborn image cannot stop the rest. +:kill_image + setlocal + REM A dry run does not count, so it prints the same on every machine. + if "%dry_run%" == "ON" ( + call :print_and_run taskkill /F /IM %~1 + endlocal + exit /b 0 + ) + set "img_count=0" + for /f "tokens=1" %%a in ('tasklist /nh /fi "IMAGENAME eq %~1" 2^>nul') do ( + if /I "%%a" == "%~1" set /a img_count+=1 + ) + if "!img_count!" == "0" ( + echo %~1: none running + endlocal + exit /b 0 + ) + echo %~1: stopping !img_count! + call :print_and_run taskkill /F /IM %~1 + endlocal + exit /b 0 + +REM print_and_run <command...> +:print_and_run + echo + %* + REM Recorded before the command runs. A set afterwards would clear the + REM errorlevel the next line returns, reporting every failure as a pass. + set "last_cmd=%*" + if not "%dry_run%" == "ON" ( + %* + exit /b !errorlevel! + ) + exit /b 0 + +REM handle_args <args...> +:handle_args + call :debug_msg starting function handle_args "%*" + if "%~1" == "" exit /b 0 + + setlocal + set arg=%~1 + set finalize_cmd= + + call :debug_msg Begin handling arg "%arg%" + + if not "%arg:~0,2%" == "--" goto :HAL_check_short + + call :debug_msg Processing long arg + + call :find_arg long %arg:~2% + %repeat_error% + set idx=%ret% + + call :get_arg_type %idx% + %repeat_error% + set type=%ret% + + if /I not "%type%" == "bool" ( + set "string_val=%~2" + if not defined string_val ( + echo Error in handle_args_loop: The option "%arg:~2%" requires a value + exit /b 1 + ) + + REM rawstring options carry cmake arguments, which start with a dash, so + REM only plain string options reject a value that looks like an option. + if /I "%type%" == "string" ( + if "!string_val:~0,1!" == "-" ( + echo Error in handle_args_loop: The option "%arg:~2%" requires a value. "!string_val!" looks like another option. + exit /b 1 + ) + ) + shift + ) + + call :set_arg %idx% "%string_val%" + %repeat_error% + goto :handle_args_loop_reset + + :HAL_check_short + if not "%arg:~0,1%" == "-" ( + echo Unknown argument: %arg% + call :print_help_msg + exit /b 1 + ) + + call :debug_msg processing short args + set /A charidx=1 + :short_arg_loop + if "!arg:~%charidx%,1!" == "" goto :handle_args_loop_reset + + call :find_arg short !arg:~%charidx%,1! + %repeat_error% + set idx=%ret% + + call :get_arg_type %idx% + %repeat_error% + set type=%ret% + + set /A start_idx = %charidx% + 1 + if /I not "%type%" == "bool" ( + REM A value may be attached (-j8) or be the next argument (-j 8). + set remaining=!arg:~%start_idx%! + if defined remaining ( + set string_val=!remaining! + ) else ( + set "string_val=%~2" + shift + ) + if not defined string_val ( + echo Error in handle_args_loop: The option "!arg:~%charidx%,1!" requires a value + exit /b 1 + ) + + REM As in the long-option branch above. + if /I "%type%" == "string" ( + if "!string_val:~0,1!" == "-" ( + echo Error in handle_args_loop: The option "!arg:~%charidx%,1!" requires a value. "!string_val!" looks like another option. + exit /b 1 + ) + ) + call :set_arg !idx! "!string_val!" + %repeat_error% + goto :handle_args_loop_reset + ) + + call :set_arg %idx% + %repeat_error% + set /A charidx+=1 + goto :short_arg_loop + + :handle_args_loop_reset + REM endlocal drops this iteration scope; finalize_cmd re-applies the options + REM it set, in the caller scope. + endlocal %finalize_cmd% + shift + goto :handle_args diff --git a/scripts/test_build_win.ps1 b/scripts/test_build_win.ps1 new file mode 100644 index 0000000000..62eaaae77b --- /dev/null +++ b/scripts/test_build_win.ps1 @@ -0,0 +1,847 @@ +<# +.SYNOPSIS + Tests build_win.bat's option handling and the commands it generates. + +.DESCRIPTION + Cases run the script with --dry-run, so nothing is configured, built or + deleted and the suite finishes in seconds. Each case asserts on the exit + code and on the command lines the script echoes. + + Adding a case means adding one row to $cases. A bare string starts a new + group. Defaults: ExpectExit is 0 and --dry-run is appended, so a row only + states what is unusual about it. + + Name what the case proves, in words + Args arguments, as an array + ExpectExit expected exit code (default 0) + DryRun append --dry-run (default $true) + First regex the first output line must match + Env environment for this case only + Contains literal strings the output must have + NotContains literal strings it must not have + Match regexes; each must match at least one output line + NotMatch regexes; none may match any output line + NotExists paths that must not exist after the case runs + +.PARAMETER Name + Run only the cases whose name matches this regex. Headings with no + matching case are not printed, and a pattern that matches nothing is a + failure rather than an empty pass. + +.EXAMPLE + powershell -File scripts/test_build_win.ps1 + +.EXAMPLE + powershell -File scripts/test_build_win.ps1 -Name solution +#> +[CmdletBinding()] +param( + [string] $Script, + [string] $Name +) + +$ErrorActionPreference = 'Stop' + +if (-not $Script) { + $here = $PSScriptRoot + if (-not $here) { $here = Split-Path -Parent $MyInvocation.MyCommand.Path } + $Script = Join-Path (Split-Path -Parent $here) 'build_win.bat' +} +if (-not (Test-Path $Script)) { throw "build_win.bat not found at $Script" } + +# cmd resumes a batch file by byte offset after `call :label`, and with LF +# endings that offset lands wrong and the label lookup fails. .gitattributes +# pins CRLF; this catches a checkout or an editor that did not honour it. +if ((Get-Content -Raw $Script) -match "(?<!`r)`n") { + throw "$Script has LF line endings; cmd needs CRLF to resume after call :label" +} +$Script = (Resolve-Path $Script).Path + +# Read the long options out of the script itself, so this cannot go stale when +# an option is added. Field order is: add_arg <var> <type> <short> <long>. +$longFlags = @( + Select-String -Path $Script -Pattern '^call :add_arg \S+ \S+ \S+ (\S+) ' | + ForEach-Object { '--' + $_.Matches[0].Groups[1].Value } +) +if ($longFlags.Count -lt 20) { throw "only found $($longFlags.Count) options in $Script; the parser above is wrong" } + +# A winget that always fails, so the prerequisite failure path runs without +# touching the machine. It has to be an .exe: a .bat invoked without `call` +# transfers control and never comes back, which would end the script instead. +# where.exe returns 1 when its patterns match nothing and never prompts. +$fixtures = Join-Path ([IO.Path]::GetTempPath()) 'build_win_test_fixtures' +New-Item -ItemType Directory -Force -Path $fixtures | Out-Null +Copy-Item "$env:SystemRoot\System32\where.exe" (Join-Path $fixtures 'winget.exe') -Force +$stubPath = "$fixtures;C:\Windows\system32;C:\Windows" + +# Stand-in ninjas that only report a version, so the 1.12 boundary in the +# progress format can be exercised on a machine whose real ninja is newer. +# A dry run skips the dev shell, so PATH here is what the script sees. +$ninjaPaths = @{} +foreach ($v in @{ old = '1.11.1'; new = '1.12.0' }.GetEnumerator()) { + $d = Join-Path $fixtures "ninja-$($v.Key)" + New-Item -ItemType Directory -Force -Path $d | Out-Null + Set-Content -Path (Join-Path $d 'ninja.bat') -Encoding ascii -Value @('@echo off', "echo $($v.Value)") + $ninjaPaths[$v.Key] = "$d;$env:PATH" +} + +# The pack stamp is checked against real dates, so a locale-dependent parse +# in the script cannot pass by looking date-shaped. Yesterday is accepted too, +# so a run that crosses midnight does not flake. +$dateStamps = @((Get-Date -Format 'yyyyMMdd'), (Get-Date).AddDays(-1).ToString('yyyyMMdd')) +$stampPattern = '_(' + ($dateStamps -join '|') + ')\.zip$' + +$cases = @( + 'argument handling' + @{ Name = 'no arguments prints help'; Args = @(); DryRun = $false + Contains = @('Usage: build_win.bat [options]', '--clang-cl') } + @{ Name = "--help lists all $($longFlags.Count) options the script defines"; Args = @('--help'); DryRun = $false + Contains = $longFlags } + @{ Name = 'help is grouped and shows usage, examples and environment'; Args = @('--help'); DryRun = $false + Contains = @('Usage: build_win.bat [options]', 'Actions:', 'Build configuration:', 'Toolchain:', + 'How much gets rebuilt:', 'Paths and extra arguments:', 'Diagnostics:', + 'Examples:', 'Environment:') } + @{ Name = 'the environment section shows what to set'; Args = @('--help'); DryRun = $false + Contains = @('ORCA_DEPS_CMAKE_ARGS', 'ORCA_SLICER_CMAKE_ARGS', 'ORCA_UPDATER_SIG_KEY', 'NINJA_STATUS', + 'set ORCA_SLICER_CMAKE_ARGS=-DSLIC3R_PCH=OFF', '(PowerShell)', 'debugscript') } + @{ Name = 'section headers do not widen the flag column'; Args = @('--help'); DryRun = $false + Match = @('^ -d, --deps +Download') } + # Windows Terminal opens at 120 columns and wraps at 120, so 119 is the + # limit. Anyone still on the old conhost gets 80 and will see wrapping. + @{ Name = 'every help line fits a 120 column console'; Args = @('--help'); DryRun = $false + NotMatch = @('^.{120,}$') } + @{ Name = 'an unknown long option is rejected'; Args = @('--nonsense'); ExpectExit = 1 + Contains = @('Failed to find arg') } + @{ Name = 'an unknown short option is rejected'; Args = @('-Z'); ExpectExit = 1 + Contains = @('Failed to find arg') } + @{ Name = 'a bare argument is rejected'; Args = @('deps'); ExpectExit = 1 + Contains = @('Unknown argument') } + @{ Name = 'an unknown architecture is rejected'; Args = @('-d', '--arch', 'sparc'); ExpectExit = 1 + Contains = @('Unknown architecture') } + @{ Name = 'a string option without a value is rejected'; Args = @('-d', '--arch'); ExpectExit = 1 + Contains = @('requires a value') } + @{ Name = 'short options can be bundled'; Args = @('-dx') + Contains = @('-G "Ninja Multi-Config"', '--target deps') } + + 'generator and compiler selection' + @{ Name = 'deps default to the Visual Studio generator'; Args = @('-d') + Contains = @('-G "Visual Studio', '-A x64', '--target deps') + NotContains = @('Ninja', 'clang-cl') } + @{ Name = '-x selects Ninja without changing compiler'; Args = @('-d', '-x') + Contains = @('-G "Ninja Multi-Config"') + NotContains = @('clang-cl', '-A x64') } + @{ Name = '-l -x builds with clang-cl under Ninja'; Args = @('-d', '-l', '-x') + Contains = @('-G "Ninja Multi-Config"', '-DCMAKE_C_COMPILER=clang-cl.exe', '-DCMAKE_CXX_COMPILER=clang-cl.exe') } + @{ Name = '-l alone uses the ClangCL toolset on the VS generator'; Args = @('-d', '-l') + Contains = @('-G "Visual Studio', '-T ClangCL') + NotContains = @('-DCMAKE_C_COMPILER') } + # --msvc and --msbuild name the defaults, so a caller can be explicit and a + # contradictory pair can be caught rather than silently resolved. + @{ Name = '--msvc is the compiler default spelled out'; Args = @('-d', '--msvc') + Contains = @('-G "Visual Studio', '-A x64') + NotContains = @('clang-cl') } + @{ Name = '--msbuild is the generator default spelled out'; Args = @('-d', '--msbuild') + Contains = @('-G "Visual Studio') + NotContains = @('Ninja') } + @{ Name = '--msvc with -x gives Ninja driving cl'; Args = @('-d', '--msvc', '-x') + Contains = @('-G "Ninja Multi-Config"') + NotContains = @('clang-cl') } + @{ Name = '--msbuild with -l gives the VS generator and the ClangCL toolset'; Args = @('-d', '--msbuild', '-l') + Contains = @('-G "Visual Studio', '-T ClangCL') } + # A developer with a standalone LLVM points at it; the VS-bundled clang + # is what a bare clang-cl.exe resolves to after the dev shell runs. + @{ Name = '--clang-path names the compiler, quoted for its spaces'; Args = @('-d', '-x', '--clang-path', 'C:\Program Files\LLVM\bin\clang-cl.exe') + Contains = @('-DCMAKE_C_COMPILER="C:\Program Files\LLVM\bin\clang-cl.exe"', + '-DCMAKE_CXX_COMPILER="C:\Program Files\LLVM\bin\clang-cl.exe"') } + @{ Name = '--clang-path is a clang request on its own'; Args = @('-d', '-x', '--clang-path', 'C:\Program Files\LLVM\bin\clang-cl.exe') + Contains = @('deps/build-clang') } + @{ Name = '--clang-path needs Ninja to take effect'; Args = @('-d', '--clang-path', 'C:\Program Files\LLVM\bin\clang-cl.exe'); ExpectExit = 1 + Contains = @('needs the Ninja generator') } + # `exist` is true for a directory too, and a directory would reach CMake + # as the compiler. + @{ Name = '--clang-path must name the exe, not its folder'; Args = @('-d', '-x', '--clang-path', 'C:\Program Files\LLVM'); ExpectExit = 1 + Contains = @('is a directory') } + @{ Name = 'a clang-cl that is not there is caught early'; Args = @('-d', '-x', '--clang-path', 'C:\nope\clang-cl.exe'); ExpectExit = 1 + Contains = @('No clang-cl at') + NotContains = @('cmake -S deps') } + @{ Name = '--clang-path contradicting --msvc is rejected'; Args = @('-d', '-x', '--msvc', '--clang-path', 'C:\Program Files\LLVM\bin\clang-cl.exe'); ExpectExit = 1 + Contains = @('select different compilers') } + @{ Name = '--clang-cl and --msvc together are rejected'; Args = @('-d', '-l', '--msvc'); ExpectExit = 1 + Contains = @('select different compilers') } + @{ Name = '--ninja and --msbuild together are rejected'; Args = @('-d', '-x', '--msbuild'); ExpectExit = 1 + Contains = @('select different generators') } + # One option with a value, driven by a table, rather than a flag per + # release. Adding a release should not need a new flag. + @{ Name = '--vs 2019 pins that release and skips autodetect'; Args = @('-d', '--vs', '2019') + Contains = @('-G "Visual Studio 16 2019"') + NotContains = @('Detecting Visual Studio') } + @{ Name = '--vs 2022 pins that release'; Args = @('-d', '--vs', '2022') + Contains = @('-G "Visual Studio 17 2022"') } + @{ Name = '--vs 2026 pins that release'; Args = @('-d', '--vs', '2026') + Contains = @('-G "Visual Studio 18 2026"') } + @{ Name = 'an unknown release is rejected and the known ones listed'; Args = @('-d', '--vs', '2015'); ExpectExit = 1 + Contains = @('Unknown Visual Studio release', '2019, 2022, 2026') } + @{ Name = '--vs and --ninja together are rejected'; Args = @('-d', '--vs', '2022', '-x'); ExpectExit = 1 + Contains = @('select different generators') } + @{ Name = 'without --vs the release is autodetected'; Args = @('-d') + Contains = @('Detecting Visual Studio') } + + 'architecture' + @{ Name = 'x64 is the default'; Args = @('-d') + Contains = @('Configuration: Release, x64') + NotContains = @('build-arm64') } + @{ Name = 'arm64 sets the generator platform and deps tree'; Args = @('-d', '--arch', 'arm64') + Contains = @('-A ARM64', 'deps/build-arm64') } + @{ Name = 'the architecture is matched case-insensitively'; Args = @('-d', '--arch', 'ARM64') + Contains = @('-A ARM64', 'deps/build-arm64') } + @{ Name = 'arm64 under Ninja has no -A but keeps the arm64 tree'; Args = @('-d', '--arch', 'arm64', '-x', '-l') + Contains = @('deps/build-clang-arm64', '-DCMAKE_C_COMPILER=clang-cl.exe') + NotContains = @('-A ') } + + 'build configurations' + # One option with a value, driven by a table, so Release is named rather + # than being whatever is left when no flag is passed. + @{ Name = 'release is the default'; Args = @('-s') + Contains = @('-DCMAKE_BUILD_TYPE=Release', 'cmake -B "build" ') } + @{ Name = '--config release is the default spelled out'; Args = @('-s', '--config', 'release') + Contains = @('-DCMAKE_BUILD_TYPE=Release', 'cmake -B "build" ') } + @{ Name = '--config debug builds into build-dbg'; Args = @('-s', '--config', 'debug') + Contains = @('-DCMAKE_BUILD_TYPE=Debug', 'cmake -B "build-dbg" ') } + @{ Name = '--config relwithdebinfo builds into build-dbginfo'; Args = @('-s', '--config', 'relwithdebinfo') + Contains = @('-DCMAKE_BUILD_TYPE=RelWithDebInfo', 'cmake -B "build-dbginfo" ') } + @{ Name = '--config minsizerel builds into build-minsize'; Args = @('-s', '--config', 'minsizerel') + Contains = @('-DCMAKE_BUILD_TYPE=MinSizeRel', 'cmake -B "build-minsize" ') } + # Batch variable names are case-insensitive, so the table lookup is too. + @{ Name = 'the configuration name is matched case-insensitively'; Args = @('-s', '--config', 'RelWithDebInfo') + Contains = @('-DCMAKE_BUILD_TYPE=RelWithDebInfo') } + @{ Name = 'an unknown configuration is rejected and the known ones listed'; Args = @('-s', '--config', 'bogus'); ExpectExit = 1 + Contains = @('Unknown configuration', 'release, debug, relwithdebinfo, minsizerel') } + # Release, RelWithDebInfo and MinSizeRel all link the /MD dependencies, so + # one tree serves all three. Debug is /MDd and cannot share. + @{ Name = 'minsizerel builds against the release deps'; Args = @('-d', '-s', '--config', 'minsizerel') + Contains = @('cmake -S deps -B "deps/build"', 'cmake -B "build-minsize" ') + NotContains = @('deps/build-minsize') } + @{ Name = 'relwithdebinfo builds against them too'; Args = @('-d', '-s', '-l', '--config', 'relwithdebinfo') + Contains = @('cmake -S deps -B "deps/build-clang"', 'cmake -B "build-dbginfo-clang" ') + NotContains = @('deps/build-dbginfo') } + @{ Name = 'debug keeps a dependency tree of its own'; Args = @('-d', '--config', 'debug') + Contains = @('cmake -S deps -B "deps/build-dbg"') } + # The build tree and the deps tree now have different names, so the + # derivation from the binary directory name cannot work and it is named. + @{ Name = 'a shared deps tree is named outright'; Args = @('-s', '-l', '--config', 'relwithdebinfo') + Match = @('-DDEP_BUILD_DIR="[A-Za-z]:\\.*\\deps\\build-clang"') } + @{ Name = 'configuration and arch combine into build-dbg-arm64'; Args = @('-s', '--config', 'debug', '--arch', 'arm64') + Contains = @('cmake -B "build-dbg-arm64" ') } + + 'what gets built' + @{ Name = 'the deps target defaults to deps'; Args = @('-d') + Contains = @('--target deps') } + @{ Name = '-t overrides the deps target'; Args = @('-d', '-t', 'dep_Boost') + Contains = @('--target dep_Boost') } + @{ Name = 'unit tests are off unless asked for'; Args = @('-s') + Contains = @('-DBUILD_TESTS=OFF') } + @{ Name = '--tests turns the unit tests on'; Args = @('-s', '--tests') + Contains = @('-DBUILD_TESTS=ON') } + @{ Name = '-a enables ASAN for the slicer'; Args = @('-s', '-a') + Contains = @('-DSLIC3R_ASAN=ON') } + @{ Name = 'the slicer build runs gettext'; Args = @('-s') + Contains = @('run_gettext.bat') } + # tools\7z.exe needs a 7z.dll beside it, which the repo does not carry, + # so the pack falls back to the bsdtar Windows ships. Either is correct; + # what matters is that one is chosen and handed the right names. + @{ Name = '-p packs the deps tree with whichever archiver is usable'; Args = @('-d', '-p') + Match = @('^\+ .*(7z\.exe a|tar\.exe -a -c -f) OrcaSlicer_dep_win-\S+\.zip OrcaSlicer_dep$') } + # The bundle is only good for what built it, so it carries the same three + # axes as the tree. Release x64 on cl keeps the historical plain name. + @{ Name = 'the bundle name carries compiler and deps flavour'; Args = @('-p', '-l', '--config', 'debug', '--arch', 'arm64') + Contains = @('OrcaSlicer_dep_win-ARM64-clang-dbg_') } + @{ Name = 'a relwithdebinfo pack is the release bundle'; Args = @('-p', '--config', 'relwithdebinfo') + Contains = @('OrcaSlicer_dep_win-x64_') + NotContains = @('-dbg', 'dbginfo') } + @{ Name = 'a plain release bundle keeps its old name'; Args = @('-p') + Contains = @('OrcaSlicer_dep_win-x64_') + NotContains = @('-clang', '-Release') } + @{ Name = 'the bundle is stamped with today, not a shuffled date'; Args = @('-p') + Match = @($stampPattern) } + # powershell.exe is not in System32 itself, so a trimmed PATH used to + # leave the stamp empty and the bundle named OrcaSlicer_dep_win-x64_.zip. + @{ Name = 'the bundle is stamped even with a bare PATH'; Args = @('-p') + Env = @{ PATH = 'C:\Windows\system32;C:\Windows' } + Match = @($stampPattern) } + @{ Name = '-p packs without rebuilding'; Args = @('-p') + Match = @('^\+ .*(7z\.exe a|tar\.exe -a -c -f) ') + NotContains = @('cmake -S deps') } + @{ Name = 'deps and slicer build in one invocation'; Args = @('-d', '-s', '-x', '-l') + Contains = @('cmake -S deps', 'cmake -B "build-clang" ') } + + 'the developer loop' + @{ Name = '--slicer-target builds one target'; Args = @('-s', '--slicer-target', 'libslic3r') + Contains = @('--config Release --target libslic3r') } + @{ Name = '--no-configure skips the slicer configure'; Args = @('-s', '--no-configure') + Contains = @('cmake --build "build"') + NotContains = @('cmake -B "build" ') } + @{ Name = '--no-configure skips the deps configure'; Args = @('-d', '--no-configure') + Contains = @('cmake --build "deps/build"') + NotContains = @('cmake -S deps') } + @{ Name = '--no-gettext skips the translation step'; Args = @('-s', '--no-gettext') + Contains = @('cmake --build "build"') + NotContains = @('run_gettext') } + # Installing copies the whole tree again for a layout only releases need, + # so it is asked for rather than assumed. + @{ Name = 'nothing is installed unless asked'; Args = @('-s') + Contains = @('run_gettext') + NotContains = @('--target install') } + @{ Name = '-i adds the install step'; Args = @('-s', '-i') + Contains = @('--target install') } + # install(TARGETS OrcaSlicer) has no OPTIONAL, so this would fail partway + # through a build instead of before it. + @{ Name = 'installing a tree with no executable is refused'; Args = @('-s', '-i', '--slicer-target', 'glad'); ExpectExit = 1 + Contains = @('--install needs the executable') + NotContains = @('cmake --build') } + # Without -s there is no install step, so there is nothing to refuse. + @{ Name = 'the same flags without a slicer build are left alone'; Args = @('-d', '-i', '--slicer-target', 'glad') + Contains = @('cmake -S deps') + NotContains = @('--install needs the executable') } + @{ Name = 'naming the executable target is allowed'; Args = @('-s', '-i', '--slicer-target', 'OrcaSlicer') + Contains = @('--target install') } + # Ninja counts compilers, so -j goes on the command line. MSBuild's -j + # counts projects while /MP still runs one cl per core inside each, so the + # cap goes to CL_MPCount in the environment instead. A dry run can only + # show that no -j is passed. + @{ Name = '-j on Ninja passes -j to cmake'; Args = @('-s', '-x', '-j', '4') + Contains = @('Parallel jobs: 4', '--config Release -j 4') } + @{ Name = '-j on Ninja reaches the deps build'; Args = @('-d', '-x', '-j', '2') + Contains = @('--target deps -j 2') } + @{ Name = '-j on MSBuild does not pass -j, which would count projects'; Args = @('-s', '-j', '4') + Contains = @('Parallel jobs: 4') + NotContains = @('-j 4') } + @{ Name = '-j on MSBuild leaves the deps build without -j too'; Args = @('-d', '-j', '2') + Contains = @('--target deps') + NotContains = @('-j 2') } + @{ Name = 'no -j means no job limit'; Args = @('-s') + NotContains = @('parallel jobs', '-j ') } + @{ Name = 'a non-numeric -j is rejected'; Args = @('-s', '-j', 'abc'); ExpectExit = 1 + Contains = @('Invalid --jobs value') } + @{ Name = 'a zero -j is rejected'; Args = @('-s', '-j', '0'); ExpectExit = 1 + Contains = @('Invalid --jobs value') } + @{ Name = 'the loop options combine into a single build command'; Args = @('-s', '-x', '--no-configure', '--no-gettext', '--slicer-target', 'libslic3r_tests', '-j', '8') + Contains = @('--target libslic3r_tests -j 8') + NotContains = @('cmake -B "build" ', 'run_gettext', '--target install') } + + 'one tree per configuration, compiler and architecture' + # CMake resets its cache and carries on when the compiler changes under an + # existing tree, leaving stamps from the old toolchain. Give each its own. + @{ Name = 'clang builds land in their own trees'; Args = @('-d', '-s', '-l') + Contains = @('cmake -S deps -B "deps/build-clang"', 'cmake -B "build-clang" ') } + @{ Name = 'MSVC keeps the historical plain names'; Args = @('-d', '-s', '--msvc') + Contains = @('cmake -S deps -B "deps/build"', 'cmake -B "build" ') + NotContains = @('build-clang') } + @{ Name = 'configuration, compiler and arch all name the tree'; Args = @('-d', '-s', '-l', '--config', 'debug', '--arch', 'arm64') + Contains = @('cmake -S deps -B "deps/build-dbg-clang-arm64"', 'cmake -B "build-dbg-clang-arm64" ') } + + 'locating the dependency tree' + # Without --deps-dir nothing is passed, so CMakeLists derives the path from + # the binary directory name as it always has. + @{ Name = 'the deps path is left to CMake by default'; Args = @('-s') + NotContains = @('-DDEP_BUILD_DIR') } + @{ Name = '--deps-dir tells the slicer where the deps are'; Args = @('-s', '--deps-dir', 'D:\orca-deps') + Contains = @('-DDEP_BUILD_DIR="D:\orca-deps"') } + @{ Name = '--deps-dir also redirects the deps build'; Args = @('-d', '--deps-dir', 'D:\orca-deps') + Contains = @('cmake -S deps -B "D:\orca-deps"', 'cmake --build "D:\orca-deps"') + NotContains = @('deps/build') } + # Paths are quoted throughout, so one with spaces survives the whole run. + @{ Name = 'a deps path with spaces survives'; Args = @('-d', '-s', '--deps-dir', 'C:\Program Files\deps') + Contains = @('-B "C:\Program Files\deps"', '-DDEP_BUILD_DIR="C:\Program Files\deps"') } + @{ Name = '--deps-dir also redirects the pack'; Args = @('-p', '--deps-dir', 'D:\orca-deps') + Contains = @('cd /d "D:\orca-deps"') } + @{ Name = 'pack uses an absolute default path'; Args = @('-p') + Match = @('^\+ cd /d "[A-Za-z]:\\.*\\deps\\build"') } + # CMakeLists derives DEP_BUILD_DIR from the build directory's name, so + # pointing the build elsewhere has to name the deps tree outright. + @{ Name = '--build-dir moves the slicer build'; Args = @('-s', '-l', '-x', '--build-dir', 'out/build/x64-clang') + Contains = @('cmake -B "out/build/x64-clang" ') } + @{ Name = '--build-dir still names the deps tree'; Args = @('-s', '-l', '-x', '--build-dir', 'out/build/x64-clang') + Match = @('-DDEP_BUILD_DIR="[A-Za-z]:\\.*\\deps\\build-clang"') } + @{ Name = '--deps-dir wins over the derived tree'; Args = @('-s', '--build-dir', 'out/build/x64-clang', '--deps-dir', 'D:\orca-deps') + Contains = @('-DDEP_BUILD_DIR="D:\orca-deps"') } + # A value ending in a backslash escapes the closing quote it is spliced + # into, so cmake would receive D:\tree" and swallow the next argument. + @{ Name = 'a trailing backslash is trimmed off a path'; Args = @('-s', '--build-dir', 'D:\tree\') + Contains = @('cmake -B "D:\tree" ') } + @{ Name = 'a build directory with spaces survives'; Args = @('-s', '--build-dir', 'C:\Program Files\tree') + Contains = @('cmake -B "C:\Program Files\tree" ') } + @{ Name = 'the default build names no deps tree'; Args = @('-s') + NotContains = @('DEP_BUILD_DIR') } + + 'saying what mode and toolchain are in play' + @{ Name = 'a dry run announces itself before the first command'; Args = @('-u') + First = '^Dry run: printing commands without running them\.$' } + @{ Name = 'the announcement leads even a full build'; Args = @('-ds') + First = '^Dry run: ' } + # Autodetect only runs for the Visual Studio generator, and only when no + # release was pinned, so it needs a Visual Studio on the machine. + @{ Name = 'the detected Visual Studio names its release year'; Args = @('-s') + Match = @('^Detected Visual Studio \d+ \(20\d\d\)$') } + @{ Name = 'pinning a release skips detection'; Args = @('-s', '--vs', '2022') + NotContains = @('Detected Visual Studio') } + + 'an action has to be asked for' + # Neither can happen without building the slicer, so they stand alone the + # way --install-vs does. + @{ Name = '--run-tests is an action on its own'; Args = @('--run-tests') + Contains = @('-DBUILD_TESTS=ON', 'ctest --test-dir') + NotContains = @('Nothing to do') } + @{ Name = '--tests is too'; Args = @('--tests') + Contains = @('-DBUILD_TESTS=ON') + NotContains = @('Nothing to do', 'ctest --test-dir') } + # Naming an action means that action, not a fuller build. + @{ Name = 'they do not add a slicer build to one already asked for'; Args = @('-d', '--tests') + Contains = @('cmake -S deps') + NotContains = @('cmake -B "build"') } + @{ Name = 'shaping options alone are not an action'; Args = @('--config', 'debug'); ExpectExit = 1 + Contains = @('Nothing to do.') + NotContains = @('Build completed') } + @{ Name = '-j alone is not an action either'; Args = @('-j', '4'); ExpectExit = 1 + Contains = @('Nothing to do.') } + @{ Name = '--install-vs counts as an action on its own'; Args = @('--install-vs', 'ide') + NotContains = @('Nothing to do.') } + + 'diagnostics' + @{ Name = '-v asks cmake for the command lines'; Args = @('-s', '-v') + Contains = @('--verbose') } + @{ Name = '-v applies to the deps build too'; Args = @('-d', '-v') + Contains = @('--target deps', '--verbose') } + # ninja's own default shows neither a percentage nor a time. %w and %W + # need ninja 1.12, and an unknown placeholder is fatal, so the format is + # chosen from the version rather than hardcoded. + @{ Name = '-v names the ninja progress format'; Args = @('-s', '-x', '-v') + Match = @('^Ninja progress format: \[.*%p.*\]') } + @{ Name = 'a ninja older than 1.12 gets the format it understands'; Args = @('-s', '-x', '-v') + Env = @{ PATH = $ninjaPaths['old'] } + Contains = @('Ninja progress format: [%s/%t %p :: %e]') } + @{ Name = 'ninja 1.12 gets elapsed and remaining'; Args = @('-s', '-x', '-v') + Env = @{ PATH = $ninjaPaths['new'] } + Contains = @('Ninja progress format: [%f/%t %p :: %w / %W]') } + @{ Name = 'a format you set yourself is left alone'; Args = @('-s', '-x', '-v') + Env = @{ NINJA_STATUS = '[mine] ' } + Contains = @('Ninja progress format: [mine]') } + @{ Name = 'MSBuild builds mention no ninja format'; Args = @('-s', '-v') + NotContains = @('Ninja progress format') } + @{ Name = 'builds are quiet without -v'; Args = @('-s') + NotContains = @('--verbose') } + + 'installing prerequisites' + # -u installs CMake, Perl and Git. Visual Studio is a separate ask, because + # most people already have it, and which one you want is a real choice. + @{ Name = '-u alone installs no Visual Studio'; Args = @('-u') + Contains = @('Kitware.CMake', 'StrawberryPerl', 'Git.Git') + NotContains = @('Microsoft.VisualStudio') } + # Asking for Visual Studio is asking to install prerequisites, so it does + # not also need -u; requiring both meant --install-vs alone did nothing. + @{ Name = '--install-vs works without -u'; Args = @('--install-vs', 'ide') + Contains = @('id=Microsoft.VisualStudio.Community', 'Kitware.CMake', 'Git.Git') } + @{ Name = '--install-vs buildtools asks for the build tools'; Args = @('-u', '--install-vs', 'buildtools') + Contains = @('id=Microsoft.VisualStudio.BuildTools') + NotContains = @('VC.CoreIde') } + @{ Name = '--install-vs ide asks for Community with the IDE component'; Args = @('-u', '--install-vs', 'ide') + Contains = @('id=Microsoft.VisualStudio.Community', 'VC.CoreIde') } + @{ Name = 'an unknown edition is rejected and the known ones listed'; Args = @('-u', '--install-vs', 'bogus'); ExpectExit = 1 + Contains = @('Unknown Visual Studio edition', 'buildtools, ide') } + @{ Name = '--vs picks which release to install'; Args = @('-u', '--vs', '2022', '--install-vs', 'buildtools') + Contains = @('id=Microsoft.VisualStudio.2022.BuildTools') } + @{ Name = '-l adds the clang compiler and the MSBuild toolset'; Args = @('-u', '--install-vs', 'buildtools', '-l') + Contains = @('VC.Llvm.Clang ', 'VC.Llvm.ClangToolset') } + # CMake 4.x breaks Boost.Context on ARM64, so the installer pins 3.31 there + # and leaves x64 on the current release. + @{ Name = 'CMake is pinned to 3.31 when installing for arm64'; Args = @('-u', '--arch', 'arm64') + Contains = @('Kitware.CMake --version 3.31.8') } + @{ Name = 'CMake is not pinned for x64'; Args = @('-u', '--arch', 'x64') + Contains = @('Kitware.CMake') + NotContains = @('--version') } + @{ Name = 'installing for arm64 asks for the ARM64 toolset'; Args = @('--install-vs', 'buildtools', '--arch', 'arm64') + Contains = @('Microsoft.VisualStudio.Component.VC.Tools.ARM64') } + @{ Name = 'an x64 install asks only for the x64 toolset'; Args = @('--install-vs', 'buildtools') + Contains = @('Microsoft.VisualStudio.Component.VC.Tools.x86.x64') + NotContains = @('VC.Tools.ARM64') } + + 'dry run changes nothing' + # clean_tree resolves the path before removing it, so the line echoed is + # absolute. It is the last thing printed before a directory goes. + @{ Name = '-c echoes the deps rmdir rather than running it'; Args = @('-d', '-c') + Match = @('^\+ rmdir /S /Q "[A-Za-z]:\\.*\\deps\\build"$') } + @{ Name = '-c removes the tree --deps-dir named, not the default one'; Args = @('-d', '-c', '--deps-dir', 'D:\orca-deps') + Match = @('^\+ rmdir /S /Q "D:\\orca-deps"$') + NotContains = @('\deps\build') } + @{ Name = '-c echoes the slicer rmdir rather than running it'; Args = @('-s', '-c') + Match = @('^\+ rmdir /S /Q "[A-Za-z]:\\.*\\build"$') } + @{ Name = 'cleaning a slicer build leaves the deps tree alone'; Args = @('-s', '-c') + NotMatch = @('rmdir.*\\deps\\') } + @{ Name = 'cleaning both builds removes both trees'; Args = @('-d', '-s', '-c') + Match = @('^\+ rmdir /S /Q "[A-Za-z]:\\[^"]*\\deps\\build"$', + '^\+ rmdir /S /Q "[A-Za-z]:\\(?!.*\\deps\\)[^"]*\\build"$') } + + # Nothing below should be reachable. They are a floor under a bug that + # hands clean_tree a path far shorter than it looks. + @{ Name = 'a configuration whose lookup comes back empty is rejected'; Args = @('-d', '-c', '--config', 'release '); ExpectExit = 1 + Contains = @('Unknown configuration') + NotContains = @('rmdir') } + @{ Name = 'a drive root is refused'; Args = @('-d', '-c', '--deps-dir', 'D:\'); ExpectExit = 1 + Contains = @('that is a drive root') + NotContains = @('+ rmdir') } + @{ Name = 'the repository itself is refused'; Args = @('-d', '-c', '--deps-dir', '.'); ExpectExit = 1 + Contains = @('that is the repository itself') + NotContains = @('+ rmdir') } + @{ Name = '-k echoes the taskkills rather than running them'; Args = @('-k') + Contains = @('+ taskkill /F /IM MSBuild.exe', '+ taskkill /F /IM cl.exe') } + @{ Name = '-k also covers the Ninja toolchain'; Args = @('-k') + Contains = @('+ taskkill /F /IM ninja.exe', '+ taskkill /F /IM clang-cl.exe') } + @{ Name = '-k announces the dry run like every other action'; Args = @('-k') + First = '^Dry run: ' } + @{ Name = '-u echoes the winget installs rather than running them'; Args = @('-u') + Contains = @('+ winget install', 'Kitware.CMake') + NotContains = @('cmake -S deps') } + # Not a dry run: winget is a stub that fails, so nothing is installed. + @{ Name = 'a failed install is reported, not claimed as success'; Args = @('-u') + DryRun = $false; ExpectExit = 1 + Env = @{ PATH = $stubPath } + Contains = @('Failed to install:', 'CMake', 'Perl', 'Git') + NotContains = @('Installed the prerequisites') } + # The reason belongs inside the frame. Every command this path ran had + # already succeeded, so naming the last one would point at the wrong thing. + @{ Name = 'a failed install names the reason, not the last command'; Args = @('-u') + DryRun = $false; ExpectExit = 1 + Env = @{ PATH = $stubPath } + Contains = @('####', 'Failed to install:') + NotContains = @('Failed: winget') } + @{ Name = 'a dry run does not claim the install happened'; Args = @('-u') + Contains = @('Dry run: nothing was installed.') + NotContains = @('are in place') } + @{ Name = '-u with a build defers that build, not a fuller one'; Args = @('-u', '-d') + Contains = @('build_win.bat -d') + NotContains = @('build_win.bat -ds') } + @{ Name = '-u on its own suggests the whole build'; Args = @('-u') + Contains = @('build_win.bat -ds') } + @{ Name = 'a dry run only echoes, it never configures'; Args = @('-d') + Contains = @('+ cmake') + NotContains = @('CMake Error', 'Configuring done') } + + 'extra configure arguments' + # --deps-args and --slicer-args are declared "rawstring", so a value that + # looks like an option is allowed through. Plain string options still + # reject one, since there it almost always means a forgotten value. + @{ Name = '--deps-args reaches the deps configure'; Args = @('-d', '--deps-args', 'FOO') + Contains = @('-DCMAKE_BUILD_TYPE=Release FOO') } + @{ Name = '--slicer-args reaches the slicer configure'; Args = @('-s', '--slicer-args', 'BAZ') + Contains = @('BAZ') } + @{ Name = '--deps-args accepts a value that starts with a dash'; Args = @('-d', '--deps-args', '-DFOO') + Contains = @('-DFOO') } + @{ Name = 'a plain string option still rejects a dash-leading value'; Args = @('-d', '--deps-target', '--tests'); ExpectExit = 1 + Contains = @('looks like another option') } + # cmd splits arguments on "=" as well as spaces, so an unquoted -D reaches + # the script as two arguments however it was invoked. + @{ Name = 'an unquoted value containing = is rejected'; Args = @('-d', '--deps-args', 'FOO=BAR'); ExpectExit = 1 + Contains = @('Unknown argument') } + # The environment overrides exist for that reason; nothing tokenises them. + @{ Name = 'ORCA_DEPS_CMAKE_ARGS reaches the deps configure'; Args = @('-d') + Env = @{ ORCA_DEPS_CMAKE_ARGS = '-DFOO=BAR -DBAZ=QUX' } + Contains = @('-DFOO=BAR -DBAZ=QUX') } + @{ Name = 'ORCA_SLICER_CMAKE_ARGS reaches the slicer configure'; Args = @('-s') + Env = @{ ORCA_SLICER_CMAKE_ARGS = '-DWANTED=1' } + Contains = @('-DWANTED=1') } + @{ Name = 'the deps override does not leak into the slicer configure'; Args = @('-s') + Env = @{ ORCA_DEPS_CMAKE_ARGS = '-DDEPSONLY=1' } + NotContains = @('-DDEPSONLY=1') } + @{ Name = 'the help points at the environment for a spaced argument'; Args = @('--help'); DryRun = $false + Contains = @('Neither form supports a value containing an ampersand') } + @{ Name = 'the help lists the environment overrides'; Args = @('--help'); DryRun = $false + Contains = @('Environment:', 'ORCA_DEPS_CMAKE_ARGS', 'ORCA_SLICER_CMAKE_ARGS') } + + 'running the unit tests' + @{ Name = '--tests builds them without running them'; Args = @('-s', '--tests') + Contains = @('-DBUILD_TESTS=ON') + NotContains = @('ctest') } + @{ Name = '--run-tests builds and runs them'; Args = @('-s', '--run-tests') + Contains = @('-DBUILD_TESTS=ON', 'ctest --test-dir "build/tests" -C Release --output-on-failure') } + @{ Name = '--run-tests follows the build type and directory'; Args = @('-s', '--run-tests', '--config', 'debug') + Contains = @('ctest --test-dir "build-dbg/tests" -C Debug') } + @{ Name = 'no tests are run by default'; Args = @('-s') + NotContains = @('ctest') } + + 'failures are reported' + @{ Name = 'a missing cmake is caught and exits non-zero'; Args = @('-d'); ExpectExit = 1 + Env = @{ PATH = 'C:\Windows\system32;C:\Windows' } + Contains = @('CMake was not found') } + @{ Name = 'packing does not need cmake, only an archiver'; Args = @('-p') + Env = @{ PATH = 'C:\Windows\system32;C:\Windows' } + NotContains = @('CMake was not found') } + # Not a dry run: a cd to a missing drive is a real failure inside a + # parenthesised block, which is where exit /b silently loses its code. + # Without the jump to :die this exits 0 and a failed build reads as a + # successful one. + @{ Name = 'a failure inside a build block reaches the caller'; Args = @('-p', '--deps-dir', 'Z:\nope') + DryRun = $false; ExpectExit = 1 + Contains = @('Exit code 1.', '####') + NotContains = @('Build completed', 'Try') } + # The retry follows the stage that failed. Offering it for the whole run + # would clean a dependency tree that was not at fault. + @{ Name = 'a failure names a retry scoped to the stage that failed'; Args = @('-d', '-s', '--deps-dir', 'Z:\nope') + DryRun = $false; ExpectExit = 1 + Contains = @('build_win.bat -d --deps-dir "Z:\nope" -c') + NotContains = @('build_win.bat -ds') } + # CMake's own failure here is hundreds of lines about package resolution. + @{ Name = 'a missing dependency tree is named, not left to CMake'; Args = @('-s', '--deps-dir', 'Z:\nope') + DryRun = $false; ExpectExit = 1 + Contains = @('Dependencies not found at', 'Build them with build_win.bat -d --deps-dir "Z:\nope"') + NotContains = @('cmake -B', 'Try') } + # Every other suggestion carries the flags that reproduce the run; a bare + # -d would point at the MSVC tree after a clang build. + @{ Name = 'the missing-deps hint names this toolchain'; Args = @('-s', '-l', '-x', '--deps-dir', 'deps/not-built') + DryRun = $false; ExpectExit = 1 + Contains = @('Build them with build_win.bat -d -l -x --deps-dir "deps/not-built"') + NotExists = @('deps/not-built') } + # A dry run configures nothing, so it must not depend on which trees happen + # to exist on the machine running the suite. + @{ Name = 'a dry run does not check for the deps tree'; Args = @('-s', '--deps-dir', 'Z:\nope') + Contains = @('cmake -B "build"') + NotContains = @('Dependencies not found') } + # -d is about to build them, so there is nothing to report yet. + @{ Name = 'building the deps in the same run skips the check'; Args = @('-d', '-s', '--deps-dir', 'Z:\nope') + DryRun = $false; ExpectExit = 1 + NotContains = @('Dependencies not found') } + # A configure fails before any compiler runs, so -v has nothing to show. + @{ Name = 'a configure failure is not offered a verbose rebuild'; Args = @('-d', '--deps-dir', 'Z:\nope') + DryRun = $false; ExpectExit = 1 + Contains = @('-c discard that tree') + NotContains = @('-v show the failing') } + # A bare --no-configure succeeds on a machine that already has a usable + # build tree, so name one that cannot exist instead. + @{ Name = 'a build failure is'; Args = @('-s', '--no-configure', '--build-dir', 'deps/no-such-tree') + DryRun = $false; ExpectExit = 1 + Contains = @('-v show the failing') + NotExists = @('deps/no-such-tree') } + # --build-dir names the slicer tree, which a deps failure has nothing to do + # with. --deps-dir stays, because that is the tree that failed. + @{ Name = 'a deps retry leaves out the slicer tree'; Args = @('-d', '-s', '--deps-dir', 'Z:\nope', '--build-dir', 'D:\b') + DryRun = $false; ExpectExit = 1 + Contains = @('build_win.bat -d --deps-dir "Z:\nope" -c') + NotContains = @('--build-dir') } + @{ Name = 'an unknown configuration stays a single line'; Args = @('-s', '--config', 'bogus'); ExpectExit = 1 + Contains = @('Unknown configuration') + NotContains = @('####', 'Try') } + @{ Name = 'a bad --jobs value is not framed either'; Args = @('-s', '-j', 'x'); ExpectExit = 1 + Contains = @('Invalid --jobs value') + NotContains = @('####') } + + 'the summary says what to do next' + # Every suggested command carries the flags that reproduce this run. + @{ Name = 'a deps build points at the slicer build'; Args = @('-d', '-l') + Contains = @('Build the slicer build_win.bat -s -l') + NotContains = @('Run it') } + @{ Name = 'a ninja slicer build offers a single target'; Args = @('-s', '-l', '-x') + Contains = @('Rebuild after edits build_win.bat -s -l -x --no-configure', 'Rebuild one target') + NotContains = @('Solution', 'Open in Visual Studio') } + @{ Name = 'a visual studio build names the solution instead'; Args = @('-s') + Contains = @('Solution ', 'Open in Visual Studio build\OrcaSlicer.sln', + 'Rebuild after edits build_win.bat -s --no-configure') + NotContains = @('Rebuild one target') } + @{ Name = 'the configuration and architecture come back'; Args = @('-s', '-l', '-x', '--config', 'debug', '--arch', 'arm64') + Contains = @('build_win.bat -s -l -x --config debug --arch arm64 --no-configure') } + @{ Name = 'the tree overrides come back quoted'; Args = @('-s', '--deps-dir', 'D:\d', '--build-dir', 'D:\b') + Contains = @('--deps-dir "D:\d" --build-dir "D:\b"') } + @{ Name = 'a pinned visual studio release comes back'; Args = @('-s', '--vs', '2022') + Contains = @('build_win.bat -s --vs 2022 --no-configure') } + # Autodetection writes what it found into the same variable, so a detected + # release must not come back as though it had been asked for. + @{ Name = 'a detected release does not'; Args = @('-s') + NotContains = @('--vs') } + @{ Name = 'the binary is named in the build tree it was built in'; Args = @('-s', '-l', '-x') + Contains = @('build-clang\src\Release\orca-slicer.exe') } + @{ Name = 'installing names the installed copy instead'; Args = @('-s', '-l', '-x', '-i') + Contains = @('build-clang\OrcaSlicer\orca-slicer.exe') } + # -i changes where the binary lands, so a rebuild that dropped it would + # leave the path above pointing at a stale copy. + @{ Name = 'the rebuild suggestion keeps -i'; Args = @('-s', '-l', '-x', '-i') + Contains = @('Rebuild after edits build_win.bat -s -l -x -i --no-configure') } + # A deps retry has no install step to repeat. + @{ Name = 'a deps retry drops it'; Args = @('-d', '-s', '-i', '--deps-dir', 'Z:\nope') + DryRun = $false; ExpectExit = 1 + Contains = @('build_win.bat -d --deps-dir "Z:\nope" -c') + NotContains = @('-d -i') } + # Naming a target builds it and its dependencies, not its dependents, so + # the binary on disk is whatever the last full build left there. + @{ Name = 'a single-target build does not claim the whole binary'; Args = @('-s', '-l', '-x', '--slicer-target', 'glad') + Contains = @('Target glad', 'Relink the binary build_win.bat -s -l -x --no-configure') + NotContains = @('Run it', 'orca-slicer.exe', 'Rebuild after edits') } + # The executable has a target of its own, and naming that one does relink. + @{ Name = 'naming the executable target still claims the binary'; Args = @('-s', '-l', '-x', '--slicer-target', 'OrcaSlicer') + Contains = @('Run it', 'orca-slicer.exe', 'Rebuild after edits') + NotContains = @('Target OrcaSlicer', 'Relink the binary') } + @{ Name = '--run-tests offers the ctest line'; Args = @('-s', '-l', '-x', '--run-tests') + Contains = @('Re-run the tests ctest --test-dir build-clang/tests -C Release') } + @{ Name = 'packing names the bundle and how to use it'; Args = @('-p', '-l') + Contains = @('Bundle ', 'Share the bundle') } + # The line supplies its own tree, so the one this run used must not ride + # along and contradict it. + @{ Name = 'the bundle line names one tree, not two'; Args = @('-s', '-p', '-l', '-x', '--deps-dir', 'D:\shared') + Contains = @('then build_win.bat -s -l -x --deps-dir <path>') + NotContains = @('--deps-dir "D:\shared" --deps-dir') } + @{ Name = 'installing prerequisites suggests the build that follows'; Args = @('-u', '-l') + Contains = @('Restart this shell', 'build_win.bat -ds -l') + NotContains = @('Run it') } + # Everything below the header line is worked out the same way in either + # run, which is why a dry run can cover it. + @{ Name = 'a dry run does not claim a build happened'; Args = @('-s', '-l', '-x') + Contains = @('Dry run: nothing was built.') + NotContains = @('Build completed in') } + # --no-configure is the iteration loop and still gets the block; four + # lines after a rebuild is not enough to be worth suppressing. + @{ Name = '--no-configure still gets the summary'; Args = @('-s', '-l', '-x', '--no-configure') + Contains = @('Next', 'Rebuild after edits') } + + 'pointing at the solution' + @{ Name = 'the VS generator says where the solution is'; Args = @('-s') + Match = @('^ Solution .*\\build\\OrcaSlicer\.sln$') } + @{ Name = 'the solution path follows the configuration'; Args = @('-s', '--config', 'debug') + Match = @('^ Solution .*\\build-dbg\\OrcaSlicer\.sln$') } + @{ Name = 'the solution line survives an install'; Args = @('-s', '-i') + Contains = @(' Solution ') } + # The path is resolved, not pasted onto the repository root, so it is + # right whether --build-dir came absolute or with forward slashes. + @{ Name = 'a moved build still prints one real path'; Args = @('-s', '--build-dir', 'out/build/x64-clang') + Match = @('^ Solution [A-Za-z]:\\[^/]+\\OrcaSlicer\.sln$') } + @{ Name = 'an absolute --build-dir is not glued onto the repo root'; Args = @('-s', '--build-dir', 'D:\tree') + Contains = @('Solution D:\tree\OrcaSlicer.sln') } +) + +function Invoke-BuildScript { + param([string[]] $Arguments, [hashtable] $Environment) + + $saved = @{} + if ($Environment) { + foreach ($key in $Environment.Keys) { + $saved[$key] = [Environment]::GetEnvironmentVariable($key) + Set-Item -Path "env:$key" -Value $Environment[$key] + } + } + try { + # 'Stop' turns a native command's stderr into a terminating error, and + # a case that exercises a real failure writes to stderr. Let the output + # through and judge the run by its exit code instead. The assignment is + # scoped to this function, so the rest of the suite keeps 'Stop'. + $ErrorActionPreference = 'Continue' + if ($Arguments.Count -eq 0) { + $out = & $Script 2>&1 | Out-String + } else { + $out = & $Script @Arguments 2>&1 | Out-String + } + return [pscustomobject]@{ Output = $out; Exit = $LASTEXITCODE } + } finally { + foreach ($key in $saved.Keys) { + if ($null -eq $saved[$key]) { Remove-Item -Path "env:$key" -ErrorAction SilentlyContinue } + else { Set-Item -Path "env:$key" -Value $saved[$key] } + } + } +} + +$knownFields = @( + 'Name', 'Args', 'ExpectExit', 'DryRun', 'First', 'Env', + 'Contains', 'NotContains', 'Match', 'NotMatch', 'NotExists' +) + +function Test-Case { + param([hashtable] $Case) + + # Read fields with the indexer, not dot notation. A hashtable exposes its + # own members too, so $Case.Contains returns the Contains *method* whenever + # the case has no key by that name. + $argv = @($Case['Args']) + if (-not $Case.ContainsKey('DryRun') -or $Case['DryRun']) { $argv += '--dry-run' } + + $expect = 0 + if ($Case.ContainsKey('ExpectExit')) { $expect = $Case['ExpectExit'] } + + $result = Invoke-BuildScript -Arguments $argv -Environment $Case['Env'] + + $problems = @() + + # A misspelled field is silently ignored by the checks below, which + # leaves the case asserting nothing at all and passing. + foreach ($field in $Case.Keys) { + if ($knownFields -notcontains $field) { $problems += "unknown field '$field'" } + } + + if ($result.Exit -ne $expect) { $problems += "exit $($result.Exit), expected $expect" } + foreach ($needle in $Case['Contains']) { + if (-not $result.Output.Contains($needle)) { $problems += "missing '$needle'" } + } + foreach ($needle in $Case['NotContains']) { + if ($result.Output.Contains($needle)) { $problems += "unexpected '$needle'" } + } + $lines = $result.Output -split "`r?`n" + if ($Case['First'] -and $lines[0] -notmatch $Case['First']) { + $problems += "first line was '$($lines[0])'" + } + foreach ($pattern in $Case['Match']) { + if (@($lines | Where-Object { $_ -match $pattern }).Count -eq 0) { + $problems += "no line matching /$pattern/" + } + } + foreach ($pattern in $Case['NotMatch']) { + foreach ($line in @($lines | Where-Object { $_ -match $pattern })) { + $problems += "line matches /$pattern/: $line" + } + } + # Output cannot show what a run did not create. + foreach ($path in $Case['NotExists']) { + $full = Join-Path (Split-Path -Parent $Script) $path + if (Test-Path $full) { + $problems += "created '$path'" + } + } + return ,$problems +} + +$pass = 0 +$failed = @() +# Held back so a filtered run does not print headings for groups it skipped. +$heading = $null + +foreach ($case in $cases) { + if ($case -is [string]) { + $heading = $case + continue + } + if ($Name -and $case['Name'] -notmatch $Name) { continue } + if ($heading) { + Write-Host '' + Write-Host $heading -ForegroundColor Cyan + $heading = $null + } + + $problems = Test-Case -Case $case + if ($problems.Count -eq 0) { + $pass++ + Write-Host (' ok ' + $case['Name']) + } else { + $failed += $case['Name'] + Write-Host (' FAIL ' + $case['Name']) -ForegroundColor Red + foreach ($problem in $problems) { Write-Host (' ' + $problem) -ForegroundColor Red } + Write-Host (' args: ' + (@($case['Args']) -join ' ')) + } +} + +Remove-Item -Recurse -Force $fixtures -ErrorAction SilentlyContinue + +Write-Host '' +# A pattern that matched nothing has proved nothing, so do not report it as +# a clean run. +if ($Name -and $pass -eq 0 -and $failed.Count -eq 0) { + Write-Host "no case matched /$Name/" -ForegroundColor Red + exit 1 +} +Write-Host "$pass passed, $($failed.Count) failed" +if ($failed.Count -gt 0) { + foreach ($name in $failed) { Write-Host " failed: $name" -ForegroundColor Red } + exit 1 +} +exit 0