Skip to content

Commit

Permalink
Use binary_toolchain to implement jq_toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
BoleynSu committed Jul 1, 2024
1 parent 7725409 commit 853bd53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 64 deletions.
4 changes: 4 additions & 0 deletions lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ toolchain_type(
name = "jq_toolchain_type",
)

toolchain_type(
name = "jq_runtime_toolchain_type",
)

toolchain_type(
name = "yq_toolchain_type",
)
Expand Down
78 changes: 14 additions & 64 deletions lib/private/jq_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,72 +57,15 @@ JQ_VERSIONS = {
},
}

JqInfo = provider(
doc = "Provide info for executing jq",
fields = {
"bin": "Executable jq binary",
},
)

def _jq_toolchain_impl(ctx):
binary = ctx.file.bin

# Make the $(JQ_BIN) variable available in places like genrules.
# See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables
template_variables = platform_common.TemplateVariableInfo({
"JQ_BIN": binary.path,
})
default_info = DefaultInfo(
files = depset([binary]),
runfiles = ctx.runfiles(files = [binary]),
)
jq_info = JqInfo(
bin = binary,
)

# Export all the providers inside our ToolchainInfo
# so the resolved_toolchain rule can grab and re-export them.
toolchain_info = platform_common.ToolchainInfo(
jqinfo = jq_info,
template_variables = template_variables,
default = default_info,
)

return [default_info, toolchain_info, template_variables]

jq_toolchain = rule(
implementation = _jq_toolchain_impl,
attrs = {
"bin": attr.label(
mandatory = True,
allow_single_file = True,
),
},
)

def _jq_toolchains_repo_impl(rctx):
# 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 @aspect_bazel_lib//lib/private:jq_toolchain.bzl
load("@aspect_bazel_lib//lib:binary_toolchain.bzl", "resolved_binary_rule")
# Forward all the providers
def _resolved_toolchain_impl(ctx):
toolchain_info = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"]
return [
toolchain_info,
toolchain_info.default,
toolchain_info.jqinfo,
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 = ["@aspect_bazel_lib//lib:jq_toolchain_type"],
incompatible_use_toolchain_transition = True,
)
resolved_toolchain = resolved_binary_rule(toolchain_type = "@aspect_bazel_lib//lib:jq_toolchain_type", template_variable = "JQ_BIN")
resolved_binary = resolved_binary_rule(toolchain_type = "@aspect_bazel_lib//lib:jq_runtime_toolchain_type", template_variable = "JQ_BIN")
"""
rctx.file("defs.bzl", starlark_content)

Expand All @@ -132,10 +75,10 @@ resolved_toolchain = rule(
# By default all these toolchains are registered by the jq_register_toolchains macro
# so you don't normally need to interact with these targets.
load(":defs.bzl", "resolved_toolchain")
load(":defs.bzl", "resolved_toolchain", "resolved_binary")
resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"])
resolved_binary(name = "resolved_binary", visibility = ["//visibility:public"])
"""

for [platform, meta] in JQ_PLATFORMS.items():
Expand All @@ -146,6 +89,12 @@ toolchain(
toolchain = "@{user_repository_name}_{platform}//:jq_toolchain",
toolchain_type = "@aspect_bazel_lib//lib:jq_toolchain_type",
)
toolchain(
name = "{platform}_runtime_toolchain",
target_compatible_with = {compatible_with},
toolchain = "@{user_repository_name}_{platform}//:jq_runtime_toolchain",
toolchain_type = "@aspect_bazel_lib//lib:jq_runtime_toolchain_type",
)
""".format(
platform = platform,
user_repository_name = rctx.attr.user_repository_name,
Expand Down Expand Up @@ -182,9 +131,10 @@ def _jq_platform_repo_impl(rctx):
integrity = JQ_VERSIONS[rctx.attr.version][release_platform],
)
build_content = """# @generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl
load("@aspect_bazel_lib//lib/private:jq_toolchain.bzl", "jq_toolchain")
load("@aspect_bazel_lib//lib:binary_toolchain.bzl", "binary_toolchain", "binary_runtime_toolchain")
exports_files(["{0}"])
jq_toolchain(name = "jq_toolchain", bin = "{0}", visibility = ["//visibility:public"])
binary_toolchain(name = "jq_toolchain", bin = "{0}", visibility = ["//visibility:public"])
binary_runtime_toolchain(name = "jq_runtime_toolchain", bin = "{0}", visibility = ["//visibility:public"])
""".format("jq.exe" if is_windows else "jq")

# Base BUILD file for this repository
Expand Down

0 comments on commit 853bd53

Please sign in to comment.