From 34f6aecd1966568135fd02c9f2bd60244a40ea5c Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Mon, 6 Feb 2023 16:32:33 +0100 Subject: [PATCH] Move resolved_toolchain into the main workspace (#48) --- mylang/BUILD.bazel | 9 +++++++++ mylang/private/BUILD.bazel | 6 ++++++ mylang/private/resolved_toolchain.bzl | 27 +++++++++++++++++++++++++ mylang/private/toolchains_repo.bzl | 29 --------------------------- 4 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 mylang/private/resolved_toolchain.bzl diff --git a/mylang/BUILD.bazel b/mylang/BUILD.bazel index 7aa3e74..17c9244 100644 --- a/mylang/BUILD.bazel +++ b/mylang/BUILD.bazel @@ -1,4 +1,5 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") +load("//mylang/private:resolved_toolchain.bzl", "resolved_toolchain") # For stardoc to reference the files exports_files(["defs.bzl"]) @@ -11,6 +12,14 @@ toolchain_type( visibility = ["//visibility:public"], ) +resolved_toolchain( + name = "resolved_toolchain", + # Marked manual so that `bazel test //...` passes + # even if no toolchain is registered. + tags = ["manual"], + visibility = ["//visibility:public"], +) + bzl_library( name = "repositories", srcs = ["repositories.bzl"], diff --git a/mylang/private/BUILD.bazel b/mylang/private/BUILD.bazel index e6421f5..eb2edb6 100644 --- a/mylang/private/BUILD.bazel +++ b/mylang/private/BUILD.bazel @@ -11,3 +11,9 @@ bzl_library( srcs = ["versions.bzl"], visibility = ["//mylang:__subpackages__"], ) + +bzl_library( + name = "resolved_toolchain", + srcs = ["resolved_toolchain.bzl"], + visibility = ["//mylang:__subpackages__"], +) diff --git a/mylang/private/resolved_toolchain.bzl b/mylang/private/resolved_toolchain.bzl new file mode 100644 index 0000000..29dc34e --- /dev/null +++ b/mylang/private/resolved_toolchain.bzl @@ -0,0 +1,27 @@ +"""This module implements an alias rule to the resolved toolchain. +""" + +DOC = """\ +Exposes a concrete toolchain which is the result of Bazel resolving the +toolchain for the execution or target platform. +Workaround for https://github.com/bazelbuild/bazel/issues/14009 +""" + +# Forward all the providers +def _resolved_toolchain_impl(ctx): + toolchain_info = ctx.toolchains["//mylang:toolchain_type"] + return [ + toolchain_info, + toolchain_info.default, + toolchain_info.mylanginfo, + toolchain_info.template_variables, + ] + +# Copied from java_toolchain_alias +# https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl +resolved_toolchain = rule( + implementation = _resolved_toolchain_impl, + toolchains = ["//mylang:toolchain_type"], + incompatible_use_toolchain_transition = True, + doc = DOC, +) diff --git a/mylang/private/toolchains_repo.bzl b/mylang/private/toolchains_repo.bzl index 200238f..ac26963 100644 --- a/mylang/private/toolchains_repo.bzl +++ b/mylang/private/toolchains_repo.bzl @@ -47,41 +47,12 @@ PLATFORMS = { } def _toolchains_repo_impl(repository_ctx): - # Expose a concrete toolchain which is the result of Bazel resolving the toolchain - # for the execution or target platform. - # Workaround for https://github.com/bazelbuild/bazel/issues/14009 - starlark_content = """# Generated by toolchains_repo.bzl - -# Forward all the providers -def _resolved_toolchain_impl(ctx): - toolchain_info = ctx.toolchains["@com_myorg_rules_mylang//mylang:toolchain_type"] - return [ - toolchain_info, - toolchain_info.default, - toolchain_info.mylanginfo, - toolchain_info.template_variables, - ] - -# Copied from java_toolchain_alias -# https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl -resolved_toolchain = rule( - implementation = _resolved_toolchain_impl, - toolchains = ["@com_myorg_rules_mylang//mylang:toolchain_type"], - incompatible_use_toolchain_transition = True, -) -""" - repository_ctx.file("defs.bzl", starlark_content) - build_content = """# Generated by toolchains_repo.bzl # # These can be registered in the workspace file or passed to --extra_toolchains flag. # By default all these toolchains are registered by the mylang_register_toolchains macro # so you don't normally need to interact with these targets. -load(":defs.bzl", "resolved_toolchain") - -resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"]) - """ for [platform, meta] in PLATFORMS.items():