Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect host alias symlink on some platforms #80

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions 3rdparty/bazel-lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "bzl_lib",
srcs = [
"repo_utils.bzl",
],
visibility = ["//visibility:public"],
)
1 change: 1 addition & 0 deletions 3rdparty/bazel-lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repo_utils.bzl vendored from https://github.com/aspect-build/bazel-lib/blob/v2.7.3/lib/private/repo_utils.bzl
118 changes: 118 additions & 0 deletions 3rdparty/bazel-lib/repo_utils.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""Utility functions for repository rules"""

def _is_darwin(rctx):
"""Returns true if the host operating system is Darwin"""
return rctx.os.name.lower().startswith("mac os")

def _is_linux(rctx):
"""Returns true if the host operating system is Linux"""
return rctx.os.name.lower().startswith("linux")

def _is_freebsd(rctx):
"""Returns true if the host operating system is FreeBSD"""
return rctx.os.name.lower().startswith("freebsd")

def _is_windows(rctx):
"""Returns true if the host operating system is Windows"""
return rctx.os.name.lower().find("windows") != -1

def _os(rctx):
"""Returns the name of the host operating system

Args:
rctx: rctx

Returns:
The string "windows", "linux", "freebsd" or "darwin" that describes the host os
"""
if _is_darwin(rctx):
return "darwin"
if _is_linux(rctx):
return "linux"
if _is_freebsd(rctx):
return "freebsd"
if _is_windows(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: rctx
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
"""

# On Windows, the HOME environment variable is named differently.
# See https://en.wikipedia.org/wiki/Home_directory#Default_home_directory_per_operating_system
if name == "HOME" and _is_windows(rctx):
name = "USERPROFILE"
if name in rctx.os.environ:
return rctx.os.environ[name]
return default

def _get_home_directory(rctx):
return _get_env_var(rctx, "HOME", None)

def _platform(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: rctx

Returns:
The normalized "<os>_<arch>" string of the host os and CPU architecture.
"""
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 == "windows":
proc_arch = (_get_env_var(rctx, "PROCESSOR_ARCHITECTURE", "") or
_get_env_var(rctx, "PROCESSOR_ARCHITEW6432", ""))
if proc_arch == "ARM64":
arch = "arm64"
else:
arch = "amd64"
else:
arch = rctx.execute(["uname", "-m"]).stdout.strip()
arch_map = {
"aarch64": "arm64",
"x86_64": "amd64",
}
if arch in arch_map.keys():
arch = arch_map[arch]
return "%s_%s" % (os, arch)

repo_utils = struct(
is_darwin = _is_darwin,
is_linux = _is_linux,
is_windows = _is_windows,
get_env_var = _get_env_var,
get_home_directory = _get_home_directory,
os = _os,
platform = _platform,
)
2 changes: 1 addition & 1 deletion MODULE.bazel.lock

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

3 changes: 3 additions & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ bzl_library(
"@bazel_tools//tools:bzl_srcs",
],
visibility = ["//visibility:public"],
deps = [
"//3rdparty/bazel-lib:bzl_lib",
],
)

stardoc(
Expand Down
8 changes: 3 additions & 5 deletions helm/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//3rdparty/bazel-lib:repo_utils.bzl", "repo_utils")
load("//helm/private:versions.bzl", "DEFAULT_HELM_URL_TEMPLATES", "DEFAULT_HELM_VERSION", "HELM_VERSIONS")

_HELM_TAR_BUILD_CONTENT = """\
Expand Down Expand Up @@ -124,12 +125,9 @@ exports_files(["helm{ext}"])
ext = ext,
))

platform = "{}_{}".format(repository_ctx.os.name, repository_ctx.os.arch)
platform_repo_name = _helm_bin_repo_name(platform)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the bugs here (and I must have not been testing under bzlmod) is that we need to use the actual name from the repository context to properly capture the bzlmod repo path in the symlink.


repository_ctx.symlink("../{platform_repo_name}/helm{ext}".format(
repository_ctx.symlink("../{name}_{platform}/helm{ext}".format(
name = repository_ctx.attr.name,
platform_repo_name = platform_repo_name,
platform = repo_utils.platform(repository_ctx),
abrisco marked this conversation as resolved.
Show resolved Hide resolved
ext = ext,
), "helm{ext}".format(ext = ext))

Expand Down
Loading