{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}

-----------------------------------------------------------------------------

-- |
-- Module      :  Distribution.Simple.Bench
-- Copyright   :  Johan Tibell 2011
-- License     :  BSD3
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- This is the entry point into running the benchmarks in a built
-- package. It performs the \"@.\/setup bench@\" action. It runs
-- benchmarks designated in the package description.
module Distribution.Simple.Bench
  ( bench
  ) where

import Distribution.Compat.Prelude
import Prelude ()

import Distribution.Compat.Environment
import qualified Distribution.PackageDescription as PD
import Distribution.Pretty
import Distribution.Simple.Build (addInternalBuildTools)
import Distribution.Simple.BuildPaths
import Distribution.Simple.Compiler
import Distribution.Simple.Errors
import Distribution.Simple.InstallDirs
import qualified Distribution.Simple.LocalBuildInfo as LBI
import Distribution.Simple.Program.Db
import Distribution.Simple.Program.Find
import Distribution.Simple.Program.Run
import Distribution.Simple.Setup.Benchmark
import Distribution.Simple.Setup.Common
import Distribution.Simple.UserHooks
import Distribution.Simple.Utils
import Distribution.System (Platform (Platform))
import Distribution.Types.Benchmark (Benchmark (benchmarkBuildInfo))
import Distribution.Types.UnqualComponentName
import Distribution.Utils.Path

import System.Directory (doesFileExist)

-- | Perform the \"@.\/setup bench@\" action.
bench
  :: Args
  -- ^ positional command-line arguments
  -> PD.PackageDescription
  -- ^ information from the .cabal file
  -> LBI.LocalBuildInfo
  -- ^ information from the configure step
  -> BenchmarkFlags
  -- ^ flags sent to benchmark
  -> IO ()
bench :: Args
-> PackageDescription -> LocalBuildInfo -> BenchmarkFlags -> IO ()
bench Args
args PackageDescription
pkg_descr LocalBuildInfo
lbi BenchmarkFlags
flags = do
  let verbosity :: Verbosity
verbosity = Flag Verbosity -> Verbosity
forall a. WithCallStack (Flag a -> a)
fromFlag (Flag Verbosity -> Verbosity) -> Flag Verbosity -> Verbosity
forall a b. (a -> b) -> a -> b
$ BenchmarkFlags -> Flag Verbosity
benchmarkVerbosity BenchmarkFlags
flags
      benchmarkNames :: Args
benchmarkNames = Args
args
      pkgBenchmarks :: [Benchmark]
pkgBenchmarks = PackageDescription -> [Benchmark]
PD.benchmarks PackageDescription
pkg_descr
      enabledBenchmarks :: [(Benchmark, ComponentLocalBuildInfo)]
