Skip to content

Run ghc and ghc-pkg from the configured programs, not ProgramDb lookups - #12624

Draft
andreabedini wants to merge 2 commits into
haskell:masterfrom
andreabedini:andrea/wip/compiler-programs
Draft

andreabedini wants to merge 2 commits into
haskell:masterfrom
andreabedini:andrea/wip/compiler-programs

Conversation

@andreabedini

Copy link
Copy Markdown
Collaborator

Configuring GHC finds ghc and ghc-pkg, builds a Compiler from them, and then throws the programs away. Everything afterwards looks them up again by name in a ProgramDb. Every lookup there returns a Maybe, so callers turn a missing entry into error, fromJust or a quiet fallback, and requireProgram will happily search PATH and find a different executable.

The first commit returns the programs found as an abstract CompilerPrograms and keeps it next to the Compiler, in LocalBuildInfo and in ElaboratedSharedConfig. You can get the paths out of it, but you can only get a program to run through compilerProgramToRun, which takes the executable from CompilerPrograms and the per-package arguments and environment from the ProgramDb.

The second commit moves the remaining lookups onto it: package-DB queries and registration, startInterpreter, compiling Setup.hs, the --merge-objs check and UHC. After this, nothing in Cabal or cabal-install looks the compiler up by name, except the code that finds it in the first place.

The first commit is shared with #12340, which needs it to tell configure scripts which compiler is in use. Whichever of the two merges second will need a rebase. This also touches UHC.hs, so it will conflict with #12623.

This changes the types of quite a few exported functions in Distribution.Simple.{Configure,GHC,Register,Build,UHC}. Both changelog entries list them.

  • Patches conform to the coding conventions.
  • Any changes that could be relevant to users have been recorded in the changelog.
    • Is the change significant? No, I don't think so.
  • The documentation has been updated, if necessary. (Note [Compiler, compiler program and ProgramDb] in Distribution.Types.LocalBuildConfig.)
  • Manual QA notes have been included. There is nothing user-visible to QA, except a new warning when the ProgramDb names a different ghc from the one the package was configured with.
  • Tests have been added. It's a refactoring; the existing test suite covers these paths.

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.
Reading and updating package databases, starting an interpreter,
compiling a Setup script, building with UHC and checking whether ghc can
merge objects all looked ghc, ghc-pkg or uhc up by name in a ProgramDb.
Each lookup returns a Maybe, which the callers turned into 'error' or a
silent fallback, and 'requireProgram' searches PATH for a program it
does not find.

Pass the CompilerPrograms found when configuring the compiler to these
functions instead, and run the programs through 'compilerProgramToRun'
and 'compilerPkgProgramToRun', which keep the arguments and environment
from the ProgramDb. The merge-objects check uses the new pure
'compilerSupports', because 'adjustBuildOptions' runs during
cabal-install's pure plan elaboration.

The v1 commands get the programs from 'configCompilerAuxEx', which now
returns them, and SetupScriptOptions pairs them with the compiler in
'useCompiler'. The changelog entry lists the exported functions whose
types change.

@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'm sorry, but I don't support this at all.

As I said in this comment I think the current arrangement is: configCompilerEx finds out which ghc and ghc-pkg to use, and then stores them in withPrograms. So there is an agreement that looking up ghc (when the compiler flavour is indeed ghc) from the ProgramDb will never fail and will always produce the correct GHC (bugs like #12535 notwithstanding).

Configuring GHC finds ghc and ghc-pkg, builds a Compiler from them, and then throws the programs away.

I don't think that's accurate: it puts the programs in the ProgramDb so that everyone can look them up using a unified interface that works the same for the compiler as for any other program (e.g. build-tool-depends etc).

Every lookup there returns a Maybe, so callers turn a missing entry into error, fromJust or a quiet fallback

Lookups should be a crash, because they reveal that an invariant was violated. If there are weird fallbacks they should be removed and converted to crashes. I don't think re-organising the types to make it total is an improvement.

requireProgram will happily search PATH and find a different executable

Then the fix is to not call requireProgram: look up in the program database, crash if the answer isn't there.

As far as I'm concerned this PR is not an improvement to the maintability of the codebase, and it doesn't fix any bugs.

I think what is worth having is a single function that is called to look up the compiler (and its hc-pkg program) which crashes with a clear error they are not found (basically like Distribution.Simple.GHC.hcPkgInfo), and to use it consistently throughout the codebase, avoiding requireProgram and avoiding fallback logic. Quick survey of where that could be used:

Already crash on a missing entry:

  • Distribution.Simple.GHC.compilerProgramDb: fromJust
  • Distribution.Simple.GHC.toPackageIndex
  • Distribution.Simple.GHC.getInstalledPackagesMonitorFiles
  • Distribution.Simple.GHC.hcPkgInfo
  • Distribution.Simple.GHC.pkgRoot

Use requireProgram:

  • Distribution.Simple.GHC.startInterpreter
  • Distribution.Simple.GHC.libAbiHash
  • Distribution.Simple.GHC.Build.build
  • Distribution.Simple.PreProcess.ppGhcCpp
  • Distribution.Client.CmdRepl.targetedRepl

Fall back quietly when ghc is missing:

  • Distribution.Simple.GHC.Build.Link.linkLibrary.combineObjectFiles
  • Distribution.Simple.Configure.adjustBuildOptions.canMergeObjects

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants