Skip to content

Commit

Permalink
wip toolchains extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ylecornec committed Jun 22, 2023
1 parent 1eb089d commit e160c0b
Show file tree
Hide file tree
Showing 18 changed files with 909 additions and 122 deletions.
103 changes: 51 additions & 52 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ bazel_dep(
version = "0.0.6",
)

# --incompatible_disable_starlark_host_transitions
bazel_dep(
name = "rules_license",
version = "0.0.7",
Expand Down Expand Up @@ -42,14 +41,15 @@ non_module_deps = use_extension(

use_repo(
non_module_deps,
"rules_haskell_python_local",
# "rules_haskell_python_local",
"rules_haskell_worker_dependencies",
"rules_haskell_stack_update",
"rules_haskell_stack",
"Cabal",
"os_info",
)

register_toolchains("@rules_haskell_python_local//:toolchain")
# register_toolchains("@rules_haskell_python_local//:toolchain")

bazel_dep(
name = "rules_python",
Expand Down Expand Up @@ -84,6 +84,33 @@ use_repo(
"rules_haskell_asterius_build_setting",
)

haskell_toolchains = use_extension(
"@rules_haskell//extensions:haskell_toolchains.bzl",
"haskell_toolchains",
)

haskell_toolchains.bindists()

use_repo(
haskell_toolchains,
"all_bindist_toolchains",
"rules_haskell_python_local",
"rules_haskell_sh_posix_local",
)

# We need to register the nix toolchain before so it take precedence
# when we use the nixpkgs config.
register_toolchains(
"@rules_haskell_ghc_nixpkgs_toolchain//:toolchain",
dev_dependency = True,
)

register_toolchains(
"@all_bindist_toolchains//:all",
# can rules_sh register the toolchain instead ?
"@rules_haskell_python_local//:toolchain",
"@rules_haskell_sh_posix_local//:all",
)
#### Development dependencies ####

# bazel_dep(
Expand Down Expand Up @@ -153,7 +180,7 @@ non_module_dev_deps = use_extension(
dev_dependency = True,
)

use_repo(non_module_dev_deps, "os_info")
# use_repo(non_module_dev_deps, "os_info")

bazel_dep(
name = "stardoc",
Expand Down Expand Up @@ -182,27 +209,16 @@ use_repo(
"rules_haskell_ghc_windows_amd64_cc_toolchain",
)

# Needs bazel 6.2.0 to be able to register the toolchain with dev_dependency = True
register_toolchains(
"@rules_haskell_ghc_nixpkgs_toolchain//:toolchain",
dev_dependency = True,
)
# We cannot depend on rules_haskell_nix so we use a custom extension
# for the dev_dependency nix toolchain.

register_toolchains(
"@rules_haskell_ghc_darwin_amd64-toolchain//:toolchain",
"@rules_haskell_ghc_darwin_arm64-toolchain//:toolchain",
"@rules_haskell_ghc_linux_amd64-toolchain//:toolchain",
"@rules_haskell_ghc_linux_arm64-toolchain//:toolchain",
"@rules_haskell_ghc_windows_amd64-toolchain//:toolchain",
"@rules_haskell_ghc_windows_amd64-toolchain//:toolchain",
"@rules_haskell_ghc_windows_amd64_cc_toolchain//:windows_cc_toolchain",
dev_dependency = True,
# needed ?
use_repo(
non_module_dev_deps,
# "nixpkgs_python_toolchain",
"nixpkgs_go_sdk_toolchains",
)

use_repo(non_module_dev_deps, "nixpkgs_python_toolchain")

use_repo(non_module_dev_deps, "nixpkgs_go_sdk_toolchains")

register_toolchains(
"@nixpkgs_go_sdk_toolchains//:all",
dev_dependency = True,
Expand All @@ -228,42 +244,24 @@ use_repo(
"stackage-exe",
)

# Go setup for compiling gazelle/buildifier
bazel_dep(
name = "rules_go",
version = "0.39.0",
dev_dependency = True,
repo_name = "io_bazel_rules_go",
)

# local_path_override(
# module_name = "rules_go",
# path = "/home/stan/src/rules_go/",
# )

# local_path_override(
# module_name = "gazelle",
# path = "/home/stan/src/bazel-gazelle/",
# )

# Using buildifier will fail on NixOS with bzlmod
# https://github.com/bazelbuild/bazel-gazelle/issues/1469

go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
go_sdk = use_extension(
"@io_bazel_rules_go//go:extensions.bzl",
"go_sdk",
dev_dependency = True,
)

go_sdk.download(
version = "1.19.7",
)

# use_repo(non_module_dev_deps, "nixpkgs_go_sdk")

# rules_go_toolchain_override = use_extension("go_toolchains_override.bzl", "rules_go_toolchain_override")

# use_repo(
# rules_go_toolchain_override,
# "rules_go_toolchain_override",
# )

# go_sdk.custom(custom_toolchain_bzl_file = "@rules_go_toolchain_override//:go_toolchain.bzl")

bazel_dep(
name = "gazelle",
version = "0.30.0",
Expand Down Expand Up @@ -294,11 +292,12 @@ use_repo(
"org_golang_x_tools",
)

use_repo(non_module_dev_deps, "linux_pandoc")

use_repo(non_module_dev_deps, "nixpkgs_pandoc")

use_repo(non_module_dev_deps, "macos_pandoc")
use_repo(
non_module_dev_deps,
"linux_pandoc",
"nixpkgs_pandoc",
"macos_pandoc",
)

register_toolchains(
"@rules_haskell//docs/pandoc:nixpkgs",
Expand Down
134 changes: 134 additions & 0 deletions extensions/haskell_toolchains.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
load(
"@rules_haskell//haskell:ghc_bindist.bzl",
"ghc_bindist",
# "ghc_bindist_toolchain",
"haskell_register_ghc_bindists",
)

_bindists_tag = tag_class(
attrs = {
"version": attr.string(),
"ghcopts": attr.string_list(),
"haddock_flags": attr.string_list(),
"repl_ghci_args": attr.string_list(),
"cabalopts": attr.string_list(),
"locale": attr.string(),
},
doc = "creates a new toolchain ",
)

_bindist_tag = tag_class(
attrs = {
"name": attr.string(
mandatory = True,
),
"version": attr.string(
mandatory = True,
),
"target": attr.string(
mandatory = True,
),
"ghcopts": attr.string_list(),
"haddock_flags": attr.string_list(),
"repl_ghci_args": attr.string_list(),
"cabalopts": attr.string_list(),
"locale": attr.string(),
},
doc = "creates a new toolchain ",
)

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
# bazel's iteration order over modules.
# toolchains declared by the root have priority

# 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 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))
ghc_bindist(
name = name,
version = bindist_tag.version,
target = bindist_tag.target,
ghcopts = bindist_tag.ghcopts,
haddock_flags = bindist_tag.haddock_flags,
repl_ghci_args = bindist_tag.repl_ghci_args,
cabalopts = bindist_tag.cabalopts,
locale = bindist_tag.locale,
register = False,
toolchain_declarations = toolchain_declarations,
)

for bindists_tag_index, bindists_tag in enumerate(module.tags.bindists):
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,
haddock_flags = bindists_tag.haddock_flags,
repl_ghci_args = bindists_tag.repl_ghci_args,
cabalopts = bindists_tag.cabalopts,
locale = bindists_tag.locale,
register = False,
toolchain_declarations = toolchain_declarations,
)
# all_bindists.extend(toolchain_declarations)

_all_toolchains(
name = "all_bindist_toolchains",
toolchains = toolchain_declarations,
)

haskell_toolchains = module_extension(
implementation = _haskell_toolchains_impl,
tag_classes = {
"bindist": _bindist_tag,
"bindists": _bindists_tag,
# "bindists": _bindists_tag,
# "override_existing":
# ask for own toolchain:
},
)
2 changes: 2 additions & 0 deletions extensions/rules_haskell_dependencies.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
""" This module extension contains rules_haskell dependencies that are not available as modules """

load("@rules_haskell//haskell:repositories.bzl", "rules_haskell_dependencies_bzlmod")
# load("@rules_haskell//tools:os_info.bzl", "os_info")

def _rules_haskell_dependencies_impl(_mctx):
rules_haskell_dependencies_bzlmod()
# os_info(name = "os_info")

rules_haskell_dependencies = module_extension(
implementation = _rules_haskell_dependencies_impl,
Expand Down
Loading

0 comments on commit e160c0b

Please sign in to comment.