Skip to content

Commit

Permalink
Merge pull request #2080 from tweag/cg/support_macos_arm64
Browse files Browse the repository at this point in the history
chore: support arm64 on MacOS
  • Loading branch information
mergify[bot] authored Dec 20, 2023
2 parents 1b476c3 + c220b71 commit c2c6d3f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 64 deletions.
13 changes: 6 additions & 7 deletions haskell/ghc_bindist.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ load(
"resolve_labels",
)
load("//haskell:ghc.bzl", "DEFAULT_GHC_VERSION")
load(":private/bazel_platforms.bzl", "bazel_platforms")

_GHC_DEFAULT_VERSION = DEFAULT_GHC_VERSION

Expand Down Expand Up @@ -567,7 +568,7 @@ def haskell_register_ghc_bindists(
configure_python3_toolchain(name = LOCAL_PYTHON_REPO_NAME, register = register)

def _configure_python3_toolchain_impl(repository_ctx):
cpu = get_cpu_value(repository_ctx)
os_cpu = get_cpu_value(repository_ctx)
python3_path = find_python(repository_ctx)
if check_bazel_version("4.2.0")[0]:
stub_shebang = """stub_shebang = "#!{python3_path}",""".format(
Expand Down Expand Up @@ -595,20 +596,18 @@ toolchain(
toolchain = ":py_runtime_pair",
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
exec_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//cpu:{cpu}",
"@platforms//os:{os}",
],
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//cpu:{cpu}",
"@platforms//os:{os}",
],
)
""".format(
python3 = python3_path,
os = {
"darwin": "osx",
"x64_windows": "windows",
}.get(cpu, "linux"),
os = bazel_platforms.get_os(os_cpu),
cpu = bazel_platforms.get_cpu(os_cpu),
stub_shebang = stub_shebang,
))

Expand Down
32 changes: 32 additions & 0 deletions haskell/private/bazel_platforms.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Module for managing/deriving official Bazel platform values."""

# The expected values for os_cpu can be found by looking at the
# implementation for get_cpu_value in lib_cc_configure.bzl.
# https://github.com/bazelbuild/bazel/blob/e11506feaea7401c3d27f55b47183ef49bd1d5a8/tools/cpp/lib_cc_configure.bzl#L186

def _get_os(os_cpu):
if os_cpu.find("darwin") >= 0:
return "osx"
if os_cpu.find("windows") >= 0:
return "windows"
return "linux"

def _get_cpu(os_cpu):
# This value could appear in older versions of Bazel.
if os_cpu == "darwin":
return "x86_64"

# This handles modern os-cpu values like darwin_arm64 or darwin_x86_64.
if os_cpu.startswith("darwin_"):
return os_cpu.removeprefix("darwin_")
if os_cpu == "arm64_windows":
return "arm64"
if os_cpu == "x64_windows":
return "x86_64"

return "x86_64"

bazel_platforms = struct(
get_os = _get_os,
get_cpu = _get_cpu,
)
57 changes: 0 additions & 57 deletions rules_haskell_nix/shell.nix

This file was deleted.

3 changes: 3 additions & 0 deletions tests/haskell_tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load(":bazel_platforms_tests.bzl", "bazel_platforms_test_suite")

bazel_platforms_test_suite()
97 changes: 97 additions & 0 deletions tests/haskell_tests/bazel_platforms_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""Tests for `bazel_platforms` module."""

load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("//haskell:private/bazel_platforms.bzl", "bazel_platforms")

def _get_os_test(ctx):
env = unittest.begin(ctx)

tests = [
struct(
msg = "darwin (legacy MacOS on x86_64)",
os_cpu = "darwin",
exp = "osx",
),
struct(
os_cpu = "darwin_x86_64",
exp = "osx",
),
struct(
os_cpu = "darwin_arm64",
exp = "osx",
),
struct(
os_cpu = "arm64_windows",
exp = "windows",
),
struct(
os_cpu = "x64_windows",
exp = "windows",
),
struct(
os_cpu = "freebsd",
exp = "linux",
),
struct(
os_cpu = "openbsd",
exp = "linux",
),
]
for t in tests:
actual = bazel_platforms.get_os(t.os_cpu)
msg = getattr(t, "msg", t.os_cpu)
asserts.equals(env, t.exp, actual, msg)

return unittest.end(env)

get_os_test = unittest.make(_get_os_test)

def _get_cpu_test(ctx):
env = unittest.begin(ctx)

tests = [
struct(
msg = "darwin (legacy MacOS on x86_64)",
os_cpu = "darwin",
exp = "x86_64",
),
struct(
os_cpu = "darwin_x86_64",
exp = "x86_64",
),
struct(
os_cpu = "darwin_arm64",
exp = "arm64",
),
struct(
os_cpu = "arm64_windows",
exp = "arm64",
),
struct(
os_cpu = "x64_windows",
exp = "x86_64",
),
struct(
os_cpu = "freebsd",
exp = "x86_64",
),
struct(
os_cpu = "openbsd",
exp = "x86_64",
),
]
for t in tests:
actual = bazel_platforms.get_cpu(t.os_cpu)
msg = getattr(t, "msg", t.os_cpu)
asserts.equals(env, t.exp, actual, msg)

return unittest.end(env)

get_cpu_test = unittest.make(_get_cpu_test)

def bazel_platforms_test_suite(name = "bazel_platforms_tests"):
return unittest.suite(
name,
get_os_test,
get_cpu_test,
)
1 change: 1 addition & 0 deletions tools/os_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def _os_info_impl(repository_ctx):
"aarch64",
"darwin",
"darwin_arm64",
"darwin_x86_64",
"k8",
"x64_windows",
]
Expand Down

0 comments on commit c2c6d3f

Please sign in to comment.