Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

haskell_toolchains extension #1914

Merged
merged 4 commits into from
Jun 29, 2023
Merged

haskell_toolchains extension #1914

merged 4 commits into from
Jun 29, 2023

Conversation

ylecornec
Copy link
Member

@ylecornec ylecornec commented Jun 27, 2023

Depends on #1912

This PR adds the haskell_toolchains module extension which is used to configure and register the haskell bindists, via the bindist and bindists tags.

  • The bindists tag corresponds to haskell_register_ghc_bindists and registers toolchains for all platforms.
  • The bindist tag corresponds to ghc_bindist and is used to register toolchains one at a time. Inside the same module, the toolchains declared with bindist take precedence over the bindists ones.

The rules_haskell module calls the bindists tag itself to register default toolchains that can be used without any configuration. Alternatively, we can register customized toolchains, as done by the rules_haskell_test module (the priority of toolchains respects bazel's iteration order over modules where the root module comes first).

We cannot use load in the MODULE.bazel files at the moment

The configuration variables such as test_repl_ghci_args are duplicated inside the MODULE.bazel file because it is not possible to load them at the moment (see bazelbuild/bazel#17880). For the same reason it is not possible to rely on is_windows to configure the cabalops variable so the windows toolchains are declared separately in this file.

Documentation

The documentation is not generated because stardoc does not support it yet (bazelbuild/stardoc#123).

Toolchain declaration

It is not possible to register aliases to toolchain with bzlmod yet (bazelbuild/bazel#16298), so ghc_bindist.bzl was refactored a bit to make ghc_bindist_toolchain_declaration public. This way the haskell_toolchains module extension can call it to build its own toolchain declarations.

@ylecornec ylecornec force-pushed the ylecornec/bindist_extension branch from 9cf5ed4 to 26926db Compare June 28, 2023 07:34
@ylecornec ylecornec marked this pull request as ready for review June 28, 2023 08:10
@ylecornec ylecornec requested a review from avdv as a code owner June 28, 2023 08:10
@ylecornec ylecornec force-pushed the ylecornec/activate_bzlmod branch 5 times, most recently from 8d445fa to 8877ccc Compare June 28, 2023 20:26
@ylecornec ylecornec force-pushed the ylecornec/bindist_extension branch from 26926db to ff84233 Compare June 28, 2023 20:41
Copy link
Member

@aherrmann aherrmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work, that looks great!

Only a couple small comments.

Btw, I really appreciate how easy this PR and #1912 are to review commit by commit. Thank you!

Comment on lines +302 to +336
test_ghc_version = "9.2.5"

test_ghcopts = [
"-XStandaloneDeriving", # Flag used at compile time
"-threaded", # Flag used at link time
# Used by `tests/repl-flags`
"-DTESTS_TOOLCHAIN_COMPILER_FLAGS",
# this is the default, so it does not harm other tests
"-XNoOverloadedStrings",
]

test_haddock_flags = ["-U"]

test_repl_ghci_args = [
# The repl test will need this flag, but set by the local
# `repl_ghci_args`.
"-UTESTS_TOOLCHAIN_REPL_FLAGS",
# The repl test will need OverloadedString
"-XOverloadedStrings",
]

test_cabalopts = [
# Used by `tests/cabal-toolchain-flags`
"--ghc-option=-DTESTS_TOOLCHAIN_CABALOPTS",
"--haddock-option=--optghc=-DTESTS_TOOLCHAIN_CABALOPTS",
]

cabalopts_windows = test_cabalopts + [
# To avoid ghcide linking errors with heapsize on Windows of the form
#
# unknown symbol `heap_view_closurePtrs'
#
# See https://github.com/haskell/ghcide/pull/954
"--disable-library-for-ghci",
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The configuration variables such as test_repl_ghci_args are duplicated inside the MODULE.bazel file because it is not possible to load them at the moment (see bazelbuild/bazel#17880). For the same reason it is not possible to rely on is_windows to configure the cabalops variable so the windows toolchains are declared separately in this file.

Perhaps another approach could be for the module extension/tags to accept a label to a JSON file holding this configuration? (Either way, out of scope for this PR)

MODULE.bazel Outdated Show resolved Hide resolved
),
},
doc = """ Used to generate the `all_bindist_toolchains` external repository.
We can then invoque `register_toolchains("@all_bindist_toolchains//:all")` in the MODULE.bazel file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo invoque --> invoke.

attrs = {
"name": attr.string(
mandatory = True,
doc = "A unique name for the repository",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs a bit more explanation. As I understand it, it's only a unique name for the tag within the calling module. I.e. it doesn't have to be a globally unique name, correct?

found_bindists = False
for module in mctx.modules:
for bindist_tag in module.tags.bindist:
name = "bindist_{}_{}_{}".format(module.name, module.version, bindist_tag.name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is bindist_tag.name actually needed, or could it be replaced by an autogenerated unique suffix (e.g. a counter, or perhaps a hash plus collision counter to reduce invalidation)? Asking in the spirit of this discussion. Since the name is scoped to within the calling MODULE and not global it doesn't seem too onerous, but, maybe it can be avoided altogether.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah indeed I think we can do without the name attribute (and without auto-generated suffixes).

I updated the code to rely on the target attribute instead.

Before, if two bindist tags shared the same target attribute, the toolchains would have the same constraints so the second one would be effectively ignored. So now, a module is not allowed to use two bindist tags with the same target, and (module.name, module.version, bindist_tag.target) can be used as an identifier.

Comment on lines 136 to 135
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be simplified to something along the lines of the following?

if module.tags.bindists:
    bindists_tag = module.tags.bindists[0]
    ...

I.e. drop the found_bindists boolean.

Base automatically changed from ylecornec/activate_bzlmod to master June 29, 2023 09:51
@dpulls
Copy link

dpulls bot commented Jun 29, 2023

🎉 All dependencies have been resolved !

These are not needed anymore in bzlmod because of the haskell_toolchains module extension.
This helps the haskell_toolchains module extensions to declare all of its toolchains in one BUILD file.
@ylecornec ylecornec force-pushed the ylecornec/bindist_extension branch from ff84233 to b0659bf Compare June 29, 2023 12:22
@ylecornec ylecornec force-pushed the ylecornec/bindist_extension branch from b0659bf to eefde79 Compare June 29, 2023 13:23
@ylecornec ylecornec added the merge-queue merge on green CI label Jun 29, 2023
@mergify mergify bot merged commit 7600541 into master Jun 29, 2023
31 checks passed
@mergify mergify bot deleted the ylecornec/bindist_extension branch June 29, 2023 20:28
@mergify mergify bot removed the merge-queue merge on green CI label Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants