Subject: [PATCH bazel-zig-cc] add cache prefix configuration for Windows

From: Fabian Hahn <[email-masked]>
Date: Sat, 27 Aug 2022 16:21:08 +0100

---
 toolchain/defs.bzl | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl
index a95f76e..53fd161 100644
--- a/toolchain/defs.bzl
+++ b/toolchain/defs.bzl
@@ -101,7 +101,20 @@ export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR
 exec "{zig}" "{zig_tool}" "$@"
 """
 
-ZIG_TOOL_WRAPPER_WINDOWS = """@echo off
+ZIG_TOOL_WRAPPER_WINDOWS_CACHE_KNOWN = """@echo off
+set ZIG_LOCAL_CACHE_DIR={cache_prefix}\\bazel-zig-cc
+set ZIG_GLOBAL_CACHE_DIR=%ZIG_LOCAL_CACHE_DIR%
+"{zig}" "{zig_tool}" %*
+"""
+
+ZIG_TOOL_WRAPPER_WINDOWS_CACHE_GUESS = """@echo off
+if exist "%TMP%\\*" goto :usertmp
+set ZIG_LOCAL_CACHE_DIR=C:\\Temp\\bazel-zig-cc
+goto zig
+:usertmp
+set ZIG_LOCAL_CACHE_DIR=%TMP%\\bazel-zig-cc
+:zig
+set ZIG_GLOBAL_CACHE_DIR=%ZIG_LOCAL_CACHE_DIR%
 "{zig}" "{zig_tool}" %*
 """
 
@@ -152,10 +165,17 @@ def _zig_repository_impl(repository_ctx):
     for zig_tool in _ZIG_TOOLS:
         cache_prefix = repository_ctx.os.environ.get("BAZEL_ZIG_CC_CACHE_PREFIX", "")
         if os == "windows":
-            zig_tool_wrapper = ZIG_TOOL_WRAPPER_WINDOWS.format(
-                zig = str(repository_ctx.path("zig")).replace("/", "\\") + ".exe",
-                zig_tool = zig_tool,
-            )
+            if cache_prefix:
+                zig_tool_wrapper = ZIG_TOOL_WRAPPER_WINDOWS_CACHE_KNOWN.format(
+                    zig = str(repository_ctx.path("zig")).replace("/", "\\") + ".exe",
+                    zig_tool = zig_tool,
+                    cache_prefix = cache_prefix,
+                )
+            else:
+                zig_tool_wrapper = ZIG_TOOL_WRAPPER_WINDOWS_CACHE_GUESS.format(
+                    zig = str(repository_ctx.path("zig")).replace("/", "\\") + ".exe",
+                    zig_tool = zig_tool,
+                )
         elif cache_prefix:
             zig_tool_wrapper = ZIG_TOOL_WRAPPER_CACHE_KNOWN.format(
                 zig = str(repository_ctx.path("zig")),
-- 
2.25.1

---

From: "builds.sr.ht" <[email-masked]>
Date: Sat, 27 Aug 2022 15:39:50 +0000

bazel-zig-cc/patches/.build.yml: SUCCESS in 18m16s

[add cache prefix configuration for Windows][0] from [Fabian Hahn][1]

[0]: https://lists.sr.ht/~motiejus/bazel-zig-cc/patches/34959
[1]: mailto:[email-masked]

✓ #832843 SUCCESS bazel-zig-cc/patches/.build.yml https://builds.sr.ht/~motiejus/job/832843

---

From: Motiejus Jakštys <[email-masked]>
Date: Mon, 29 Aug 2022 14:46:38 +0300

>  1 file changed, 25 insertions(+), 5 deletions(-)

Hi Fabian,

I've applied your diff and cleaned up[1][2] the accumulated mess, then
pushed to main. If I broke anything on Windows in the process, please
ping me, preferably in the next couple of days.

Thanks!
Motiejus

[1]: https://git.sr.ht/~motiejus/bazel-zig-cc/commit/e861c5b4bedb752adbdca9bfb863e9cc131c2a5c
[2]: https://git.sr.ht/~motiejus/bazel-zig-cc/commit/342b239bb90e3bd8c35e64d6bc14924600e980c5