From 64be98b830a474e51d1fdc6eff612327d5d59679 Mon Sep 17 00:00:00 2001 From: laurynasl Date: Thu, 7 Apr 2022 10:15:34 +0000 Subject: [PATCH] Hide toolchain internals --- toolchain/defs.bzl | 10 ++++- toolchain/private/BUILD.sdk.bazel | 6 +++ toolchain/private/cc_toolchains.bzl | 68 +++++++++++++++++++++++++++++ toolchain/toolchain/BUILD | 7 +++ toolchain/toolchain/BUILD.sdk.bazel | 10 ----- toolchain/toolchain/defs.bzl | 68 ++--------------------------- 6 files changed, 92 insertions(+), 77 deletions(-) create mode 100644 toolchain/private/BUILD.sdk.bazel create mode 100644 toolchain/private/cc_toolchains.bzl delete mode 100644 toolchain/toolchain/BUILD.sdk.bazel diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl index 6d32056..ebcb65e 100644 --- a/toolchain/defs.bzl +++ b/toolchain/defs.bzl @@ -143,6 +143,7 @@ def _zig_repository_impl(repository_ctx): Label("//toolchain/platform:BUILD"), "platform/BUILD", ) + repository_ctx.template( "BUILD", Label("//toolchain:BUILD.sdk.bazel"), @@ -152,9 +153,14 @@ def _zig_repository_impl(repository_ctx): }, ) - repository_ctx.template( + repository_ctx.symlink( + Label("//toolchain/toolchain:BUILD"), "toolchain/BUILD", - Label("//toolchain/toolchain:BUILD.sdk.bazel"), + ) + + repository_ctx.template( + "private/BUILD", + Label("//toolchain/private:BUILD.sdk.bazel"), executable = False, substitutions = { "{absolute_path}": shell.quote(str(repository_ctx.path(""))), diff --git a/toolchain/private/BUILD.sdk.bazel b/toolchain/private/BUILD.sdk.bazel new file mode 100644 index 0000000..39a6a93 --- /dev/null +++ b/toolchain/private/BUILD.sdk.bazel @@ -0,0 +1,6 @@ +load("@bazel-zig-cc//toolchain/private:cc_toolchains.bzl", "declare_cc_toolchains") + +declare_cc_toolchains( + absolute_path = {absolute_path}, + zig_include_root = {zig_include_root}, +) diff --git a/toolchain/private/cc_toolchains.bzl b/toolchain/private/cc_toolchains.bzl new file mode 100644 index 0000000..64e99f1 --- /dev/null +++ b/toolchain/private/cc_toolchains.bzl @@ -0,0 +1,68 @@ +load(":defs.bzl", "DEFAULT_INCLUDE_DIRECTORIES", "ZIG_TOOL_PATH", "target_structs") +load("@bazel-zig-cc//toolchain:zig_toolchain.bzl", "zig_cc_toolchain_config") + +DEFAULT_TOOL_PATHS = { + "ar": "ar", + "gcc": "c++", # https://github.com/bazelbuild/bazel/issues/4644 + "cpp": "/usr/bin/false", + "gcov": "/usr/bin/false", + "nm": "/usr/bin/false", + "objdump": "/usr/bin/false", + "strip": "/usr/bin/false", +}.items() + +def declare_cc_toolchains(absolute_path, zig_include_root): + for target_config in target_structs(): + gotarget = target_config.gotarget + zigtarget = target_config.zigtarget + + cxx_builtin_include_directories = [] + for d in DEFAULT_INCLUDE_DIRECTORIES + target_config.includes: + d = zig_include_root + d + cxx_builtin_include_directories.append(absolute_path + "/" + d) + for d in getattr(target_config, "toplevel_include", []): + cxx_builtin_include_directories.append(absolute_path + "/" + d) + + absolute_tool_paths = {} + for name, path in target_config.tool_paths.items() + DEFAULT_TOOL_PATHS: + if path[0] == "/": + absolute_tool_paths[name] = path + continue + tool_path = ZIG_TOOL_PATH.format(zig_tool = path) + absolute_tool_paths[name] = "%s/%s" % (absolute_path, tool_path) + + linkopts = target_config.linkopts + copts = target_config.copts + for s in getattr(target_config, "linker_version_scripts", []): + linkopts = linkopts + ["-Wl,--version-script,%s/%s" % (absolute_path, s)] + for incl in getattr(target_config, "compiler_extra_includes", []): + copts = copts + ["-include", absolute_path + "/" + incl] + + zig_cc_toolchain_config( + name = zigtarget + "_cc_config", + target = zigtarget, + tool_paths = absolute_tool_paths, + cxx_builtin_include_directories = cxx_builtin_include_directories, + copts = copts, + linkopts = linkopts, + target_cpu = target_config.bazel_target_cpu, + target_system_name = "unknown", + target_libc = "unknown", + compiler = "clang", + abi_version = "unknown", + abi_libc_version = "unknown", + ) + + native.cc_toolchain( + name = zigtarget + "_cc", + toolchain_identifier = zigtarget + "-toolchain", + toolchain_config = ":%s_cc_config" % zigtarget, + all_files = "@zig_sdk//:zig", + ar_files = "@zig_sdk//:zig", + compiler_files = "@zig_sdk//:zig", + linker_files = "@zig_sdk//:zig", + dwp_files = "@zig_sdk//:empty", + objcopy_files = "@zig_sdk//:empty", + strip_files = "@zig_sdk//:empty", + supports_param_files = 0, + ) diff --git a/toolchain/toolchain/BUILD b/toolchain/toolchain/BUILD index e69de29..7192d86 100644 --- a/toolchain/toolchain/BUILD +++ b/toolchain/toolchain/BUILD @@ -0,0 +1,7 @@ +load("@bazel-zig-cc//toolchain/toolchain:defs.bzl", "declare_toolchains") + +package( + default_visibility = ["//visibility:public"], +) + +declare_toolchains() diff --git a/toolchain/toolchain/BUILD.sdk.bazel b/toolchain/toolchain/BUILD.sdk.bazel deleted file mode 100644 index 7cbca73..0000000 --- a/toolchain/toolchain/BUILD.sdk.bazel +++ /dev/null @@ -1,10 +0,0 @@ -load("@bazel-zig-cc//toolchain/toolchain:defs.bzl", "declare_toolchains") - -package( - default_visibility = ["//visibility:public"], -) - -declare_toolchains( - absolute_path = {absolute_path}, - zig_include_root = {zig_include_root}, -) diff --git a/toolchain/toolchain/defs.bzl b/toolchain/toolchain/defs.bzl index 1b0c087..2c9bf96 100644 --- a/toolchain/toolchain/defs.bzl +++ b/toolchain/toolchain/defs.bzl @@ -1,79 +1,17 @@ -load("@bazel-zig-cc//toolchain:zig_toolchain.bzl", "zig_cc_toolchain_config") load("@bazel-zig-cc//toolchain/private:defs.bzl", "DEFAULT_INCLUDE_DIRECTORIES", "ZIG_TOOL_PATH", "target_structs") -DEFAULT_TOOL_PATHS = { - "ar": "ar", - "gcc": "c++", # https://github.com/bazelbuild/bazel/issues/4644 - "cpp": "/usr/bin/false", - "gcov": "/usr/bin/false", - "nm": "/usr/bin/false", - "objdump": "/usr/bin/false", - "strip": "/usr/bin/false", -}.items() - -def declare_toolchains(absolute_path, zig_include_root): +def declare_toolchains(): for target_config in target_structs(): gotarget = target_config.gotarget zigtarget = target_config.zigtarget - cxx_builtin_include_directories = [] - for d in DEFAULT_INCLUDE_DIRECTORIES + target_config.includes: - d = zig_include_root + d - cxx_builtin_include_directories.append(absolute_path + "/" + d) - for d in getattr(target_config, "toplevel_include", []): - cxx_builtin_include_directories.append(absolute_path + "/" + d) - - absolute_tool_paths = {} - for name, path in target_config.tool_paths.items() + DEFAULT_TOOL_PATHS: - if path[0] == "/": - absolute_tool_paths[name] = path - continue - tool_path = ZIG_TOOL_PATH.format(zig_tool = path) - absolute_tool_paths[name] = "%s/%s" % (absolute_path, tool_path) - - linkopts = target_config.linkopts - copts = target_config.copts - for s in getattr(target_config, "linker_version_scripts", []): - linkopts = linkopts + ["-Wl,--version-script,%s/%s" % (absolute_path, s)] - for incl in getattr(target_config, "compiler_extra_includes", []): - copts = copts + ["-include", absolute_path + "/" + incl] - - zig_cc_toolchain_config( - name = zigtarget + "_cc_config", - target = zigtarget, - tool_paths = absolute_tool_paths, - cxx_builtin_include_directories = cxx_builtin_include_directories, - copts = copts, - linkopts = linkopts, - target_cpu = target_config.bazel_target_cpu, - target_system_name = "unknown", - target_libc = "unknown", - compiler = "clang", - abi_version = "unknown", - abi_libc_version = "unknown", - ) - - native.cc_toolchain( - name = zigtarget + "_cc", - toolchain_identifier = zigtarget + "-toolchain", - toolchain_config = ":%s_cc_config" % zigtarget, - all_files = "@zig_sdk//:zig", - ar_files = "@zig_sdk//:zig", - compiler_files = "@zig_sdk//:zig", - linker_files = "@zig_sdk//:zig", - dwp_files = "@zig_sdk//:empty", - objcopy_files = "@zig_sdk//:empty", - strip_files = "@zig_sdk//:empty", - supports_param_files = 0, - ) - # register two kinds of toolchain targets: Go and Zig conventions. # Go convention: amd64/arm64, linux/darwin native.toolchain( name = gotarget, exec_compatible_with = None, target_compatible_with = target_config.constraint_values, - toolchain = ":%s_cc" % zigtarget, + toolchain = "@zig_sdk//private:%s_cc" % zigtarget, toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", ) @@ -82,6 +20,6 @@ def declare_toolchains(absolute_path, zig_include_root): name = zigtarget, exec_compatible_with = None, target_compatible_with = target_config.constraint_values, - toolchain = ":%s_cc" % zigtarget, + toolchain = "@zig_sdk//private:%s_cc" % zigtarget, toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", )