Skip to content

Commit 74bbd17

Browse files
committed
cabal-install: call Cabal in-library
This commit modifies the SetupWrapper mechanism, adding a new way of building a package: directly calling Cabal library functions (e.g. 'build', 'configure' etc). This currently requires a bit of GADT trickery to accomodate the fact that configure returns a LocalBuildInfo which must then be passed to subsequent phases, while with the old Setup interface everything returns IO () and communication is done through the filesystem (the local build info file). To handle 'build-type: Hooks', this commit introduces the hooks-exe package, which contains: - the hooks-exe library, used to compile a set of SetupHooks into an external executable, - the hooks-cli library, which is used by cabal-install to communicate with an external hooks executable. This package depends on the new `CommunicationHandle` functionality from haskell/process#308.
1 parent c3d1c7a commit 74bbd17

File tree

61 files changed

+2838
-1039
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2838
-1039
lines changed

Cabal/src/Distribution/Simple.hs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ defaultMainWithSetupHooksArgs setupHooks =
153153
, hscolourHook = setup_hscolourHook
154154
}
155155
where
156+
preBuildHook =
157+
case SetupHooks.preBuildComponentRules (SetupHooks.buildHooks setupHooks) of
158+
Nothing -> const $ return []
159+
Just pbcRules -> \pbci -> runPreBuildHooks pbci pbcRules
160+
postBuildHook =
161+
case SetupHooks.postBuildComponentHook (SetupHooks.buildHooks setupHooks) of
162+
Nothing -> const $ return ()
163+
Just hk -> hk
164+
156165
setup_confHook
157166
:: (GenericPackageDescription, HookedBuildInfo)
158167
-> ConfigFlags
@@ -168,12 +177,13 @@ defaultMainWithSetupHooksArgs setupHooks =
168177
-> BuildFlags
169178
-> IO ()
170179
setup_buildHook pkg_descr lbi hooks flags =
171-
build_setupHooks
172-
(SetupHooks.buildHooks setupHooks)
173-
pkg_descr
174-
lbi
175-
flags
176-
(allSuffixHandlers hooks)
180+
void $
181+
build_setupHooks
182+
(preBuildHook, postBuildHook)
183+
pkg_descr
184+
lbi
185+
flags
186+
(allSuffixHandlers hooks)
177187

178188
setup_copyHook
179189
:: PackageDescription
@@ -207,7 +217,7 @@ defaultMainWithSetupHooksArgs setupHooks =
207217
-> IO ()
208218
setup_replHook pkg_descr lbi hooks flags args =
209219
repl_setupHooks
210-
(SetupHooks.buildHooks setupHooks)
220+
preBuildHook
211221
pkg_descr
212222
lbi
213223
flags
@@ -221,12 +231,13 @@ defaultMainWithSetupHooksArgs setupHooks =
221231
-> HaddockFlags
222232
-> IO ()
223233
setup_haddockHook pkg_descr lbi hooks flags =
224-
haddock_setupHooks
225-
(SetupHooks.buildHooks setupHooks)
226-
pkg_descr
227-
lbi
228-
(allSuffixHandlers hooks)
229-
flags
234+
void $
235+
haddock_setupHooks
236+
preBuildHook
237+
pkg_descr
238+
lbi
239+
(allSuffixHandlers hooks)
240+
flags
230241

231242
setup_hscolourHook
232243
:: PackageDescription
@@ -236,7 +247,7 @@ defaultMainWithSetupHooksArgs setupHooks =
236247
-> IO ()
237248
setup_hscolourHook pkg_descr lbi hooks flags =
238249
hscolour_setupHooks
239-
(SetupHooks.buildHooks setupHooks)
250+
preBuildHook
240251
pkg_descr
241252
lbi
242253
(allSuffixHandlers hooks)

Cabal/src/Distribution/Simple/Build.hs

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ module Distribution.Simple.Build
2525
( -- * Build
2626
build
2727
, build_setupHooks
28+
, buildComponent
29+
, runPostBuildHooks
2830

2931
-- * Repl
3032
, repl
@@ -33,6 +35,7 @@ module Distribution.Simple.Build
3335

3436
-- * Build preparation
3537
, preBuildComponent
38+
, runPreBuildHooks
3639
, AutogenFile (..)
3740
, AutogenFileContents
3841
, writeBuiltinAutogenFiles
@@ -91,6 +94,7 @@ import Distribution.Simple.BuildPaths
9194
import Distribution.Simple.BuildTarget
9295
import Distribution.Simple.BuildToolDepends
9396
import Distribution.Simple.Configure
97+
import Distribution.Simple.Errors
9498
import Distribution.Simple.Flag
9599
import Distribution.Simple.LocalBuildInfo
96100
import Distribution.Simple.PreProcess
@@ -104,9 +108,8 @@ import Distribution.Simple.Setup.Common
104108
import Distribution.Simple.Setup.Config
105109
import Distribution.Simple.Setup.Repl
106110
import Distribution.Simple.SetupHooks.Internal
107-
( BuildHooks (..)
108-
, BuildingWhat (..)
109-
, noBuildHooks
111+
( BuildingWhat (..)
112+
, buildingWhatVerbosity
110113
)
111114
import qualified Distribution.Simple.SetupHooks.Internal as SetupHooks
112115
import qualified Distribution.Simple.SetupHooks.Rule as SetupHooks
@@ -126,7 +129,6 @@ import Distribution.Compat.Graph (IsNode (..))
126129
import Control.Monad
127130
import qualified Data.ByteString.Lazy as LBS
128131
import qualified Data.Map as Map
129-
import Distribution.Simple.Errors
130132
import System.Directory (doesFileExist, removeFile)
131133
import System.FilePath (takeDirectory)
132134

@@ -143,10 +145,16 @@ build
143145
-> [PPSuffixHandler]
144146
-- ^ preprocessors to run before compiling
145147
-> IO ()
146-
build = build_setupHooks noBuildHooks
148+
build pkg lbi flags suffixHandlers =
149+
void $ build_setupHooks noHooks pkg lbi flags suffixHandlers
150+
where
151+
noHooks = (const $ return [], const $ return ())
147152

148153
build_setupHooks
149-
:: BuildHooks
154+
:: ( SetupHooks.PreBuildComponentInputs -> IO [SetupHooks.MonitorFilePath]
155+
, SetupHooks.PostBuildComponentInputs -> IO ()
156+
)
157+
-- ^ build hooks
150158
-> PackageDescription
151159
-- ^ Mostly information from the .cabal file
152160
-> LocalBuildInfo
@@ -155,13 +163,15 @@ build_setupHooks
155163
-- ^ Flags that the user passed to build
156164
-> [PPSuffixHandler]
157165
-- ^ preprocessors to run before compiling
158-
-> IO ()
166+
-> IO [SetupHooks.MonitorFilePath]
159167
build_setupHooks
160-
(BuildHooks{preBuildComponentRules = mbPbcRules, postBuildComponentHook = mbPostBuild})
168+
(preBuildHook, postBuildHook)
161169
pkg_descr
162170
lbi
163171
flags
164172
suffixHandlers = do
173+
let verbosity = fromFlag $ buildVerbosity flags
174+
distPref = fromFlag $ buildDistPref flags
165175
checkSemaphoreSupport verbosity (compiler lbi) flags
166176
targets <- readTargetInfos verbosity pkg_descr lbi (buildTargets flags)
167177
let componentsToBuild = neededTargetsInBuildOrder' pkg_descr lbi (map nodeKey targets)
@@ -188,7 +198,7 @@ build_setupHooks
188198
curDir <- absoluteWorkingDirLBI lbi
189199

190200
-- Now do the actual building
191-
(\f -> foldM_ f (installedPkgs lbi) componentsToBuild) $ \index target -> do
201+
(mons, _) <- (\f -> foldM f ([], installedPkgs lbi) componentsToBuild) $ \(monsAcc, index) target -> do
192202
let comp = targetComponent target
193203
clbi = targetCLBI target
194204
bi = componentBuildInfo comp
@@ -200,18 +210,8 @@ build_setupHooks
200210
, withPackageDB = withPackageDB lbi ++ [internalPackageDB]
201211
, installedPkgs = index
202212
}
203-
runPreBuildHooks :: LocalBuildInfo -> TargetInfo -> IO ()
204-
runPreBuildHooks lbi2 tgt =
205-
let inputs =
206-
SetupHooks.PreBuildComponentInputs
207-
{ SetupHooks.buildingWhat = BuildNormal flags
208-
, SetupHooks.localBuildInfo = lbi2
209-
, SetupHooks.targetInfo = tgt
210-
}
211-
in for_ mbPbcRules $ \pbcRules -> do
212-
(ruleFromId, _mons) <- SetupHooks.computeRules verbosity inputs pbcRules
213-
SetupHooks.executeRules verbosity lbi2 tgt ruleFromId
214-
preBuildComponent runPreBuildHooks verbosity lbi' target
213+
pbci = SetupHooks.PreBuildComponentInputs (BuildNormal flags) lbi' target
214+
mons <- preBuildComponent (preBuildHook pbci) verbosity lbi' target
215215
let numJobs = buildNumJobs flags
216216
par_strat <-
217217
toFlag <$> case buildUseSemaphore flags of
@@ -239,13 +239,40 @@ build_setupHooks
239239
, SetupHooks.localBuildInfo = lbi'
240240
, SetupHooks.targetInfo = target
241241
}
242-
for_ mbPostBuild ($ postBuildInputs)
243-
return (maybe index (Index.insert `flip` index) mb_ipi)
242+
postBuildHook postBuildInputs
243+
return (monsAcc ++ mons, maybe index (Index.insert `flip` index) mb_ipi)
244+
return mons
245+
246+
runPreBuildHooks
247+
:: SetupHooks.PreBuildComponentInputs
248+
-> SetupHooks.Rules SetupHooks.PreBuildComponentInputs
249+
-> IO [SetupHooks.MonitorFilePath]
250+
runPreBuildHooks
251+
pbci@SetupHooks.PreBuildComponentInputs
252+
{ SetupHooks.buildingWhat = what
253+
, SetupHooks.localBuildInfo = lbi
254+
, SetupHooks.targetInfo = tgt
255+
}
256+
pbRules = do
257+
let verbosity = buildingWhatVerbosity what
258+
(rules, monitors) <- SetupHooks.computeRules verbosity pbci pbRules
259+
SetupHooks.executeRules verbosity lbi tgt rules
260+
return monitors
244261

