Skip to content

Commit 6fcde2f

Browse files
committed
Use binary_toolchain to implement jq_toolchain/jq
1 parent 7725409 commit 6fcde2f

File tree

4 files changed

+33
-65
lines changed

4 files changed

+33
-65
lines changed

lib/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ toolchain_type(
4040
name = "jq_toolchain_type",
4141
)
4242

43+
toolchain_type(
44+
name = "jq_runtime_toolchain_type",
45+
)
46+
4347
toolchain_type(
4448
name = "yq_toolchain_type",
4549
)

lib/private/jq.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def _expand_locations(ctx, s):
3030
return expand_locations(ctx, s, targets = ctx.attr.data).split(" ")
3131

3232
def _jq_impl(ctx):
33-
jq_bin = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"].jqinfo.bin
33+
jq_bin = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"].binary_info.bin[DefaultInfo].files_to_run.executable
3434

3535
out = ctx.outputs.out or ctx.actions.declare_file(ctx.attr.name + ".json")
3636
if ctx.attr.expand_args:

lib/private/jq_toolchain.bzl

Lines changed: 14 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -57,72 +57,15 @@ JQ_VERSIONS = {
5757
},
5858
}
5959

60-
JqInfo = provider(
61-
doc = "Provide info for executing jq",
62-
fields = {
63-
"bin": "Executable jq binary",
64-
},
65-
)
66-
67-
def _jq_toolchain_impl(ctx):
68-
binary = ctx.file.bin
69-
70-
# Make the $(JQ_BIN) variable available in places like genrules.
71-
# See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables
72-
template_variables = platform_common.TemplateVariableInfo({
73-
"JQ_BIN": binary.path,
74-
})
75-
default_info = DefaultInfo(
76-
files = depset([binary]),
77-
runfiles = ctx.runfiles(files = [binary]),
78-
)
79-
jq_info = JqInfo(
80-
bin = binary,
81-
)
82-
83-
# Export all the providers inside our ToolchainInfo
84-
# so the resolved_toolchain rule can grab and re-export them.
85-
toolchain_info = platform_common.ToolchainInfo(
86-
jqinfo = jq_info,
87-
template_variables = template_variables,
88-
default = default_info,
89-
)
90-
91-
return [default_info, toolchain_info, template_variables]
92-
93-
jq_toolchain = rule(
94-
implementation = _jq_toolchain_impl,
95-
attrs = {
96-
"bin": attr.label(
97-
mandatory = True,
98-
allow_single_file = True,
99-
),
100-
},
101-
)
102-
10360
def _jq_toolchains_repo_impl(rctx):
10461
# Expose a concrete toolchain which is the result of Bazel resolving the toolchain
10562
# for the execution or target platform.
10663
# Workaround for https://github.com/bazelbuild/bazel/issues/14009
10764
starlark_content = """# @generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl
65+
load("@aspect_bazel_lib//lib:binary_toolchain.bzl", "resolved_binary_rule")
10866
109-
# Forward all the providers
110-
def _resolved_toolchain_impl(ctx):
111-
toolchain_info = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"]
112-
return [
113-
toolchain_info,
114-
toolchain_info.default,
115-
toolchain_info.jqinfo,
116-
toolchain_info.template_variables,
117-
]
118-
119-
# Copied from java_toolchain_alias
120-
# https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl
121-
resolved_toolchain = rule(
122-
implementation = _resolved_toolchain_impl,
123-
toolchains = ["@aspect_bazel_lib//lib:jq_toolchain_type"],
124-
incompatible_use_toolchain_transition = True,
125-
)
67+
resolved_toolchain = resolved_binary_rule(toolchain_type = "@aspect_bazel_lib//lib:jq_toolchain_type", template_variable = "JQ_BIN")
68+
resolved_binary = resolved_binary_rule(toolchain_type = "@aspect_bazel_lib//lib:jq_runtime_toolchain_type", template_variable = "JQ_BIN")
12669
"""
12770
rctx.file("defs.bzl", starlark_content)
12871

@@ -132,10 +75,10 @@ resolved_toolchain = rule(
13275
# By default all these toolchains are registered by the jq_register_toolchains macro
13376
# so you don't normally need to interact with these targets.
13477
135-
load(":defs.bzl", "resolved_toolchain")
78+
load(":defs.bzl", "resolved_toolchain", "resolved_binary")
13679
13780
resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"])
138-
81+
resolved_binary(name = "resolved_binary", visibility = ["//visibility:public"])
13982
"""
14083

14184
for [platform, meta] in JQ_PLATFORMS.items():
@@ -146,6 +89,12 @@ toolchain(
14689
toolchain = "@{user_repository_name}_{platform}//:jq_toolchain",
14790
toolchain_type = "@aspect_bazel_lib//lib:jq_toolchain_type",
14891
)
92+
toolchain(
93+
name = "{platform}_runtime_toolchain",
94+
target_compatible_with = {compatible_with},
95+
toolchain = "@{user_repository_name}_{platform}//:jq_runtime_toolchain",
96+
toolchain_type = "@aspect_bazel_lib//lib:jq_runtime_toolchain_type",
97+
)
14998
""".format(
15099
platform = platform,
151100
user_repository_name = rctx.attr.user_repository_name,
@@ -182,9 +131,10 @@ def _jq_platform_repo_impl(rctx):
182131
integrity = JQ_VERSIONS[rctx.attr.version][release_platform],
183132
)
184133
build_content = """# @generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl
185-
load("@aspect_bazel_lib//lib/private:jq_toolchain.bzl", "jq_toolchain")
134+
load("@aspect_bazel_lib//lib:binary_toolchain.bzl", "binary_toolchain", "binary_runtime_toolchain")
186135
exports_files(["{0}"])
187-
jq_toolchain(name = "jq_toolchain", bin = "{0}", visibility = ["//visibility:public"])
136+
binary_toolchain(name = "jq_toolchain", bin = "{0}", visibility = ["//visibility:public"])
137+
binary_runtime_toolchain(name = "jq_runtime_toolchain", bin = "{0}", visibility = ["//visibility:public"])
188138
""".format("jq.exe" if is_windows else "jq")
189139

190140
# Base BUILD file for this repository

lib/tests/jq/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ diff_test(
164164
file2 = "a_pretty.json",
165165
)
166166

167+
genrule(
168+
name = "case_genrule_tool",
169+
srcs = ["a.json"],
170+
outs = ["genrule_tool_output.json"],
171+
cmd = "$(execpath @jq_toolchains//:resolved_binary) '.' $(location a.json) > $@",
172+
tools = ["@jq_toolchains//:resolved_binary"],
173+
)
174+
175+
diff_test(
176+
name = "case_genrule_tool_test",
177+
file1 = "genrule_tool_output.json",
178+
file2 = "a_pretty.json",
179+
)
180+
167181
# Raw input (--raw-input / -R)
168182
jq(
169183
name = "case_raw_input",

0 commit comments

Comments
 (0)