Skip to content

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

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

Cabal: tell configure scripts which compiler is in use#12340
andreabedini wants to merge 1 commit 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 lite review requested due to automatic review settings September 9, 2026 03:35
'runConfigureScript' passes '--with-compiler=' to a package's configure
script, but takes the value from 'configHcPath', which cabal-install never
sets: it hands the compiler over as a program path override instead. The
script therefore got the bare flavour name ("--with-compiler=ghc") and,
following autoconf convention, looked up "ghc" on PATH. A script that
needs the actual compiler then ran 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 broke building GHC's stage2 libraries
with the stage1 compiler.

Take the compiler and its package tool from the configured ProgramDb when
the flags do not name them, and pass them as '--with-compiler=' and
'--with-hc-pkg=', as the standard autoconf 'HC=/path' argument next to the
existing 'CC='/'CXX=' (the form haskell#2947 asks for; '--with-compiler' stays for
the scripts that use it), and for GHC as GHC and GHC_PKG in the environment,
for scripts that use AC_ARG_VAR([GHC]) rather than an option. Explicit
'--with-compiler'/'--with-hc-pkg' flags are left as given.

The new ConfigureCompiler test has a configure script that checks the
arguments are executable paths, that HC= agrees with --with-compiler, and
that the variables are set.

Fixes haskell#7452.

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.

Comment on lines +112 to +116
hcEnv =
[ (var, Just path)
| Just GHC <- [flagToMaybe (configHcFlavor cfg)]
, (var, Just path) <- [("GHC", mHcPath), ("GHC_PKG", mHcPkgPath)]
]
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

3 participants