245-
return ()
246-
where
247-
distPref = fromFlag (buildDistPref flags)
248-
verbosity = fromFlag (buildVerbosity flags)
262+
runPostBuildHooks
263+
:: BuildFlags
264+
-> LocalBuildInfo
265+
-> TargetInfo
266+
-> (SetupHooks.PostBuildComponentInputs -> IO ())
267+
-> IO ()
268+
runPostBuildHooks flags lbi tgt postBuild =
269+
let inputs =
270+
SetupHooks.PostBuildComponentInputs
271+
{ SetupHooks.buildFlags = flags
272+
, SetupHooks.localBuildInfo = lbi
273+
, SetupHooks.targetInfo = tgt
274+
}
275+
in postBuild inputs
249276

250277
-- | Check for conditions that would prevent the build from succeeding.
251278
checkSemaphoreSupport
@@ -329,11 +356,11 @@ repl
329356
-- ^ preprocessors to run before compiling
330357
-> [String]
331358
-> IO ()
332-
repl = repl_setupHooks noBuildHooks
359+
repl = repl_setupHooks (const $ return [])
333360

334361
repl_setupHooks
335-
:: BuildHooks
336-
-- ^ build hook
362+
:: (SetupHooks.PreBuildComponentInputs -> IO [SetupHooks.MonitorFilePath])
363+
-- ^ pre-build hook
337364
-> PackageDescription
338365
-- ^ Mostly information from the .cabal file
339366
-> LocalBuildInfo
@@ -345,7 +372,7 @@ repl_setupHooks
345372
-> [String]
346373
-> IO ()
347374
repl_setupHooks
348-
(BuildHooks{preBuildComponentRules = mbPbcRules})
375+
preBuildHook
349376
pkg_descr
350377
lbi
351378
flags
@@ -388,25 +415,16 @@ repl_setupHooks
388415
(componentBuildInfo comp)
389416
(withPrograms lbi')
390417
}
391-
runPreBuildHooks :: LocalBuildInfo -> TargetInfo -> IO ()
392-
runPreBuildHooks lbi2 tgt =
393-
let inputs =
394-
SetupHooks.PreBuildComponentInputs
395-
{ SetupHooks.buildingWhat = BuildRepl flags
396-
, SetupHooks.localBuildInfo = lbi2
397-
, SetupHooks.targetInfo = tgt
398-
}
399-
in for_ mbPbcRules $ \pbcRules -> do
400-
(ruleFromId, _mons) <- SetupHooks.computeRules verbosity inputs pbcRules
401-
SetupHooks.executeRules verbosity lbi2 tgt ruleFromId
418+
pbci lbi' tgt = SetupHooks.PreBuildComponentInputs (BuildRepl flags) lbi' tgt
402419

