f9d17e93d8de80ed5921482863b8a591c8a3c391
[ghc.git] / hadrian / src / Rules / Dependencies.hs
1 module Rules.Dependencies (buildPackageDependencies) where
2
3 import Data.Bifunctor
4 import Data.Function
5
6 import Base
7 import Context
8 import Expression
9 import Oracles.ModuleFiles
10 import Rules.Generate
11 import Target
12 import Utilities
13
14 buildPackageDependencies :: [(Resource, Int)] -> Context -> Rules ()
15 buildPackageDependencies rs context@Context {..} =
16 "//" ++ contextDir context -/- ".dependencies" %> \deps -> do
17 srcs <- hsSources context
18 need srcs
19 orderOnly =<< interpretInContext context generatedDependencies
20 let mk = deps <.> "mk"
21 if null srcs
22 then writeFile' mk ""
23 else buildWithResources rs $
24 target context (Ghc FindHsDependencies stage) srcs [mk]
25 removeFile $ mk <.> "bak"
26 mkDeps <- liftIO $ readFile mk
27 writeFileChanged deps . unlines
28 . map (\(src, deps) -> unwords $ src : deps)
29 . map (bimap unifyPath (map unifyPath))
30 . map (bimap head concat . unzip)
31 . groupBy ((==) `on` fst)
32 . sortBy (compare `on` fst)
33 $ parseMakefile mkDeps