Skip to content

Commit

Permalink
Refactor ghc_bindist.bzl for use with haskell_toolchains extension
Browse files Browse the repository at this point in the history
This helps the haskell_toolchains module extensions to declare all of its toolchains in one BUILD file.
  • Loading branch information
ylecornec committed Jun 27, 2023
1 parent c9afe8a commit 7b71da4
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions haskell/ghc_bindist.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ _ghc_bindist = repository_rule(
},
)

def _ghc_bindist_toolchain_impl(ctx):
os, _, arch = ctx.attr.target.partition("_")
def ghc_bindist_toolchain_declaration(target, bindist_name, toolchain_name):
os, _, arch = target.partition("_")
os_constraint = {
"darwin": "osx",
"linux": "linux",
Expand All @@ -290,21 +290,29 @@ def _ghc_bindist_toolchain_impl(ctx):
"@platforms//cpu:{}".format(cpu_constraint),
]
target_constraints = exec_constraints
ctx.file(
"BUILD",
executable = False,
content = """
return """
toolchain(
name = "toolchain",
name = "{toolchain_name}",
toolchain_type = "@rules_haskell//haskell:toolchain",
toolchain = "@{bindist_name}//:toolchain-impl",
exec_compatible_with = {exec_constraints},
target_compatible_with = {target_constraints},
)
""".format(
""".format(
toolchain_name = toolchain_name,
bindist_name = bindist_name,
exec_constraints = exec_constraints,
target_constraints = target_constraints,
)

def _ghc_bindist_toolchain_impl(ctx):
ctx.file(
"BUILD",
executable = False,
content = ghc_bindist_toolchain_declaration(
target = ctx.attr.target,
bindist_name = ctx.attr.bindist_name,
exec_constraints = exec_constraints,
target_constraints = target_constraints,
toolchain_name = "toolchain",
),
)

Expand Down Expand Up @@ -342,6 +350,21 @@ _windows_cc_toolchain = repository_rule(
},
)

# Toolchains declarations for bindists used by the `haskell_toolchains` module
# extension to register all the haskell toolchains in the same BUILD file
def ghc_bindists_toolchain_declarations(version):
version = version or _GHC_DEFAULT_VERSION
if not GHC_BINDIST.get(version):
fail("Binary distribution of GHC {} not available.".format(version))
return [
ghc_bindist_toolchain_declaration(
target = target,
bindist_name = "rules_haskell_ghc_{}".format(target),
toolchain_name = "rules_haskell_ghc_{}".format(target),
)
for target in GHC_BINDIST[version]
]

def ghc_bindist(
name,
version,
Expand Down

0 comments on commit 7b71da4

Please sign in to comment.