feat: add os_arch_name() function to repo_utils and cleanup yq & jq toolchains for repository names follow this convention (#82)

This commit is contained in:
Greg Magolan 2022-04-21 17:45:33 -07:00 committed by GitHub
parent 0cd42cd03e
commit 207d219a88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 251 additions and 65 deletions

87
docs/repo_utils.md generated
View File

@ -2,6 +2,30 @@
Public API
<a id="#get_env_var"></a>
## get_env_var
<pre>
get_env_var(<a href="#get_env_var-rctx">rctx</a>, <a href="#get_env_var-name">name</a>, <a href="#get_env_var-default">default</a>)
</pre>
Find an environment variable in system. Doesn't %-escape the value!
**PARAMETERS**
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="get_env_var-rctx"></a>rctx | repository_ctx | none |
| <a id="get_env_var-name"></a>name | environment variable name | none |
| <a id="get_env_var-default"></a>default | default value to return if env var is not set in system | none |
**RETURNS**
The environment variable value or the default if it is not set
<a id="#is_darwin_os"></a>
## is_darwin_os
@ -56,6 +80,69 @@ Returns true if the host operating system is Windows
| <a id="is_windows_os-rctx"></a>rctx | <p align="center"> - </p> | none |
<a id="#os_arch_name"></a>
## os_arch_name
<pre>
os_arch_name(<a href="#os_arch_name-rctx">rctx</a>)
</pre>
Returns a normalized name of the host os and CPU architecture.
Alias archictures names are normalized:
x86_64 => amd64
aarch64 => arm64
The result can be used to generate repository names for host toolchain
repositories for toolchains that use these normalized names.
Common os & architecture pairs that are returned are,
- darwin_amd64
- darwin_arm64
- linux_amd64
- linux_arm64
- linux_s390x
- linux_ppc64le
- windows_amd64
**PARAMETERS**
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="os_arch_name-rctx"></a>rctx | repository_ctx | none |
**RETURNS**
The normalized "<os_name>_<arch>" string of the host os and CPU architecture.
<a id="#os_name"></a>
## os_name
<pre>
os_name(<a href="#os_name-rctx">rctx</a>)
</pre>
Returns the name of the host operating system
**PARAMETERS**
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="os_name-rctx"></a>rctx | repository_ctx | none |
**RETURNS**
The string "windows", "linux" or "darwin" that describes the host os
<a id="#patch"></a>
## patch

View File

@ -1,30 +1,23 @@
"Setup jq toolchain repositories and rules"
# Platform names follow the os_arch_name() convention in lib/private/repo_utils.bzl
JQ_PLATFORMS = {
"linux32": struct(
"darwin_amd64": struct(
release_platform = "osx-amd64",
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_32",
"@platforms//os:macos",
"@platforms//cpu:x86_64",
],
),
"linux64": struct(
"linux_amd64": struct(
release_platform = "linux64",
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
),
"osx-amd64": struct(
compatible_with = [
"@platforms//os:macos",
],
),
"win32": struct(
compatible_with = [
"@platforms//os:windows",
"@platforms//cpu:x86_32",
],
),
"win64": struct(
"windows_amd64": struct(
release_platform = "win64",
compatible_with = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
@ -38,17 +31,13 @@ JQ_PLATFORMS = {
# shasum -b -a 384 [downloaded file] | awk '{ print $1 }' | xxd -r -p | base64
JQ_VERSIONS = {
"1.6": {
"linux32": "sha384-hBGwNC3R0WyEbDQnrabzvcURSSV9BGxVrUVXLCH1C+Ilo7YDlzfTJSr4gadVssVI",
"linux64": "sha384-+K6tuwxrC/P4WBYRJ7YXcpeLS7GesbbnUhq4r9w7k0lCUC1KlhyXXf0sFQgOg0dI",
"osx-amd64": "sha384-ZLZljM9OyKCJbJbv7s1SRYSeMbVxfRc6kFNUlk9U/IL10Xm2xr4cxx3SZKv93QFO",
"win32": "sha384-PO+MMFELa0agwy35NuKhrxn8C6GjNq8gnzL3NvYSWNx/pwClCl7yzCONGhLFknMc",
"win64": "sha384-O4qdyhb+0zU1XAuUKc1Mil5hlbSbCUcPQOGRtkJUqryv7X0IeKcMCIuZw97q9WGr",
},
"1.5": {
"linux32": "sha-384MPO/DYgSPNRkrGEOCvZBZ8UvTdP4YVzXJoSYnWz9/IuywSRVqqyO6se9S72sue56",
"linux64": "sha384-/Su0ihtb867nCQTzQlTHjve+KpwfzsPws5ILj6hl7k33Qw+FwnyxAVITDh/pOOYw",
"osx-amd64": "sha384-X3VGwLkqaLafis82SySkqFPGIiJMdWdzcHPWLJ0q87XF+MGVc/e2n65a1yMBW6Nf",
"win32": "sha384-zZoz1F0nrhl5yvnGm37TxDw7dMWUQtJeDVmHfdAhLYMRGynIxefJgmB4Ty8gjNeu",
"win64": "sha384-NtaejeSFoKaXxxT1nPqxdOWRmIZAFF8wFTKjqs/4W0qYMYLohmO73AGKKR2XIg84",
},
}
@ -163,10 +152,13 @@ jq_toolchains_repo = repository_rule(
)
def _jq_platform_repo_impl(repository_ctx):
is_windows = repository_ctx.attr.platform == "win32" or repository_ctx.attr.platform == "win64"
is_windows = repository_ctx.attr.platform.startswith("windows_")
meta = JQ_PLATFORMS[repository_ctx.attr.platform]
release_platform = meta.release_platform if hasattr(meta, "release_platform") else repository_ctx.attr.platform
url = "https://github.com/stedolan/jq/releases/download/jq-{0}/jq-{1}{2}".format(
repository_ctx.attr.jq_version,
repository_ctx.attr.platform,
repository_ctx.attr.version,
release_platform,
".exe" if is_windows else "",
)
@ -174,7 +166,7 @@ def _jq_platform_repo_impl(repository_ctx):
url = url,
output = "jq.exe" if is_windows else "jq",
executable = True,
integrity = JQ_VERSIONS[repository_ctx.attr.jq_version][repository_ctx.attr.platform],
integrity = JQ_VERSIONS[repository_ctx.attr.version][release_platform],
)
build_content = """#Generated by lib/repositories.bzl
load("@aspect_bazel_lib//lib/private:jq_toolchain.bzl", "jq_toolchain")
@ -189,7 +181,7 @@ jq_platform_repo = repository_rule(
implementation = _jq_platform_repo_impl,
doc = "Fetch external tools needed for jq toolchain",
attrs = {
"jq_version": attr.string(mandatory = True, values = JQ_VERSIONS.keys()),
"version": attr.string(mandatory = True, values = JQ_VERSIONS.keys()),
"platform": attr.string(mandatory = True, values = JQ_PLATFORMS.keys()),
},
)

View File

@ -1,9 +1,5 @@
"""Utility functions for repository rules"""
def _is_windows_os(rctx):
"""Returns true if the host operating system is Windows"""
return rctx.os.name.lower().find("windows") != -1
def _is_darwin_os(rctx):
"""Returns true if the host operating system is Darwin"""
return rctx.os.name.lower().os_name.startswith("mac os")
@ -12,8 +8,96 @@ def _is_linux_os(rctx):
"""Returns true if the host operating system is Linux"""
return rctx.os.name.lower().startswith("linux")
def _is_windows_os(rctx):
"""Returns true if the host operating system is Windows"""
return rctx.os.name.lower().find("windows") != -1
def _os_name(rctx):
"""Returns the name of the host operating system
Args:
rctx: repository_ctx
Returns:
The string "windows", "linux" or "darwin" that describes the host os
"""
if _is_darwin_os(rctx):
return "darwin"
if _is_linux_os(rctx):
return "linux"
if _is_windows_os(rctx):
return "windows"
fail("unrecognized os")
def _get_env_var(rctx, name, default):
"""Find an environment variable in system. Doesn't %-escape the value!
Args:
rctx: repository_ctx
name: environment variable name
default: default value to return if env var is not set in system
Returns:
The environment variable value or the default if it is not set
"""
if name in rctx.os.environ:
return rctx.os.environ[name]
return default
def _os_arch_name(rctx):
"""Returns a normalized name of the host os and CPU architecture.
Alias archictures names are normalized:
x86_64 => amd64
aarch64 => arm64
The result can be used to generate repository names for host toolchain
repositories for toolchains that use these normalized names.
Common os & architecture pairs that are returned are,
- darwin_amd64
- darwin_arm64
- linux_amd64
- linux_arm64
- linux_s390x
- linux_ppc64le
- windows_amd64
Args:
rctx: repository_ctx
Returns:
The normalized "<os_name>_<arch>" string of the host os and CPU architecture.
"""
os_name = _os_name(rctx)
# NB: in bazel 5.1.1 rctx.os.arch was added which https://github.com/bazelbuild/bazel/commit/32d1606dac2fea730abe174c41870b7ee70ae041.
# Once we drop support for anything older than Bazel 5.1.1 than we can simplify
# this function.
if os_name == "windows":
proc_arch = (_get_env_var(rctx, "PROCESSOR_ARCHITECTURE", "", False) or
_get_env_var(rctx, "PROCESSOR_ARCHITEW6432", "", False))
if proc_arch == "ARM64":
arch = "arm64"
else:
arch = "amd64"
else:
arch = rctx.execute(["uname", "-m"]).stdout.strip()
arch_map = {
"x86_64": "amd64",
"aarch64": "arm64",
}
if arch in arch_map.keys():
arch = arch_map[arch]
return "%s_%s" % arch
repo_utils = struct(
is_windows_os = _is_windows_os,
is_darwin_os = _is_darwin_os,
is_linux_os = _is_linux_os,
is_windows_os = _is_windows_os,
get_env_var = _get_env_var,
os_name = _os_name,
os_arch_name = _os_arch_name,
)

View File

@ -130,6 +130,7 @@ _write_source_file_attrs = {
# and it goes into an infinite update, notify loop when running this target.
# See https://github.com/aspect-build/bazel-lib/pull/52 for more context.
"out_file": attr.string(mandatory = False),
# buildifier: disable=attr-cfg
"additional_update_targets": attr.label_list(cfg = "host", mandatory = False),
"is_windows": attr.bool(mandatory = True),
}

View File

@ -1,5 +1,6 @@
"Setup yq toolchain repositories and rules"
# Platform names follow the os_arch_name() convention in lib/private/repo_utils.bzl
YQ_PLATFORMS = {
"darwin_amd64": struct(
compatible_with = [
@ -13,22 +14,28 @@ YQ_PLATFORMS = {
"@platforms//cpu:aarch64",
],
),
"linux_386": struct(
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_32",
],
),
"linux_amd64": struct(
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
),
"windows_386": struct(
"linux_arm64": struct(
compatible_with = [
"@platforms//os:windows",
"@platforms//cpu:x86_32",
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
),
"linux_s390x": struct(
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:s390x",
],
),
"linux_ppc64le": struct(
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:ppc",
],
),
"windows_amd64": struct(
@ -42,23 +49,28 @@ YQ_PLATFORMS = {
# https://github.com/mikefarah/yq/releases
#
# The integrity hashes can be automatically fetched for the latest yq release by running
# tools/yq_mirror_release.sh. Alternatively, you can compute them manually by running
# shasum -b -a 384 [downloaded file] | awk '{ print $1 }' | xxd -r -p | base64
# `tools/yq_mirror_release.sh`. To calculate for a specific release run
# `tools/yq_mirror_release.sh <release_version>`
#
# Alternatively, you can compute them manually by running
# `shasum -b -a 384 [downloaded file] | awk '{ print $1 }' | xxd -r -p | base64`
YQ_VERSIONS = {
"v4.24.5": {
"4.24.5": {
"darwin_amd64": "sha384-Y6Utm9NAX7q69apRHLAU6oNYk5Kn5b6LUccBolbTm2CXXYye8pabeFPsaREFIHbw",
"darwin_arm64": "sha384-d6+hFiZrsUeqnXJufnvadTi0BL/sfbd6K7LnJyLVDy31C0isjyHipVqlibKYbFSu",
"linux_386": "sha384-skSDYmjm3uvi6xFKpzlIARzoiWaX0ml5CPAeLNxIybtRD3IBS1MSBoKkeWnS9n6h",
"linux_amd64": "sha384-FEWzb66XTTiMfz5wA/hCs/n0N+PVj4lXzKX8ZIUXnM3JTlFlBvA9X59elqqEJUPq",
"windows_386": "sha384-+BbsyeEO5BUN47u20qcwr0CGgVfo3Inj32BQsH6myca3C3hGqAE1nYVuy4JLBj+K",
"linux_arm64": "sha384-u8H3RxTssXKr1lEylydi1tzXKKsoax7aDXi4R/JF8irZ7RTwCqU/ogMj30B0Xo01",
"linux_s390x": "sha384-ccipOj8IBVDb6ZxBYDyRDVvfOTHRSD4nGuMbikrDrigGdYyI/iVb+R8lb6kdLarb",
"linux_ppc64le": "sha384-HWzKwuNx+uZI/8KXSNFVg+drCZiZU/17hIl8gG+b+UyLMAFZ/sOB/nu7yzEOdzvH",
"windows_amd64": "sha384-6T42wIkqXZ8OCetIeMjTlTIVQDwlRpTXj8pi+SrGzU4r5waq3SwIYSrDqUxMD43j",
},
"v4.24.4": {
"4.24.4": {
"darwin_amd64": "sha384-H5JnUD7c0jpbOvvN1pGz12XFi3XrX+ism4iGnH9wv37i+qdkD2AdTbTe4MIFtMR+",
"darwin_arm64": "sha384-9B85+dFTGRmMWWP2M+PVOkl8CtAb/HV4+XNGC0OBfdBvdJU85FyiTb12XGEgNjFp",
"linux_386": "sha384-TiesqbEG9ITqnOyFNMilVnciVM65dCAlRNYp/pK19jrqs2x5MhbpJ0a7Q9XwZmz8",
"linux_amd64": "sha384-y8vr5fWIqSvJhMoHwldoVPOJpAfLi4iHcnhfTcm/nuJAxGAJmI2MiBbk3t7lQNHC",
"windows_386": "sha384-YJTz4Y+5rcy6Ii/J44Qb6J2JZuzfh40WHGTc6jFTHFhJ47Ht+y9s4bS6h8WX6S0m",
"linux_arm64": "sha384-nxvFzxOVNtbt1lQZshkUnM6SHQnXKkzWKEw4TzU9HOms6mUJnYbYXc0x0LwPkpQK",
"linux_s390x": "sha384-525bIc8L80mIMVH+PmNDi4vBP4AfvBw/736ISW0F7+7zowSYOUK+EN/REo31kNdN",
"linux_ppc64le": "sha384-Sm3PniOqhRIlYaVBZOwncKRpPDLhiuHNCvVWUW9ihnAQM3woXvhb5iNfbws0Rz+G",
"windows_amd64": "sha384-f8jkaz3oRaDcn8jiXupeDO665t6d2tTnFuU0bKwLWszXSz8r29My/USG+UoO9hOr",
},
}
@ -173,12 +185,14 @@ yq_toolchains_repo = repository_rule(
)
def _yq_platform_repo_impl(repository_ctx):
is_windows = repository_ctx.attr.platform == "windows_386" or repository_ctx.attr.platform == "windows_amd64"
is_windows = repository_ctx.attr.platform.startswith("windows_")
meta = YQ_PLATFORMS[repository_ctx.attr.platform]
release_platform = meta.release_platform if hasattr(meta, "release_platform") else repository_ctx.attr.platform
#https://github.com/mikefarah/yq/releases/download/v4.24.4/yq_linux_386
url = "https://github.com/mikefarah/yq/releases/download/{0}/yq_{1}{2}".format(
repository_ctx.attr.yq_version,
repository_ctx.attr.platform,
url = "https://github.com/mikefarah/yq/releases/download/v{0}/yq_{1}{2}".format(
repository_ctx.attr.version,
release_platform,
".exe" if is_windows else "",
)
@ -186,7 +200,7 @@ def _yq_platform_repo_impl(repository_ctx):
url = url,
output = "yq.exe" if is_windows else "yq",
executable = True,
integrity = YQ_VERSIONS[repository_ctx.attr.yq_version][repository_ctx.attr.platform],
integrity = YQ_VERSIONS[repository_ctx.attr.version][release_platform],
)
build_content = """#Generated by @aspect_bazel_lib//lib/private/yq_toolchain.bzl
load("@aspect_bazel_lib//lib/private:yq_toolchain.bzl", "yq_toolchain")
@ -201,7 +215,7 @@ yq_platform_repo = repository_rule(
implementation = _yq_platform_repo_impl,
doc = "Fetch external tools needed for yq toolchain",
attrs = {
"yq_version": attr.string(mandatory = True, values = YQ_VERSIONS.keys()),
"version": attr.string(mandatory = True, values = YQ_VERSIONS.keys()),
"platform": attr.string(mandatory = True, values = YQ_PLATFORMS.keys()),
},
)

View File

@ -3,7 +3,10 @@
load("//lib/private:repo_utils.bzl", "repo_utils")
load("//lib/private:patch.bzl", _patch = "patch")
is_windows_os = repo_utils.is_windows_os
is_darwin_os = repo_utils.is_darwin_os
is_linux_os = repo_utils.is_linux_os
is_windows_os = repo_utils.is_windows_os
get_env_var = repo_utils.get_env_var
os_name = repo_utils.os_name
os_arch_name = repo_utils.os_arch_name
patch = _patch

View File

@ -24,11 +24,11 @@ def register_jq_toolchains(version, name = "jq"):
version: the version of jq to execute (see https://github.com/stedolan/jq/releases)
name: override the prefix for the generated toolchain repositories
"""
for platform in JQ_PLATFORMS.keys():
for [platform, meta] in JQ_PLATFORMS.items():
jq_platform_repo(
name = "%s_toolchains_%s" % (name, platform),
platform = platform,
jq_version = version,
version = version,
)
native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform))
@ -43,11 +43,11 @@ def register_yq_toolchains(version, name = "yq"):
version: the version of yq to execute (see https://github.com/mikefarah/yq/releases)
name: override the prefix for the generated toolchain repositories
"""
for platform in YQ_PLATFORMS.keys():
for [platform, meta] in YQ_PLATFORMS.items():
yq_platform_repo(
name = "%s_toolchains_%s" % (name, platform),
platform = platform,
yq_version = "v%s" % version,
version = version,
)
native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform))

19
tools/yq_mirror_release.sh Normal file → Executable file
View File

@ -1,23 +1,28 @@
#!/bin/bash/env bash
#!/usr/bin/env bash
# Produce a dictionary for the current yq release,
# suitable for adding to lib/private/yq_toolchain.bzl
set -o errexit
set -o errexit -o nounset -o pipefail
# Find the latest version
version=$(curl --silent "https://api.github.com/repos/mikefarah/yq/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ "${1:-}" ]; then
version=$1
else
version=$(curl --silent "https://api.github.com/repos/mikefarah/yq/releases/latest" | grep '"tag_name":' | sed -E 's/.*"v([^"]+)".*/\1/')
fi
# yq publishes its checksums and a script to extract them
curl --silent --location "https://github.com/mikefarah/yq/releases/download/$version/extract-checksum.sh" -o /tmp/extract-checksum.sh
curl --silent --location "https://github.com/mikefarah/yq/releases/download/$version/checksums_hashes_order" -o /tmp/checksums_hashes_order
curl --silent --location "https://github.com/mikefarah/yq/releases/download/$version/checksums" -o /tmp/checksums
curl --silent --location "https://github.com/mikefarah/yq/releases/download/v$version/extract-checksum.sh" -o /tmp/extract-checksum.sh
curl --silent --location "https://github.com/mikefarah/yq/releases/download/v$version/checksums_hashes_order" -o /tmp/checksums_hashes_order
curl --silent --location "https://github.com/mikefarah/yq/releases/download/v$version/checksums" -o /tmp/checksums
cd /tmp
chmod u+x extract-checksum.sh
# Extract the checksums and output a starlark map entry
echo "\"$version\": {"
for release in darwin_{amd,arm}64 linux_{386,amd64} windows_{386,amd64}; do
platforms=(darwin_{amd64,arm64} linux_{amd64,arm64,s390x,ppc64le} windows_amd64)
for release in ${platforms[@]}; do
artifact=$release
if [[ $release == windows* ]]; then
artifact="$release.exe"