|
| 1 | +@echo off |
| 2 | +setlocal EnableExtensions EnableDelayedExpansion |
| 3 | + |
| 4 | +rem Check `bazelisk` properly bootstraps `bazel` or fail with instructions |
| 5 | +if not defined BAZEL_REAL ( |
| 6 | + >&2 type "%~dp0bazelisk.md" |
| 7 | + exit /b 1 |
| 8 | +) else if not "%BAZELISK_SKIP_WRAPPER%"=="true" ( |
| 9 | + >&2 type "%~dp0bazelisk.md" |
| 10 | + exit /b 2 |
| 11 | +) |
| 12 | + |
| 13 | +rem Not in CI: simply execute `bazel` - done |
| 14 | +if not defined CI_PROJECT_DIR ( |
| 15 | + "%BAZEL_REAL%" %* |
| 16 | + exit /b !errorlevel! |
| 17 | +) |
| 18 | + |
| 19 | +rem In CI: first, verify directory environment variables are set and normalize their paths |
| 20 | +for %%v in (BAZEL_DISK_CACHE BAZEL_OUTPUT_USER_ROOT BAZEL_REPO_CONTENTS_CACHE RUNNER_TEMP_PROJECT_DIR VSTUDIO_ROOT) do ( |
| 21 | + if not defined %%v ( |
| 22 | + >&2 echo %~nx0: %%v: unbound variable |
| 23 | + exit /b 3 |
| 24 | + ) |
| 25 | + rem Path separators: `bazel` is fine with both `/` and `\\` but fails with `\`, so the simplest is to favor `/` |
| 26 | + set "%%v=!%%v:\=/!" |
| 27 | +) |
| 28 | +rem TODO(regis, if later needed): set "BAZEL_SH=C:/tools/msys64/usr/bin/bash.exe" |
| 29 | +set "BAZEL_VS=!VSTUDIO_ROOT!" |
| 30 | +set "ext_repo_contents_cache=!RUNNER_TEMP_PROJECT_DIR!/bazel-repo-contents-cache" |
| 31 | + |
| 32 | +rem Externalize `--repo_contents_cache` to the job's sibling temporary directory created alongside $CI_PROJECT_DIR |
| 33 | +rem - https://github.com/bazelbuild/bazel/issues/26384 for why |
| 34 | +rem - https://docs.gitlab.com/runner/configuration/advanced-configuration/ for `RUNNER_TEMP_PROJECT_DIR` |
| 35 | +rem - https://sissource.ethz.ch/sispub/gitlab-ci-euler-image/-/blob/main/entrypoint.sh#L43 for inspiration |
| 36 | +if exist "!BAZEL_REPO_CONTENTS_CACHE!" ( |
| 37 | + call :robomove "!BAZEL_REPO_CONTENTS_CACHE!" "!ext_repo_contents_cache!" |
| 38 | + set "rc=!errorlevel!" |
| 39 | + if !rc! neq 0 exit /b !rc! |
| 40 | +) |
| 41 | + |
| 42 | +rem Pass CI-specific options through `.user.bazelrc` so any nested `bazel run` and next `bazel shutdown` also honor them |
| 43 | +( |
| 44 | + echo startup --output_user_root=!BAZEL_OUTPUT_USER_ROOT! |
| 45 | + echo common --repo_contents_cache=!ext_repo_contents_cache! |
| 46 | + echo build --disk_cache=!BAZEL_DISK_CACHE! |
| 47 | +) >"!CI_PROJECT_DIR!\user.bazelrc" |
| 48 | + |
| 49 | +rem Payload: execute `bazel` and remember exit status |
| 50 | +"!BAZEL_REAL!" %* |
| 51 | +set "bazel_exit=!errorlevel!" |
| 52 | + |
| 53 | +rem Stop `bazel` (if still running) to close files and proceed with cleanup |
| 54 | +>&2 "!BAZEL_REAL!" shutdown --ui_event_filters=-info |
| 55 | +>&2 del /f /q "!CI_PROJECT_DIR!\user.bazelrc" |
| 56 | + |
| 57 | +rem Reintegrate `--repo_contents_cache` to original directory |
| 58 | +if exist "!ext_repo_contents_cache!" ( |
| 59 | + call :robomove "!ext_repo_contents_cache!" "!BAZEL_REPO_CONTENTS_CACHE!" |
| 60 | + set "rc=!errorlevel!" |
| 61 | + if !bazel_exit!==0 set "bazel_exit=!rc!" |
| 62 | +) |
| 63 | + |
| 64 | +rem Done |
| 65 | +exit /b !bazel_exit! |
| 66 | + |
| 67 | +:robomove |
| 68 | +rem Contrarily to `copy`, `move` and `xcopy`, `robocopy` avoids messing up with recursive symlinks, thanks to `/xj` |
| 69 | +>&2 robocopy "%~1" "%~2" /b /copyall /dcopy:dat /mir /move /ndl /nfl /njh /njs /np /secfix /sl /timfix /w:0 /xj |
| 70 | +rem See: https://ss64.com/nt/robocopy-exit.html |
| 71 | +set /a rc=!errorlevel! ^& (8 ^| 16) |
| 72 | +if exist "%~1" ( |
| 73 | + >&2 echo 🟡 Purging leftovers, most likely due to recursive symbolic links/junction points: |
| 74 | + >&2 dir /a /b /s "%~1" |
| 75 | + >&2 rmdir /q /s "%~1" |
| 76 | +) |
| 77 | +exit /b !rc! |
0 commit comments