Commit 3929d248 authored by Mikhail Glushenkov's avatar Mikhail Glushenkov
Browse files

Add a helper for checking whether a given executable exists.

parent d7bbef5b
......@@ -61,7 +61,7 @@ import Distribution.Simple.Program.Find
import Distribution.Simple.Program.Builtin
( builtinPrograms )
import Distribution.Simple.Utils
( die )
( die, doesExecutableExist )
import Distribution.Version
( Version, VersionRange, isAnyVersion, withinRange )
import Distribution.Text
......@@ -76,9 +76,6 @@ import Data.Maybe
import qualified Data.Map as Map
import Control.Monad
( join, foldM )
import System.Directory
( doesFileExist )
-- ------------------------------------------------------------
-- * Programs database
......@@ -300,7 +297,7 @@ configureProgram verbosity prog conf = do
Nothing -> programFindLocation prog verbosity (progSearchPath conf)
>>= return . fmap FoundOnSystem
Just path -> do
absolute <- doesFileExist path
absolute <- doesExecutableExist path
if absolute
then return (Just (UserSpecified path))
else findProgramOnSearchPath verbosity (progSearchPath conf) path
......
......@@ -31,11 +31,11 @@ module Distribution.Simple.Program.Find (
import Distribution.Verbosity
( Verbosity )
import Distribution.Simple.Utils
( debug )
( debug, doesExecutableExist )
import Distribution.System
( OS(..), buildOS )
import System.Directory
( findExecutable, doesFileExist, Permissions(..), getPermissions )
( findExecutable )
import Distribution.Compat.Environment
( getEnvironment )
import System.FilePath
......@@ -97,26 +97,20 @@ findProgramOnSearchPath verbosity searchpath prog = do
-- https://ghc.haskell.org/trac/ghc/ticket/2184
mExe <- findExecutable prog
case mExe of
Just exe -> do isExe <- checkExe exe
if isExe
Just exe -> do
exeExists <- doesExecutableExist exe
if exeExists
then return mExe
else return Nothing
_ -> return mExe
findFirstExe [] = return Nothing
findFirstExe (f:fs) = do
isExe <- checkExe f
isExe <- doesExecutableExist f
if isExe
then return (Just f)
else findFirstExe fs
checkExe f = do
exists <- doesFileExist f
if exists
then do perms <- getPermissions f
return (executable perms)
else return False
-- | Interpret a 'ProgramSearchPath' to construct a new @$PATH@ env var.
-- Note that this is close but not perfect because on Windows the search
-- algorithm looks at more than just the @%PATH%@.
......
......@@ -84,6 +84,7 @@ module Distribution.Simple.Utils (
installDirectoryContents,
-- * File permissions
doesExecutableExist,
setFileOrdinary,
setFileExecutable,
......@@ -980,6 +981,18 @@ installDirectoryContents verbosity srcDir destDir = do
srcFiles <- getDirectoryContentsRecursive srcDir
installOrdinaryFiles verbosity destDir [ (srcDir, f) | f <- srcFiles ]
-------------------
-- File permissions
-- | Like 'doesFileExist', but also checks that the file is executable.
doesExecutableExist :: FilePath -> IO Bool
doesExecutableExist f = do
exists <- doesFileExist f
if exists
then do perms <- getPermissions f
return (executable perms)
else return False
---------------------------------
-- Deprecated file copy functions
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment