Skip to content

Cabal: tell configure scripts which compiler is in use - #12340

Open
andreabedini wants to merge 3 commits into
haskell:masterfrom
andreabedini:andrea/wip/prep/configure-script-compiler
Open

andreabedini wants to merge 3 commits into
haskell:masterfrom
andreabedini:andrea/wip/prep/configure-script-compiler

Conversation

@andreabedini

Copy link
Copy Markdown
Collaborator

Fixes #7452. First step of #2947.

runConfigureScript passes --with-compiler= to a package's configure script, but takes the
value from configHcPath, and cabal-install never sets that: it hands the compiler over as a
program path override (--ghc=/path) and leaves configHcPath empty on purpose. So the script
gets the flavour name and, following autoconf convention, goes looking for ghc on PATH.

With cabal-install 3.16.1.0 and a configure script that just echoes its arguments:

$ cabal build -w ~/.ghcup/bin/ghc-9.12.2
configure args: --with-compiler=ghc --prefix=... CC=/usr/lib64/ccache/gcc CXX=/usr/lib64/ccache/g++
GHC=<unset>

A script that needs the actual compiler runs the wrong one. GHC's own ghc-internal does
exactly that ($GHC --print-prim-module to generate GHC/Internal/Prim.hs) and fails under any
other ghc, which is how I ran into it: building GHC's stage2 libraries with the stage1 compiler
via cabal picked up the ghc on PATH.

The fix takes the compiler and its package tool from the configured ProgramDb when the flags
do not name them, and passes them as:

Explicit --with-compiler/--with-hc-pkg flags are left as given. With the fix:

configure args: --with-compiler=/home/andrea/.ghcup/bin/ghc-9.12.2 --with-hc-pkg=/home/andrea/.ghcup/ghc/9.12.2/bin/ghc-pkg-9.12.2 ... CC=... CXX=... HC=/home/andrea/.ghcup/bin/ghc-9.12.2
GHC=/home/andrea/.ghcup/bin/ghc-9.12.2

I have not removed --with-compiler (the second half of #2947); the six or so packages on
Hackage that implement it still work, and dropping it is a separate compatibility decision.

Testing

New cabal-testsuite/PackageTests/ConfigureCompiler: a build-type: Configure package whose
hand-written configure checks that --with-compiler/--with-hc-pkg are executable paths,
that HC= matches --with-compiler, that GHC/GHC_PKG are set, and that the compiler runs.
It fails on master ([ -x ghc ] is false) and passes with the change. The existing Configure
tests (Configure, BuildTypeConfigure, ConfigureCXX, ConfigureComponent) still pass.
Skipped on Windows (POSIX shell script).

QA notes

Create a package with build-type: Configure whose configure script prints its arguments and
$GHC, then cabal build -w /full/path/to/ghc. Before: --with-compiler=ghc, GHC unset.
After: the full path in --with-compiler, --with-hc-pkg, HC= and GHC.

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.

🟡 Changes recommended

The new HC= and GHC/GHC_PKG exports can disagree with an explicitly provided --with-compiler/--with-hc-pkg, causing inconsistent inputs to configure scripts.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes how build-type: Configure packages learn which Haskell compiler Cabal is using, ensuring configure scripts receive the actual configured compiler path (not just the flavor name like ghc) and adding a regression test to prevent future breakage.

Changes:

  • Recover compiler and package-tool paths from the configured ProgramDb and pass them to configure via --with-compiler, --with-hc-pkg, HC=..., and (for GHC) GHC/GHC_PKG env vars.
  • Add a new testsuite package (PackageTests/ConfigureCompiler) whose configure script asserts the above are executable paths and that the compiler runs.
  • Document the behavior change in the changelog.
File summaries
File Description
changelog.d/configure-script-compiler.md Changelog entry describing the new configure script compiler-path propagation.
Cabal/src/Distribution/Simple/ConfigureScript.hs Implements compiler/path recovery from ProgramDb and exports it to configure via args and env.
cabal-testsuite/PackageTests/ConfigureCompiler/configure-compiler.cabal New minimal build-type: Configure test package.
cabal-testsuite/PackageTests/ConfigureCompiler/configure New configure script asserting correct --with-compiler/HC=/env behavior.
cabal-testsuite/PackageTests/ConfigureCompiler/cabal.test.hs New testsuite driver (skips on Windows, runs v2-build).
cabal-testsuite/PackageTests/ConfigureCompiler/cabal.project New test project file for the testsuite package.
cabal-testsuite/PackageTests/ConfigureCompiler/cabal.out Golden output for the new test.
cabal-testsuite/PackageTests/ConfigureCompiler/A.hs Minimal library module for the test package.
Review details

Suppressed comments (1)

Cabal/src/Distribution/Simple/ConfigureScript.hs:222

  • HC= is currently derived from mHcPath (the ProgramDb value) rather than the effective --with-compiler value passed via configureArgs cfg'. This can make HC= disagree with --with-compiler when configHcPath is explicitly set. Prefer using configHcPath cfg' so all configure-script spellings stay in sync.
          -- The standard autoconf spelling for the Haskell compiler (#2947);
          -- '--with-compiler' above is kept for the scripts that use it.
          ++ ["HC=" ++ hcPath | Just hcPath <- [mHcPath]]
  • Files reviewed: 8/8 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@Mikolaj

Mikolaj commented Sep 17, 2026

Copy link
Copy Markdown
Member

Could you include such a configure script that you QA note requires, e.g., in a comment in this issue?

People may be depending on this bug, so please write in your changelog snippet that this requires a Release Notes point with maybe a hint of how to get back the old behaviour.

@andreabedini
andreabedini force-pushed the andrea/wip/prep/configure-script-compiler branch from e90a74a to d3c12d6 Compare September 22, 2026 04:47
@andreabedini

Copy link
Copy Markdown
Collaborator Author

The changelog now says this needs a release notes entry and how to get the old value back: --configure-option=--with-compiler=ghc (or configure-options: in cabal.project) comes after Cabal's own arguments and autoconf keeps the last one.

A configure script for the QA notes, next to any build-type: Configure package:

#!/bin/sh
echo "configure args: $*"
echo "GHC=${GHC:-<unset>}"

With cabal build -w /full/path/to/ghc, before this PR:

configure args: --with-compiler=ghc --prefix=... CC=/usr/lib64/ccache/gcc CXX=/usr/lib64/ccache/g++
GHC=<unset>

after:

configure args: --with-compiler=/home/andrea/.ghcup/bin/ghc --with-hc-pkg=/home/andrea/.ghcup/ghc/9.12.2/bin/ghc-pkg-9.12.2 --prefix=... CC=/usr/lib64/ccache/gcc CXX=/usr/lib64/ccache/g++ HC=/home/andrea/.ghcup/bin/ghc
GHC=/home/andrea/.ghcup/bin/ghc

@Mikolaj
Mikolaj requested a review from tdammers September 24, 2026 17:43
@andreabedini
andreabedini force-pushed the andrea/wip/prep/configure-script-compiler branch 2 times, most recently from 62f15cb to 94918e2 Compare October 27, 2026 07:41
@andreabedini
andreabedini force-pushed the andrea/wip/prep/configure-script-compiler branch 2 times, most recently from f07371e to 2823f57 Compare November 10, 2026 04:45
@mergify mergify Bot added the 1️⃣ more approval needed Set by Mergify: has one of the two required approvals label Nov 10, 2026
@andreabedini
andreabedini requested a review from ffaf1 November 11, 2026 08:55
@andreabedini
andreabedini force-pushed the andrea/wip/prep/configure-script-compiler branch from 2823f57 to e76f793 Compare November 11, 2026 10:04
@andreabedini

Copy link
Copy Markdown
Collaborator Author

@sheaf can I have a review?

@sheaf

sheaf commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

@sheaf can I have a review?

Yes I'll take a look today. Thanks for the ping.

@sheaf sheaf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I strongly dislike that the logic for constructing arguments to ./configure is scattered between Distribution.Simple.Setup.Config.configureArgs and Distribution.Simple.ConfigureScript.runConfigureScript. It makes it hard to figure out what overrides what.

It's of course absolutely not your fault, but I don't think we should paper over it any further.

I think we should:

  • Make sure that all information about the compiler flows via configCompilerEx instead of being re-derived from ConfigFlags.
  • Ensure HC and --with-compiler are passed next to eachother (which they aren't today).

Slightly more ambitiously I think we should also put the user-written arguments at the end (including after CC, CXX etc). This is a breaking change but I believe is the correct thing to do, resulting in more predictable behaviour (and simpler code!).

Comment on lines +110 to +111
{ configHcPath = configHcPath cfg `orConfigured` mHcPath
, configHcPkg = configHcPkg cfg `orConfigured` mHcPkgPath

@sheaf sheaf Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Today, Cabal figures out the compiler in use with the call to configCompilerEx in Distribution.Simple.Configure.preConfigurePackage. That returns a Compiler and a ProgramDb containing that compiler.

We should not duplicate the logic of configCompilerEx by going back to looking through flags such as configHcFlavor, configHcPath, configHcPkg: they are inputs to configCompilerEx and I think looking at the latter just invites bugs.

So I think we should instead pass the Compiler we got from configCompilerEx to runConfigureScript and then look up that compiler in the ProgramDb that is passed to runConfigureScript.

I think it would make sense to define a new function, right below configCompilerEx:

compilerPrograms :: Compiler -> ProgramDb -> (ConfiguredProgram, Maybe ConfiguredProgram)

that returns the hc and hc-pkg programs by looking them up in the program database.

Comment on lines +118 to +122
hcEnv =
[ (var, Just path)
| Just GHC <- [flagToMaybe (configHcFlavor cfg)]
, (var, Just path) <- [("GHC", hcPath), ("GHC_PKG", hcPkgPath)]
]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In doc/cabal-packge-description-file.rst, we should update the list of environment variables passed to ./configure (see "Moreover, various bits of build configuration will be passed via environment variables").

Comment on lines +116 to +117
hcPath = flagToMaybe (configHcPath cfg')
hcPkgPath = flagToMaybe (configHcPkg cfg')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Similarly these should not consult ConfigFlags but rather use the result of configCompilerEx.

Comment on lines 220 to 229
args = configureArgs backwardsCompatHack cfg'
args' =
configureFile'
: args
++ ["CC=" ++ ccProgShort]
++ ["CXX=" ++ cxxProgShort | Just cxxProgShort <- [mcxxProgShort]]
-- The standard autoconf spelling for the Haskell compiler (#2947);
-- '--with-compiler' above is kept for the scripts that use it.
++ ["HC=" ++ p | Just p <- [hcPath]]
++ maybeHostFlag

@sheaf sheaf Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think the fundamental issue that makes this code hard to reason about is that configureArgs is wrong. As I've been arguing in other comments, we should not use ConfigFlags to determine the compiler in use; that work has already been done by configCompilerEx. As far as configureArgs is concerned, we should either:

  • keep its type as is and remove everything relating to the compiler from it,
  • or pass it a Compiler and have ALL of the logic relating to the compiler dealt with there.

@mergify mergify Bot removed the 1️⃣ more approval needed Set by Mergify: has one of the two required approvals label Nov 12, 2026
@ulysses4ever

Copy link
Copy Markdown
Collaborator

You're both partly right, and I think this can land without the bigger cleanup.

@sheaf is right that the compiler should come from configCompilerEx's result, not from ConfigFlags. Both callers of runConfigureScript already have the Compiler (in LocalBuildInfo and PackageBuildDescr), so looking up the compiler and its package tool in the ProgramDb replaces the cfg' patching and keeps --with-compiler, HC= and GHC consistent.

Also, HC= comes after the user's configure-options, so --configure-option=HC=... can't override it (autoconf keeps the last value); the changelog's escape hatch only works for --with-compiler. Passing HC= next to --with-compiler fixes that.

So for this PR: the compiler from configCompilerEx, HC= next to --with-compiler, and GHC/GHC_PKG in the docs' list of environment variables. Moving all user arguments last and changing the exported configureArgs are breaking changes; I'd do them separately.

Configuring GHC finds the ghc program, uses it to build the Compiler,
then finds ghc-pkg next to it, but returns only the Compiler and a
ProgramDb. Anything that needs either program afterwards looks it up
again by name, although every ProgramDb lookup returns a Maybe and
'requireProgram' searches PATH for a program it does not find:
'compilerProgramDb' does so with 'fromJust' right after
'configureCompiler'. cabal-install's in-library configure makes it
worse: it takes the Compiler and the ProgramDb from
ElaboratedSharedConfig, where they are separate fields that go through
the plan cache and per-package changes to the ProgramDb.

Return the programs found, as an abstract CompilerPrograms, together
with the Compiler, and keep them with it from then on: in
PackageBuildDescr (hence LocalBuildInfo) as 'compilerPrograms', and in
ElaboratedSharedConfig, from where InLibrary passes them to
'configureFinal'.

CompilerPrograms gives the paths of the executables, for code that only
needs to say which compiler is in use, but gives a program to run only
through 'compilerProgramToRun'. The configured program alone is not
enough to run the compiler, because per-package arguments and
environment such as --ghc-options live in the ProgramDb.
'compilerProgramToRun' takes the executable from CompilerPrograms and
the arguments and environment from the ProgramDb, and warns if the
ProgramDb names another executable. Building, linking, the ABI hash,
the global package root and the GHC CPP preprocessor now run ghc this
way. Merging objects no longer falls back to `ld -r` when ghc is
missing from the ProgramDb.

This changes the exported types of 'configCompilerEx', related
functions and 'LocalBuildInfo'; the changelog entry lists them.
build-info, `cabal path --compiler-info` and multi-target `cabal repl`
each found the compiler by looking up a program in a ProgramDb:
build-info and `cabal path` chose the program from the compiler's
flavour, and failed for a flavour they did not list; `cabal repl`
always asked for ghc. All three need the executable the project was
configured with, which is now recorded next to the Compiler, in
LocalBuildInfo's 'compilerPrograms' and ElaboratedSharedConfig's
'pkgConfigCompilerPrograms'. `cabal path` reports its path; build-info
and `cabal repl` take the program from 'compilerProgramToRun'.

This retires 'UnknownCompilerFlavor' (Cabal-3102), which only
build-info raised.
'runConfigureScript' passed '--with-compiler=' from 'configHcPath',
which cabal-install never sets: it gives the compiler as a program
path instead. The script therefore got the bare flavour name
("--with-compiler=ghc") and, following autoconf convention, ran
whatever ghc was on PATH. GHC's ghc-internal runs
'ghc --print-prim-module' to generate GHC/Internal/Prim.hs and fails
under any other ghc, which broke building GHC's stage2 libraries with
the stage1 compiler.

Pass 'runConfigureScript' the compiler and hc-pkg programs recorded in
PackageBuildDescr, rather than re-deriving them from ConfigFlags that
were only inputs to configuring the compiler. The script gets their
paths as '--with-compiler=' and '--with-hc-pkg=', the compiler also as
the standard autoconf 'HC=' that haskell#2947 asks for, and, for GHC, as GHC
and GHC_PKG in the environment for scripts that use AC_ARG_VAR.

These arguments come before the user's configure options, so a script
that relied on the bare name can get it back with
'--configure-option=--with-compiler=ghc'. The new ConfigureCompiler
test checks that order.

Fixes haskell#7452.
@andreabedini
andreabedini force-pushed the andrea/wip/prep/configure-script-compiler branch from e76f793 to 00b0721 Compare November 24, 2026 09:49
@andreabedini

Copy link
Copy Markdown
Collaborator Author

@sheaf You are absolutely right. In this PR I had just followed the existing pattern, but after reading your comments I have been looking for the right way to do it. I went a step further than your suggestion, because the ProgramDb cannot express that we have already found a compiler: a lookup always returns a Maybe ConfiguredProgram. This is my idea:

  • We add data CompilerPrograms = CompilerPrograms ConfiguredProgram (Maybe ConfiguredProgram), the compiler and its package tool. The type is abstract.
  • configCompilerEx returns a CompilerPrograms, which ends up in PackageBuildDescr and in ElaboratedSharedConfig.
  • compilerPath and compilerPkgPath give the paths, for code that only needs to say which compiler is in use.
  • A program to run is only ever available from compilerProgramToRun :: Verbosity -> CompilerPrograms -> ProgramDb -> IO ConfiguredProgram. It always uses the compiler in CompilerPrograms and uses the ProgramDb only to look up arguments and environment.

With that, runConfigureScript takes the Compiler and the CompilerPrograms and doesn't look at ConfigFlags for the compiler anymore. --with-compiler=, --with-hc-pkg= and HC= are passed together, before the user's configure-options, so those can override all three. GHC and GHC_PKG are in the docs now. runConfigureScript doesn't call configureArgs anymore; as @ulysses4ever suggested I left the exported function alone, and I haven't moved the user's arguments after CC=/CXX=.

The cost is that the type of configCompilerEx changes, LocalBuildInfo gets a new field, and the PR is now three commits. The first one is shared with #12624, which moves the remaining by-name lookups onto CompilerPrograms.

What do you think? If you'd rather have this refactor reviewed on its own first, I can split it out.

@sheaf

sheaf commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

What do you think?

We don't want there to be TWO sources of truth for ghc and ghc-pkg: we either get them from withPrograms, or we remove them entirely from withPrograms and put them in your new field. Not both. Given that they're already in withPrograms I don't see the need to move them out; I think this is not at all the right tradeoff to get rid of a Maybe in a lookup. I think it's completely reasonable for there to be a explicit invariant "the ghc and ghc-pkg found by configCompilerEx are stored in withPrograms" with lookups crashing if that invariant is violated; IMHO enforcing it in the types would make the code worse for very little gain.

If there are pre-existing issues relating to "the GHC in the program database may not be the one that configCompilerEx put in" then those issues should be fixed separately (see e.g. #12536).

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.

build-type: configure configure script isn't passed the GHC exe set with -w to be used

7 participants