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

Refactor the core component building logic: gbuild vs buildOrReplLib #9602

Merged
merged 2 commits into from
Jan 29, 2024

Conversation

alt-romes
Copy link
Collaborator

@alt-romes alt-romes commented Jan 9, 2024

  1. Refactors the duplicated buildExtraSources function from gbuild and
    buildOrReplLib into a standalone monadic computation in the context of
    building a component. This refactor allows
    us to share the code for building an extra source amongst the two
    functions.

  2. Creates a new module Distribution.Simple.GHC.Build.Modules which, in the
    same spirit as ...GHC.Build.ExtraModules, defines an action which builds
    all the Haskell modules of the component being built.

    This function clarifies and re-implements the logic of building Haskell
    modules in the different possible ways, while accounting for
    Template Haskell special "way requirements", which was previously
    duplicated in a non-obvious manner in gbuild and buildOrReplLib.

    The Note [Building Haskell modules accounting for TH] in that module
    explains the big picture, and the implementation is re-done in light of
    it.

  3. Re-work the linker invocations, focusing on preserving existing
    behaviour before simplifying or fixing bugs any further.

Template Β: This PR does not modify cabal behaviour (documentation, tests, refactoring, etc.)

Include the following checklist in your PR:

@alt-romes alt-romes force-pushed the wip/romes/cabal-9389 branch 2 times, most recently from 03688b1 to 0083fab Compare January 9, 2024 14:17
@alt-romes
Copy link
Collaborator Author

@mergify refresh

Copy link
Contributor

mergify bot commented Jan 9, 2024

refresh

✅ Pull request refreshed

@alt-romes alt-romes force-pushed the wip/romes/cabal-9389 branch 17 times, most recently from 0ca44da to 946a977 Compare January 16, 2024 19:50
@alt-romes alt-romes changed the title Build extra sources as a standalone BuildM action Refactor gbuild vs buildOrReplLib Jan 16, 2024
@philderbeast
Copy link
Collaborator

Thanks for the sneak peek "live preview" of this today @alt-romes. Should it be converted to draft or is it ready for review? It would be good to have people familiar with GHC development involved in the review.

Copy link
Collaborator

@andreabedini andreabedini left a comment

Choose a reason for hiding this comment

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

TBH I am not sure of the value of BuildM, at least in the way it is used here. It looks like that, most of the times, the environment it carries is immediately extracted:

buildHaskellModules numJobs ghcProg pkg_descr buildTargetDir wantedWays = do
  verbosity <- buildVerbosity
  component <- buildComponent
  clbi <- buildCLBI
  lbi <- buildLBI
...

This is just off the top of my head. I need to have a close look.

newtype BuildM a = BuildM' (ReaderT PreBuildComponentInputs IO a)
deriving (Functor, Applicative, Monad, MonadReader PreBuildComponentInputs, MonadIO)

-- Ideally we'd use deriving via ReaderT PreBuildComponentInputs IO, but ghc 8.4 doesn't support it.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Depending on how you read the policy, we might not need to support GHC 8.4 anymore. Last 8.4 is 8.4.4, which was released on 14/10/2018, more than 5 years ago.

@alt-romes alt-romes force-pushed the wip/romes/cabal-9389 branch 3 times, most recently from 74b5e60 to 0187e2f Compare January 17, 2024 15:50
@alt-romes alt-romes force-pushed the wip/romes/cabal-9389 branch 2 times, most recently from 231d3bd to 3b6e66e Compare January 22, 2024 11:01
@philderbeast
Copy link
Collaborator

Leaving TODO comments with this refactor is an easy way to leave a marker for later intent. Right now it would not be so easy to raise an issue pointing at code. Existing lines of code will likely be replaced and we'll not have a permalink to the new lines until they're merged, will we?

@mpickering
Copy link
Collaborator

I have debugged with @alt-romes why there are CI failures on 7.6.3 and he is going to investigate how to fix this.

@alt-romes alt-romes self-assigned this Jan 22, 2024
@alt-romes alt-romes force-pushed the wip/romes/cabal-9389 branch 4 times, most recently from e40e5d8 to dd2b8df Compare January 22, 2024 16:09
@alt-romes
Copy link
Collaborator Author

Full CI is passing!

I've furthermore tested the changes of the refactor against master by compiling head.hackage with both and diffing the GHC invocations: Nothing is different!

This is ready to be merged, except for the ROMES:TODO: that we still must decide what to do with.

Copy link
Collaborator

@andreabedini andreabedini left a comment

Choose a reason for hiding this comment

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

This looks really good now, thank you.

I think most of the ROMES:TODO comments can be rephrased as general observations about the code, I think questions would be ok too.

