Skip to content

Commit

Permalink
Move resolved_toolchain into the main workspace (bazelbuild#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
aherrmann authored Feb 6, 2023
1 parent 5a3164d commit 34f6aec
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
9 changes: 9 additions & 0 deletions mylang/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"])
Expand All @@ -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"],
Expand Down
6 changes: 6 additions & 0 deletions mylang/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ bzl_library(
srcs = ["versions.bzl"],
visibility = ["//mylang:__subpackages__"],
)

bzl_library(
name = "resolved_toolchain",
srcs = ["resolved_toolchain.bzl"],
visibility = ["//mylang:__subpackages__"],
)
27 changes: 27 additions & 0 deletions mylang/private/resolved_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -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,
)
29 changes: 0 additions & 29 deletions mylang/private/toolchains_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 34f6aec

Please sign in to comment.