diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl index 53fd161..57e4fde 100644 --- a/toolchain/defs.bzl +++ b/toolchain/defs.bzl @@ -76,14 +76,41 @@ def toolchains( }, ) -ZIG_TOOL_WRAPPER_CACHE_KNOWN = """#!/usr/bin/env sh +_ZIG_TOOLS = [ + "c++", + "cc", + "ar", + "ld.lld", # ELF + "ld64.lld", # Mach-O + "lld-link", # COFF + "wasm-ld", # WebAssembly +] + +_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}" %* +""" + +_ZIG_TOOL_WRAPPER_CACHE_KNOWN = """#!/usr/bin/env sh _cache_prefix="{cache_prefix}" export ZIG_LOCAL_CACHE_DIR="$_cache_prefix/bazel-zig-cc" export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR exec "{zig}" "{zig_tool}" "$@" """ -ZIG_TOOL_WRAPPER_CACHE_GUESS = """#!/usr/bin/env sh +_ZIG_TOOL_WRAPPER_CACHE_GUESS = """#!/usr/bin/env sh set -e if [ -n "$TMPDIR" ]; then _cache_prefix=$TMPDIR @@ -101,32 +128,22 @@ export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR exec "{zig}" "{zig_tool}" "$@" """ -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}" %* -""" +def _zig_tool_wrapper(zig_tool, zig, is_windows, cache_prefix): + kwargs = dict( + zig = str(zig).replace("/", "\\") + ".exe" if is_windows else zig, + zig_tool = zig_tool, + cache_prefix = cache_prefix, + ) -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}" %* -""" - -_ZIG_TOOLS = [ - "c++", - "cc", - "ar", - "ld.lld", # ELF - "ld64.lld", # Mach-O - "lld-link", # COFF - "wasm-ld", # WebAssembly -] + if is_windows: + if cache_prefix: + return _ZIG_TOOL_WRAPPER_WINDOWS_CACHE_KNOWN.format(**kwargs) + else: + return _ZIG_TOOL_WRAPPER_WINDOWS_CACHE_GUESS.format(**kwargs) + elif cache_prefix: + return _ZIG_TOOL_WRAPPER_CACHE_KNOWN.format(**kwargs) + else: + return _ZIG_TOOL_WRAPPER_CACHE_GUESS.format(**kwargs) def _quote(s): return "'" + s.replace("'", "'\\''") + "'" @@ -163,30 +180,12 @@ 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": - 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")), - zig_tool = zig_tool, - cache_prefix = cache_prefix, - ) - else: - zig_tool_wrapper = ZIG_TOOL_WRAPPER_CACHE_GUESS.format( - zig = str(repository_ctx.path("zig")), - zig_tool = zig_tool, - ) + 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", ""), + ) repository_ctx.file( zig_tool_path(os).format(zig_tool = zig_tool),