From a728683f1c4d0b678d3873c11611a7941616bc60 Mon Sep 17 00:00:00 2001 From: Yves-Stan Le Cornec Date: Tue, 27 Jun 2023 18:30:59 +0200 Subject: [PATCH] Refactor ghc_bindist.bzl for use with haskell_toolchains extension This helps the haskell_toolchains module extensions to declare all of its toolchains in one BUILD file. --- haskell/ghc_bindist.bzl | 43 +++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/haskell/ghc_bindist.bzl b/haskell/ghc_bindist.bzl index 30ba305c08..4a1190d703 100644 --- a/haskell/ghc_bindist.bzl +++ b/haskell/ghc_bindist.bzl @@ -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", @@ -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", ), ) @@ -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,