Skip to content

Commit

Permalink
feat: support jq 1.7 toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide committed Sep 20, 2023
1 parent 5633834 commit 18092b4
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 32 deletions.
2 changes: 1 addition & 1 deletion docs/jq.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/repositories.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/jq.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def jq(name, srcs, filter = None, filter_file = None, args = [], out = None, **k
# This 'as' syntax results in $stamp being null in unstamped builds.
"$ARGS.named.STAMP as $stamp",
# Provide a default using the "alternative operator" in case $stamp is null.
".version = ($stamp.BUILD_EMBED_LABEL // \"<unstamped>\")",
".version = ($stamp[0].BUILD_EMBED_LABEL // \"<unstamped>\")",
]),
)
```
Expand Down
9 changes: 1 addition & 8 deletions lib/private/jq.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,7 @@ def _jq_impl(ctx):
)
inputs.append(stamp_json)

# jq says of --argfile:
# > Do not use. Use --slurpfile instead.
# > (This option is like --slurpfile, but when the file has just one text,
# > then that is used, else an array of texts is used as in --slurpfile.)
# However there's no indication that it's deprecated. Maybe it's a style convention.
# For our purposes, "$STAMP.BUILD_TIMESTAMP" looks a lot more sensible in a BUILD file
# than "$STAMP[0].BUILD_TIMESTAMP".
args = args + ["--argfile", "STAMP", stamp_json.path]
args = args + ["--slurpfile", "STAMP", stamp_json.path]

cmd = "{jq} {args} {filter} {sources} > {out}".format(
jq = jq_bin.path,
Expand Down
37 changes: 18 additions & 19 deletions lib/private/jq_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,33 @@ load(":repo_utils.bzl", "repo_utils")
# Platform names follow the platform naming convention in @aspect_bazel_lib//:lib/private/repo_utils.bzl
JQ_PLATFORMS = {
"darwin_amd64": struct(
release_platform = "osx-amd64",
release_platform = "macos-amd64",
compatible_with = [
"@platforms//os:macos",
"@platforms//cpu:x86_64",
],
),
"darwin_arm64": struct(
# JQ only ships a universal binary; it should work on
# Apple Silicon (aarch64) as well.
release_platform = "osx-amd64",
release_platform = "macos-arm64",
compatible_with = [
"@platforms//os:macos",
"@platforms//cpu:aarch64",
],
),
# There is currently no linux-arm64 JQ toolchain, as there is no upstream binary.
# The alternative is to either build JQ manually from source, or use YQ which has the same functionality as JQ,
# and bazel-lib provides an linux-arm64 toolchain for.
# See https://github.com/aspect-build/bazel-lib/issues/268
"linux_amd64": struct(
release_platform = "linux64",
release_platform = "linux-amd64",
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
),
"linux_arm64": struct(
release_platform = "linux-arm64",
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
),
"windows_amd64": struct(
release_platform = "win64",
compatible_with = [
Expand All @@ -40,22 +41,19 @@ JQ_PLATFORMS = {
),
}

DEFAULT_JQ_VERSION = "1.6"
DEFAULT_JQ_VERSION = "1.7"

# https://github.com/stedolan/jq/releases
#
# The integrity hashes can be computed with
# shasum -b -a 384 [downloaded file] | awk '{ print $1 }' | xxd -r -p | base64
JQ_VERSIONS = {
"1.6": {
"linux64": "sha384-+K6tuwxrC/P4WBYRJ7YXcpeLS7GesbbnUhq4r9w7k0lCUC1KlhyXXf0sFQgOg0dI",
"osx-amd64": "sha384-ZLZljM9OyKCJbJbv7s1SRYSeMbVxfRc6kFNUlk9U/IL10Xm2xr4cxx3SZKv93QFO",
"win64": "sha384-O4qdyhb+0zU1XAuUKc1Mil5hlbSbCUcPQOGRtkJUqryv7X0IeKcMCIuZw97q9WGr",
},
"1.5": {
"linux64": "sha384-/Su0ihtb867nCQTzQlTHjve+KpwfzsPws5ILj6hl7k33Qw+FwnyxAVITDh/pOOYw",
"osx-amd64": "sha384-X3VGwLkqaLafis82SySkqFPGIiJMdWdzcHPWLJ0q87XF+MGVc/e2n65a1yMBW6Nf",
"win64": "sha384-NtaejeSFoKaXxxT1nPqxdOWRmIZAFF8wFTKjqs/4W0qYMYLohmO73AGKKR2XIg84",
"1.7": {
"linux-amd64": "sha384-4wJ15NoxFf7r1Zf5YVGUeMPx/pfWlSfMJWLFcu4fUcBFe5L4BOpF/njEK8AH58od",
"linux-arm64": "sha384-y9BwX+RyXf2a16xwtvcjHFfIBp3K3Ukyg4GjtmxBtynD/BKNf+0tuLtZx64TTI+/",
"macos-amd64": "sha384-N0WdpiD8zl1k9888yGxWW/dHzztOTU+RTlZrzOYJMXXUUMqjnqXq8GwnHDsC9Lk3",
"macos-arm64": "sha384-0nnKlrEAU7NCzTM63XYkhAGGapA/IT2O2jkU+H+ZbQFu3E+XEbgw5E/+o0oHjLGf",
"win64": "sha384-2QfBgUpi1I5KPVrKtZnPcur+Wn/iE+tZVPFKXiIPoBKTpqZKhzc/CdqjcBn+IPiy",
},
}

Expand Down Expand Up @@ -168,6 +166,7 @@ jq_toolchains_repo = repository_rule(

def _jq_platform_repo_impl(rctx):
is_windows = rctx.attr.platform.startswith("windows_")

meta = JQ_PLATFORMS[rctx.attr.platform]
release_platform = meta.release_platform if hasattr(meta, "release_platform") else rctx.attr.platform

Expand Down
4 changes: 4 additions & 0 deletions lib/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def register_jq_toolchains(name = "jq", version = DEFAULT_JQ_VERSION, register =
register: whether to call through to native.register_toolchains.
Should be True for WORKSPACE users, but false when used under bzlmod extension
"""
(major, minor) = version.split(".")
if int(major) < 1 or int(major) == 1 and int(minor) < 7:
fail("Minimum jq version supported is 1.7")

for [platform, meta] in JQ_PLATFORMS.items():
jq_platform_repo(
name = "%s_%s" % (name, platform),
Expand Down
4 changes: 2 additions & 2 deletions lib/tests/jq/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ diff_test(
# Don't directly reference $STAMP as it's only set when stamping
"$ARGS.named.STAMP as $stamp",
# Provide a default using the "alternative operator"
".foo = ($stamp.BUILD_EMBED_LABEL // \"<unstamped>\")",
".value = ($stamp.BUILD_TIMESTAMP // 1 | tonumber)",
".foo = ($stamp[0].BUILD_EMBED_LABEL // \"<unstamped>\")",
".value = ($stamp[0].BUILD_TIMESTAMP // 1 | tonumber)",
]),
stamp = stamp,
)
Expand Down

0 comments on commit 18092b4

Please sign in to comment.