Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cabal/src/Distribution/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,8 @@ getBuildConfig globalFlags hooks verbosity distPref = do
let lbi =
lbi_wo_programs
{ withPrograms =
restoreProgramDb
restoreCompilerProgDb
(compiler lbi_wo_programs)
(builtinPrograms ++ hookedPrograms hooks)
(withPrograms lbi_wo_programs)
}
Expand Down
2 changes: 2 additions & 0 deletions Cabal/src/Distribution/Simple/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,8 @@ createInternalPackageDB verbosity lbi distPref = do
-- directory environment variable for the current package to the current
-- 'progOverrideEnv', so that any programs configured from now on will be
-- able to invoke these build tools.
--
-- See (B) in Note [Program search paths] in "Distribution.Simple.Configure".
addInternalBuildTools
:: AbsolutePath (Dir Pkg)
-> PackageDescription
Expand Down
169 changes: 153 additions & 16 deletions Cabal/src/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ module Distribution.Simple.Configure
, configCompilerEx
, configCompilerAuxEx
, configCompilerProgDb
, addCompilerPrograms
, configCompilerPrograms
, restoreCompilerProgDb
, compilerToolchainProgramNames
, computeEffectiveProfiling
, adjustBuildOptions
, buildOptionsAdjustmentWarnings
Expand Down Expand Up @@ -103,9 +107,10 @@ import Distribution.Simple.Program
import Distribution.Simple.Program.Db
( ProgramDb (..)
, lookupProgramByName
, modifyProgramSearchPath
, prependProgramSearchPath
, reuseConfiguredPrograms
, updateConfiguredProgs
, userSpecifiedPath
)
import Distribution.Simple.Program.GHC (GhcFeature (..), ghcSupports)
import Distribution.Simple.Setup.Common as Setup
Expand Down Expand Up @@ -1119,7 +1124,8 @@ configurePackage verbHandles cfg lbc0 pkg_descr00 flags enabled comp platform pa
externBuildToolDeps ++ unknownBuildTools

programDb1 <-
configureAllKnownPrograms (modifyVerbosityFlags lessVerbose verbosity) programDb0
configureBuildTools (modifyVerbosityFlags lessVerbose verbosity) requiredBuildTools programDb0
>>= configureAllKnownPrograms (modifyVerbosityFlags lessVerbose verbosity)
>>= configureRequiredPrograms verbosity requiredBuildTools