enabledBenchmarks = PackageDescription
-> LocalBuildInfo -> [(Benchmark, ComponentLocalBuildInfo)]
LBI.enabledBenchLBIs PackageDescription
pkg_descr LocalBuildInfo
lbi
      mbWorkDir :: Maybe (SymbolicPath CWD ('Dir Pkg))
mbWorkDir = Flag (SymbolicPath CWD ('Dir Pkg))
-> Maybe (SymbolicPath CWD ('Dir Pkg))
forall a. Flag a -> Maybe a
flagToMaybe (Flag (SymbolicPath CWD ('Dir Pkg))
 -> Maybe (SymbolicPath CWD ('Dir Pkg)))
-> Flag (SymbolicPath CWD ('Dir Pkg))
-> Maybe (SymbolicPath CWD ('Dir Pkg))
forall a b. (a -> b) -> a -> b
$ BenchmarkFlags -> Flag (SymbolicPath CWD ('Dir Pkg))
benchmarkWorkingDir BenchmarkFlags
flags
      i :: SymbolicPathX allowAbsolute Pkg to -> FilePath
i = Maybe (SymbolicPath CWD ('Dir Pkg))
-> SymbolicPathX allowAbsolute Pkg to -> FilePath
forall from (allowAbsolute :: AllowAbsolute) (to :: FileOrDir).
Maybe (SymbolicPath CWD ('Dir from))
-> SymbolicPathX allowAbsolute from to -> FilePath
interpretSymbolicPath Maybe (SymbolicPath CWD ('Dir Pkg))
mbWorkDir -- See Note [Symbolic paths] in Distribution.Utils.Path

      -- Run the benchmark
      doBench :: (PD.Benchmark, LBI.ComponentLocalBuildInfo) -> IO ExitCode
      doBench :: (Benchmark, ComponentLocalBuildInfo) -> IO ExitCode
doBench (Benchmark
bm, ComponentLocalBuildInfo
clbi) = do
        let lbiForBench :: LocalBuildInfo
lbiForBench =
              LocalBuildInfo
lbi
                { -- Include any build-tool-depends on build tools internal to the current package.
                  LBI.withPrograms =
                    addInternalBuildTools
                      pkg_descr
                      lbi
                      (benchmarkBuildInfo bm)
                      (LBI.withPrograms lbi)
                }
        case Benchmark -> BenchmarkInterface
PD.benchmarkInterface Benchmark
bm of
          PD.BenchmarkExeV10 Version
_ RelativePath Source 'File
_ -> do
            let cmd :: FilePath
cmd = SymbolicPathX 'AllowAbsolute Pkg (ZonkAny 0) -> FilePath
forall {allowAbsolute :: AllowAbsolute} {to :: FileOrDir}.
SymbolicPathX allowAbsolute Pkg to -> FilePath
i (SymbolicPathX 'AllowAbsolute Pkg (ZonkAny 0) -> FilePath)
-> SymbolicPathX 'AllowAbsolute Pkg (ZonkAny 0) -> FilePath
forall a b. (a -> b) -> a -> b
$ LocalBuildInfo -> SymbolicPath Pkg ('Dir Build)
LBI.buildDir LocalBuildInfo
lbiForBench SymbolicPath Pkg ('Dir Build)
-> RelativePath Build (ZonkAny 0)
-> SymbolicPathX 'AllowAbsolute Pkg (ZonkAny 0)
forall p q r. PathLike p q r => p -> q -> r
</> FilePath -> RelativePath Build (ZonkAny 0)
forall from (to :: FileOrDir).
HasCallStack =>
FilePath -> RelativePath from to
makeRelativePathEx (FilePath
name FilePath -> FilePath -> FilePath
forall p q r. PathLike p q r => p -> q -> r
</> FilePath
name FilePath -> FilePath -> FilePath
forall p. FileLike p => p -> FilePath -> p
<.> Platform -> FilePath
exeExtension (LocalBuildInfo -> Platform
LBI.hostPlatform LocalBuildInfo
lbi))
                options :: Args
options =
                  (PathTemplate -> FilePath) -> [PathTemplate] -> Args
forall a b. (a -> b) -> [a] -> [b]
map (PackageDescription
-> LocalBuildInfo -> Benchmark -> PathTemplate -> FilePath
benchOption PackageDescription
pkg_descr LocalBuildInfo
lbiForBench Benchmark
bm) ([PathTemplate] -> Args) -> [PathTemplate] -> Args
forall a b. (a -> b) -> a -> b
$
                    BenchmarkFlags -> [PathTemplate]
benchmarkOptions BenchmarkFlags
flags
            -- Check that the benchmark executable exists.
            exists <- FilePath -> IO Bool
doesFileExist FilePath
cmd
            unless exists $
              dieWithException verbosity $
                NoBenchMarkProgram cmd

            existingEnv <- getEnvironment

            -- Compute the appropriate environment for running the benchmark
            let progDb = LocalBuildInfo -> ProgramDb
LBI.withPrograms LocalBuildInfo
lbiForBench
                pathVar = ProgramDb -> ProgramSearchPath
progSearchPath ProgramDb
progDb
                envOverrides = ProgramDb -> [(FilePath, Maybe FilePath)]
progOverrideEnv ProgramDb
progDb
            newPath <- programSearchPathAsPATHVar pathVar
            overrideEnv <- fromMaybe [] <$> getEffectiveEnvironment ([("PATH", Just newPath)] ++ envOverrides)
            let shellEnv = [(FilePath, FilePath)]
overrideEnv [(FilePath, FilePath)]
-> [(FilePath, FilePath)] -> [(FilePath, FilePath)]
forall a. [a] -> [a] -> [a]
++ [(FilePath, FilePath)]
existingEnv

            -- Add (DY)LD_LIBRARY_PATH if needed
            shellEnv' <-
              if LBI.withDynExe lbiForBench
                then do
                  let (Platform _ os) = LBI.hostPlatform lbiForBench
                  paths <- LBI.depLibraryPaths True False lbiForBench clbi
                  return (addLibraryPath os paths shellEnv)
                else return shellEnv

            notice verbosity $ startMessage name
            -- This will redirect the child process
            -- stdout/stderr to the parent process.
            exitcode <- rawSystemExitCode verbosity mbWorkDir cmd options (Just shellEnv')
            notice verbosity $ finishMessage name exitcode
            return exitcode
          BenchmarkInterface
_ -> do
            Verbosity -> FilePath -> IO ()
notice Verbosity
verbosity (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$
              FilePath
"No support for running "
                FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"benchmark "
                FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
name
                FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
" of type: "
                FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ BenchmarkType -> FilePath
forall a. Pretty a => a -> FilePath
prettyShow (Benchmark -> BenchmarkType
PD.benchmarkType Benchmark
bm)
            IO ExitCode
forall a. IO a
exitFailure
        where
          name :: FilePath
name = UnqualComponentName -> FilePath
unUnqualComponentName (UnqualComponentName -> FilePath)
-> UnqualComponentName -> FilePath
forall a b. (a -> b) -> a -> b
$ Benchmark -> UnqualComponentName
PD.benchmarkName Benchmark
bm

  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (PackageDescription -> Bool
PD.hasBenchmarks PackageDescription
pkg_descr) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
    Verbosity -> FilePath -> IO ()
notice Verbosity
verbosity FilePath
"Package has no benchmarks."
    IO ()
forall a. IO a
exitSuccess

  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (PackageDescription -> Bool
PD.hasBenchmarks PackageDescription
pkg_descr Bool -> Bool -> Bool
&& [(Benchmark, ComponentLocalBuildInfo)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Benchmark, ComponentLocalBuildInfo)]
enabledBenchmarks) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
    Verbosity -> CabalException -> IO ()
forall a1 a.
(HasCallStack, Show a1, Typeable a1,
 Exception (VerboseException a1)) =>
Verbosity -> a1 -> IO a
dieWithException Verbosity
verbosity CabalException
EnableBenchMark

  bmsToRun <- case Args
benchmarkNames of
    [] -> [(Benchmark, ComponentLocalBuildInfo)]
-> IO [(Benchmark, ComponentLocalBuildInfo)]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return [(Benchmark, ComponentLocalBuildInfo)]
enabledBenchmarks
    Args
names -> Args
-> (FilePath -> IO (Benchmark, ComponentLocalBuildInfo))
-> IO [(Benchmark, ComponentLocalBuildInfo)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for Args
names ((FilePath -> IO (Benchmark, ComponentLocalBuildInfo))
 -> IO [(Benchmark, ComponentLocalBuildInfo)])
-> (FilePath -> IO (Benchmark, ComponentLocalBuildInfo))
-> IO [(Benchmark, ComponentLocalBuildInfo)]
forall a b. (a -> b) -> a -> b
$ \FilePath
bmName ->
      let benchmarkMap :: [(UnqualComponentName, (Benchmark, ComponentLocalBuildInfo))]
benchmarkMap = [UnqualComponentName]
-> [(Benchmark, ComponentLocalBuildInfo)]
-> [(UnqualComponentName, (Benchmark, ComponentLocalBuildInfo))]
forall a b. [a] -> [b] -> [(a, b)]
zip [UnqualComponentName]
enabledNames [(Benchmark, ComponentLocalBuildInfo)]
enabledBenchmarks
          enabledNames :: [UnqualComponentName]
enabledNames = ((Benchmark, ComponentLocalBuildInfo) -> UnqualComponentName)
-> [(Benchmark, ComponentLocalBuildInfo)] -> [UnqualComponentName]
forall a b. (a -> b) -> [a] -> [b]
map (Benchmark -> UnqualComponentName
PD.benchmarkName (Benchmark -> UnqualComponentName)
-> ((Benchmark, ComponentLocalBuildInfo) -> Benchmark)
-> (Benchmark, ComponentLocalBuildInfo)
-> UnqualComponentName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Benchmark, ComponentLocalBuildInfo) -> Benchmark
forall a b. (a, b) -> a
fst) [(Benchmark, ComponentLocalBuildInfo)]
enabledBenchmarks
          allNames :: [UnqualComponentName]
allNames = (Benchmark -> UnqualComponentName)
-> [Benchmark] -> [UnqualComponentName]
forall a b. (a -> b) -> [a] -> [b]
map Benchmark -> UnqualComponentName
PD.benchmarkName [Benchmark]
pkgBenchmarks
       in case UnqualComponentName
-> [(UnqualComponentName, (Benchmark, ComponentLocalBuildInfo))]
-> Maybe (Benchmark, ComponentLocalBuildInfo)
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup (FilePath -> UnqualComponentName
mkUnqualComponentName FilePath
bmName) [(UnqualComponentName, (Benchmark, ComponentLocalBuildInfo))]
benchmarkMap of
            Just (Benchmark, ComponentLocalBuildInfo)
t -> (Benchmark, ComponentLocalBuildInfo)
-> IO (Benchmark, ComponentLocalBuildInfo)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Benchmark, ComponentLocalBuildInfo)
t
            Maybe (Benchmark, ComponentLocalBuildInfo)
_
              | FilePath -> UnqualComponentName
mkUnqualComponentName FilePath
bmName UnqualComponentName -> [UnqualComponentName] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [UnqualComponentName]
allNames ->
                  Verbosity
-> CabalException -> IO (Benchmark, ComponentLocalBuildInfo)
forall a1 a.
(HasCallStack, Show a1, Typeable a1,
 Exception (VerboseException a1)) =>
Verbosity -> a1 -> IO a
dieWithException Verbosity
verbosity (CabalException -> IO (Benchmark, ComponentLocalBuildInfo))
-> CabalException -> IO (Benchmark, ComponentLocalBuildInfo)
forall a b. (a -> b) -> a -> b
$ FilePath -> CabalException
BenchMarkNameDisabled FilePath
bmName
              | Bool
otherwise -> Verbosity
-> CabalException -> IO (Benchmark, ComponentLocalBuildInfo)
forall a1 a.
(HasCallStack, Show a1, Typeable a1,
 Exception (VerboseException a1)) =>
Verbosity -> a1 -> IO a
dieWithException Verbosity
verbosity (CabalException -> IO (Benchmark, ComponentLocalBuildInfo))
-> CabalException -> IO (Benchmark, ComponentLocalBuildInfo)
forall a b. (a -> b) -> a -> b
$ FilePath -> CabalException
NoBenchMark FilePath
bmName

  let totalBenchmarks = [(Benchmark, ComponentLocalBuildInfo)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Benchmark, ComponentLocalBuildInfo)]
bmsToRun
  notice verbosity $ "Running " ++ show totalBenchmarks ++ " benchmarks..."
  exitcodes <- traverse doBench bmsToRun

  let allOk = Int
totalBenchmarks Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [ExitCode] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ((ExitCode -> Bool) -> [ExitCode] -> [ExitCode]
forall a. (a -> Bool) -> [a] -> [a]
filter (ExitCode -> ExitCode -> Bool
forall a. Eq a => a -> a -> Bool
== ExitCode
ExitSuccess) [ExitCode]
exitcodes)
  unless allOk exitFailure
  where
    startMessage :: FilePath -> FilePath
startMessage FilePath
name = FilePath
"Benchmark " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
name FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
": RUNNING...\n"
    finishMessage :: FilePath -> ExitCode -> FilePath
finishMessage FilePath
name ExitCode
exitcode =
      FilePath
"Benchmark "
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
name
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
": "
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ ( case ExitCode
exitcode of
              ExitCode
ExitSuccess -> FilePath
"FINISH"
              ExitFailure Int
_ -> FilePath
"ERROR"
           )

-- TODO: This is abusing the notion of a 'PathTemplate'.  The result isn't
-- necessarily a path.
benchOption
  :: PD.PackageDescription
  -> LBI.LocalBuildInfo
  -> PD.Benchmark
  -> PathTemplate
  -> String
benchOption :: PackageDescription
-> LocalBuildInfo -> Benchmark -> PathTemplate -> FilePath
benchOption PackageDescription
pkg_descr LocalBuildInfo
lbi Benchmark
bm PathTemplate
template =
  PathTemplate -> FilePath
fromPathTemplate (PathTemplate -> FilePath) -> PathTemplate -> FilePath
forall a b. (a -> b) -> a -> b
$ PathTemplateEnv -> PathTemplate -> PathTemplate
substPathTemplate PathTemplateEnv
env PathTemplate
template
  where
    env :: PathTemplateEnv
env =
      PackageIdentifier
-> UnitId -> CompilerInfo -> Platform -> PathTemplateEnv
initialPathTemplateEnv
        (PackageDescription -> PackageIdentifier
PD.package PackageDescription
pkg_descr)
        (LocalBuildInfo -> UnitId
LBI.localUnitId LocalBuildInfo
lbi)
        (Compiler -> CompilerInfo
compilerInfo (Compiler -> CompilerInfo) -> Compiler -> CompilerInfo
forall a b. (a -> b) -> a -> b
$ LocalBuildInfo -> Compiler
LBI.compiler LocalBuildInfo
lbi)
        (LocalBuildInfo -> Platform
LBI.hostPlatform LocalBuildInfo
lbi)
        PathTemplateEnv -> PathTemplateEnv -> PathTemplateEnv
forall a. [a] -> [a] -> [a]
++ [(PathTemplateVariable
BenchmarkNameVar, FilePath -> PathTemplate
toPathTemplate (FilePath -> PathTemplate) -> FilePath -> PathTemplate
forall a b. (a -> b) -> a -> b
$ UnqualComponentName -> FilePath
unUnqualComponentName (UnqualComponentName -> FilePath)
-> UnqualComponentName -> FilePath
forall a b. (a -> b) -> a -> b
$ Benchmark -> UnqualComponentName
PD.benchmarkName Benchmark
bm)]