Refactor dependency oracles
[ghc.git] / src / Oracles / LookupInPath.hs
1 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
2 module Oracles.LookupInPath (lookupInPath, lookupInPathOracle) where
3
4 import System.Directory
5
6 import Base
7
8 newtype LookupInPath = LookupInPath String
9 deriving (Binary, Eq, Hashable, NFData, Show, Typeable)
10
11 -- | Lookup an executable in @PATH@.
12 lookupInPath :: FilePath -> Action FilePath
13 lookupInPath name
14 | name == takeFileName name = askOracle $ LookupInPath name
15 | otherwise = return name
16
17 lookupInPathOracle :: Rules ()
18 lookupInPathOracle = void $
19 addOracle $ \(LookupInPath name) -> do
20 maybePath <- liftIO $ findExecutable name
21 path <- case maybePath of
22 Just value -> return $ unifyPath value
23 Nothing -> error $ "Cannot find executable '" ++ name ++ "'."
24 putLoud $ "Executable found: " ++ name ++ " => " ++ path
25 return path