{-# LANGUAGE DataKinds #-}
module Distribution.Client.Run (run, splitRunArgs)
where
import Distribution.Client.Compat.Prelude
import Prelude ()
import Distribution.Types.LocalBuildInfo (componentNameTargets')
import Distribution.Types.TargetInfo (targetCLBI)
import Distribution.Client.Utils (tryCanonicalizePath)
import Distribution.PackageDescription
( Benchmark (..)
, BuildInfo (buildable)
, Executable (..)
, PackageDescription (..)
, TestSuite (..)
)
import Distribution.Simple (PackageDBX (..))
import Distribution.Simple.Build (addInternalBuildTools)
import Distribution.Simple.BuildPaths (exeExtension)
import Distribution.Simple.Compiler (CompilerFlavor (..), compilerFlavor)
import Distribution.Simple.Flag (fromFlag)
import Distribution.Simple.LocalBuildInfo
( ComponentName (..)
, LocalBuildInfo (..)
, absoluteWorkingDirLBI
, buildDir
, depLibraryPaths
, interpretSymbolicPathLBI
, mbWorkDirLBI
)
import Distribution.Simple.Program.Db
import Distribution.Simple.Program.Find
import Distribution.Simple.Program.Run
import Distribution.Simple.Register (internalPackageDBPath)
import Distribution.Simple.Setup (ConfigFlags (..))
import Distribution.Simple.Utils
( addLibraryPath
, dieWithException
, notice
, rawSystemExitWithEnvCwd
, warn
)
import Distribution.System (Platform (..))
import Distribution.Types.UnqualComponentName
import qualified Distribution.Simple.GHCJS as GHCJS
import Distribution.Client.Errors
import Distribution.Utils.Path
splitRunArgs
:: Verbosity
-> LocalBuildInfo
-> [String]
-> IO (Executable, [String])
splitRunArgs :: Verbosity
-> LocalBuildInfo -> [[Char]] -> IO (Executable, [[Char]])
splitRunArgs Verbosity
verbosity LocalBuildInfo
lbi [[Char]]
args =
case Either [Char] (Bool, Executable, [[Char]])
whichExecutable of
Left [Char]
err -> do
Verbosity -> [Char] -> IO ()
warn Verbosity
verbosity ([Char] -> IO ()) -> Maybe [Char] -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
`traverse_` Maybe [Char]
maybeWarning
Verbosity -> CabalInstallException -> IO (Executable, [[Char]])
forall a1 a.
(HasCallStack, Show a1, Typeable a1,
Exception (VerboseException a1)) =>
Verbosity -> a1 -> IO a
dieWithException Verbosity
verbosity (CabalInstallException -> IO (Executable, [[Char]]))
-> CabalInstallException -> IO (Executable, [[Char]])
forall a b. (a -> b) -> a -> b
$ [Char] -> CabalInstallException
SplitRunArgs [Char]
err
Right (Bool
True, Executable
exe, [[Char]]
xs) -> (Executable, [[Char]]) -> IO (Executable, [[Char]])
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Executable
exe, [[Char]]
xs)
Right (Bool
False, Executable
exe, [[Char]]
xs) -> do
let addition :: [Char]
addition =
[Char]
" Interpreting all parameters to `run` as a parameter to"
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" the default executable."
Verbosity -> [Char] -> IO ()
warn Verbosity
verbosity ([Char] -> IO ()) -> Maybe [Char] -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
`traverse_` ([Char] -> [Char]) -> Maybe [Char] -> Maybe [Char]
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
addition) Maybe [Char]
maybeWarning
(Executable, [[Char]]) -> IO (Executable, [[Char]])
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Executable
exe, [[Char]]
xs)
where
pkg_descr :: PackageDescription
pkg_descr = LocalBuildInfo -> PackageDescription
localPkgDescr LocalBuildInfo
lbi
whichExecutable
:: Either
String
( Bool
, Executable
, [String]
)
whichExecutable :: Either [Char] (Bool, Executable, [[Char]])
whichExecutable = case ([Executable]
enabledExes, [[Char]]
args) of
([], [[Char]]
_) -> [Char] -> Either [Char] (Bool, Executable, [[Char]])
forall a b. a -> Either a b
Left [Char]
"Couldn't find any enabled executables."
([Executable
exe], []) -> (Bool, Executable, [[Char]])
-> Either [Char] (Bool, Executable, [[Char]])
forall a. a -> Either [Char] a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
False, Executable
exe, [])
([Executable
exe], ([Char]
x : [[Char]]
xs))
| [Char]
x [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== UnqualComponentName -> [Char]
unUnqualComponentName (Executable -> UnqualComponentName
exeName Executable
exe) -> (Bool, Executable, [[Char]])
-> Either [Char] (Bool, Executable, [[Char]])
forall a. a -> Either [Char] a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
True, Executable
exe, [[Char]]
xs)
| Bool
otherwise -> (Bool, Executable, [[Char]])
-> Either [Char] (Bool, Executable, [[Char]])
forall a. a -> Either [Char] a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
False, Executable
exe, [[Char]]
args)
([Executable]
_, []) ->
[Char] -> Either [Char] (Bool, Executable, [[Char]])
forall a b. a -> Either a b
Left ([Char] -> Either [Char] (Bool, Executable, [[Char]]))
-> [Char] -> Either [Char] (Bool, Executable, [[Char]])
forall a b. (a -> b) -> a -> b
$
[Char]
"This package contains multiple executables. "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"You must pass the executable name as the first argument "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"to 'cabal run'."
([Executable]
_, ([Char]
x : [[Char]]
xs)) ->
case (Executable -> Bool) -> [Executable] -> Maybe Executable
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\Executable
exe -> UnqualComponentName -> [Char]
unUnqualComponentName (Executable -> UnqualComponentName
exeName Executable
exe) [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
x) [Executable]
enabledExes of
Maybe Executable
Nothing -> [Char] -> Either [Char] (Bool, Executable, [[Char]])
forall a b. a -> Either a b
Left ([Char] -> Either [Char] (Bool, Executable, [[Char]]))
-> [Char] -> Either [Char] (Bool, Executable, [[Char]])
forall a b. (a -> b) -> a -> b
$ [Char]
"No executable named '" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
x [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"'."
Just Executable
exe -> (Bool, Executable, [[Char]])
-> Either [Char] (Bool, Executable, [[Char]])
forall a. a -> Either [Char] a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
True, Executable
exe, [[Char]]
xs)
where
enabledExes :: [Executable]
enabledExes = (Executable -> Bool) -> [Executable] -> [Executable]
forall a. (a -> Bool) -> [a] -> [a]
filter (BuildInfo -> Bool
buildable (BuildInfo -> Bool)
-> (Executable -> BuildInfo) -> Executable -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Executable -> BuildInfo
buildInfo) (PackageDescription -> [Executable]
executables PackageDescription
pkg_descr)
maybeWarning :: Maybe String
maybeWarning :: Maybe [Char]
maybeWarning = case [[Char]]
args of
[] -> Maybe [Char]
forall a. Maybe a
Nothing
([Char]
x : [[Char]]
_) -> UnqualComponentName
-> [(UnqualComponentName, [Char])] -> Maybe [Char]
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup ([Char] -> UnqualComponentName
mkUnqualComponentName [Char]
x) [(UnqualComponentName, [Char])]
components
where
components :: [(UnqualComponentName, String)]
components :: [(UnqualComponentName, [Char])]
components =
[ (UnqualComponentName
name, [Char]
"The executable '" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ UnqualComponentName -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow UnqualComponentName
name [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"' is disabled.")
| Executable
e <- PackageDescription -> [Executable]
executables PackageDescription
pkg_descr
, Bool -> Bool
not (Bool -> Bool) -> (Executable -> Bool) -> Executable -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildInfo -> Bool
buildable (BuildInfo -> Bool)
-> (Executable -> BuildInfo) -> Executable -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Executable -> BuildInfo
buildInfo (Executable -> Bool) -> Executable -> Bool
forall a b. (a -> b) -> a -> b
$ Executable
e
, let name :: UnqualComponentName
name = Executable -> UnqualComponentName
exeName Executable
e
]
[(UnqualComponentName, [Char])]
-> [(UnqualComponentName, [Char])]
-> [(UnqualComponentName, [Char])]
forall a. [a] -> [a] -> [a]
++ [ ( UnqualComponentName
name
, [Char]
"There is a test-suite '"
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ UnqualComponentName -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow UnqualComponentName
name
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"',"
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" but the `run` command is only for executables."
)
| TestSuite
t <- PackageDescription -> [TestSuite]
testSuites PackageDescription
pkg_descr
, let name :: UnqualComponentName
name = TestSuite -> UnqualComponentName
testName TestSuite
t
]
[(UnqualComponentName, [Char])]
-> [(UnqualComponentName, [Char])]
-> [(UnqualComponentName, [Char])]
forall a. [a] -> [a] -> [a]
++ [ ( UnqualComponentName
name
, [Char]
"There is a benchmark '"
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ UnqualComponentName -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow UnqualComponentName
name
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"',"
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" but the `run` command is only for executables."
)
| Benchmark
b <- PackageDescription -> [Benchmark]
benchmarks PackageDescription
pkg_descr
, let name :: UnqualComponentName
name = Benchmark -> UnqualComponentName
benchmarkName Benchmark
b
]
run :: Verbosity -> LocalBuildInfo -> Executable -> [String] -> IO ()
run :: Verbosity -> LocalBuildInfo -> Executable -> [[Char]] -> IO ()
run Verbosity
verbosity LocalBuildInfo
lbi Executable
exe [[Char]]
exeArgs = do
curDir <- LocalBuildInfo -> IO (AbsolutePath ('Dir Pkg))
absoluteWorkingDirLBI LocalBuildInfo
lbi
let distPref = Flag (SymbolicPath Pkg ('Dir Dist)) -> SymbolicPath Pkg ('Dir Dist)
forall a. WithCallStack (Flag a -> a)
fromFlag (Flag (SymbolicPath Pkg ('Dir Dist))
-> SymbolicPath Pkg ('Dir Dist))
-> Flag (SymbolicPath Pkg ('Dir Dist))
-> SymbolicPath Pkg ('Dir Dist)
forall a b. (a -> b) -> a -> b
$ ConfigFlags -> Flag (SymbolicPath Pkg ('Dir Dist))
configDistPref (ConfigFlags -> Flag (SymbolicPath Pkg ('Dir Dist)))
-> ConfigFlags -> Flag (SymbolicPath Pkg ('Dir Dist))
forall a b. (a -> b) -> a -> b
$ LocalBuildInfo -> ConfigFlags
configFlags LocalBuildInfo
lbi
buildPref = LocalBuildInfo -> SymbolicPath Pkg ('Dir Build)
buildDir LocalBuildInfo
lbi
pkg_descr = LocalBuildInfo -> PackageDescription
localPkgDescr LocalBuildInfo
lbi
i = LocalBuildInfo -> SymbolicPathX allowAbsolute Pkg to -> [Char]
forall (allowAbsolute :: AllowAbsolute) (to :: FileOrDir).
LocalBuildInfo -> SymbolicPathX allowAbsolute Pkg to -> [Char]
interpretSymbolicPathLBI LocalBuildInfo
lbi
mbWorkDir = LocalBuildInfo -> Maybe (SymbolicPath CWD ('Dir Pkg))
mbWorkDirLBI LocalBuildInfo
lbi
internalPkgDb = LocalBuildInfo
-> SymbolicPath Pkg ('Dir Dist) -> SymbolicPath Pkg ('Dir PkgDB)
internalPackageDBPath LocalBuildInfo
lbi SymbolicPath Pkg ('Dir Dist)
distPref
lbiForExe =
LocalBuildInfo
lbi
{ withPackageDB = withPackageDB lbi ++ [SpecificPackageDB internalPkgDb]
,
withPrograms =
addInternalBuildTools
curDir
pkg_descr
lbi
(buildInfo exe)
(withPrograms lbi)
}
(path, runArgs) <-
let exeName' = UnqualComponentName -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow (UnqualComponentName -> [Char]) -> UnqualComponentName -> [Char]
forall a b. (a -> b) -> a -> b
$ Executable -> UnqualComponentName
exeName Executable
exe
in case compilerFlavor (compiler lbiForExe) of
CompilerFlavor
GHCJS -> do
let ([Char]
script, [Char]
cmd, [[Char]]
cmdArgs) =
ProgramDb -> [Char] -> ([Char], [Char], [[Char]])
GHCJS.runCmd
(LocalBuildInfo -> ProgramDb
withPrograms LocalBuildInfo
lbiForExe)
(SymbolicPath Pkg ('Dir Build) -> [Char]
forall {allowAbsolute :: AllowAbsolute} {to :: FileOrDir}.
SymbolicPathX allowAbsolute Pkg to -> [Char]
i SymbolicPath Pkg ('Dir Build)
buildPref [Char] -> [Char] -> [Char]
forall p q r. PathLike p q r => p -> q -> r
</> [Char]
exeName' [Char] -> [Char] -> [Char]
forall p q r. PathLike p q r => p -> q -> r
</> [Char]
exeName')
script' <- [Char] -> IO [Char]
tryCanonicalizePath [Char]
script
return (cmd, cmdArgs ++ [script'])
CompilerFlavor
_ -> do
p <-
[Char] -> IO [Char]
tryCanonicalizePath ([Char] -> IO [Char]) -> [Char] -> IO [Char]
forall a b. (a -> b) -> a -> b
$
SymbolicPath Pkg ('Dir Build) -> [Char]
forall {allowAbsolute :: AllowAbsolute} {to :: FileOrDir}.
SymbolicPathX allowAbsolute Pkg to -> [Char]
i SymbolicPath Pkg ('Dir Build)
buildPref [Char] -> [Char] -> [Char]
forall p q r. PathLike p q r => p -> q -> r
</> [Char]
exeName' [Char] -> [Char] -> [Char]
forall p q r. PathLike p q r => p -> q -> r
</> ([Char]
exeName' [Char] -> [Char] -> [Char]
forall p. FileLike p => p -> [Char] -> p
<.> Platform -> [Char]
exeExtension (LocalBuildInfo -> Platform
hostPlatform LocalBuildInfo
lbiForExe))
return (p, [])
let progDb = LocalBuildInfo -> ProgramDb
withPrograms LocalBuildInfo
lbiForExe
pathVar = ProgramDb -> ProgramSearchPath
progSearchPath ProgramDb
progDb
envOverrides = ProgramDb -> [([Char], Maybe [Char])]
progOverrideEnv ProgramDb
progDb
newPath <- programSearchPathAsPATHVar pathVar
env <- getFullEnvironment ([("PATH", Just newPath)] ++ envOverrides)
env' <-
if withDynExe lbiForExe
then do
let (Platform _ os) = hostPlatform lbiForExe
clbi <- case componentNameTargets' pkg_descr lbiForExe (CExeName (exeName exe)) of
[TargetInfo
target] -> ComponentLocalBuildInfo -> IO ComponentLocalBuildInfo
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (TargetInfo -> ComponentLocalBuildInfo
targetCLBI TargetInfo
target)
[] -> Verbosity -> CabalInstallException -> IO ComponentLocalBuildInfo
forall a1 a.
(HasCallStack, Show a1, Typeable a1,
Exception (VerboseException a1)) =>
Verbosity -> a1 -> IO a
dieWithException Verbosity
verbosity CabalInstallException
CouldNotFindExecutable
[TargetInfo]
_ -> Verbosity -> CabalInstallException -> IO ComponentLocalBuildInfo
forall a1 a.
(HasCallStack, Show a1, Typeable a1,
Exception (VerboseException a1)) =>
Verbosity -> a1 -> IO a
dieWithException Verbosity
verbosity CabalInstallException
FoundMultipleMatchingExes
paths <- depLibraryPaths True False lbiForExe clbi
return (addLibraryPath os paths env)
else return env
notice verbosity $ "Running " ++ prettyShow (exeName exe) ++ "..."
rawSystemExitWithEnvCwd verbosity mbWorkDir path (runArgs ++ exeArgs) env'