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

Simplify CPU arch detection in get_host_triple #2183

Merged
merged 2 commits into from
Oct 7, 2023
Merged
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
80 changes: 15 additions & 65 deletions rust/platform/triple.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,73 +67,19 @@ def triple(triple):
str = triple,
)

_CPU_ARCH_ERROR_MSG = """\
Command failed with exit code '{code}': {args}
----------stdout:
{stdout}
----------stderr:
{stderr}
"""

def _query_cpu_architecture(repository_ctx, expected_archs, is_windows = False):
"""Detect the host CPU architecture
def _validate_cpu_architecture(arch, expected_archs):
"""Validate the host CPU architecture

Args:
repository_ctx (repository_ctx): The repository_rule's context object
arch (string): a CPU architecture
expected_archs (list): A list of expected architecture strings
is_windows (bool, optional): If true, the cpu lookup will use the windows method (`wmic` vs `uname`)

Returns:
str: The host's CPU architecture
"""
if is_windows:
arguments = ["wmic", "os", "get", "osarchitecture"]
else:
arguments = ["uname", "-m"]

result = repository_ctx.execute(arguments)

if result.return_code:
fail(_CPU_ARCH_ERROR_MSG.format(
code = result.return_code,
args = arguments,
stdout = result.stdout,
stderr = result.stderr,
))

if is_windows:
# Example output:
# OSArchitecture
# 64-bit
#
# In some cases windows can return the same but with an uppercase b
# OSArchitecture
# 64-Bit
lines = result.stdout.split("\n")
arch = lines[1].strip().lower()

# Translate 64-bit to a compatible rust platform
# https://doc.rust-lang.org/nightly/rustc/platform-support.html
if arch.startswith("arm 64-bit"):
arch = "aarch64"
elif arch == "64-bit":
arch = "x86_64"
else:
arch = result.stdout.strip("\n")

# Correct the arm architecture for macos
if "mac" in repository_ctx.os.name and arch == "arm64":
arch = "aarch64"

if not arch in expected_archs:
fail("{} is not a expected cpu architecture {}\n{}".format(
if arch not in expected_archs:
fail("{} is not a expected cpu architecture {}".format(
arch,
expected_archs,
result.stdout,
))

return arch

def get_host_triple(repository_ctx, abi = None):
"""Query host information for the appropriate triple to use with load_arbitrary_tool or the crate_universe resolver

Expand Down Expand Up @@ -173,21 +119,25 @@ def get_host_triple(repository_ctx, abi = None):
"windows": ["aarch64", "x86_64"],
}

arch = repository_ctx.os.arch
if arch == "amd64":
arch = "x86_64"

if "linux" in repository_ctx.os.name:
cpu = _query_cpu_architecture(repository_ctx, supported_architectures["linux"])
_validate_cpu_architecture(arch, supported_architectures["linux"])
return triple("{}-unknown-linux-{}".format(
cpu,
arch,
abi or "gnu",
))

if "mac" in repository_ctx.os.name:
cpu = _query_cpu_architecture(repository_ctx, supported_architectures["macos"])
return triple("{}-apple-darwin".format(cpu))
_validate_cpu_architecture(arch, supported_architectures["macos"])
return triple("{}-apple-darwin".format(arch))

if "win" in repository_ctx.os.name:
cpu = _query_cpu_architecture(repository_ctx, supported_architectures["windows"], True)
_validate_cpu_architecture(arch, supported_architectures["windows"])
return triple("{}-pc-windows-{}".format(
cpu,
arch,
abi or "msvc",
))

Expand Down
Loading