Build GHCi libraries with ghc --merge-objs - #12358
andreabedini wants to merge 3 commits into
Conversation
Add GhcFeature: each constructor names the flag whose presence in `ghc --show-options` signals it. Configuring ghc queries once and records the answers as program properties; ghcSupports reads them back. This is detection by capability rather than by version, unlike GhcImplInfo. First feature: the --merge-objs mode (GHC >= 9.4). Add the matching GhcMode too. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2790548 to
7b4e71e
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
Two moderate issues remain around missing merge-tool handling and test coverage.
Pull request overview
This pull request updates Cabal to use ghc --merge-objs for GHCi libraries when supported, while retaining the ld -r fallback.
Changes:
- Adds GHC capability detection and merge-mode support.
- Avoids unnecessary
ldprobing when GHC handles merging. - Adds changelog and integration-test coverage.
File summaries
| File | Summary | Review note |
|---|---|---|
changelog.d/merge-objs.md |
Documents the new merging behavior. | No final comment. |
Cabal/src/Distribution/Simple/Program/GHC.hs |
Detects GHC features and renders merge commands. | No final comment. |
Cabal/src/Distribution/Simple/Program/Builtin.hs |
Detects GHC capabilities during configuration. | No final comment. |
Cabal/src/Distribution/Simple/GHC/Internal.hs |
Avoids ld probing when appropriate. |
No final comment. |
Cabal/src/Distribution/Simple/GHC/Build/Link.hs |
Uses GHC merging with an ld fallback. |
No final comment. |
Cabal/src/Distribution/Simple/Configure.hs |
Determines GHCi library availability. | Moderate (1 vote): A missing Merge objects command can still enable GHCi libraries; it should be treated as unsupported. |
cabal-testsuite/PackageTests/LibraryForGhci/setup.test.hs |
Tests merge and fallback behavior. | Moderate (1 vote): The test skips the platform where the empty-setting case matters; add controlled fixture or Windows coverage. |
cabal-testsuite/PackageTests/LibraryForGhci/p.cabal |
Defines the test package. | No final comment. |
cabal-testsuite/PackageTests/LibraryForGhci/Lib/Other.hs |
Provides an additional test module. | No final comment. |
cabal-testsuite/PackageTests/LibraryForGhci/Lib.hs |
Provides test library code. | No final comment. |
Review details
Suppressed comments (2)
Cabal/src/Distribution/Simple/Configure.hs:963
- When
Merge objects commandis absent, this expression yieldsNothing, and thefromMaybe Trueat line 968 enables GHCi libraries anyway. That lets a GHC advertising--merge-objsproceed without a configured merge tool (the comment above notes that GHC then falls back to an archive), so a missing setting should be treated as unsupported rather than capable.
not . null <$> Map.lookup "Merge objects command" (compilerProperties comp)
cabal-testsuite/PackageTests/LibraryForGhci/setup.test.hs:10
- The test skips exactly the platform where the new empty
Merge objects commandcase is expected to matter, so it never verifies that--enable-library-for-ghciis disabled instead of invokingghc --merge-objswithout a merge tool. Please add a controlled compiler/settings fixture (or Windows coverage) that exercises this branch and checks the resulting configuration/warning.
skipIfWindows "GHCi libraries are not built with GHC's Windows toolchain"
- Files reviewed: 10/10 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
This looks reasonable. Did Copilot say there’s an issue when no merge tool is available. Is this spurious? |
We used to run `ld -r` ourselves, finding ld from the ghc settings and probing it for -r and -x. When ghc has --merge-objs, let it do the merging: it uses the tool and flags it was configured with and handles long argument lists. Whether a GHCi library can be built at all then follows from "Merge objects command" being set, not from probing `ld --help`. GHCs without --merge-objs keep the ld path. Revives haskell#9226 with feature detection instead of a version gate. Refs haskell#7828, haskell#9301, haskell#12332. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
With --merge-objs we never run ld, so skip the two probes at configure time: compiling a C file through ghc to test `ld -x` (not always possible, see haskell#10970) and `ld --help` for relocatable output. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
7b4e71e to
c783c0c
Compare
It wasn't going to be a problem but it was cheap to fix anyway. Any GHC with --merge-objs (9.4+) also reports |
| -- When GHC can merge objects itself (@ghc --merge-objs@, see | ||
| -- 'GhcMergeObjs') Cabal never runs @ld@, so we skip probing it. | ||
| -- The probe below compiles a C file through GHC, which is not always | ||
| -- possible (#10970), and the @--help@ probe in 'ldProgram' is only |
There was a problem hiding this comment.
If it's not always possible, why do we return cp unconditionally? Or does ghcSupports check that, too?
| -- Meant to be called from the program's 'programPostConf'. If GHC cannot be | ||
| -- queried (e.g. it predates @--show-options@) every feature is recorded as | ||
| -- unsupported. | ||
| detectGhcFeatures :: Verbosity -> ConfiguredProgram -> IO ConfiguredProgram |
There was a problem hiding this comment.
Nice new functionality! Can we maybe use it in other places where we currently detect capabilities ad hoc?
The only thing Cabal does with
ldisld -rto produce the GHCi object. GHC has exposed exactly that asghc --merge-objssince 9.4 (GHC#20712, added so Cabal could drop its copy of the logic). This makes Cabal use it.When ghc advertises
--merge-objs, the GHCi object is produced by ghc with the merge tool and flags it was configured with, and whether a GHCi library can be built at all follows fromMerge objects commandbeing set in the settings (empty on the Windows bindists, so GHCi libs stay off there as today). On those GHCs we also stop probingldat configure time, which is what #10970 wanted rid of. GHCs without the mode keep theld -rpath unchanged.Detection is by capability, not version: a small
GhcFeaturetype inDistribution.Simple.Program.GHCmaps features to the flag that signals them inghc --show-options, queried once when ghc is configured and recorded as program properties.--merge-objsis the first member; other version checks can migrate later. This is what stalled #9226, which this supersedes.Tested with 9.12.2 and with a wrapper ghc that hides
--merge-objsto exercise the fallback; the newLibraryForGhcitest covers both. Not tested on Windows.Refs #7828, #9301, #12332, #10970. Supersedes #9226.
This PR was prepared with the help of an AI agent (Claude Code); I reviewed and tested the result.
🤖 Generated with Claude Code