Use binary_toolchain to implement jq_toolchain/jq

This commit is contained in:
Boleyn Su 2024-07-01 16:11:46 +08:00
parent 7725409b8a
commit 6fcde2fc81
No known key found for this signature in database
GPG Key ID: 4CC5C09DA14E2E7D
4 changed files with 33 additions and 65 deletions

View File

@ -40,6 +40,10 @@ toolchain_type(
name = "jq_toolchain_type", name = "jq_toolchain_type",
) )
toolchain_type(
name = "jq_runtime_toolchain_type",
)
toolchain_type( toolchain_type(
name = "yq_toolchain_type", name = "yq_toolchain_type",
) )

View File

@ -30,7 +30,7 @@ def _expand_locations(ctx, s):
return expand_locations(ctx, s, targets = ctx.attr.data).split(" ") return expand_locations(ctx, s, targets = ctx.attr.data).split(" ")
def _jq_impl(ctx): def _jq_impl(ctx):
jq_bin = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"].jqinfo.bin jq_bin = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"].binary_info.bin[DefaultInfo].files_to_run.executable
out = ctx.outputs.out or ctx.actions.declare_file(ctx.attr.name + ".json") out = ctx.outputs.out or ctx.actions.declare_file(ctx.attr.name + ".json")
if ctx.attr.expand_args: if ctx.attr.expand_args:

View File

@ -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): def _jq_toolchains_repo_impl(rctx):
# Expose a concrete toolchain which is the result of Bazel resolving the toolchain # Expose a concrete toolchain which is the result of Bazel resolving the toolchain
# for the execution or target platform. # for the execution or target platform.
# Workaround for https://github.com/bazelbuild/bazel/issues/14009 # Workaround for https://github.com/bazelbuild/bazel/issues/14009
starlark_content = """# @generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl 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 resolved_toolchain = resolved_binary_rule(toolchain_type = "@aspect_bazel_lib//lib:jq_toolchain_type", template_variable = "JQ_BIN")
def _resolved_toolchain_impl(ctx): resolved_binary = resolved_binary_rule(toolchain_type = "@aspect_bazel_lib//lib:jq_runtime_toolchain_type", template_variable = "JQ_BIN")
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,
)
""" """
rctx.file("defs.bzl", starlark_content) rctx.file("defs.bzl", starlark_content)
@ -132,10 +75,10 @@ resolved_toolchain = rule(
# By default all these toolchains are registered by the jq_register_toolchains macro # By default all these toolchains are registered by the jq_register_toolchains macro
# so you don't normally need to interact with these targets. # 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_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"])
resolved_binary(name = "resolved_binary", visibility = ["//visibility:public"])
""" """
for [platform, meta] in JQ_PLATFORMS.items(): for [platform, meta] in JQ_PLATFORMS.items():
@ -146,6 +89,12 @@ toolchain(
toolchain = "@{user_repository_name}_{platform}//:jq_toolchain", toolchain = "@{user_repository_name}_{platform}//:jq_toolchain",
toolchain_type = "@aspect_bazel_lib//lib:jq_toolchain_type", 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( """.format(
platform = platform, platform = platform,
user_repository_name = rctx.attr.user_repository_name, user_repository_name = rctx.attr.user_repository_name,
@ -182,9 +131,10 @@ def _jq_platform_repo_impl(rctx):
integrity = JQ_VERSIONS[rctx.attr.version][release_platform], integrity = JQ_VERSIONS[rctx.attr.version][release_platform],
) )
build_content = """# @generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl 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}"]) 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") """.format("jq.exe" if is_windows else "jq")
# Base BUILD file for this repository # Base BUILD file for this repository

View File

@ -164,6 +164,20 @@ diff_test(
file2 = "a_pretty.json", file2 = "a_pretty.json",
) )
genrule(
name = "case_genrule_tool",
srcs = ["a.json"],
outs = ["genrule_tool_output.json"],
cmd = "$(execpath @jq_toolchains//:resolved_binary) '.' $(location a.json) > $@",
tools = ["@jq_toolchains//:resolved_binary"],
)
diff_test(
name = "case_genrule_tool_test",
file1 = "genrule_tool_output.json",
file2 = "a_pretty.json",
)
# Raw input (--raw-input / -R) # Raw input (--raw-input / -R)
jq( jq(
name = "case_raw_input", name = "case_raw_input",