Skip to content

Commit 47c01bc

Browse files
committed
Add bazel_platforms module to derive values for OS and CPU.
1 parent 1b476c3 commit 47c01bc

File tree

2 files changed

+62
-31
lines changed

2 files changed

+62
-31
lines changed

haskell/ghc_bindist.bzl

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
load("@bazel_skylib//lib:paths.bzl", "paths")
44
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "patch")
55
load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value")
6-
load("@rules_sh//sh:posix.bzl", "sh_posix_configure")
76
load("@rules_cc//cc:find_cc_toolchain.bzl", "CC_TOOLCHAIN_TYPE")
7+
load("@rules_sh//sh:posix.bzl", "sh_posix_configure")
8+
load("//haskell:ghc.bzl", "DEFAULT_GHC_VERSION")
9+
load(":private/bazel_platforms.bzl", "bazel_platforms")
810
load(
911
":private/pkgdb_to_bzl.bzl",
1012
"pkgdb_to_bzl",
@@ -17,7 +19,6 @@ load(
1719
"find_python",
1820
"resolve_labels",
1921
)
20-
load("//haskell:ghc.bzl", "DEFAULT_GHC_VERSION")
2122

2223
_GHC_DEFAULT_VERSION = DEFAULT_GHC_VERSION
2324

@@ -86,7 +87,7 @@ def _ghc_bindist_impl(ctx):
8687
if GHC_BINDIST_STRIP_PREFIX.get(version) != None and GHC_BINDIST_STRIP_PREFIX[version].get(target) != None:
8788
stripPrefix = GHC_BINDIST_STRIP_PREFIX[version][target]
8889
else:
89-
arch_suffix = {"arm64": "aarch64", "amd64": "x86_64"}.get(arch)
90+
arch_suffix = {"amd64": "x86_64", "arm64": "aarch64"}.get(arch)
9091

9192
if os == "windows" and version_tuple >= (9, 0, 1):
9293
stripPrefix += "-{}-unknown-mingw32".format(arch_suffix)
@@ -262,10 +263,10 @@ rm -f
262263
"BUILD",
263264
filepaths["@rules_haskell//haskell:ghc.BUILD.tpl"],
264265
substitutions = {
265-
"%{toolchain_libraries}": toolchain_libraries,
266-
"%{toolchain}": toolchain,
267266
"%{docdir}": docdir,
268267
"%{is_clang}": str(is_clang),
268+
"%{toolchain_libraries}": toolchain_libraries,
269+
"%{toolchain}": toolchain,
269270
},
270271
executable = False,
271272
)
@@ -274,24 +275,11 @@ _ghc_bindist = repository_rule(
274275
_ghc_bindist_impl,
275276
local = False,
276277
attrs = {
277-
"version": attr.string(
278-
default = _GHC_DEFAULT_VERSION,
279-
doc = "The desired GHC version",
280-
),
281-
"target": attr.string(),
278+
"cabalopts": attr.string_list(),
282279
"ghcopts": attr.string_list(),
283280
"haddock_flags": attr.string_list(),
284-
"repl_ghci_args": attr.string_list(),
285-
"cabalopts": attr.string_list(),
286-
"patches": attr.label_list(
287-
default = [],
288-
doc =
289-
"A list of files that are to be applied as patches afer " +
290-
"extracting the archive.",
291-
),
292-
"patch_tool": attr.string(
293-
default = "patch",
294-
doc = "The patch(1) utility to use.",
281+
"locale": attr.string(
282+
mandatory = False,
295283
),
296284
"patch_args": attr.string_list(
297285
default = ["-p0"],
@@ -301,8 +289,21 @@ _ghc_bindist = repository_rule(
301289
default = [],
302290
doc = "Sequence of commands to be applied after patches are applied.",
303291
),
304-
"locale": attr.string(
305-
mandatory = False,
292+
"patch_tool": attr.string(
293+
default = "patch",
294+
doc = "The patch(1) utility to use.",
295+
),
296+
"patches": attr.label_list(
297+
default = [],
298+
doc =
299+
"A list of files that are to be applied as patches afer " +
300+
"extracting the archive.",
301+
),
302+
"repl_ghci_args": attr.string_list(),
303+
"target": attr.string(),
304+
"version": attr.string(
305+
default = _GHC_DEFAULT_VERSION,
306+
doc = "The desired GHC version",
306307
),
307308
"_relpath_script": attr.label(
308309
allow_single_file = True,
@@ -474,7 +475,7 @@ def ghc_bindist(
474475
"9.2.1": ["@rules_haskell//haskell:assets/ghc_9_2_1_mac.patch"],
475476
}.get(version)
476477

477-
extra_attrs = {"patches": patches, "patch_args": ["-p0"]} if patches else {}
478+
extra_attrs = {"patch_args": ["-p0"], "patches": patches} if patches else {}
478479

479480
# We want the toolchain definition to be tucked away in a separate
480481
# repository, that way `bazel build //...` will not match it (and
@@ -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 "linux"
28+
29+
bazel_platforms = struct(
30+
get_os = _get_os,
31+
get_cpu = _get_cpu,
32+
)

0 commit comments

Comments
 (0)