{-# LANGUAGE CPP #-}
module Distribution.Compat.Process (
createProcess,
runInteractiveProcess,
rawSystem,
enableProcessJobs,
) where
import System.Exit (ExitCode (..))
import System.IO (Handle)
import System.Process (CreateProcess, ProcessHandle)
import qualified System.Process as Process
#if MIN_VERSION_process(1,2,0)
import System.Process (waitForProcess)
#endif
#if defined(mingw32_HOST_OS) && MIN_VERSION_process(1,6,9)
import System.IO.Unsafe (unsafePerformIO)
import System.Win32.Info.Version (dwMajorVersion, dwMinorVersion, getVersionEx)
#endif
#if defined(mingw32_HOST_OS) && MIN_VERSION_process(1,6,9)
{-# NOINLINE isWindows8OrLater #-}
isWindows8OrLater :: Bool
isWindows8OrLater = unsafePerformIO $ do
v <- getVersionEx
pure $ (dwMajorVersion v, dwMinorVersion v) >= (6, 2)
#endif
enableProcessJobs :: CreateProcess -> CreateProcess
#if defined(mingw32_HOST_OS) && MIN_VERSION_process(1,6,9)
enableProcessJobs cp = cp {Process.use_process_jobs = isWindows8OrLater}
#else
enableProcessJobs :: CreateProcess -> CreateProcess
enableProcessJobs CreateProcess
cp = CreateProcess
cp
#endif
createProcess :: CreateProcess
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
createProcess :: CreateProcess
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
createProcess = CreateProcess
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
Process.createProcess forall b c a. (b -> c) -> (a -> b) -> a -> c
. CreateProcess -> CreateProcess
enableProcessJobs
rawSystem :: String -> [String] -> IO ExitCode
rawSystem :: String -> [String] -> IO ExitCode
rawSystem String
cmd [String]
args = do
#if MIN_VERSION_process(1,2,0)
(Maybe Handle
_,Maybe Handle
_,Maybe Handle
_,ProcessHandle
p) <- CreateProcess
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
createProcess (String -> [String] -> CreateProcess
Process.proc String
cmd [String]
args) { delegate_ctlc :: Bool
Process.delegate_ctlc = Bool
True }
ProcessHandle -> IO ExitCode
waitForProcess ProcessHandle
p
#else
Process.rawSystem cmd args
#endif
runInteractiveProcess
:: FilePath
-> [String]
-> Maybe FilePath
-> Maybe [(String,String)]
-> IO (Handle,Handle,Handle,ProcessHandle)
runInteractiveProcess :: String
-> [String]
-> Maybe String
-> Maybe [(String, String)]
-> IO (Handle, Handle, Handle, ProcessHandle)
runInteractiveProcess String
cmd [String]
args Maybe String
mb_cwd Maybe [(String, String)]
mb_env = do
(Maybe Handle
mb_in, Maybe Handle
mb_out, Maybe Handle
mb_err, ProcessHandle
p) <-
CreateProcess
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
createProcess (String -> [String] -> CreateProcess
Process.proc String
cmd [String]
args)
{ std_in :: StdStream
Process.std_in = StdStream
Process.CreatePipe,
std_out :: StdStream
Process.std_out = StdStream
Process.CreatePipe,
std_err :: StdStream
Process.std_err = StdStream
Process.CreatePipe,
env :: Maybe [(String, String)]
Process.env = Maybe [(String, String)]
mb_env,
cwd :: Maybe String
Process.cwd = Maybe String
mb_cwd }
forall (m :: * -> *) a. Monad m => a -> m a
return (forall {b}. Maybe b -> b
fromJust Maybe Handle
mb_in, forall {b}. Maybe b -> b
fromJust Maybe Handle
mb_out, forall {b}. Maybe b -> b
fromJust Maybe Handle
mb_err, ProcessHandle
p)
where
fromJust :: Maybe b -> b
fromJust = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. HasCallStack => String -> a
error String
"runInteractiveProcess: fromJust") forall a. a -> a
id