feat: add @aspect_bazel_lib_host repository and normalize function names in repo_utils (#90)
This commit is contained in:
parent
626af72fe8
commit
41ce34470f
|
@ -88,4 +88,9 @@ stardoc_with_diff_test(
|
|||
bzl_library_target = "//lib:yq",
|
||||
)
|
||||
|
||||
stardoc_with_diff_test(
|
||||
name = "host_repo",
|
||||
bzl_library_target = "//lib:host_repo",
|
||||
)
|
||||
|
||||
update_docs()
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
<!-- Generated with Stardoc: http://skydoc.bazel.build -->
|
||||
|
||||
Public API
|
||||
|
||||
<a id="#host_repo"></a>
|
||||
|
||||
## host_repo
|
||||
|
||||
<pre>
|
||||
host_repo(<a href="#host_repo-name">name</a>, <a href="#host_repo-repo_mapping">repo_mapping</a>)
|
||||
</pre>
|
||||
|
||||
Exposes information about the host platform
|
||||
|
||||
**ATTRIBUTES**
|
||||
|
||||
|
||||
| Name | Description | Type | Mandatory | Default |
|
||||
| :------------- | :------------- | :------------- | :------------- | :------------- |
|
||||
| <a id="host_repo-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
|
||||
| <a id="host_repo-repo_mapping"></a>repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry <code>"@foo": "@bar"</code> declares that, for any time this repository depends on <code>@foo</code> (such as a dependency on <code>@foo//some:target</code>, it should actually resolve that dependency within globally-declared <code>@bar</code> (<code>@bar//some:target</code>). | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | required | |
|
||||
|
||||
|
|
@ -2,147 +2,6 @@
|
|||
|
||||
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
|
||||
|
||||
<pre>
|
||||
is_darwin_os(<a href="#is_darwin_os-rctx">rctx</a>)
|
||||
</pre>
|
||||
|
||||
Returns true if the host operating system is Darwin
|
||||
|
||||
**PARAMETERS**
|
||||
|
||||
|
||||
| Name | Description | Default Value |
|
||||
| :------------- | :------------- | :------------- |
|
||||
| <a id="is_darwin_os-rctx"></a>rctx | <p align="center"> - </p> | none |
|
||||
|
||||
|
||||
<a id="#is_linux_os"></a>
|
||||
|
||||
## is_linux_os
|
||||
|
||||
<pre>
|
||||
is_linux_os(<a href="#is_linux_os-rctx">rctx</a>)
|
||||
</pre>
|
||||
|
||||
Returns true if the host operating system is Linux
|
||||
|
||||
**PARAMETERS**
|
||||
|
||||
|
||||
| Name | Description | Default Value |
|
||||
| :------------- | :------------- | :------------- |
|
||||
| <a id="is_linux_os-rctx"></a>rctx | <p align="center"> - </p> | none |
|
||||
|
||||
|
||||
<a id="#is_windows_os"></a>
|
||||
|
||||
## is_windows_os
|
||||
|
||||
<pre>
|
||||
is_windows_os(<a href="#is_windows_os-rctx">rctx</a>)
|
||||
</pre>
|
||||
|
||||
Returns true if the host operating system is Windows
|
||||
|
||||
**PARAMETERS**
|
||||
|
||||
|
||||
| Name | Description | Default Value |
|
||||
| :------------- | :------------- | :------------- |
|
||||
| <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
|
||||
|
@ -174,3 +33,144 @@ then they are taken from `ctx.attr`.
|
|||
| <a id="patch-patch_directory"></a>patch_directory | Directory to apply the patches in | <code>None</code> |
|
||||
|
||||
|
||||
<a id="#repo_utils.is_darwin"></a>
|
||||
|
||||
## repo_utils.is_darwin
|
||||
|
||||
<pre>
|
||||
repo_utils.is_darwin(<a href="#repo_utils.is_darwin-rctx">rctx</a>)
|
||||
</pre>
|
||||
|
||||
Returns true if the host operating system is Darwin
|
||||
|
||||
**PARAMETERS**
|
||||
|
||||
|
||||
| Name | Description | Default Value |
|
||||
| :------------- | :------------- | :------------- |
|
||||
| <a id="repo_utils.is_darwin-rctx"></a>rctx | <p align="center"> - </p> | none |
|
||||
|
||||
|
||||
<a id="#repo_utils.is_linux"></a>
|
||||
|
||||
## repo_utils.is_linux
|
||||
|
||||
<pre>
|
||||
repo_utils.is_linux(<a href="#repo_utils.is_linux-rctx">rctx</a>)
|
||||
</pre>
|
||||
|
||||
Returns true if the host operating system is Linux
|
||||
|
||||
**PARAMETERS**
|
||||
|
||||
|
||||
| Name | Description | Default Value |
|
||||
| :------------- | :------------- | :------------- |
|
||||
| <a id="repo_utils.is_linux-rctx"></a>rctx | <p align="center"> - </p> | none |
|
||||
|
||||
|
||||
<a id="#repo_utils.is_windows"></a>
|
||||
|
||||
## repo_utils.is_windows
|
||||
|
||||
<pre>
|
||||
repo_utils.is_windows(<a href="#repo_utils.is_windows-rctx">rctx</a>)
|
||||
</pre>
|
||||
|
||||
Returns true if the host operating system is Windows
|
||||
|
||||
**PARAMETERS**
|
||||
|
||||
|
||||
| Name | Description | Default Value |
|
||||
| :------------- | :------------- | :------------- |
|
||||
| <a id="repo_utils.is_windows-rctx"></a>rctx | <p align="center"> - </p> | none |
|
||||
|
||||
|
||||
<a id="#repo_utils.get_env_var"></a>
|
||||
|
||||
## repo_utils.get_env_var
|
||||
|
||||
<pre>
|
||||
repo_utils.get_env_var(<a href="#repo_utils.get_env_var-rctx">rctx</a>, <a href="#repo_utils.get_env_var-name">name</a>, <a href="#repo_utils.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="repo_utils.get_env_var-rctx"></a>rctx | repository_ctx | none |
|
||||
| <a id="repo_utils.get_env_var-name"></a>name | environment variable name | none |
|
||||
| <a id="repo_utils.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="#repo_utils.os"></a>
|
||||
|
||||
## repo_utils.os
|
||||
|
||||
<pre>
|
||||
repo_utils.os(<a href="#repo_utils.os-rctx">rctx</a>)
|
||||
</pre>
|
||||
|
||||
Returns the name of the host operating system
|
||||
|
||||
**PARAMETERS**
|
||||
|
||||
|
||||
| Name | Description | Default Value |
|
||||
| :------------- | :------------- | :------------- |
|
||||
| <a id="repo_utils.os-rctx"></a>rctx | repository_ctx | none |
|
||||
|
||||
**RETURNS**
|
||||
|
||||
The string "windows", "linux" or "darwin" that describes the host os
|
||||
|
||||
|
||||
<a id="#repo_utils.platform"></a>
|
||||
|
||||
## repo_utils.platform
|
||||
|
||||
<pre>
|
||||
repo_utils.platform(<a href="#repo_utils.platform-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="repo_utils.platform-rctx"></a>rctx | repository_ctx | none |
|
||||
|
||||
**RETURNS**
|
||||
|
||||
The normalized "<os>_<arch>" string of the host os and CPU architecture.
|
||||
|
||||
|
||||
|
|
|
@ -156,3 +156,12 @@ bzl_library(
|
|||
visibility = ["//visibility:public"],
|
||||
deps = ["//lib/private:yq"],
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
name = "host_repo",
|
||||
srcs = ["host_repo.bzl"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//lib/private:host_repo",
|
||||
],
|
||||
)
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
"Public API"
|
||||
|
||||
load("//lib/private:host_repo.bzl", _host_repo = "host_repo")
|
||||
|
||||
host_repo = _host_repo
|
|
@ -157,3 +157,10 @@ bzl_library(
|
|||
srcs = ["yq.bzl"],
|
||||
visibility = ["//lib:__subpackages__"],
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
name = "host_repo",
|
||||
srcs = ["host_repo.bzl"],
|
||||
visibility = ["//lib:__subpackages__"],
|
||||
deps = [":repo_utils"],
|
||||
)
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
"A repository that exposes information about the host platform"
|
||||
|
||||
load(":repo_utils.bzl", "repo_utils")
|
||||
|
||||
def _impl(rctx):
|
||||
# Base BUILD file for this repository
|
||||
rctx.file("BUILD.bazel", """# Generated by @aspect_bazel_lib//lib/private:host_repo.bzl
|
||||
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
|
||||
bzl_library(
|
||||
name = "defs",
|
||||
srcs = ["defs.bzl"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
""")
|
||||
|
||||
# defs.bzl file for this repository
|
||||
rctx.file("defs.bzl", content = """# Generated by @aspect_bazel_lib//lib/private:host_repo.bzl
|
||||
# Information about the host platform
|
||||
host = struct(
|
||||
is_darwin = {is_darwin},
|
||||
is_linux = {is_linux},
|
||||
is_windows = {is_windows},
|
||||
os = "{os}",
|
||||
platform = "{platform}",
|
||||
)
|
||||
""".format(
|
||||
is_darwin = repo_utils.is_darwin(rctx),
|
||||
is_linux = repo_utils.is_linux(rctx),
|
||||
is_windows = repo_utils.is_windows(rctx),
|
||||
os = repo_utils.os(rctx),
|
||||
platform = repo_utils.platform(rctx),
|
||||
))
|
||||
|
||||
host_repo = repository_rule(
|
||||
implementation = _impl,
|
||||
doc = "Exposes information about the host platform",
|
||||
)
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
load(":repo_utils.bzl", "repo_utils")
|
||||
|
||||
# Platform names follow the os_arch_name() convention in lib/private/repo_utils.bzl
|
||||
# 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",
|
||||
|
@ -197,13 +197,11 @@ alias(name = "jq", actual = "@{name}_{platform}//:jq")
|
|||
exports_files(["index.bzl"])
|
||||
""".format(
|
||||
name = repository_ctx.name,
|
||||
platform = repo_utils.os_arch_name(repository_ctx),
|
||||
platform = repo_utils.platform(repository_ctx),
|
||||
))
|
||||
|
||||
# index.bzl file for this repository
|
||||
repository_ctx.file("index.bzl", content = """# Generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl
|
||||
load("@aspect_bazel_lib//lib:repo_utils.bzl", "is_windows_os")
|
||||
|
||||
host_platform="{host_platform}"
|
||||
|
||||
def bin():
|
||||
|
@ -211,8 +209,8 @@ def bin():
|
|||
return Label("@{name}_{host_platform}//:jq{maybe_windows}")
|
||||
""".format(
|
||||
name = repository_ctx.name,
|
||||
host_platform = repo_utils.os_arch_name(repository_ctx),
|
||||
maybe_windows = ".exe" if repo_utils.is_windows_os(repository_ctx) else "",
|
||||
host_platform = repo_utils.platform(repository_ctx),
|
||||
maybe_windows = ".exe" if repo_utils.is_windows(repository_ctx) else "",
|
||||
))
|
||||
|
||||
jq_host_alias_repo = repository_rule(
|
||||
|
|
|
@ -140,7 +140,7 @@ def patch(ctx, patches = None, patch_cmds = None, patch_cmds_win = None, patch_t
|
|||
fail("Error applying patch %s:\n%s%s" %
|
||||
(str(patchfile), st.stderr, st.stdout))
|
||||
|
||||
if repo_utils.is_windows_os(ctx) and patch_cmds_win:
|
||||
if repo_utils.is_windows(ctx) and patch_cmds_win:
|
||||
for cmd in patch_cmds_win:
|
||||
st = ctx.execute([powershell_exe, "/c", cmd], working_directory = patch_directory)
|
||||
if st.return_code:
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
"""Utility functions for repository rules"""
|
||||
|
||||
def _is_darwin_os(rctx):
|
||||
def _is_darwin(rctx):
|
||||
"""Returns true if the host operating system is Darwin"""
|
||||
return rctx.os.name.lower().startswith("mac os")
|
||||
|
||||
def _is_linux_os(rctx):
|
||||
def _is_linux(rctx):
|
||||
"""Returns true if the host operating system is Linux"""
|
||||
return rctx.os.name.lower().startswith("linux")
|
||||
|
||||
def _is_windows_os(rctx):
|
||||
def _is_windows(rctx):
|
||||
"""Returns true if the host operating system is Windows"""
|
||||
return rctx.os.name.lower().find("windows") != -1
|
||||
|
||||
def _os_name(rctx):
|
||||
def _os(rctx):
|
||||
"""Returns the name of the host operating system
|
||||
|
||||
Args:
|
||||
|
@ -21,11 +21,11 @@ def _os_name(rctx):
|
|||
Returns:
|
||||
The string "windows", "linux" or "darwin" that describes the host os
|
||||
"""
|
||||
if _is_darwin_os(rctx):
|
||||
if _is_darwin(rctx):
|
||||
return "darwin"
|
||||
if _is_linux_os(rctx):
|
||||
if _is_linux(rctx):
|
||||
return "linux"
|
||||
if _is_windows_os(rctx):
|
||||
if _is_windows(rctx):
|
||||
return "windows"
|
||||
fail("unrecognized os")
|
||||
|
||||
|
@ -44,7 +44,7 @@ def _get_env_var(rctx, name, default):
|
|||
return rctx.os.environ[name]
|
||||
return default
|
||||
|
||||
def _os_arch_name(rctx):
|
||||
def _platform(rctx):
|
||||
"""Returns a normalized name of the host os and CPU architecture.
|
||||
|
||||
Alias archictures names are normalized:
|
||||
|
@ -69,14 +69,14 @@ def _os_arch_name(rctx):
|
|||
rctx: repository_ctx
|
||||
|
||||
Returns:
|
||||
The normalized "<os_name>_<arch>" string of the host os and CPU architecture.
|
||||
The normalized "<os>_<arch>" string of the host os and CPU architecture.
|
||||
"""
|
||||
os_name = _os_name(rctx)
|
||||
os = _os(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":
|
||||
if os == "windows":
|
||||
proc_arch = (_get_env_var(rctx, "PROCESSOR_ARCHITECTURE", "", False) or
|
||||
_get_env_var(rctx, "PROCESSOR_ARCHITEW6432", "", False))
|
||||
if proc_arch == "ARM64":
|
||||
|
@ -91,13 +91,13 @@ def _os_arch_name(rctx):
|
|||
}
|
||||
if arch in arch_map.keys():
|
||||
arch = arch_map[arch]
|
||||
return "%s_%s" % (os_name, arch)
|
||||
return "%s_%s" % (os, arch)
|
||||
|
||||
repo_utils = struct(
|
||||
is_darwin_os = _is_darwin_os,
|
||||
is_linux_os = _is_linux_os,
|
||||
is_windows_os = _is_windows_os,
|
||||
is_darwin = _is_darwin,
|
||||
is_linux = _is_linux,
|
||||
is_windows = _is_windows,
|
||||
get_env_var = _get_env_var,
|
||||
os_name = _os_name,
|
||||
os_arch_name = _os_arch_name,
|
||||
os = _os,
|
||||
platform = _platform,
|
||||
)
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
load(":repo_utils.bzl", "repo_utils")
|
||||
|
||||
# Platform names follow the os_arch_name() convention in lib/private/repo_utils.bzl
|
||||
# Platform names follow the platform naming convention in @aspect_bazel_lib//:lib/private/repo_utils.bzl
|
||||
YQ_PLATFORMS = {
|
||||
"darwin_amd64": struct(
|
||||
compatible_with = [
|
||||
|
@ -231,12 +231,11 @@ alias(name = "yq", actual = "@{name}_{platform}//:yq")
|
|||
exports_files(["index.bzl"])
|
||||
""".format(
|
||||
name = repository_ctx.name,
|
||||
platform = repo_utils.os_arch_name(repository_ctx),
|
||||
platform = repo_utils.platform(repository_ctx),
|
||||
))
|
||||
|
||||
# index.bzl file for this repository
|
||||
repository_ctx.file("index.bzl", content = """# Generated by @aspect_bazel_lib//lib/private:yq_toolchain.bzl
|
||||
|
||||
host_platform="{host_platform}"
|
||||
|
||||
def bin():
|
||||
|
@ -244,8 +243,8 @@ def bin():
|
|||
return Label("@{name}_{host_platform}//:yq{maybe_windows}")
|
||||
""".format(
|
||||
name = repository_ctx.name,
|
||||
host_platform = repo_utils.os_arch_name(repository_ctx),
|
||||
maybe_windows = ".exe" if repo_utils.is_windows_os(repository_ctx) else "",
|
||||
host_platform = repo_utils.platform(repository_ctx),
|
||||
maybe_windows = ".exe" if repo_utils.is_windows(repository_ctx) else "",
|
||||
))
|
||||
|
||||
yq_host_alias_repo = repository_rule(
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
"Public API"
|
||||
|
||||
load("//lib/private:repo_utils.bzl", "repo_utils")
|
||||
load("//lib/private:repo_utils.bzl", _repo_utils = "repo_utils")
|
||||
load("//lib/private:patch.bzl", _patch = "patch")
|
||||
|
||||
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
|
||||
repo_utils = _repo_utils
|
||||
patch = _patch
|
||||
|
|
Loading…
Reference in New Issue