Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ylecornec committed Jun 22, 2023
1 parent e5038df commit e1f8c4e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 154 deletions.
45 changes: 16 additions & 29 deletions extensions/haskell_toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,41 +38,17 @@ _bindist_tag = tag_class(
)

def _all_toolchains_impl(rctx):
# toolchains = []
# for (i, toolchain) in enumerate(rctx.attr.toolchains):
# # Registering toolchains with alias does not work yet, so we copy the content of the BUILD files.
# # https://github.com/bazelbuild/bazel/issues/16298
# build_file = Label("@@{}//:BUILD".format(toolchain.workspace_name))
# s = rctx.read(rctx.path(build_file))
# print("BUILD=", s)

# toolchains = [
# """
# alias(
# name = "toolchain_{i}",
# actual = {actual},
# )
# """.format(i = i, actual = repr(str(toolchain)))
# for (i, toolchain) in enumerate(rctx.attr.toolchains)
# ]
# rctx.file("BUILD.bazel", content = "\n".join(toolchains))
content = "\n".join(rctx.attr.toolchains)
print("content=", content)
rctx.file("BUILD.bazel", content = content)

_all_toolchains = repository_rule(
implementation = _all_toolchains_impl,
attrs = {
# "toolchains": attr.label_list(),
"toolchains": attr.string_list(),
},
)

def _haskell_toolchains_impl(mctx):
# We only consider the first `bindists` tag, because subsequent
# ones would have the same constraints and lower priority.
found_bindists = False

# Instead of creating one external repository for each `toolchain(...)` declaration,
# we recover them in a list and declare them all in the `all_bindist_toolchains` repository.
# In a way that respects
Expand All @@ -83,11 +59,22 @@ def _haskell_toolchains_impl(mctx):
# if/once the following issue is resolved
# https://github.com/bazelbuild/bazel/issues/16298
toolchain_declarations = []
toolchain_names = []
found_bindists = False

for module_index, module in enumerate(mctx.modules):
for bindist_tag_index, bindist_tag in enumerate(module.tags.bindist):
name = "bindist_{}_{}_{}".format(module_index, bindist_tag_index, bindist_tag.name)
toolchain_declarations.append("# module:{} tag:bindist_{}".format(module.name, bindist_tag_index))
name = "bindist_{}_{}_{}".format(module.name, module.version, bindist_tag.name)
if name in toolchain_names:
fail(
"""module "{module}~{version}" used the "bindist" tag twice with the "{tag_name}" name""".format(
tag_name = bindist_tag.name,
module = module.name,
version = module.version,
),
)
else:
toolchain_names.append(name)
ghc_bindist(
name = name,
version = bindist_tag.version,
Expand All @@ -101,10 +88,11 @@ def _haskell_toolchains_impl(mctx):
toolchain_declarations = toolchain_declarations,
)

for bindists_tag_index, bindists_tag in enumerate(module.tags.bindists):
for bindists_tag in module.tags.bindists:
# We only consider the first `bindists` tag, because subsequent
# ones would have the same constraints and lower priority.
if not found_bindists:
found_bindists = True
toolchain_declarations.append("# module:{} tag:bindists_{}".format(module.name, bindists_tag_index))
haskell_register_ghc_bindists(
version = bindists_tag.version,
ghcopts = bindists_tag.ghcopts,
Expand All @@ -115,7 +103,6 @@ def _haskell_toolchains_impl(mctx):
register = False,
toolchain_declarations = toolchain_declarations,
)
# all_bindists.extend(toolchain_declarations)

_all_toolchains(
name = "all_bindist_toolchains",
Expand Down
5 changes: 0 additions & 5 deletions haskell/ghc_bindist.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,6 @@ def ghc_bindist(
locale = locale,
**extra_attrs
)
ghc_bindist_toolchain_kwargs = {
"name": toolchain_name,
"bindist_name": bindist_name,
"target": target,
}
if toolchain_declarations == None:
_ghc_bindist_toolchain(
name = toolchain_name,
Expand Down
16 changes: 11 additions & 5 deletions rules_haskell_nix/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ bazel_dep(
version = "0.16",
)

bazel_dep(
name = "rules_sh",
version = "0.3.0",
)

bazel_dep(
name = "rules_nixpkgs_core",
version = "0.9.0",
Expand Down Expand Up @@ -46,20 +51,21 @@ nix_haskell_toolchains.config(

use_repo(
nix_haskell_toolchains,
# "all_nix_toolchains",
# "nix_toolchain_0_0_name_ghc_nixpkgs",
# "nix_toolchain_1_0_name_ghc_nixpkgs",
# "nix_toolchain_0_0_nix_toolchain_from_tests_ghc_nixpkgs",
"hub",
"all_posix_toolchains",
)

declare_nix_toolchains = use_extension(
"//:declare_toolchains.bzl",
"declare_nix_toolchains",
)

use_repo(declare_nix_toolchains, "all_nix_toolchains")
use_repo(
declare_nix_toolchains,
"all_nix_toolchains",
)

register_toolchains(
"@all_nix_toolchains//:all",
"@all_posix_toolchains//:all",
)
11 changes: 7 additions & 4 deletions rules_haskell_nix/declare_toolchains.bzl
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
load(
"@hub//:nix_ghcs.bzl",
"ghc_labels",
"toolchain_configs",
"toolchain_keys",
"toolchains_2",
)
load("@rules_haskell_nix//:nixpkgs.bzl", "register_ghc_from_nixpkgs_package")

def _nix_toolchains_repo_impl(rctx):
content = "\n".join(rctx.attr.toolchain_declarations)
print("content=", content)
rctx.file("BUILD.bazel", content = content)

_nix_toolchains_repo = repository_rule(
Expand All @@ -20,7 +18,13 @@ _nix_toolchains_repo = repository_rule(
)

def _declare_nix_toolchains_impl(mctx):
print("os from extension=", mctx.os.name)
# Instead of creating one external repository for each `toolchain(...)` declaration,
# we recover them in a list and declare them all in the `all_bindist_toolchains` repository.
# Following bazel's iteration order over modules so that toolchains declared by the root module take precedence

# an alternative would be to use aliases to the (to get rid of the toolchain_declarations parameter)
# if/once the following issue is resolved
# https://github.com/bazelbuild/bazel/issues/16298
toolchain_declarations = []
for toolchain in toolchain_keys:
kwargs = dict(toolchains_2[toolchain])
Expand All @@ -29,7 +33,6 @@ def _declare_nix_toolchains_impl(mctx):
kwargs["nixpkgs_ghc"] = ghc_labels[toolchain]
kwargs["module_ctx"] = mctx
register_ghc_from_nixpkgs_package(**kwargs)
print("toolchain_declarations=", toolchain_declarations)
_nix_toolchains_repo(
name = "all_nix_toolchains",
toolchain_declarations = toolchain_declarations,
Expand Down
Loading

0 comments on commit e1f8c4e

Please sign in to comment.