I've furthermore tested the changes of the refactor against master by compiling head.hackage with both and diffing the GHC invocations: Nothing is different!

Fantastic. I know that the CLC uses https://github.com/Bodigrim/clc-stackage#how-to to assess changes. Maybe we should standarise a procedure to assess changes. If you have any notes to share about how you did the test, free free to share them here ☺️

Cabal/src/Distribution/Simple/GHC/Build/Modules.hs Outdated Show resolved Hide resolved
Comment on lines 126 to 127
-> Bool
-- ^ Want dynamic?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Who is wanting dynamic linking (I assume)? Is it that some kind extra sources need to force dynamic linking? Actually it seem to only affect ghcOptFPic, I am not sure. Can you explain in the comment the role of this.
Can we also use a more descriptive rather than a Bool?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this is about JS sources not requiring particularly to be built in the Dynamic way, even if the GHC is dynamic, because they behave differently from other object files like those from C. It may be as well because GHCJS could not even support dynamic-related flags such as -fPIC.

Really, this was the way it was done before: all extra sources except for GHCJS do "wantDyn = True", and JS sources (for the GHCJS compiler) "wantDyn = False". So I'm just preserving this behaviour.

I could investigate, but I think it would be better not mess around with GHCJS bits, especially because GHC's JS backend behaves somewhat differently to GHCJS and might result in a more uniform story here.

I will improve the comment.

Cabal/src/Distribution/Simple/GHC/Build/Utils.hs Outdated Show resolved Hide resolved
@alt-romes alt-romes force-pushed the wip/romes/cabal-9389 branch 2 times, most recently from b5add4e to aa7dee6 Compare January 24, 2024 09:20
@alt-romes
Copy link
Collaborator Author

Thanks @andreabedini. I've addressed your review, and cleared the ROMES:TODOs comments for more general comments or todos.

@alt-romes
Copy link
Collaborator Author

@andreabedini, regarding the testing strategy:

I didn't do anything super high-tech. Here are a few steps:

Have a GHC wrapper called ghc-new which logs all invocations:

#!/bin/sh

echo "ghc $@" >> "/Users/romes/ghc-dev/cabal-9389/ghc-log-new"

exec "ghc" ${1+"$@"}

And another one called ghc-old, which logs to a different file.

Then, I ran the cabal testuite with two different cabal versions, passing -w ghc-new to one, and -w ghc-old to the other.
Finally, vimdiff ghc-log-new ghc-log-old.

Then I cloned the head.hackage repository twice and built all packages in it with the two different cabals, each logging to a different file. And vimdiff again.

Copy link
Collaborator

@andreabedini andreabedini left a comment

Choose a reason for hiding this comment

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

Awesome work 👍

@mpickering
Copy link
Collaborator

Looks good to me, given the robust testing strategy. It's impossible to review this patch as-is due to the large changes but I am happy to merge if it results in the same output for the tested packages.

@alt-romes
Copy link
Collaborator Author

@mpickering thank you, that makes sense.

I'll proceed with merging. If you think we should wait feel free to remove the merge label.

@alt-romes alt-romes added merge me Tell Mergify Bot to merge and removed attention: needs-review labels Jan 26, 2024
@mergify mergify bot added the merge delay passed Applied (usually by Mergify) when PR approved and received no updates for 2 days label Jan 28, 2024
@alt-romes alt-romes force-pushed the wip/romes/cabal-9389 branch 3 times, most recently from 4419418 to 35660e9 Compare January 29, 2024 11:19
1. Refactors the duplicated `buildExtraSources` function from `gbuild` and
    `buildOrReplLib` into a standalone monadic computation in the context of
    building a component. This refactor allows
    us to share the code for building an extra source amongst the two
    functions.

2. Creates a new module Distribution.Simple.GHC.Build.Modules which, in the
    same spirit as ...GHC.Build.ExtraModules, defines an action which builds
    all the Haskell modules of the component being built.

    This function clarifies and re-implements the logic of building Haskell
    modules in the different possible ways, while accounting for
    Template Haskell special "way requirements", which was previously
    duplicated in a non-obvious manner in gbuild and buildOrReplLib.

    The Note [Building Haskell modules accounting for TH] in that module
    explains the big picture, and the implementation is re-done in light of
    it.

3. Re-work the linker invocations, focusing on preserving existing
behaviour before simplifying or fixing bugs any further.

Fixes haskell#9389.
@mergify mergify bot merged commit 1bab371 into haskell:master Jan 29, 2024
49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge delay passed Applied (usually by Mergify) when PR approved and received no updates for 2 days merge me Tell Mergify Bot to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants