@@ -612,8 +612,8 @@ setupWrapper
612612 -> IO (SetupRunnerRes setupSpec )
613613setupWrapper verbosity options mpkg cmd getCommonFlags getFlags getExtraArgs wrapperArgs = do
614614 let allowInLibrary = case wrapperArgs of
615- NotInLibrary -> Don'tAllowInLibrary
616615 InLibraryArgs {} -> AllowInLibrary
616+ NotInLibrary -> Don'tAllowInLibrary
617617 ASetup (setup :: Setup kind ) <- getSetup verbosity options mpkg allowInLibrary
618618 let version = setupVersion setup
619619 flags <- getFlags version
@@ -643,21 +643,31 @@ setupWrapper verbosity options mpkg cmd getCommonFlags getFlags getExtraArgs wra
643643 InLibraryArgs libArgs ->
644644 case libArgs of
645645 InLibraryConfigureArgs elabSharedConfig elabReadyPkg -> do
646- -- See (1)(a) in Note [Constructing the ProgramDb]
646+ -- Start from the pre-configured compiler ProgramDb, augmented
647+ -- with all builtin programs (restored as unconfigured).
648+ -- This ensures:
649+ -- (a) configureAllKnownPrograms inside configureFinal skips
650+ -- compiler programs (already configured at project level),
651+ -- (b) builtin preprocessors like alex and happy are present as
652+ -- unconfigured programs, so configureFinal's
653+ -- configureAllKnownPrograms can find them using the
654+ -- per-package search path (respecting extra-prog-path).
655+ -- See (1) in Note [Constructing the ProgramDb].
656+
657+ -- Apply per-package user-supplied program args/paths.
658+ -- See (2)(a) in Note [Constructing the ProgramDb]
647659 baseProgDb <-
648- prependProgramSearchPath verbosity
649- (useExtraPathEnv options)
650- (useExtraEnvOverrides options) =<<
651- mkProgramDb verbHandles flags -- Passes user-supplied arguments to e.g. GHC
652- (restoreProgramDb builtinPrograms $
653- useProgramDb options) -- Recall that 'useProgramDb' is set to 'pkgConfigCompilerProgs'
654- -- See (2) in Note [Constructing the ProgramDb]
660+ mkProgramDb verbHandles flags
661+ (restoreProgramDb builtinPrograms $
662+ pkgConfigCompilerProgs elabSharedConfig)
655663 setupProgDb <-
656- configCompilerProgDb
657- verbosity
658- (pkgConfigCompiler elabSharedConfig)
659- baseProgDb
660- Nothing -- we use configProgramPaths instead
664+ if null (useExtraPathEnv options)
665+ then return baseProgDb
666+ else
667+ prependProgramSearchPath verbosity
668+ (useExtraPathEnv options)
669+ (useExtraEnvOverrides options)
670+ baseProgDb
661671 lbi0 <-
662672 InLibrary. configure
663673 (InLibrary. libraryConfigureInputsFromElabPackage
@@ -670,7 +680,7 @@ setupWrapper verbosity options mpkg cmd getCommonFlags getFlags getExtraArgs wra
670680 )
671681 flags
672682 let progs0 = LBI. withPrograms lbi0
673- -- See (1 )(b) in Note [Constructing the ProgramDb]
683+ -- See (2 )(b) in Note [Constructing the ProgramDb]
674684 progs1 <- updatePathProgDb verbosity progs0
675685 let
676686 lbi =
@@ -715,7 +725,24 @@ includes the call to 'configCompilerEx'.
715725To obtain a program database with all the required information, we do a few
716726things:
717727
718- (1)
728+ (1) We retrieve the pre-configured compiler program database (typically
729+ containing ghc, ghc-pkg, haddock, and toolchain programs such as ar, ld),
730+ and restore all builtin programs (alex, happy, hsc2hs, ...) as unconfigured
731+ entries on top of it.
732+
733+ This serves two purposes:
734+
735+ (a) The compiler programs (ghc, ghc-pkg, haddock, hsc2hs, ...) that were
736+ already configured at the project level appear in 'configuredProgs'.
737+ The 'configureAllKnownPrograms' call inside the Cabal per-package
738+ 'configureFinal' skips them, saving redundant work.
739+
740+ (b) Builtin preprocessors (e.g. alex, happy) are NOT configured at the
741+ project level; restoring them here means the call to
742+ 'configureAllKnownPrograms' in 'configureFinal' can find them using
743+ the per-package search path (including 'extra-prog-path').
744+
745+ (2)
719746 (a) When building a package with internal build tools, we must ensure that
720747 these build tools are available in PATH, with appropriate environment
721748 variable overrides for their data directory. To do this, we call
@@ -724,12 +751,6 @@ things:
724751 (b) Moreover, these programs must be available in the search paths for the
725752 compiler itself, in case they are run at compile-time (e.g. with a Template
726753 Haskell splice). We achieve this using 'updatePathProgDb'.
727-
728- (2) Given the compiler, we must compute the ProgramDb of programs that are
729- specified alongside the compiler, such as ghc-pkg, haddock, and toolchain
730- programs such as ar, ld.
731-
732- We do this using the function 'configCompilerProgDb'.
733754-}
734755
735756-- ------------------------------------------------------------
0 commit comments