Skip to content

Build GHCi libraries with ghc --merge-objs - #12358

Open
andreabedini wants to merge 3 commits into
haskell:masterfrom
andreabedini:andrea/wip/merge-objs
Open

andreabedini wants to merge 3 commits into
haskell:masterfrom
andreabedini:andrea/wip/merge-objs

Conversation

@andreabedini

Copy link
Copy Markdown
Collaborator

The only thing Cabal does with ld is ld -r to produce the GHCi object. GHC has exposed exactly that as ghc --merge-objs since 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 from Merge objects command being set in the settings (empty on the Windows bindists, so GHCi libs stay off there as today). On those GHCs we also stop probing ld at configure time, which is what #10970 wanted rid of. GHCs without the mode keep the ld -r path unchanged.

Detection is by capability, not version: a small GhcFeature type in Distribution.Simple.Program.GHC maps features to the flag that signals them in ghc --show-options, queried once when ghc is configured and recorded as program properties. --merge-objs is 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-objs to exercise the fallback; the new LibraryForGhci test 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

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>
Copilot AI lite review requested due to automatic review settings September 15, 2026 06:16
andreabedini and others added 2 commits September 15, 2026 14:17
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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 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 ld probing 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 command is absent, this expression yields Nothing, and the fromMaybe True at line 968 enables GHCi libraries anyway. That lets a GHC advertising --merge-objs proceed 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 command case is expected to matter, so it never verifies that --enable-library-for-ghci is disabled instead of invoking ghc --merge-objs without 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.

@ulysses4ever

Copy link
Copy Markdown
Collaborator

This looks reasonable. Did Copilot say there’s an issue when no merge tool is available. Is this spurious?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants