2022-04-12 01:08:30 +00:00
|
|
|
"""Utility functions for repository rules"""
|
|
|
|
|
2022-04-28 21:18:06 +00:00
|
|
|
def _is_darwin(rctx):
|
2022-04-12 01:08:30 +00:00
|
|
|
"""Returns true if the host operating system is Darwin"""
|
2022-04-22 01:35:10 +00:00
|
|
|
return rctx.os.name.lower().startswith("mac os")
|
2022-04-12 01:08:30 +00:00
|
|
|
|
2022-04-28 21:18:06 +00:00
|
|
|
def _is_linux(rctx):
|
2022-04-12 01:08:30 +00:00
|
|
|
"""Returns true if the host operating system is Linux"""
|
|
|
|
return rctx.os.name.lower().startswith("linux")
|
|
|
|
|
2022-04-28 21:18:06 +00:00
|
|
|
def _is_windows(rctx):
|
2022-04-22 00:45:33 +00:00
|
|
|
"""Returns true if the host operating system is Windows"""
|
|
|
|
return rctx.os.name.lower().find("windows") != -1
|
|
|
|
|
2022-04-28 21:18:06 +00:00
|
|
|
def _os(rctx):
|
2022-04-22 00:45:33 +00:00
|
|
|
"""Returns the name of the host operating system
|
|
|
|
|
|
|
|
Args:
|
|
|
|
rctx: repository_ctx
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The string "windows", "linux" or "darwin" that describes the host os
|
|
|
|
"""
|
2022-04-28 21:18:06 +00:00
|
|
|
if _is_darwin(rctx):
|
2022-04-22 00:45:33 +00:00
|
|
|
return "darwin"
|
2022-04-28 21:18:06 +00:00
|
|
|
if _is_linux(rctx):
|
2022-04-22 00:45:33 +00:00
|
|
|
return "linux"
|
2022-04-28 21:18:06 +00:00
|
|
|
if _is_windows(rctx):
|
2022-04-22 00:45:33 +00:00
|
|
|
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
|
|
|
|
|
2022-04-28 21:18:06 +00:00
|
|
|
def _platform(rctx):
|
2022-04-22 00:45:33 +00:00
|
|
|
"""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:
|
2022-04-28 21:18:06 +00:00
|
|
|
The normalized "<os>_<arch>" string of the host os and CPU architecture.
|
2022-04-22 00:45:33 +00:00
|
|
|
"""
|
2022-04-28 21:18:06 +00:00
|
|
|
os = _os(rctx)
|
2022-04-22 00:45:33 +00:00
|
|
|
|
|
|
|
# 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.
|
2022-04-28 21:18:06 +00:00
|
|
|
if os == "windows":
|
2022-04-22 00:45:33 +00:00
|
|
|
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]
|
2022-04-28 21:18:06 +00:00
|
|
|
return "%s_%s" % (os, arch)
|
2022-04-22 00:45:33 +00:00
|
|
|
|
2022-04-12 01:08:30 +00:00
|
|
|
repo_utils = struct(
|
2022-04-28 21:18:06 +00:00
|
|
|
is_darwin = _is_darwin,
|
|
|
|
is_linux = _is_linux,
|
|
|
|
is_windows = _is_windows,
|
2022-04-22 00:45:33 +00:00
|
|
|
get_env_var = _get_env_var,
|
2022-04-28 21:18:06 +00:00
|
|
|
os = _os,
|
|
|
|
platform = _platform,
|
2022-04-12 01:08:30 +00:00
|
|
|
)
|