2 {-# LANGUAGE DataKinds #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE RankNTypes #-}
6 -----------------------------------------------------------------------------
8 -----------------------------------------------------------------------------
11 -- Module : Distribution.Client.Sandbox
12 -- Maintainer : cabal-devel@haskell.org
13 -- Portability : portable
15 -- UI for the sandboxing functionality.
16 module Distribution
.Client
.Sandbox
17 ( loadConfigOrSandboxConfig
20 , getPersistOrConfigCompiler
23 import Distribution
.Client
.Compat
.Prelude
26 import Distribution
.Client
.Config
31 import Distribution
.Client
.Setup
32 ( CommonSetupFlags
(..)
38 import Distribution
.Client
.Sandbox
.PackageEnvironment
39 ( PackageEnvironmentType
(..)
40 , classifyPackageEnvironment
43 import Distribution
.Client
.SetupWrapper
44 ( SetupScriptOptions
(..)
45 , defaultSetupScriptOptions
47 import Distribution
.Simple
.Compiler
(Compiler
(..))
48 import Distribution
.Simple
.Configure
50 , findDistPrefOrDefault
51 , maybeGetPersistBuildConfig
53 import qualified Distribution
.Simple
.LocalBuildInfo
as LocalBuildInfo
54 import Distribution
.Simple
.Program
(ProgramDb
)
55 import Distribution
.Simple
.Setup
60 import Distribution
.System
(Platform
)
61 import Distribution
.Utils
.Path
hiding
66 import System
.Directory
70 -- * Basic sandbox functions.
74 updateInstallDirs
:: Flag
Bool -> SavedConfig
-> SavedConfig
75 updateInstallDirs userInstallFlag savedConfig
=
77 { savedConfigureFlags
=
79 { configInstallDirs
= installDirs
83 configureFlags
= savedConfigureFlags savedConfig
84 userInstallDirs
= savedUserInstallDirs savedConfig
85 globalInstallDirs
= savedGlobalInstallDirs savedConfig
87 | userInstall
= userInstallDirs
88 |
otherwise = globalInstallDirs
92 (configUserInstall configureFlags `mappend` userInstallFlag
)
94 -- | Check which type of package environment we're in and return a
95 -- correctly-initialised @SavedConfig@ and a @UseSandbox@ value that indicates
96 -- whether we're working in a sandbox.
97 loadConfigOrSandboxConfig
100 -- ^ For @--config-file@ and
101 -- @--sandbox-config-file@.
103 loadConfigOrSandboxConfig verbosity globalFlags
= do
104 let configFileFlag
= globalConfigFile globalFlags
106 pkgEnvDir
<- getCurrentDirectory
107 pkgEnvType
<- classifyPackageEnvironment pkgEnvDir
109 -- Only @cabal.config@ is present.
110 UserPackageEnvironment
-> do
111 config
<- loadConfig verbosity configFileFlag
112 userConfig
<- loadUserConfig verbosity pkgEnvDir Nothing
113 let config
' = config `mappend` userConfig
116 -- Neither @cabal.sandbox.config@ nor @cabal.config@ are present.
117 AmbientPackageEnvironment
-> do
118 config
<- loadConfig verbosity configFileFlag
119 let globalConstraintsOpt
=
120 flagToMaybe
. globalConstraintsFile
. savedGlobalFlags
$ config
121 globalConstraintConfig
<-
122 loadUserConfig verbosity pkgEnvDir globalConstraintsOpt
123 let config
' = config `mappend` globalConstraintConfig
126 -- | Return the saved \"dist/\" prefix, or the default prefix.
127 findSavedDistPref
:: SavedConfig
-> Flag
(SymbolicPath Pkg
(Dir Dist
)) -> IO (SymbolicPath Pkg
(Dir Dist
))
128 findSavedDistPref config flagDistPref
= do
129 let defDistPref
= useDistPref defaultSetupScriptOptions
131 (setupDistPref
(configCommonFlags
$ savedConfigureFlags config
))
132 `mappend` flagDistPref
133 findDistPref defDistPref flagDistPref
'
135 -- Utils (transitionary)
138 -- | Try to read the most recently configured compiler from the
139 -- 'localBuildInfoFile', falling back on 'configCompilerAuxEx' if it
141 getPersistOrConfigCompiler
143 -> IO (Compiler
, Platform
, ProgramDb
)
144 getPersistOrConfigCompiler configFlags
= do
145 let common
= configCommonFlags configFlags
146 distPref
<- findDistPrefOrDefault
(setupDistPref common
)
147 mlbi
<- maybeGetPersistBuildConfig
(flagToMaybe
$ setupWorkingDir common
) distPref
149 Nothing
-> do configCompilerAux
' configFlags
152 ( LocalBuildInfo
.compiler lbi
153 , LocalBuildInfo
.hostPlatform lbi
154 , LocalBuildInfo
.withPrograms lbi