1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE RankNTypes #-}
4 -----------------------------------------------------------------------------
7 -- Module : Distribution.Simple.Bench
8 -- Copyright : Johan Tibell 2011
11 -- Maintainer : cabal-devel@haskell.org
12 -- Portability : portable
14 -- This is the entry point into running the benchmarks in a built
15 -- package. It performs the \"@.\/setup bench@\" action. It runs
16 -- benchmarks designated in the package description.
17 module Distribution
.Simple
.Bench
21 import Distribution
.Compat
.Prelude
24 import qualified Distribution
.PackageDescription
as PD
25 import Distribution
.Pretty
26 import Distribution
.Simple
.BuildPaths
27 import Distribution
.Simple
.Compiler
28 import Distribution
.Simple
.Flag
(fromFlag
)
29 import Distribution
.Simple
.InstallDirs
30 import qualified Distribution
.Simple
.LocalBuildInfo
as LBI
31 import Distribution
.Simple
.Setup
.Benchmark
32 import Distribution
.Simple
.UserHooks
33 import Distribution
.Simple
.Utils
35 import Distribution
.Types
.UnqualComponentName
37 import Distribution
.Simple
.Errors
38 import System
.Directory
(doesFileExist)
39 import System
.FilePath ((<.>), (</>))
41 -- | Perform the \"@.\/setup bench@\" action.
44 -- ^ positional command-line arguments
45 -> PD
.PackageDescription
46 -- ^ information from the .cabal file
48 -- ^ information from the configure step
50 -- ^ flags sent to benchmark
52 bench args pkg_descr lbi flags
= do
53 let verbosity
= fromFlag
$ benchmarkVerbosity flags
55 pkgBenchmarks
= PD
.benchmarks pkg_descr
56 enabledBenchmarks
= map fst (LBI
.enabledBenchLBIs pkg_descr lbi
)
59 doBench
:: PD
.Benchmark
-> IO ExitCode
61 case PD
.benchmarkInterface bm
of
62 PD
.BenchmarkExeV10 _ _
-> do
63 let cmd
= LBI
.buildDir lbi
</> name
</> name
<.> exeExtension
(LBI
.hostPlatform lbi
)
65 map (benchOption pkg_descr lbi bm
) $
66 benchmarkOptions flags
67 -- Check that the benchmark executable exists.
68 exists
<- doesFileExist cmd
70 dieWithException verbosity
$
71 NoBenchMarkProgram cmd
73 notice verbosity
$ startMessage name
74 -- This will redirect the child process
75 -- stdout/stderr to the parent process.
76 exitcode
<- rawSystemExitCode verbosity cmd options
77 notice verbosity
$ finishMessage name exitcode
81 "No support for running "
85 ++ prettyShow
(PD
.benchmarkType bm
)
88 name
= unUnqualComponentName
$ PD
.benchmarkName bm
90 unless (PD
.hasBenchmarks pkg_descr
) $ do
91 notice verbosity
"Package has no benchmarks."
94 when (PD
.hasBenchmarks pkg_descr
&& null enabledBenchmarks
) $
95 dieWithException verbosity EnableBenchMark
97 bmsToRun
<- case benchmarkNames
of
98 [] -> return enabledBenchmarks
99 names
-> for names
$ \bmName
->
100 let benchmarkMap
= zip enabledNames enabledBenchmarks
101 enabledNames
= map PD
.benchmarkName enabledBenchmarks
102 allNames
= map PD
.benchmarkName pkgBenchmarks
103 in case lookup (mkUnqualComponentName bmName
) benchmarkMap
of
106 | mkUnqualComponentName bmName `
elem` allNames
->
107 dieWithException verbosity
$ BenchMarkNameDisabled bmName
108 |
otherwise -> dieWithException verbosity
$ NoBenchMark bmName
110 let totalBenchmarks
= length bmsToRun
111 notice verbosity
$ "Running " ++ show totalBenchmarks
++ " benchmarks..."
112 exitcodes
<- traverse doBench bmsToRun
113 let allOk
= totalBenchmarks
== length (filter (== ExitSuccess
) exitcodes
)
114 unless allOk
exitFailure
116 startMessage name
= "Benchmark " ++ name
++ ": RUNNING...\n"
117 finishMessage name exitcode
=
121 ++ ( case exitcode
of
122 ExitSuccess
-> "FINISH"
123 ExitFailure _
-> "ERROR"
126 -- TODO: This is abusing the notion of a 'PathTemplate'. The result isn't
127 -- necessarily a path.
129 :: PD
.PackageDescription
130 -> LBI
.LocalBuildInfo
134 benchOption pkg_descr lbi bm template
=
135 fromPathTemplate
$ substPathTemplate env template
138 initialPathTemplateEnv
139 (PD
.package pkg_descr
)
140 (LBI
.localUnitId lbi
)
141 (compilerInfo
$ LBI
.compiler lbi
)
142 (LBI
.hostPlatform lbi
)
143 ++ [(BenchmarkNameVar
, toPathTemplate
$ unUnqualComponentName
$ PD
.benchmarkName bm
)]