From e861c5b4bedb752adbdca9bfb863e9cc131c2a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 29 Aug 2022 12:46:33 +0300 Subject: [PATCH] cache_prefix: clean up OS logic 'Windows' and 'Other' and guessing of cache prefix was convoluted --- toolchain/defs.bzl | 101 ++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 51 deletions(-) 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),