1
Fork 0

use a specific template if cache prefix is known

This commit is contained in:
Motiejus Jakštys 2022-08-26 10:55:29 +03:00
parent 0612c6ed9c
commit 4d664146a0
1 changed files with 23 additions and 14 deletions

View File

@ -76,15 +76,19 @@ def toolchains(
}, },
) )
ZIG_TOOL_WRAPPER = """#!/usr/bin/env bash ZIG_TOOL_WRAPPER_CACHE_KNOWN = """#!/usr/bin/env sh
set -e
if [[ -n "{cache_prefix}" ]]; then
_cache_prefix="{cache_prefix}" _cache_prefix="{cache_prefix}"
elif [[ -n "$TMPDIR" ]]; then 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
set -e
if [ -n "$TMPDIR" ]; then
_cache_prefix=$TMPDIR _cache_prefix=$TMPDIR
elif [[ -n "$HOME" ]]; then elif [ -n "$HOME" ]; then
if [[ "$(uname)" = Darwin ]]; then if [ "$(uname)" = Darwin ]; then
_cache_prefix="$HOME/Library/Caches" _cache_prefix="$HOME/Library/Caches"
else else
_cache_prefix="$HOME/.cache" _cache_prefix="$HOME/.cache"
@ -92,10 +96,8 @@ elif [[ -n "$HOME" ]]; then
else else
_cache_prefix=/tmp _cache_prefix=/tmp
fi fi
export ZIG_LOCAL_CACHE_DIR="$_cache_prefix/bazel-zig-cc" export ZIG_LOCAL_CACHE_DIR="$_cache_prefix/bazel-zig-cc"
export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR
exec "{zig}" "{zig_tool}" "$@" exec "{zig}" "{zig_tool}" "$@"
""" """
@ -148,16 +150,23 @@ def _zig_repository_impl(repository_ctx):
) )
for zig_tool in _ZIG_TOOLS: for zig_tool in _ZIG_TOOLS:
zig_tool_wrapper = ZIG_TOOL_WRAPPER.format( cache_prefix = repository_ctx.os.environ.get("BAZEL_ZIG_CC_CACHE_PREFIX", "")
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": if os == "windows":
zig_tool_wrapper = ZIG_TOOL_WRAPPER_WINDOWS.format( zig_tool_wrapper = ZIG_TOOL_WRAPPER_WINDOWS.format(
zig = str(repository_ctx.path("zig")).replace("/", "\\") + ".exe", zig = str(repository_ctx.path("zig")).replace("/", "\\") + ".exe",
zig_tool = zig_tool, 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,
)
repository_ctx.file( repository_ctx.file(
zig_tool_path(os).format(zig_tool = zig_tool), zig_tool_path(os).format(zig_tool = zig_tool),