-
Notifications
You must be signed in to change notification settings - Fork 698
/
CmdExec.hs
293 lines (275 loc) · 9.49 KB
/
CmdExec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
{-# LANGUAGE RecordWildCards #-}
-- |
-- Module : Distribution.Client.Exec
-- Maintainer : cabal-devel@haskell.org
-- Portability : portable
--
-- Implementation of the 'v2-exec' command for running an arbitrary executable
-- in an environment suited to the part of the store built for a project.
module Distribution.Client.CmdExec
( execAction
, execCommand
-- TODO: use common util module?
, withTempEnvFile
) where
import Distribution.Client.DistDirLayout
( DistDirLayout (..)
)
import Distribution.Client.InstallPlan
( GenericPlanPackage (..)
, toGraph
)
import Distribution.Client.NixStyleOptions
( NixStyleFlags (..)
, defaultNixStyleFlags
, nixStyleOptions
)
import Distribution.Client.ProjectFlags
( removeIgnoreProjectOption
)
import Distribution.Client.ProjectOrchestration
( BuildTimeSettings (..)
, CurrentCommand (..)
, ProjectBaseContext (..)
, ProjectBuildContext (..)
, commandLineFlagsToProjectConfig
, distDirLayout
, establishProjectBaseContext
, runProjectPreBuildPhase
)
import Distribution.Client.ProjectPlanOutput
( PostBuildProjectStatus
, argsEquivalentOfGhcEnvironmentFile
, createPackageEnvironment
, updatePostBuildProjectStatus
)
import Distribution.Client.ProjectPlanning
( ElaboratedInstallPlan
, ElaboratedSharedConfig (..)
)
import qualified Distribution.Client.ProjectPlanning as Planning
import Distribution.Client.Setup
( ConfigFlags (configVerbosity)
, GlobalFlags
)
import Distribution.Simple.Command
( CommandUI (..)
)
import Distribution.Simple.Flag
( fromFlagOrDefault
)
import Distribution.Simple.GHC
( GhcImplInfo (supportsPkgEnvFiles)
, getImplInfo
)
import Distribution.Simple.Program.Db
( configuredPrograms
, modifyProgramSearchPath
, requireProgram
)
import Distribution.Simple.Program.Find
( ProgramSearchPathEntry (..)
)
import Distribution.Simple.Program.Run
( programInvocation
, runProgramInvocation
)
import Distribution.Simple.Program.Types
( ConfiguredProgram
, programDefaultArgs
, programOverrideEnv
, programPath
, simpleProgram
)
import Distribution.Simple.Utils
( createDirectoryIfMissingVerbose
, dieWithException
, info
, notice
, withTempDirectory
, wrapText
)
import Distribution.Verbosity
( normal
)
import Distribution.Client.Compat.Prelude
import Prelude ()
import qualified Data.Map as M
import qualified Data.Set as S
import Distribution.Client.Errors
execCommand :: CommandUI (NixStyleFlags ())
execCommand =
CommandUI
{ commandName = "v2-exec"
, commandSynopsis = "Give a command access to the store."
, commandUsage = \pname ->
"Usage: " ++ pname ++ " v2-exec [FLAGS] [--] COMMAND [--] [ARGS]\n"
, commandDescription = Just $ \pname ->
wrapText $
"During development it is often useful to run build tasks and perform"
++ " one-off program executions to experiment with the behavior of build"
++ " tools. It is convenient to run these tools in the same way "
++ pname
++ " itself would. The `"
++ pname
++ " v2-exec` command provides a way to"
++ " do so.\n"
++ "\n"
++ "Compiler tools will be configured to see the same subset of the store"
++ " that builds would see. The PATH is modified to make all executables in"
++ " the dependency tree available (provided they have been built already)."
++ " Commands are also rewritten in the way cabal itself would. For"
++ " example, `"
++ pname
++ " v2-exec ghc` will consult the configuration"
++ " to choose an appropriate version of ghc and to include any"
++ " ghc-specific flags requested."
, commandNotes = Nothing
, commandOptions =
removeIgnoreProjectOption
. nixStyleOptions (const [])
, commandDefaultFlags = defaultNixStyleFlags ()
}
execAction :: NixStyleFlags () -> [String] -> GlobalFlags -> IO ()
execAction flags@NixStyleFlags{..} extraArgs globalFlags = do
baseCtx <- establishProjectBaseContext verbosity cliConfig OtherCommand
-- To set up the environment, we'd like to select the libraries in our
-- dependency tree that we've already built. So first we set up an install
-- plan, but we walk the dependency tree without first executing the plan.
buildCtx <-
runProjectPreBuildPhase
verbosity
baseCtx
(\plan -> return (plan, M.empty))
-- We use the build status below to decide what libraries to include in the
-- compiler environment, but we don't want to actually build anything. So we
-- pass mempty to indicate that nothing happened and we just want the current
-- status.
buildStatus <-
updatePostBuildProjectStatus
verbosity
(distDirLayout baseCtx)
(elaboratedPlanOriginal buildCtx)
(pkgsBuildStatus buildCtx)
mempty
-- Some dependencies may have executables. Let's put those on the PATH.
extraPaths <- pathAdditions verbosity baseCtx buildCtx
let programDb =
modifyProgramSearchPath
(map ProgramSearchPathDir extraPaths ++)
. pkgConfigCompilerProgs
. elaboratedShared
$ buildCtx
-- Now that we have the packages, set up the environment. We accomplish this
-- by creating an environment file that selects the databases and packages we
-- computed in the previous step, and setting an environment variable to
-- point at the file.
-- In case ghc is too old to support environment files,
-- we pass the same info as arguments
let compiler = pkgConfigCompiler $ elaboratedShared buildCtx
envFilesSupported = supportsPkgEnvFiles (getImplInfo compiler)
case extraArgs of
[] -> dieWithException verbosity SpecifyAnExecutable
exe : args -> do
(program, _) <- requireProgram verbosity (simpleProgram exe) programDb
let argOverrides =
argsEquivalentOfGhcEnvironmentFile
compiler
(distDirLayout baseCtx)
(elaboratedPlanOriginal buildCtx)
buildStatus
programIsConfiguredCompiler =
matchCompilerPath
(elaboratedShared buildCtx)
program
argOverrides' =
if envFilesSupported
|| not programIsConfiguredCompiler
then []
else argOverrides
( if envFilesSupported
then withTempEnvFile verbosity baseCtx buildCtx buildStatus
else \f -> f []
)
$ \envOverrides -> do
let program' =
withOverrides
envOverrides
argOverrides'
program
invocation = programInvocation program' args
dryRun =
buildSettingDryRun (buildSettings baseCtx)
|| buildSettingOnlyDownload (buildSettings baseCtx)
if dryRun
then notice verbosity "Running of executable suppressed by flag(s)"
else runProgramInvocation verbosity invocation
where
verbosity = fromFlagOrDefault normal (configVerbosity configFlags)
cliConfig =
commandLineFlagsToProjectConfig
globalFlags
flags
mempty -- ClientInstallFlags, not needed here
withOverrides env args program =
program
{ programOverrideEnv = programOverrideEnv program ++ env
, programDefaultArgs = programDefaultArgs program ++ args
}
matchCompilerPath :: ElaboratedSharedConfig -> ConfiguredProgram -> Bool
matchCompilerPath elaboratedShared program =
programPath program
`elem` (programPath <$> configuredCompilers)
where
configuredCompilers = configuredPrograms $ pkgConfigCompilerProgs elaboratedShared
-- | Execute an action with a temporary .ghc.environment file reflecting the
-- current environment. The action takes an environment containing the env
-- variable which points ghc to the file.
withTempEnvFile
:: Verbosity
-> ProjectBaseContext
-> ProjectBuildContext
-> PostBuildProjectStatus
-> ([(String, Maybe String)] -> IO a)
-> IO a
withTempEnvFile verbosity baseCtx buildCtx buildStatus action = do
createDirectoryIfMissingVerbose verbosity True (distTempDirectory (distDirLayout baseCtx))
withTempDirectory
verbosity
(distTempDirectory (distDirLayout baseCtx))
"environment."
( \tmpDir -> do
envOverrides <-
createPackageEnvironment
verbosity
tmpDir
(elaboratedPlanToExecute buildCtx)
(elaboratedShared buildCtx)
buildStatus
action envOverrides
)
pathAdditions :: Verbosity -> ProjectBaseContext -> ProjectBuildContext -> IO [FilePath]
pathAdditions verbosity ProjectBaseContext{..} ProjectBuildContext{..} = do
info verbosity . unlines $
"Including the following directories in PATH:"
: paths
return paths
where
paths =
S.toList $
binDirectories distDirLayout elaboratedShared elaboratedPlanToExecute
binDirectories
:: DistDirLayout
-> ElaboratedSharedConfig
-> ElaboratedInstallPlan
-> Set FilePath
binDirectories layout config = fromElaboratedInstallPlan
where
fromElaboratedInstallPlan = fromGraph . toGraph
fromGraph = foldMap fromPlan
fromSrcPkg = S.fromList . Planning.binDirectories layout config
fromPlan (PreExisting _) = mempty
fromPlan (Configured pkg) = fromSrcPkg pkg
fromPlan (Installed pkg) = fromSrcPkg pkg