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

From: ~motiejus <[email-masked]>
Date: Sun, 11 Dec 2022 04:56:01 +0000

I am doing a change in Windows wrapper, which I have no means to test
nor know is correct. Fabian, please test/ack.

`--experimental_use_hermetic_linux_sandbox` option is interesting,
because instead of symlinks in the sandbox it creates hardlinks. Which
has a chance to fix https://github.com/ziglang/zig/issues/13619 ;
Bazel's acceptance test in https://git.sr.ht/~motiejus/test-zigcc.

Hermetic sandbox also means one thing: all outside-sandbox directories
need to be explicitly allowlisted with `sandbox_mount_pair`. Since the
toolchain wrapper uses /bin/sh, it brings a bunch of stuff with it:
/bin, /lib, /lib64. If my experiments with
`experimental_use_hermetic_linux_sandbox` will fix the performance
issue, the zig wrapper rewrite to zig will happen sooner than later (and
I may have an interesting bootstrap problem to solve).

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

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

-- 
2.34.5

---

From: ~motiejus <[email-masked]>
Date: Sun, 11 Dec 2022 06:43:28 +0200

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 3d08abd..752a663 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
 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
 :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
 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
 :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.34.5

---

From: "builds.sr.ht" <[email-masked]>
Date: Sun, 11 Dec 2022 05:14:13 +0000

bazel-zig-cc/patches/.build.yml: FAILED in 18m9s

[changes in windows zig toolchain wrapper][0] from [~motiejus][1]

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

✗ #902752 FAILED bazel-zig-cc/patches/.build.yml https://builds.sr.ht/~motiejus/job/902752