Subject: [PATCH bazel-zig-cc] Support configuring cache prefix with an environment variable

From: ~kmicklas <[email-masked]>
Date: Thu, 25 Aug 2022 19:24:38 +0100

From: Ken Micklas <[email-masked]>

The user may want to build with `--sandbox_tmpfs_path=/tmp`, in which
case the Zig cache will not be shared between sandboxes, massively
slowing down the build. With this feature, the user can pass flags
like `--repo_env=BAZEL_ZIG_CC_CACHE_PREFIX=$HOME/.cache/bazel-zig-cc`
together with `--sandbox_writable_path=$HOME/.cache/bazel-zig-cc` to
put the cache somewhere else which can be shared between sandboxes.
---
 toolchain/defs.bzl | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl
index 965cf22..c8b5be0 100644
--- a/toolchain/defs.bzl
+++ b/toolchain/defs.bzl
@@ -79,7 +79,9 @@ def toolchains(
 ZIG_TOOL_WRAPPER = """#!/usr/bin/env bash
 set -e
 
-if [[ -n "$TMPDIR" ]]; then
+if [[ -n "{cache_prefix}" ]]; then
+  _cache_prefix="{cache_prefix}"
+elif [[ -n "$TMPDIR" ]]; then
     _cache_prefix=$TMPDIR
 elif [[ -n "$HOME" ]]; then
     if [[ "$(uname)" = Darwin ]]; then
@@ -149,6 +151,7 @@ def _zig_repository_impl(repository_ctx):
         zig_tool_wrapper = ZIG_TOOL_WRAPPER.format(
             zig = str(repository_ctx.path("zig")),
             zig_tool = zig_tool,
+            cache_prefix = repository_ctx.os.environ.get("BAZEL_ZIG_CC_CACHE_PREFIX", ""),
         )
         if os == "windows":
             zig_tool_wrapper = ZIG_TOOL_WRAPPER_WINDOWS.format(
-- 
2.34.4

---

From: "builds.sr.ht" <[email-masked]>
Date: Fri, 26 Aug 2022 07:45:11 +0000

bazel-zig-cc/patches/.build.yml: SUCCESS in 17m14s

[Support configuring cache prefix with an environment variable][0] from [~kmicklas][1]

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

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

---

From: Motiejus Jakštys <[email-masked]>
Date: Fri, 26 Aug 2022 11:08:28 +0300

> put the cache somewhere else which can be shared between sandboxes.

Thanks, applied.

Fabian, would you like to keep compatibility and do this[1] for Windows?

[1]: https://git.sr.ht/~motiejus/bazel-zig-cc/commit/15f957859146deaf524bbdf1a4b537124b305405

---

From: [email-masked]
Date: Fri, 26 Aug 2022 10:06:56 +0100

> Fabian, would you like to keep compatibility and do this[1] for 

Sure, I'll take a look at it this weekend.