(pkg_descr2, programDb2) <-
Expand Down Expand Up @@ -1601,23 +1607,75 @@ mkPromisedDepsSet comps = Map.fromList [((packageName pn, CLibName ln), p) | p@(
-- arguments.
mkProgramDb :: VerbosityHandles -> ConfigFlags -> ProgramDb -> IO ProgramDb
mkProgramDb verbHandles cfg initialProgramDb = do
programDb <-
modifyProgramSearchPath (getProgramSearchPath initialProgramDb ++) -- We need to have the paths to programs installed by build-tool-depends before all other paths
<$> prependProgramSearchPath verbosity searchpath [] initialProgramDb
pure
-- See (B), (E) and (P) in Note [Program search paths]
programDb <- prependProgramSearchPath verbosity searchpath [] initialProgramDb
-- The programs that are already configured in the initial program database
-- are only kept if this configuration leads to the same programs.
reuseConfiguredPrograms verbosity
. userSpecifyArgss (configProgramArgs cfg)
. userSpecifyPaths (configProgramPaths cfg)
$ programDb
where
verbosity = mkVerbosity verbHandles $ fromFlagOrDefault normal (configVerbosity cfg)
searchpath = fromNubList (configProgramPathExtra cfg)

-- Note. We try as much as possible to _prepend_ rather than postpend the extra-prog-path
-- so that we can override the system path. However, in a v2-build, at this point, the "system" path
-- has already been extended by both the built-tools-depends paths, as well as the program-path-extra
-- so for v2 builds adding it again is entirely unnecessary. However, it needs to get added again _anyway_
-- so as to take effect for v1 builds or standalone calls to Setup.hs
-- In this instance, the lesser evil is to not allow it to override the system path.
{- Note [Program search paths]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Within Cabal, configuration (of a package or of a single component within a
package) produces a program search path (see 'progSearchPath'). It is used:

1. When configuring a program, to find its location
(see 'configureUnconfiguredProgram').
2. When running a program: 'programOverrideEnv' modifies the environment a
program runs in so that it gets a PATH that reflects the search path we
used to locate the program when we configured it.

Program lookup proceeds as follows, in order of precedence:

(O) The explicit override, if there was one (e.g. "--with-happy=<path>").

The override is given by 'configProgramPaths' (one entry per --with-PROG flag)
and recorded in the 'ProgramDb' by 'userSpecifyPaths'.

(B) The build tool specified in 'build-tool-depends'. This includes both
internal and external build tools.

(C) If the program comes bundled with the compiler, the program found next to
the compiler (e.g. `ghc-pkg` next to where we found `ghc`).
See 'guessToolFromGhcPath'.

(E) The program found in the search paths given with --extra-prog-path,
e.g. '--extra-prog-path=/opt/llvm/bin'.

(P) The program found in the PATH environment variable.

We arrange this as follows:

1. Cabal puts internal build tools at the front of the program search path,
and configures them as the programs of the same name, with
'addInternalBuildTools'. (This is a bit of a special case, as it happens
when building, not at configure time.)
2. The rest of B & E is seen by Cabal as a single list that it gets given
(see 'configProgramPathExtra'), which comes before P in the program search
path. The caller must put the directories of external build tools first in
this list.
3. C is not part of the program search path: a program that comes bundled
with the compiler has a 'programFindLocation' that looks next to the
compiler before the program search path (see 'guessToolFromGhcPath').
4. The build tools that a package requires are looked for in the directories
of B and E first (see 'configureBuildTools'). Technically this does mean
that, for build tools specifically, E comes before C. This doesn't really
matter in practice; e.g. when using cabal-install the build tools will have
been already built and placed in the appropriate directory, so we never
fall back to E.

B takes precedence over C, E and P to ensure that build tools that were solved
for are used (e.g. to ensure the build tool has the exact version requested by
the user, as in #9756).
C takes precedence over E and P so that the programs bundled with the compiler
match the compiler (e.g. `haddock` and `ghc-pkg`).
E takes precedence over P to allow users to customise program locations.
-}

-- -----------------------------------------------------------------------------
-- Helper functions for configure
Expand Down Expand Up @@ -2447,6 +2505,39 @@ combinedConstraints constraints dependencies installedPackages = do
-- -----------------------------------------------------------------------------
-- Configuring program dependencies

-- | Configure the build tools required by a package.
--
-- A build tool overrides a program of the same name that comes bundled with
-- the compiler: see (B) in Note [Program search paths].
configureBuildTools
:: Verbosity
-> [LegacyExeDependency]
-> ProgramDb
-> IO ProgramDb
configureBuildTools verbosity deps progdb0 =
foldM configureBuildTool progdb0 deps
where
searchDirs =
[dir | dir@(ProgramSearchPathDir _) <- getProgramSearchPath progdb0]
configureBuildTool progdb (LegacyExeDependency toolName _) =
case lookupKnownProgram toolName progdb of
Nothing -> return progdb
Just prog
| isJust (userSpecifiedPath prog progdb) -> return progdb
| otherwise -> do
location <- findProgramOnSearchPath verbosity searchDirs toolName
case location of
Nothing -> return progdb
Just (path, _)
-- The same program was already configured: nothing to do.
| fmap programPath (lookupProgram prog progdb) == Just path ->
return progdb
| otherwise ->
configureProgram
verbosity
prog{programFindLocation = \_ _ -> return location}
progdb

configureRequiredPrograms
:: Verbosity
-> [LegacyExeDependency]
Expand Down Expand Up @@ -2719,7 +2810,8 @@ configCompiler mbFlavor hcPath progdb verbosity = do
return (comp, fromMaybe buildPlatform maybePlatform, programDb)

-- | Configure programs associated to the compiler, such as @hc-pkg@, @haddock@
-- and toolchain program such as @ar@, @ld@.
-- and toolchain program such as @ar@, @ld@:
-- see 'addCompilerPrograms' and 'configCompilerPrograms'.
configCompilerProgDb
:: Verbosity
-> Compiler
Expand All @@ -2728,10 +2820,55 @@ configCompilerProgDb
-> Maybe FilePath
-- ^ user-specified @hc-pkg@ path (optional)
-> IO ProgramDb
configCompilerProgDb verbosity comp hcProgDb hcPkgPath = do
configCompilerProgDb verbosity comp hcProgDb hcPkgPath =
configCompilerPrograms verbosity comp (addCompilerPrograms comp hcProgDb) hcPkgPath

-- | Given a program database containing the configured compiler, add the
-- programs associated to the compiler, such as @hc-pkg@, @haddock@ and
-- toolchain programs such as @ar@, @ld@, as unconfigured programs.
--
-- User-specified locations and arguments already recorded in the program
-- database for these programs are preserved. More can be given afterwards
-- (with 'userSpecifyPath' or 'userSpecifyArgs'), before calling
-- 'configCompilerPrograms'.
addCompilerPrograms :: Compiler -> ProgramDb -> ProgramDb
addCompilerPrograms comp hcProgDb =
case compilerFlavor comp of
GHC -> GHC.addCompilerPrograms comp hcProgDb
_ -> hcProgDb

-- | Configure the programs associated to the compiler that must be configured
-- up-front, such as @hc-pkg@.
configCompilerPrograms
:: Verbosity
-> Compiler
-> ProgramDb
-- ^ must come from 'addCompilerPrograms' for this compiler
-> Maybe FilePath
-- ^ user-specified @hc-pkg@ path (optional), overriding any location of
-- @hc-pkg@ recorded in the program database
-> IO ProgramDb
configCompilerPrograms verbosity comp progdb hcPkgPath =
case compilerFlavor comp of
GHC -> GHC.configureCompilerPrograms verbosity comp progdb hcPkgPath
_ -> return progdb

-- | The names of the programs in a compiler's toolchain.
compilerToolchainProgramNames :: Compiler -> ToolchainProgramNames
compilerToolchainProgramNames comp =
case compilerFlavor comp of
GHC -> GHC.compilerProgramDb verbosity comp hcProgDb hcPkgPath
_ -> return hcProgDb
GHC -> GHC.toolchainProgramNames
flavour ->
error $
"compilerToolchainProgramNames: unsupported compiler " ++ prettyShow flavour

-- | 'restoreProgramDb' for a program database that contains the configured
-- compiler: the given programs (e.g. 'builtinPrograms') are restored, as well
-- as the programs associated to the compiler (see 'addCompilerPrograms'),
-- which take precedence.
restoreCompilerProgDb :: Compiler -> [Program] -> ProgramDb -> ProgramDb
restoreCompilerProgDb comp progs =
addCompilerPrograms comp . restoreProgramDb progs

-- -----------------------------------------------------------------------------
-- Testing C lib and header dependencies
Expand Down
18 changes: 7 additions & 11 deletions Cabal/src/Distribution/Simple/ConfigureScript.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ import Distribution.Pretty
import Distribution.Simple.Configure (findDistPrefOrDefault)
import Distribution.Simple.Errors
import Distribution.Simple.Program
import Distribution.Simple.Program.Db
import Distribution.Simple.Program.Find (programSearchPathAsPATHVar)
import Distribution.Simple.Setup.Common
import Distribution.Simple.Setup.Config
import Distribution.Simple.Utils
import Distribution.System (Platform, buildPlatform)
import Distribution.Utils.NubList
import Distribution.Utils.Path
import Distribution.Verbosity

Expand Down Expand Up @@ -167,18 +166,15 @@ runConfigureScript verbHandles cfg flags programDb hp = do
, Just $ unwords [showFlagValue fv | fv <- unFlagAssignment flags]
)
]
let extraPath = fromNubList $ configProgramPathExtra cfg
-- The configure script runs with the same search path as the package's
-- programs: see Note [Program search paths] in Distribution.Simple.Configure.
let searchPath = getProgramSearchPath programDb
pathEnv <- programSearchPathAsPATHVar searchPath
let mkFlagsEnv fs var = maybe (unwords fs) (++ (" " ++ unwords fs)) (lookup var env)
spSep = [FilePath.searchPathSeparator]
pathEnv =
maybe
(intercalate spSep extraPath)
((intercalate spSep extraPath ++ spSep) ++)
$ lookup "PATH" env
overEnv =
("CFLAGS", Just (mkFlagsEnv ccFlags "CFLAGS"))
: [("CXXFLAGS", Just (mkFlagsEnv cxxFlags "CXXFLAGS")) | Just cxxFlags <- [mcxxFlags]]
++ [("PATH", Just pathEnv) | not (null extraPath)]
++ [("PATH", Just pathEnv)]
++ cabalFlagEnv
maybeHostFlag = ["--host=" ++ show (pretty hp) | hp /= buildPlatform]
args' =
Expand All @@ -188,7 +184,7 @@ runConfigureScript verbHandles cfg flags programDb hp = do
++ ["CXX=" ++ cxxProgShort | Just cxxProgShort <- [mcxxProgShort]]
++ maybeHostFlag
shProg = simpleProgram "sh"
progDb <- prependProgramSearchPath verbosity extraPath [] emptyProgramDb
let progDb = setProgramSearchPath searchPath emptyProgramDb
shConfiguredProg <-
lookupProgram shProg
`fmap` configureProgram verbosity shProg progDb
Expand Down
Loading
Loading