1

split all tools to per-codename subdirectories

Go ignores CFLAGS for some commands. That causes unnecessary build of
glibc shims (and libc++.a).

In the GCC days cross-compiler toolchains used to expose a "tool" per
architecture. Let's do the same here. Then Go cannot cheat with skipping
CFLAGS which we normally *always* expect.

The wrapper code is getting gnarly, I know. But it still fits in my head
somehow, so we can still keep adding to it.
This commit is contained in:
Motiejus Jakštys 2022-10-01 14:26:45 +03:00
parent e96f5bb59c
commit 9b510810cc
3 changed files with 33 additions and 22 deletions

View File

@ -102,7 +102,7 @@ set ZIG_LIB_DIR=external\\zig_sdk\\lib
:set_zig_lib_dir :set_zig_lib_dir
set ZIG_LOCAL_CACHE_DIR={cache_prefix}\\bazel-zig-cc set ZIG_LOCAL_CACHE_DIR={cache_prefix}\\bazel-zig-cc
set ZIG_GLOBAL_CACHE_DIR=%ZIG_LOCAL_CACHE_DIR% set ZIG_GLOBAL_CACHE_DIR=%ZIG_LOCAL_CACHE_DIR%
"{zig}" "{zig_tool}" %* "{zig}" "{zig_tool}" {maybe_target} %*
""" """
_ZIG_TOOL_WRAPPER_WINDOWS_CACHE_GUESS = """@echo off _ZIG_TOOL_WRAPPER_WINDOWS_CACHE_GUESS = """@echo off
@ -119,28 +119,29 @@ goto zig
set ZIG_LOCAL_CACHE_DIR=%TMP%\\bazel-zig-cc set ZIG_LOCAL_CACHE_DIR=%TMP%\\bazel-zig-cc
:zig :zig
set ZIG_GLOBAL_CACHE_DIR=%ZIG_LOCAL_CACHE_DIR% set ZIG_GLOBAL_CACHE_DIR=%ZIG_LOCAL_CACHE_DIR%
"{zig}" "{zig_tool}" %* "{zig}" "{zig_tool}" {maybe_target} %*
""" """
_ZIG_TOOL_WRAPPER_CACHE_KNOWN = """#!/bin/sh _ZIG_TOOL_WRAPPER_CACHE_KNOWN = """#!/bin/sh
set -e set -e
if [ -d external/zig_sdk/lib ]; then if [ -d external/zig_sdk/lib ]; then
export ZIG_LIB_DIR=external/zig_sdk/lib ZIG_LIB_DIR=external/zig_sdk/lib
else else
export ZIG_LIB_DIR="$(dirname "$0")/../lib" ZIG_LIB_DIR="$(dirname "$0")/../lib"
fi fi
export ZIG_LIB_DIR
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="{cache_prefix}/bazel-zig-cc" export ZIG_GLOBAL_CACHE_DIR="{cache_prefix}/bazel-zig-cc"
{common} {common}
exec "{zig}" "{zig_tool}" "$@" $maybe_o2 exec "{zig}" "{zig_tool}" {maybe_target} "$@" $maybe_o2
""" """
_ZIG_TOOL_WRAPPER_CACHE_GUESS = """#!/bin/sh _ZIG_TOOL_WRAPPER_CACHE_GUESS = """#!/bin/sh
set -e set -e
if [ -d external/zig_sdk/lib ]; then if [ -d external/zig_sdk/lib ]; then
export ZIG_LIB_DIR=external/zig_sdk/lib ZIG_LIB_DIR=external/zig_sdk/lib
else else
export ZIG_LIB_DIR="$(dirname "$0")/../lib" ZIG_LIB_DIR="$(dirname "$0")/../../lib"
fi fi
if [ -n "$TMPDIR" ]; then if [ -n "$TMPDIR" ]; then
_cache_prefix=$TMPDIR _cache_prefix=$TMPDIR
@ -153,10 +154,11 @@ elif [ -n "$HOME" ]; then
else else
_cache_prefix=/tmp _cache_prefix=/tmp
fi fi
export ZIG_LIB_DIR
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
{common} {common}
exec "{zig}" "{zig_tool}" "$@" $maybe_o2 exec "{zig}" "{zig_tool}" {maybe_target} "$@" $maybe_o2
""" """
# The abomination below adds "-O2" to Go's link-prober command. Saves around # The abomination below adds "-O2" to Go's link-prober command. Saves around
@ -173,12 +175,13 @@ while [ "$#" -gt 6 ]; do shift; done
eval set -- "$saved" eval set -- "$saved"
""" """
def _zig_tool_wrapper(zig_tool, zig, is_windows, cache_prefix): def _zig_tool_wrapper(zig_tool, zig, is_windows, cache_prefix, zigtarget):
kwargs = dict( kwargs = dict(
zig = str(zig).replace("/", "\\") + ".exe" if is_windows else zig, zig = str(zig).replace("/", "\\") + ".exe" if is_windows else zig,
zig_tool = zig_tool, zig_tool = zig_tool,
cache_prefix = cache_prefix, cache_prefix = cache_prefix,
common = "" if is_windows else _ZIG_TOOL_COMMON_UNIX, common = "" if is_windows else _ZIG_TOOL_COMMON_UNIX,
maybe_target = "-target {}".format(zigtarget) if zig_tool == "c++" else "",
) )
if is_windows: if is_windows:
@ -186,7 +189,7 @@ def _zig_tool_wrapper(zig_tool, zig, is_windows, cache_prefix):
return _ZIG_TOOL_WRAPPER_WINDOWS_CACHE_KNOWN.format(**kwargs) return _ZIG_TOOL_WRAPPER_WINDOWS_CACHE_KNOWN.format(**kwargs)
else: else:
return _ZIG_TOOL_WRAPPER_WINDOWS_CACHE_GUESS.format(**kwargs) return _ZIG_TOOL_WRAPPER_WINDOWS_CACHE_GUESS.format(**kwargs)
else: # linux. Keeping this comment for buildifier to shut up. else: # keep this comment to shut up buildifier.
if cache_prefix: if cache_prefix:
return _ZIG_TOOL_WRAPPER_CACHE_KNOWN.format(**kwargs) return _ZIG_TOOL_WRAPPER_CACHE_KNOWN.format(**kwargs)
else: else:
@ -256,17 +259,22 @@ def _zig_repository_impl(repository_ctx):
) )
for zig_tool in _ZIG_TOOLS: for zig_tool in _ZIG_TOOLS:
zig_tool_wrapper = _zig_tool_wrapper( for target_config in target_structs():
zig_tool, zig_tool_wrapper = _zig_tool_wrapper(
str(repository_ctx.path("zig")), zig_tool,
os == "windows", str(repository_ctx.path("zig")),
repository_ctx.os.environ.get("BAZEL_ZIG_CC_CACHE_PREFIX", ""), os == "windows",
) repository_ctx.os.environ.get("BAZEL_ZIG_CC_CACHE_PREFIX", ""),
zigtarget = target_config.zigtarget,
)
repository_ctx.file( repository_ctx.file(
zig_tool_path(os).format(zig_tool = zig_tool), zig_tool_path(os).format(
zig_tool_wrapper, zig_tool = zig_tool,
) zigtarget = target_config.zigtarget,
),
zig_tool_wrapper,
)
repository_ctx.file( repository_ctx.file(
"glibc-hacks/fcntl.map", "glibc-hacks/fcntl.map",

View File

@ -25,7 +25,10 @@ def declare_cc_toolchains(os, zig_sdk_path, zig_include_root):
if path[0] == "/": if path[0] == "/":
absolute_tool_paths[name] = path absolute_tool_paths[name] = path
continue continue
tool_path = zig_tool_path(os).format(zig_tool = path) tool_path = zig_tool_path(os).format(
zig_tool = path,
zigtarget = zigtarget,
)
absolute_tool_paths[name] = tool_path absolute_tool_paths[name] = tool_path
linkopts = target_config.linkopts linkopts = target_config.linkopts

View File

@ -1,4 +1,4 @@
_ZIG_TOOL_PATH = "tools/{zig_tool}" _ZIG_TOOL_PATH = "tools/{zigtarget}/{zig_tool}"
# Zig supports even older glibcs than defined below, but we have tested only # Zig supports even older glibcs than defined below, but we have tested only
# down to 2.17. # down to 2.17.