403420
-- build any dependent components
404421
sequence_
405422
[ do
406423
let clbi = targetCLBI subtarget
407424
comp = targetComponent subtarget
408425
lbi' <- lbiForComponent comp lbi
409-
preBuildComponent runPreBuildHooks verbosity lbi' subtarget
426+
_monitors <-
427+
preBuildComponent (preBuildHook (pbci lbi' subtarget)) verbosity lbi' subtarget
410428
buildComponent
411429
(mempty{buildCommonFlags = mempty{setupVerbosity = toFlag verbosity}})
412430
NoFlag
@@ -423,7 +441,8 @@ repl_setupHooks
423441
let clbi = targetCLBI target
424442
comp = targetComponent target
425443
lbi' <- lbiForComponent comp lbi
426-
preBuildComponent runPreBuildHooks verbosity lbi' target
444+
_monitors <-
445+
preBuildComponent (preBuildHook (pbci lbi' target)) verbosity lbi' target
427446
replComponent flags verbosity pkg_descr lbi' suffixHandlers comp clbi distPref
428447

429448
-- | Start an interpreter without loading any package files.
@@ -1118,20 +1137,20 @@ componentInitialBuildSteps _distPref pkg_descr lbi clbi verbosity = do
11181137
-- | Creates the autogenerated files for a particular configured component,
11191138
-- and runs the pre-build hook.
11201139
preBuildComponent
1121-
:: (LocalBuildInfo -> TargetInfo -> IO ())
1140+
:: IO r
11221141
-- ^ pre-build hook
11231142
-> Verbosity
11241143
-> LocalBuildInfo
11251144
-- ^ Configuration information
11261145
-> TargetInfo
1127-
-> IO ()
1146+
-> IO r
11281147
preBuildComponent preBuildHook verbosity lbi tgt = do
11291148
let pkg_descr = localPkgDescr lbi
11301149
clbi = targetCLBI tgt
11311150
compBuildDir = interpretSymbolicPathLBI lbi $ componentBuildDir lbi clbi
11321151
createDirectoryIfMissingVerbose verbosity True compBuildDir
11331152
writeBuiltinAutogenFiles verbosity pkg_descr lbi clbi
1134-
preBuildHook lbi tgt
1153+
preBuildHook
11351154

11361155
-- | Generate and write to disk all built-in autogenerated files
11371156
-- for the specified component. These files will be put in the

Cabal/src/Distribution/Simple/Compiler.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,11 @@ profilingVanillaSupported comp = waySupported "p" comp
511511
profilingDynamicSupported :: Compiler -> Maybe Bool
512512
profilingDynamicSupported comp
513513
| GHC <- compilerFlavor comp
514-
-- Certainly not before 9.11, as prof+dyn was not implemented yet.
515-
, compilerVersion comp <= mkVersion [9, 11, 0]
516-
= Just False
517-
| otherwise
518-
= waySupported "p_dyn" comp
514+
, -- Certainly not before 9.11, as prof+dyn was not implemented yet.
515+
compilerVersion comp <= mkVersion [9, 11, 0] =
516+
Just False
517+
| otherwise =
518+
waySupported "p_dyn" comp
519519

520520
-- | Either profiling dynamic is definitely supported or we don't know (so assume
521521
-- it is)

0 commit comments

Comments
 (0)