Drop redundant brackets.
[hadrian.git] / src / Rules / Dependencies.hs
1 module Rules.Dependencies (buildPackageDependencies) where
2
3 import Development.Shake.Util (parseMakefile)
4
5 import Base
6 import Context
7 import Expression
8 import Oracles.PackageData
9 import Rules.Actions
10 import Settings
11 import Target
12
13 -- TODO: simplify handling of AutoApply.cmm
14 buildPackageDependencies :: [(Resource, Int)] -> Context -> Rules ()
15 buildPackageDependencies rs context @ Context {..} =
16 let path = contextPath context
17 buildPath = path -/- "build"
18 dropBuild = (pkgPath package ++) . drop (length buildPath)
19 hDepFile = buildPath -/- ".hs-dependencies"
20 in do
21 fmap (buildPath ++)
22 [ "//*.c.deps", "//*.cmm.deps", "//*.S.deps" ] |%> \out -> do
23 let srcFile = if "//AutoApply.*" ?== out
24 then dropExtension out
25 else dropBuild . dropExtension $ out
26 need [srcFile]
27 build $ Target context (GccM stage) [srcFile] [out]
28
29 hDepFile %> \out -> do
30 srcs <- interpretInContext context getPackageSources
31 need srcs
32 if srcs == []
33 then writeFileChanged out ""
34 else buildWithResources rs $ Target context (GhcM stage) srcs [out]
35 removeFileIfExists $ out <.> "bak"
36
37 -- TODO: don't accumulate *.deps into .dependencies
38 buildPath -/- ".dependencies" %> \out -> do
39 cSrcs <- pkgDataList $ CSrcs path
40 let cDepFiles = [ buildPath -/- src <.> "deps" | src <- cSrcs
41 , not ("//AutoApply.cmm" ?== src) ]
42 ++ [ src <.> "deps" | src <- cSrcs, "//AutoApply.cmm" ?== src ]
43
44 need $ hDepFile : cDepFiles -- need all for more parallelism
45 cDeps <- fmap concat $ traverse readFile' cDepFiles
46 hDeps <- readFile' hDepFile
47 let result = unlines
48 . map (\(src, deps) -> unwords $ src : deps)
49 . map (bimap unifyPath (map unifyPath))
50 . map (bimap head concat . unzip)
51 . groupBy ((==) `on` fst)
52 . sortBy (compare `on` fst)
53 . parseMakefile $ cDeps ++ hDeps
54 writeFileChanged out result