Repository navigation
Run ghc and ghc-pkg from the configured programs, not ProgramDb lookups - #12624
andreabedini wants to merge 2 commits into
Conversation
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.
f33721f to
8b94a6f
Compare
There was a problem hiding this comment.
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.
requireProgramwill 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:fromJustDistribution.Simple.GHC.toPackageIndexDistribution.Simple.GHC.getInstalledPackagesMonitorFilesDistribution.Simple.GHC.hcPkgInfoDistribution.Simple.GHC.pkgRoot
Use requireProgram:
Distribution.Simple.GHC.startInterpreterDistribution.Simple.GHC.libAbiHashDistribution.Simple.GHC.Build.buildDistribution.Simple.PreProcess.ppGhcCppDistribution.Client.CmdRepl.targetedRepl
Fall back quietly when ghc is missing:
Distribution.Simple.GHC.Build.Link.linkLibrary.combineObjectFilesDistribution.Simple.Configure.adjustBuildOptions.canMergeObjects
Configuring GHC finds
ghcandghc-pkg, builds aCompilerfrom them, and then throws the programs away. Everything afterwards looks them up again by name in aProgramDb. Every lookup there returns aMaybe, so callers turn a missing entry intoerror,fromJustor a quiet fallback, andrequireProgramwill happily searchPATHand find a different executable.The first commit returns the programs found as an abstract
CompilerProgramsand keeps it next to theCompiler, inLocalBuildInfoand inElaboratedSharedConfig. You can get the paths out of it, but you can only get a program to run throughcompilerProgramToRun, which takes the executable fromCompilerProgramsand the per-package arguments and environment from theProgramDb.The second commit moves the remaining lookups onto it: package-DB queries and registration,
startInterpreter, compilingSetup.hs, the--merge-objscheck 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.Distribution.Types.LocalBuildConfig.)ProgramDbnames a differentghcfrom the one the package was configured with.