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 7, 2023
1 parent d2203e9 commit 8befbba
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 42 deletions.
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.

131 changes: 92 additions & 39 deletions lib/private/jq_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,101 @@
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",
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",
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",
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
),
"windows_amd64": struct(
release_platform = "win64",
compatible_with = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
),
_JQ_PLATFORMS = {
"<1.7": {
"darwin_amd64": struct(
release_platform = "osx-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",
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",
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
),
"windows_amd64": struct(
release_platform = "win64",
compatible_with = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
),
},
">=1.7": {
"darwin_amd64": struct(
release_platform = "macos-amd64",
compatible_with = [
"@platforms//os:macos",
"@platforms//cpu:x86_64",
],
),
"darwin_arm64": struct(
release_platform = "macos-arm64",
compatible_with = [
"@platforms//os:macos",
"@platforms//cpu:aarch64",
],
),
"linux_amd64": struct(
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 = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
),
},
}

DEFAULT_JQ_VERSION = "1.6"
def jq_platforms_for_version(version):
(major, minor) = version.split(".")
pre_1_7 = int(major) < 1 or int(major) == 1 and int(minor) < 7
return _JQ_PLATFORMS["<1.7"] if pre_1_7 else _JQ_PLATFORMS[">=1.7"]

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.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",
},
"1.6": {
"linux64": "sha384-+K6tuwxrC/P4WBYRJ7YXcpeLS7GesbbnUhq4r9w7k0lCUC1KlhyXXf0sFQgOg0dI",
"osx-amd64": "sha384-ZLZljM9OyKCJbJbv7s1SRYSeMbVxfRc6kFNUlk9U/IL10Xm2xr4cxx3SZKv93QFO",
Expand Down Expand Up @@ -140,7 +191,7 @@ resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:publ
"""

for [platform, meta] in JQ_PLATFORMS.items():
for [platform, meta] in jq_platforms_for_version(rctx.attr.version).items():
build_content += """
toolchain(
name = "{platform}_toolchain",
Expand All @@ -163,12 +214,14 @@ jq_toolchains_repo = repository_rule(
which can be registered or selected.""",
attrs = {
"user_repository_name": attr.string(doc = "Base name for toolchains repository"),
"version": attr.string(mandatory = True, values = JQ_VERSIONS.keys()),
},
)

def _jq_platform_repo_impl(rctx):
is_windows = rctx.attr.platform.startswith("windows_")
meta = JQ_PLATFORMS[rctx.attr.platform]

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

url = "https://github.com/stedolan/jq/releases/download/jq-{0}/jq-{1}{2}".format(
Expand Down Expand Up @@ -197,7 +250,7 @@ jq_platform_repo = repository_rule(
doc = "Fetch external tools needed for jq toolchain",
attrs = {
"version": attr.string(mandatory = True, values = JQ_VERSIONS.keys()),
"platform": attr.string(mandatory = True, values = JQ_PLATFORMS.keys()),
"platform": attr.string(mandatory = True, values = _JQ_PLATFORMS["<1.7"].keys() + _JQ_PLATFORMS[">=1.7"].keys()),
},
)

Expand Down
5 changes: 3 additions & 2 deletions lib/repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"Macros for loading dependencies and registering toolchains"

load("//lib/private:jq_toolchain.bzl", "JQ_PLATFORMS", "jq_host_alias_repo", "jq_platform_repo", "jq_toolchains_repo", _DEFAULT_JQ_VERSION = "DEFAULT_JQ_VERSION")
load("//lib/private:jq_toolchain.bzl", "jq_host_alias_repo", "jq_platform_repo", "jq_platforms_for_version", "jq_toolchains_repo", _DEFAULT_JQ_VERSION = "DEFAULT_JQ_VERSION")
load("//lib/private:yq_toolchain.bzl", "YQ_PLATFORMS", "yq_host_alias_repo", "yq_platform_repo", "yq_toolchains_repo", _DEFAULT_YQ_VERSION = "DEFAULT_YQ_VERSION")
load("//lib/private:copy_to_directory_toolchain.bzl", "COPY_TO_DIRECTORY_PLATFORMS", "copy_to_directory_platform_repo", "copy_to_directory_toolchains_repo")
load("//lib/private:coreutils_toolchain.bzl", "COREUTILS_PLATFORMS", "coreutils_platform_repo", "coreutils_toolchains_repo", _DEFAULT_COREUTILS_VERSION = "DEFAULT_COREUTILS_VERSION")
Expand Down Expand Up @@ -60,7 +60,7 @@ 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
"""
for [platform, meta] in JQ_PLATFORMS.items():
for [platform, meta] in jq_platforms_for_version(version).items():
jq_platform_repo(
name = "%s_%s" % (name, platform),
platform = platform,
Expand All @@ -74,6 +74,7 @@ def register_jq_toolchains(name = "jq", version = DEFAULT_JQ_VERSION, register =
jq_toolchains_repo(
name = "%s_toolchains" % name,
user_repository_name = name,
version = version,
)

def register_yq_toolchains(name = "yq", version = DEFAULT_YQ_VERSION, register = True):
Expand Down

0 comments on commit 8befbba

Please sign in to comment.