Repository navigation
Cabal: tell configure scripts which compiler is in use - #12340
andreabedini wants to merge 3 commits into
Conversation
559d3a0 to
e90a74a
Compare
There was a problem hiding this comment.
🟡 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
ProgramDband pass them toconfigurevia--with-compiler,--with-hc-pkg,HC=..., and (for GHC)GHC/GHC_PKGenv vars. - Add a new testsuite package (
PackageTests/ConfigureCompiler) whoseconfigurescript 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 frommHcPath(theProgramDbvalue) rather than the effective--with-compilervalue passed viaconfigureArgs cfg'. This can makeHC=disagree with--with-compilerwhenconfigHcPathis explicitly set. Prefer usingconfigHcPath 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.
|
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. |
e90a74a to
d3c12d6
Compare
|
The changelog now says this needs a release notes entry and how to get the old value back: A #!/bin/sh
echo "configure args: $*"
echo "GHC=${GHC:-<unset>}"With after: |
62f15cb to
94918e2
Compare
f07371e to
2823f57
Compare
2823f57 to
e76f793
Compare
|
@sheaf can I have a review? |
Yes I'll take a look today. Thanks for the ping. |
sheaf
left a comment
There was a problem hiding this comment.
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
configCompilerExinstead of being re-derived fromConfigFlags. - Ensure
HCand--with-compilerare 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!).
| { configHcPath = configHcPath cfg `orConfigured` mHcPath | ||
| , configHcPkg = configHcPkg cfg `orConfigured` mHcPkgPath |
There was a problem hiding this comment.
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.
| hcEnv = | ||
| [ (var, Just path) | ||
| | Just GHC <- [flagToMaybe (configHcFlavor cfg)] | ||
| , (var, Just path) <- [("GHC", hcPath), ("GHC_PKG", hcPkgPath)] | ||
| ] |
There was a problem hiding this comment.
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").
| hcPath = flagToMaybe (configHcPath cfg') | ||
| hcPkgPath = flagToMaybe (configHcPkg cfg') |
There was a problem hiding this comment.
Similarly these should not consult ConfigFlags but rather use the result of configCompilerEx.
| 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 |
There was a problem hiding this comment.
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
Compilerand have ALL of the logic relating to the compiler dealt with there.
|
You're both partly right, and I think this can land without the bigger cleanup. @sheaf is right that the compiler should come from Also, So for this PR: the compiler from |
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.
e76f793 to
00b0721
Compare
|
@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
With that, The cost is that the type of What do you think? If you'd rather have this refactor reviewed on its own first, I can split it out. |
We don't want there to be TWO sources of truth for If there are pre-existing issues relating to "the GHC in the program database may not be the one that |
Fixes #7452. First step of #2947.
runConfigureScriptpasses--with-compiler=to a package'sconfigurescript, but takes thevalue from
configHcPath, and cabal-install never sets that: it hands the compiler over as aprogram path override (
--ghc=/path) and leavesconfigHcPathempty on purpose. So the scriptgets the flavour name and, following autoconf convention, goes looking for
ghconPATH.With cabal-install 3.16.1.0 and a configure script that just echoes its arguments:
A script that needs the actual compiler runs the wrong one. GHC's own
ghc-internaldoesexactly that (
$GHC --print-prim-moduleto generateGHC/Internal/Prim.hs) and fails under anyother ghc, which is how I ran into it: building GHC's stage2 libraries with the stage1 compiler
via cabal picked up the
ghconPATH.The fix takes the compiler and its package tool from the configured
ProgramDbwhen the flagsdo not name them, and passes them as:
--with-compiler=/pathand--with-hc-pkg=/path(what existing scripts look at);HC=/pathas a configure argument, the standard autoconf form suggested in Replace the--with-compilerconfigure script argument with HC=/path/to/hc #2947, alongsidethe existing
CC=/CXX=;GHCandGHC_PKGin the environment, for GHC, for scripts that useAC_ARG_VAR([GHC])rather than an option.
Explicit
--with-compiler/--with-hc-pkgflags are left as given. With the fix:I have not removed
--with-compiler(the second half of #2947); the six or so packages onHackage that implement it still work, and dropping it is a separate compatibility decision.
Testing
New
cabal-testsuite/PackageTests/ConfigureCompiler: abuild-type: Configurepackage whosehand-written
configurechecks that--with-compiler/--with-hc-pkgare executable paths,that
HC=matches--with-compiler, thatGHC/GHC_PKGare set, and that the compiler runs.It fails on master (
[ -x ghc ]is false) and passes with the change. The existing Configuretests (
Configure,BuildTypeConfigure,ConfigureCXX,ConfigureComponent) still pass.Skipped on Windows (POSIX shell script).
QA notes
Create a package with
build-type: Configurewhoseconfigurescript prints its arguments and$GHC, thencabal build -w /full/path/to/ghc. Before:--with-compiler=ghc,GHCunset.After: the full path in
--with-compiler,--with-hc-pkg,HC=andGHC.