2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE RankNTypes #-}
5 -----------------------------------------------------------------------------
7 -----------------------------------------------------------------------------
10 -- Module : Distribution.Client.Sandbox
11 -- Maintainer : cabal-devel@haskell.org
12 -- Portability : portable
14 -- UI for the sandboxing functionality.
15 module Distribution
.Client
.Sandbox
16 ( loadConfigOrSandboxConfig
19 , getPersistOrConfigCompiler
22 import Distribution
.Client
.Compat
.Prelude
25 import Distribution
.Client
.Config
30 import Distribution
.Client
.Setup
36 import Distribution
.Client
.Sandbox
.PackageEnvironment
37 ( PackageEnvironmentType
(..)
38 , classifyPackageEnvironment
41 import Distribution
.Client
.SetupWrapper
42 ( SetupScriptOptions
(..)
43 , defaultSetupScriptOptions
45 import Distribution
.Simple
.Compiler
(Compiler
(..))
46 import Distribution
.Simple
.Configure
48 , findDistPrefOrDefault
49 , maybeGetPersistBuildConfig
51 import qualified Distribution
.Simple
.LocalBuildInfo
as LocalBuildInfo
52 import Distribution
.Simple
.Program
(ProgramDb
)
53 import Distribution
.Simple
.Setup
58 import Distribution
.System
(Platform
)
60 import System
.Directory
(getCurrentDirectory)
62 -- * Basic sandbox functions.
66 updateInstallDirs
:: Flag
Bool -> SavedConfig
-> SavedConfig
67 updateInstallDirs userInstallFlag savedConfig
=
69 { savedConfigureFlags
=
71 { configInstallDirs
= installDirs
75 configureFlags
= savedConfigureFlags savedConfig
76 userInstallDirs
= savedUserInstallDirs savedConfig
77 globalInstallDirs
= savedGlobalInstallDirs savedConfig
79 | userInstall
= userInstallDirs
80 |
otherwise = globalInstallDirs
84 (configUserInstall configureFlags `mappend` userInstallFlag
)
86 -- | Check which type of package environment we're in and return a
87 -- correctly-initialised @SavedConfig@ and a @UseSandbox@ value that indicates
88 -- whether we're working in a sandbox.
89 loadConfigOrSandboxConfig
92 -- ^ For @--config-file@ and
93 -- @--sandbox-config-file@.
95 loadConfigOrSandboxConfig verbosity globalFlags
= do
96 let configFileFlag
= globalConfigFile globalFlags
98 pkgEnvDir
<- getCurrentDirectory
99 pkgEnvType
<- classifyPackageEnvironment pkgEnvDir
101 -- Only @cabal.config@ is present.
102 UserPackageEnvironment
-> do
103 config
<- loadConfig verbosity configFileFlag
104 userConfig
<- loadUserConfig verbosity pkgEnvDir Nothing
105 let config
' = config `mappend` userConfig
108 -- Neither @cabal.sandbox.config@ nor @cabal.config@ are present.
109 AmbientPackageEnvironment
-> do
110 config
<- loadConfig verbosity configFileFlag
111 let globalConstraintsOpt
=
112 flagToMaybe
. globalConstraintsFile
. savedGlobalFlags
$ config
113 globalConstraintConfig
<-
114 loadUserConfig verbosity pkgEnvDir globalConstraintsOpt
115 let config
' = config `mappend` globalConstraintConfig
118 -- | Return the saved \"dist/\" prefix, or the default prefix.
119 findSavedDistPref
:: SavedConfig
-> Flag
FilePath -> IO FilePath
120 findSavedDistPref config flagDistPref
= do
121 let defDistPref
= useDistPref defaultSetupScriptOptions
123 configDistPref
(savedConfigureFlags config
)
124 `mappend` flagDistPref
125 findDistPref defDistPref flagDistPref
'
127 -- Utils (transitionary)
130 -- | Try to read the most recently configured compiler from the
131 -- 'localBuildInfoFile', falling back on 'configCompilerAuxEx' if it
133 getPersistOrConfigCompiler
135 -> IO (Compiler
, Platform
, ProgramDb
)
136 getPersistOrConfigCompiler configFlags
= do
137 distPref
<- findDistPrefOrDefault
(configDistPref configFlags
)
138 mlbi
<- maybeGetPersistBuildConfig distPref
140 Nothing
-> do configCompilerAux
' configFlags
143 ( LocalBuildInfo
.compiler lbi
144 , LocalBuildInfo
.hostPlatform lbi
145 , LocalBuildInfo
.withPrograms lbi