c2a05e27e5d5d9ba5d6f868037d3678e89dff1e1
[hadrian.git] / src / Oracles / LookupInPath.hs
1 {-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving #-}
2 module Oracles.LookupInPath (
3 lookupInPath, lookupInPathOracle
4 ) where
5
6 import Base
7
8 newtype LookupInPath = LookupInPath String
9 deriving (Show, Typeable, Eq, Hashable, Binary, NFData)
10
11 -- | Fetches the absolute FilePath to a given FilePath from the
12 -- Oracle.
13 commandPath :: FilePath -> Action FilePath
14 commandPath = askOracle . LookupInPath
15
16 -- | Lookup a @command@ in @PATH@ environment.
17 lookupInPath :: FilePath -> Action FilePath
18 lookupInPath c
19 | c /= takeFileName c = return c
20 | otherwise = commandPath c
21
22 lookupInPathOracle :: Rules ()
23 lookupInPathOracle = do
24 o <- newCache $ \c -> do
25 envPaths <- wordsWhen (== ':') <$> getEnvWithDefault "" "PATH"
26 let candidates = map (-/- c) envPaths
27 -- this will crash if we do not find any valid candidate.
28 fullCommand <- head <$> filterM doesFileExist candidates
29 putOracle $ "Found '" ++ c ++ "' at " ++ "'" ++ fullCommand ++ "'"
30 return fullCommand
31 _ <- addOracle $ \(LookupInPath c) -> o c
32 return ()