Skip to content

Commit c2c6d3f

Browse files
authored
Merge pull request #2080 from tweag/cg/support_macos_arm64
chore: support arm64 on MacOS
2 parents 1b476c3 + c220b71 commit c2c6d3f

File tree

6 files changed

+139
-64
lines changed

6 files changed

+139
-64
lines changed

haskell/ghc_bindist.bzl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ load(
1818
"resolve_labels",
1919
)
2020
load("//haskell:ghc.bzl", "DEFAULT_GHC_VERSION")
21+
load(":private/bazel_platforms.bzl", "bazel_platforms")
2122

2223
_GHC_DEFAULT_VERSION = DEFAULT_GHC_VERSION
2324

@@ -567,7 +568,7 @@ def haskell_register_ghc_bindists(
567568
configure_python3_toolchain(name = LOCAL_PYTHON_REPO_NAME, register = register)
568569

569570
def _configure_python3_toolchain_impl(repository_ctx):
570-
cpu = get_cpu_value(repository_ctx)
571+
os_cpu = get_cpu_value(repository_ctx)
571572
python3_path = find_python(repository_ctx)
572573
if check_bazel_version("4.2.0")[0]:
573574
stub_shebang = """stub_shebang = "#!{python3_path}",""".format(
@@ -595,20 +596,18 @@ toolchain(
595596
toolchain = ":py_runtime_pair",
596597
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
597598
exec_compatible_with = [
598-
"@platforms//cpu:x86_64",
599+
"@platforms//cpu:{cpu}",
599600
"@platforms//os:{os}",
600601
],
601602
target_compatible_with = [
602-
"@platforms//cpu:x86_64",
603+
"@platforms//cpu:{cpu}",
603604
"@platforms//os:{os}",
604605
],
605606
)
606607
""".format(
607608
python3 = python3_path,
608-
os = {
609-
"darwin": "osx",
610-
"x64_windows": "windows",
611-
}.get(cpu, "linux"),
609+
os = bazel_platforms.get_os(os_cpu),
610+
cpu = bazel_platforms.get_cpu(os_cpu),
612611
stub_shebang = stub_shebang,
613612
))
614613

haskell/private/bazel_platforms.bzl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Module for managing/deriving official Bazel platform values."""
2+
3+
# The expected values for os_cpu can be found by looking at the
4+
# implementation for get_cpu_value in lib_cc_configure.bzl.
5+
# https://github.com/bazelbuild/bazel/blob/e11506feaea7401c3d27f55b47183ef49bd1d5a8/tools/cpp/lib_cc_configure.bzl#L186
6+
7+
def _get_os(os_cpu):
8+
if os_cpu.find("darwin") >= 0:
9+
return "osx"
10+
if os_cpu.find("windows") >= 0:
11+
return "windows"
12+
return "linux"
13+
14+
def _get_cpu(os_cpu):
15+
# This value could appear in older versions of Bazel.
16+
if os_cpu == "darwin":
17+
return "x86_64"
18+
19+
# This handles modern os-cpu values like darwin_arm64 or darwin_x86_64.
20+
if os_cpu.startswith("darwin_"):
21+
return os_cpu.removeprefix("darwin_")
22+
if os_cpu == "arm64_windows":
23+
return "arm64"
24+
if os_cpu == "x64_windows":
25+
return "x86_64"
26+
27+
return "x86_64"
28+
29+
bazel_platforms = struct(
30+
get_os = _get_os,
31+
get_cpu = _get_cpu,
32+
)

rules_haskell_nix/shell.nix

Lines changed: 0 additions & 57 deletions
This file was deleted.

tests/haskell_tests/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load(":bazel_platforms_tests.bzl", "bazel_platforms_test_suite")
2+
3+
bazel_platforms_test_suite()
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""Tests for `bazel_platforms` module."""
2+
3+
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
4+
load("//haskell:private/bazel_platforms.bzl", "bazel_platforms")
5+
6+
def _get_os_test(ctx):
7+
env = unittest.begin(ctx)
8+
9+
tests = [
10+
struct(
11+
msg = "darwin (legacy MacOS on x86_64)",
12+
os_cpu = "darwin",
13+
exp = "osx",
14+
),
15+
struct(
16+
os_cpu = "darwin_x86_64",
17+
exp = "osx",
18+
),
19+
struct(
20+
os_cpu = "darwin_arm64",
21+
exp = "osx",
22+
),
23+
struct(
24+
os_cpu = "arm64_windows",
25+
exp = "windows",
26+
),
27+
struct(
28+
os_cpu = "x64_windows",
29+
exp = "windows",
30+
),
31+
struct(
32+
os_cpu = "freebsd",
33+
exp = "linux",
34+
),
35+
struct(
36+
os_cpu = "openbsd",
37+
exp = "linux",
38+
),
39+
]
40+
for t in tests:
41+
actual = bazel_platforms.get_os(t.os_cpu)
42+
msg = getattr(t, "msg", t.os_cpu)
43+
asserts.equals(env, t.exp, actual, msg)
44+
45+
return unittest.end(env)
46+
47+
get_os_test = unittest.make(_get_os_test)
48+
49+
def _get_cpu_test(ctx):
50+
env = unittest.begin(ctx)
51+
52+
tests = [
53+
struct(
54+
msg = "darwin (legacy MacOS on x86_64)",
55+
os_cpu = "darwin",
56+
exp = "x86_64",
57+
),
58+
struct(
59+
os_cpu = "darwin_x86_64",
60+
exp = "x86_64",
61+
),
62+
struct(
63+
os_cpu = "darwin_arm64",
64+
exp = "arm64",
65+
),
66+
struct(
67+
os_cpu = "arm64_windows",
68+
exp = "arm64",
69+
),
70+
struct(
71+
os_cpu = "x64_windows",
72+
exp = "x86_64",
73+
),
74+
struct(
75+
os_cpu = "freebsd",
76+
exp = "x86_64",
77+
),
78+
struct(
79+
os_cpu = "openbsd",
80+
exp = "x86_64",
81+
),
82+
]
83+
for t in tests:
84+
actual = bazel_platforms.get_cpu(t.os_cpu)
85+
msg = getattr(t, "msg", t.os_cpu)
86+
asserts.equals(env, t.exp, actual, msg)
87+
88+
return unittest.end(env)
89+
90+
get_cpu_test = unittest.make(_get_cpu_test)
91+
92+
def bazel_platforms_test_suite(name = "bazel_platforms_tests"):
93+
return unittest.suite(
94+
name,
95+
get_os_test,
96+
get_cpu_test,
97+
)

tools/os_info.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def _os_info_impl(repository_ctx):
1616
"aarch64",
1717
"darwin",
1818
"darwin_arm64",
19+
"darwin_x86_64",
1920
"k8",
2021
"x64_windows",
2122
]

0 commit comments

Comments
 (0)