Subject: [PATCH bazel-zig-cc v2 0/1] changes in windows zig toolchain wrapper

From: Fabian Hahn <[email-masked]>
Date: Sun, 11 Dec 2022 14:59:28 +0000

This is a revised version of
https://lists.sr.ht/~motiejus/bazel-zig-cc/patches/37457 which fixes
Windows host support. The key is to use %VAR% notation to interpolate
environment variables within batch file strings, and to use the proper
exe extension for the zig binary.

Note that in order for this to work on Windows, this other patch I
submitted also needs to be applied:
https://lists.sr.ht/~motiejus/bazel-zig-cc/patches/37465
This is because the current main is broken on Windows otherwise.

Best,
Fabian

Motiejus Jakštys (1):
  toolchain wrapper: zig_exe path is relative

 toolchain/defs.bzl | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

-- 
2.25.1

---

From: Fabian Hahn <[email-masked]>
Date: Sun, 11 Dec 2022 14:59:29 +0000

From: Motiejus Jakštys <[email-masked]>

Just like we know the path to zig_lib_dir, we do know the path to
zig_exe. Lets use that.

This was surfaced by experimenting with
`--experimental_use_hermetic_linux_sandbox`.
`repository_ctx.path("zig")` would return the real file system path to
zig (outside of the sandbox), which is not good when the sandbox is
hermetic.
---
 toolchain/defs.bzl | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl
index aa2e4e6..746156d 100644
--- a/toolchain/defs.bzl
+++ b/toolchain/defs.bzl
@@ -92,21 +92,25 @@ _ZIG_TOOLS = [
 _ZIG_TOOL_WRAPPER_WINDOWS_CACHE_KNOWN = """@echo off
 if exist "external\\zig_sdk\\lib\\*" goto :have_external_zig_sdk_lib
 set ZIG_LIB_DIR=%~dp0\\..\\..\\lib
+set ZIG_EXE=%~dp0\\..\\..\\zig.exe
 goto :set_zig_lib_dir
 :have_external_zig_sdk_lib
 set ZIG_LIB_DIR=external\\zig_sdk\\lib
+set ZIG_EXE=external\\zig_sdk\\zig.exe
 :set_zig_lib_dir
 set ZIG_LOCAL_CACHE_DIR={cache_prefix}\\bazel-zig-cc
 set ZIG_GLOBAL_CACHE_DIR=%ZIG_LOCAL_CACHE_DIR%
-"{zig}" "{zig_tool}" {maybe_target} %*
+"%ZIG_EXE%" "{zig_tool}" {maybe_target} %*
 """
 
 _ZIG_TOOL_WRAPPER_WINDOWS_CACHE_GUESS = """@echo off
 if exist "external\\zig_sdk\\lib\\*" goto :have_external_zig_sdk_lib
 set ZIG_LIB_DIR=%~dp0\\..\\..\\lib
+set ZIG_EXE=%~dp0\\..\\..\\zig.exe
 goto :set_zig_lib_dir
 :have_external_zig_sdk_lib
 set ZIG_LIB_DIR=external\\zig_sdk\\lib
+set ZIG_EXE=external\\zig_sdk\\zig.exe
 :set_zig_lib_dir
 if exist "%TMP%\\*" goto :usertmp
 set ZIG_LOCAL_CACHE_DIR=C:\\Temp\\bazel-zig-cc
@@ -115,29 +119,33 @@ goto zig
 set ZIG_LOCAL_CACHE_DIR=%TMP%\\bazel-zig-cc
 :zig
 set ZIG_GLOBAL_CACHE_DIR=%ZIG_LOCAL_CACHE_DIR%
-"{zig}" "{zig_tool}" {maybe_target} %*
+"%ZIG_EXE%" "{zig_tool}" {maybe_target} %*
 """
 
 _ZIG_TOOL_WRAPPER_CACHE_KNOWN = """#!/bin/sh
 set -e
 if [ -d external/zig_sdk/lib ]; then
     ZIG_LIB_DIR=external/zig_sdk/lib
+    ZIG_EXE=external/zig_sdk/zig
 else
     ZIG_LIB_DIR="$(dirname "$0")/../../lib"
+    ZIG_EXE="$(dirname "$0")/../../zig"
 fi
 export ZIG_LIB_DIR
 export ZIG_LOCAL_CACHE_DIR="{cache_prefix}/bazel-zig-cc"
 export ZIG_GLOBAL_CACHE_DIR="{cache_prefix}/bazel-zig-cc"
 {maybe_gohack}
-exec "{zig}" "{zig_tool}" {maybe_target} "$@"
+exec "$ZIG_EXE" "{zig_tool}" {maybe_target} "$@"
 """
 
 _ZIG_TOOL_WRAPPER_CACHE_GUESS = """#!/bin/sh
 set -e
 if [ -d external/zig_sdk/lib ]; then
     ZIG_LIB_DIR=external/zig_sdk/lib
+    ZIG_EXE=external/zig_sdk/zig
 else
     ZIG_LIB_DIR="$(dirname "$0")/../../lib"
+    ZIG_EXE="$(dirname "$0")/../../zig"
 fi
 if [ -n "$TMPDIR" ]; then
     _cache_prefix=$TMPDIR
@@ -154,7 +162,7 @@ export ZIG_LIB_DIR
 export ZIG_LOCAL_CACHE_DIR="$_cache_prefix/bazel-zig-cc"
 export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR
 {maybe_gohack}
-exec "{zig}" "{zig_tool}" {maybe_target} "$@"
+exec "$ZIG_EXE" "{zig_tool}" {maybe_target} "$@"
 """
 
 # The abomination below adds "-O2" to Go's link-prober command. Saves around
@@ -176,14 +184,13 @@ fi
 eval set -- "$saved"
 """
 
-def _zig_tool_wrapper(zig_tool, zig, is_windows, cache_prefix, zigtarget):
+def _zig_tool_wrapper(zig_tool, is_windows, cache_prefix, zigtarget):
     if zig_tool in ["c++", "build-exe", "build-lib", "build-obj"]:
         maybe_target = "-target {}".format(zigtarget)
     else:
         maybe_target = ""
 
     kwargs = dict(
-        zig = str(zig).replace("/", "\\") + ".exe" if is_windows else zig,
         zig_tool = zig_tool,
         cache_prefix = cache_prefix,
         maybe_gohack = _ZIG_TOOL_GOHACK if (zig_tool == "c++" and not is_windows) else "",
@@ -266,7 +273,6 @@ def _zig_repository_impl(repository_ctx):
         for target_config in target_structs():
             zig_tool_wrapper = _zig_tool_wrapper(
                 zig_tool,
-                str(repository_ctx.path("zig")),
                 os == "windows",
                 repository_ctx.os.environ.get("BAZEL_ZIG_CC_CACHE_PREFIX", ""),
                 zigtarget = target_config.zigtarget,
-- 
2.25.1

---

From: "builds.sr.ht" <[email-masked]>
Date: Sun, 11 Dec 2022 15:18:33 +0000

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

[changes in windows zig toolchain wrapper][0] v2 from [Fabian Hahn][1]

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

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

---

From: Motiejus Jakštys <[email-masked]>
Date: Sun, 11 Dec 2022 18:31:56 +0200

> This is because the current main is broken on Windows otherwise.

Hi Fabian,

thank you, applied both.

Be assured that I will break Windows again. However, I will definitely
ask for your testing/feedback before I cut v1.0. Thanks for your
continued contributions.

Motiejus