From 65a8d7c4bbb0573a98896fbf5e7b4f61aed5aea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Tue, 13 Dec 2022 14:38:51 +0200 Subject: [PATCH] toolchain files: reduce the number of files in the sandbox We already know which headers are necessary, so it's wasteful to put the full zig_sdk to every sandbox. This reduces the number of files in `external/zig_sdk` from 15k to 4k or 6k, depending on the action. --- toolchain/defs.bzl | 55 ++++++++++++++++++++++++++++- toolchain/private/cc_toolchains.bzl | 8 ++--- toolchain/private/defs.bzl | 13 +++++-- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl index 746156d..ba8ec07 100644 --- a/toolchain/defs.bzl +++ b/toolchain/defs.bzl @@ -319,11 +319,64 @@ def declare_files(os): else: native.exports_files(["zig"], visibility = ["//visibility:public"]) filegroup(name = "lib/std", srcs = native.glob(["lib/std/**"])) - lazy_filegroups = {} for target_config in target_structs(): + all_includes = [native.glob(["lib/{}/**".format(i)]) for i in target_config.includes] + all_includes.append(getattr(target_config, "compiler_extra_includes", [])) + + cxx_tool_label = ":" + zig_tool_path(os).format( + zig_tool = "c++", + zigtarget = target_config.zigtarget, + ) + + filegroup( + name = "{}_includes".format(target_config.zigtarget), + srcs = _flatten(all_includes), + ) + + filegroup( + name = "{}_compiler_files".format(target_config.zigtarget), + srcs = [ + ":zig", + ":{}_includes".format(target_config.zigtarget), + cxx_tool_label, + ], + ) + + filegroup( + name = "{}_linker_files".format(target_config.zigtarget), + srcs = [ + ":zig", + ":{}_includes".format(target_config.zigtarget), + cxx_tool_label, + ] + native.glob([ + "lib/libc/{}/**".format(target_config.libc), + "lib/libcxx/**", + "lib/libcxxabi/**", + "lib/libunwind/**", + "lib/compiler_rt/**", + "lib/std/**", + "lib/*.zig", + "lib/*.h", + ]), + ) + + filegroup( + name = "{}_all_files".format(target_config.zigtarget), + srcs = [ + ":{}_linker_files".format(target_config.zigtarget), + ":{}_compiler_files".format(target_config.zigtarget), + ], + ) + for d in _DEFAULT_INCLUDE_DIRECTORIES + target_config.includes: d = "lib/" + d if d not in lazy_filegroups: lazy_filegroups[d] = filegroup(name = d, srcs = native.glob([d + "/**"])) + +def _flatten(iterable): + result = [] + for element in iterable: + result += element + return result diff --git a/toolchain/private/cc_toolchains.bzl b/toolchain/private/cc_toolchains.bzl index fa5ec82..5b7b894 100644 --- a/toolchain/private/cc_toolchains.bzl +++ b/toolchain/private/cc_toolchains.bzl @@ -60,10 +60,10 @@ def declare_cc_toolchains(os, zig_sdk_path): name = zigtarget + "_cc", toolchain_identifier = zigtarget + "-toolchain", toolchain_config = ":%s_cc_config" % zigtarget, - all_files = "@zig_sdk//:all", - ar_files = "@zig_sdk//:all", - compiler_files = "@zig_sdk//:all", - linker_files = "@zig_sdk//:all", + all_files = "@zig_sdk//:%s_all_files" % zigtarget, + ar_files = "@zig_sdk//:zig", + compiler_files = "@zig_sdk//:%s_compiler_files" % zigtarget, + linker_files = "@zig_sdk//:%s_linker_files" % zigtarget, dwp_files = "@zig_sdk//:empty", objcopy_files = "@zig_sdk//:empty", strip_files = "@zig_sdk//:empty", diff --git a/toolchain/private/defs.bzl b/toolchain/private/defs.bzl index 25ee8e0..ea9931f 100644 --- a/toolchain/private/defs.bzl +++ b/toolchain/private/defs.bzl @@ -41,9 +41,7 @@ def target_structs(): return ret def _target_darwin(gocpu, zigcpu): - min_os = "10" - if zigcpu == "aarch64": - min_os = "11" + min_os = "11" return struct( gotarget = "darwin_{}".format(gocpu), zigtarget = "{}-macos-none".format(zigcpu), @@ -53,9 +51,11 @@ def _target_darwin(gocpu, zigcpu): "libc/include/{}-macos.{}-none".format(zigcpu, min_os), "libc/include/any-macos.{}-any".format(min_os), "libc/include/any-macos-any", + "include", ], dynamic_library_linkopts = ["-Wl,-undefined=dynamic_lookup"], copts = [], + libc = "darwin", bazel_target_cpu = "darwin", constraint_values = [ "@platforms//os:macos", @@ -69,11 +69,14 @@ def _target_windows(gocpu, zigcpu): gotarget = "windows_{}".format(gocpu), zigtarget = "{}-windows-gnu".format(zigcpu), includes = [ + "libc/mingw", "libunwind/include", "libc/include/any-windows-any", + "include", ], dynamic_library_linkopts = [], copts = [], + libc = "mingw", bazel_target_cpu = "x64_windows", constraint_values = [ "@platforms//os:windows", @@ -100,12 +103,14 @@ def _target_linux_gnu(gocpu, zigcpu, glibc_version): (["libc/include/x86-linux-any"] if zigcpu == "x86_64" else []) + (["libc/include/{}-linux-any".format(zigcpu)] if zigcpu != "x86_64" else []) + [ "libc/include/any-linux-any", + "include", ], toplevel_include = ["glibc-hacks"] if fcntl_hack else [], compiler_extra_includes = ["glibc-hacks/glibchack-fcntl.h"] if fcntl_hack else [], linker_version_scripts = ["glibc-hacks/fcntl.map"] if fcntl_hack else [], dynamic_library_linkopts = [], copts = [], + libc = "glibc", bazel_target_cpu = "k8", constraint_values = [ "@platforms//os:linux", @@ -127,9 +132,11 @@ def _target_linux_musl(gocpu, zigcpu): (["libc/include/x86-linux-any"] if zigcpu == "x86_64" else []) + (["libc/include/{}-linux-any".format(zigcpu)] if zigcpu != "x86_64" else []) + [ "libc/include/any-linux-any", + "include", ], dynamic_library_linkopts = [], copts = ["-D_LIBCPP_HAS_MUSL_LIBC", "-D_LIBCPP_HAS_THREAD_API_PTHREAD"], + libc = "musl", bazel_target_cpu = "k8", constraint_values = [ "@platforms//os:linux",