Clean up code, add comments.
[hadrian.git] / src / Oracles / PackageDeps.hs
1 {-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving #-}
2 module Oracles.PackageDeps (packageDeps, packageDepsOracle) where
3
4 import Base
5 import Package
6 import qualified Data.HashMap.Strict as Map
7
8 newtype PackageDepsKey = PackageDepsKey PackageName
9 deriving (Show, Typeable, Eq, Hashable, Binary, NFData)
10
11 -- packageDeps name is an action that given a package looks up its dependencies
12 -- in Base.packageDependencies file. The dependencies need to be computed by
13 -- scanning package cabal files (see Rules.Cabal).
14 packageDeps :: Package -> Action [PackageName]
15 packageDeps pkg = do
16 res <- askOracle . PackageDepsKey . pkgName $ pkg
17 return . fromMaybe [] $ res
18
19 -- Oracle for the package dependencies file
20 packageDepsOracle :: Rules ()
21 packageDepsOracle = do
22 deps <- newCache $ \_ -> do
23 putOracle $ "Reading package dependencies..."
24 contents <- readFileLines packageDependencies
25 return . Map.fromList
26 $ [ (head ps, tail ps) | line <- contents, let ps = words line ]
27 _ <- addOracle $ \(PackageDepsKey pkg) -> Map.lookup pkg <$> deps ()
28 return ()