Update Hadrian
[ghc.git] / hadrian / src / Utilities.hs
1 module Utilities (
2 build, buildWithResources, buildWithCmdOptions, runBuilder, runBuilderWith,
3 needLibrary, contextDependencies, stage1Dependencies, libraryTargets,
4 topsortPackages
5 ) where
6
7 import qualified Hadrian.Builder as H
8 import Hadrian.Haskell.Cabal
9 import Hadrian.Utilities
10
11 import Context
12 import Expression hiding (stage)
13 import Oracles.PackageData
14 import Settings
15 import Target
16 import UserSettings
17
18 build :: Target -> Action ()
19 build target = H.build target getArgs
20
21 buildWithResources :: [(Resource, Int)] -> Target -> Action ()
22 buildWithResources rs target = H.buildWithResources rs target getArgs
23
24 buildWithCmdOptions :: [CmdOption] -> Target -> Action ()
25 buildWithCmdOptions opts target = H.buildWithCmdOptions opts target getArgs
26
27 -- TODO: Cache the computation.
28 -- | Given a 'Context' this 'Action' looks up the package dependencies and wraps
29 -- the results in appropriate contexts. The only subtlety here is that we never
30 -- depend on packages built in 'Stage2' or later, therefore the stage of the
31 -- resulting dependencies is bounded from above at 'Stage1'. To compute package
32 -- dependencies we transitively scan @.cabal@ files using 'pkgDependencies'
33 -- defined in "Hadrian.Haskell.Cabal".
34 contextDependencies :: Context -> Action [Context]
35 contextDependencies Context {..} = do
36 depPkgs <- go [package]
37 return [ Context depStage pkg way | pkg <- depPkgs, pkg /= package ]
38 where
39 depStage = min stage Stage1
40 go pkgs = do
41 deps <- concatMapM step pkgs
42 let newPkgs = nubOrd $ sort (deps ++ pkgs)
43 if pkgs == newPkgs then return pkgs else go newPkgs
44 step pkg = case pkgCabalFile pkg of
45 Nothing -> return [] -- Non-Cabal packages have no dependencies.
46 Just cabalFile -> do
47 deps <- pkgDependencies cabalFile
48 active <- sort <$> stagePackages depStage
49 return $ intersectOrd (compare . pkgName) active deps
50
51 -- | Lookup dependencies of a 'Package' in the vanilla Stage1 context.
52 stage1Dependencies :: Package -> Action [Package]
53 stage1Dependencies =
54 fmap (map Context.package) . contextDependencies . vanillaContext Stage1
55
56 -- | Given a library 'Package' this action computes all of its targets. See
57 -- 'packageTargets' for the explanation of the @includeGhciLib@ parameter.
58 libraryTargets :: Bool -> Context -> Action [FilePath]
59 libraryTargets includeGhciLib context = do
60 confFile <- pkgConfFile context
61 libFile <- pkgLibraryFile context
62 lib0File <- pkgLibraryFile0 context
63 lib0 <- buildDll0 context
64 ghciLib <- pkgGhciLibraryFile context
65 ghciFlag <- if includeGhciLib
66 then interpretInContext context $ getPkgData BuildGhciLib
67 else return "NO"
68 let ghci = ghciFlag == "YES" && (stage context == Stage1 || stage1Only)
69 return $ [ confFile, libFile ] ++ [ lib0File | lib0 ] ++ [ ghciLib | ghci ]
70
71 -- | Coarse-grain 'need': make sure all given libraries are fully built.
72 needLibrary :: [Context] -> Action ()
73 needLibrary cs = need =<< concatMapM (libraryTargets True) cs
74
75 -- HACK (izgzhen), see https://github.com/snowleopard/hadrian/issues/344.
76 -- | Topological sort of packages according to their dependencies.
77 topsortPackages :: [Package] -> Action [Package]
78 topsortPackages pkgs = do
79 elems <- mapM (\p -> (p,) <$> stage1Dependencies p) pkgs
80 return $ map fst $ topSort elems
81 where
82 annotateInDeg es e =
83 (foldr (\e' s -> if fst e' `elem` snd e then s + 1 else s) (0 :: Int) es, e)
84 topSort [] = []
85 topSort es =
86 let annotated = map (annotateInDeg es) es
87 inDegZero = map snd $ filter ((== 0). fst) annotated
88 in inDegZero ++ topSort (es \\ inDegZero)