1
Fork 0

cache_prefix: clean up OS logic

'Windows' and 'Other' and guessing of cache prefix was convoluted
This commit is contained in:
Motiejus Jakštys 2022-08-29 12:46:33 +03:00
parent a410141ca4
commit e861c5b4be
1 changed files with 50 additions and 51 deletions

View File

@ -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}" _cache_prefix="{cache_prefix}"
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}" "$@"
""" """
ZIG_TOOL_WRAPPER_CACHE_GUESS = """#!/usr/bin/env sh _ZIG_TOOL_WRAPPER_CACHE_GUESS = """#!/usr/bin/env sh
set -e set -e
if [ -n "$TMPDIR" ]; then if [ -n "$TMPDIR" ]; then
_cache_prefix=$TMPDIR _cache_prefix=$TMPDIR
@ -101,32 +128,22 @@ export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR
exec "{zig}" "{zig_tool}" "$@" exec "{zig}" "{zig_tool}" "$@"
""" """
ZIG_TOOL_WRAPPER_WINDOWS_CACHE_KNOWN = """@echo off def _zig_tool_wrapper(zig_tool, zig, is_windows, cache_prefix):
set ZIG_LOCAL_CACHE_DIR={cache_prefix}\\bazel-zig-cc kwargs = dict(
set ZIG_GLOBAL_CACHE_DIR=%ZIG_LOCAL_CACHE_DIR% zig = str(zig).replace("/", "\\") + ".exe" if is_windows else zig,
"{zig}" "{zig_tool}" %* zig_tool = zig_tool,
""" cache_prefix = cache_prefix,
)
ZIG_TOOL_WRAPPER_WINDOWS_CACHE_GUESS = """@echo off if is_windows:
if exist "%TMP%\\*" goto :usertmp if cache_prefix:
set ZIG_LOCAL_CACHE_DIR=C:\\Temp\\bazel-zig-cc return _ZIG_TOOL_WRAPPER_WINDOWS_CACHE_KNOWN.format(**kwargs)
goto zig else:
:usertmp return _ZIG_TOOL_WRAPPER_WINDOWS_CACHE_GUESS.format(**kwargs)
set ZIG_LOCAL_CACHE_DIR=%TMP%\\bazel-zig-cc elif cache_prefix:
:zig return _ZIG_TOOL_WRAPPER_CACHE_KNOWN.format(**kwargs)
set ZIG_GLOBAL_CACHE_DIR=%ZIG_LOCAL_CACHE_DIR% else:
"{zig}" "{zig_tool}" %* return _ZIG_TOOL_WRAPPER_CACHE_GUESS.format(**kwargs)
"""
_ZIG_TOOLS = [
"c++",
"cc",
"ar",
"ld.lld", # ELF
"ld64.lld", # Mach-O
"lld-link", # COFF
"wasm-ld", # WebAssembly
]
def _quote(s): def _quote(s):
return "'" + s.replace("'", "'\\''") + "'" return "'" + s.replace("'", "'\\''") + "'"
@ -163,30 +180,12 @@ def _zig_repository_impl(repository_ctx):
) )
for zig_tool in _ZIG_TOOLS: for zig_tool in _ZIG_TOOLS:
cache_prefix = repository_ctx.os.environ.get("BAZEL_ZIG_CC_CACHE_PREFIX", "") zig_tool_wrapper = _zig_tool_wrapper(
if os == "windows": zig_tool,
if cache_prefix: str(repository_ctx.path("zig")),
zig_tool_wrapper = ZIG_TOOL_WRAPPER_WINDOWS_CACHE_KNOWN.format( os == "windows",
zig = str(repository_ctx.path("zig")).replace("/", "\\") + ".exe", repository_ctx.os.environ.get("BAZEL_ZIG_CC_CACHE_PREFIX", ""),
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,
)
repository_ctx.file( repository_ctx.file(
zig_tool_path(os).format(zig_tool = zig_tool), zig_tool_path(os).format(zig_tool = zig_tool),