Squashed 'hadrian/' content from commit 438dc57
[ghc.git] / 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 -- | Given a 'Context' this 'Action' look up the package dependencies and wrap
28 -- the results in appropriate contexts. The only subtlety here is that we never
29 -- depend on packages built in 'Stage2' or later, therefore the stage of the
30 -- resulting dependencies is bounded from above at 'Stage1'. To compute package
31 -- dependencies we scan package @.cabal@ files, see 'pkgDependencies' defined
32 -- in "Hadrian.Haskell.Cabal".
33 contextDependencies :: Context -> Action [Context]
34 contextDependencies Context {..} = case pkgCabalFile package of
35 Nothing -> return [] -- Non-Cabal packages have no dependencies.
36 Just cabalFile -> do
37 let depStage = min stage Stage1
38 depContext = \pkg -> Context depStage pkg way
39 deps <- pkgDependencies cabalFile
40 pkgs <- sort <$> stagePackages depStage
41 return . map depContext $ intersectOrd (compare . pkgName) pkgs deps
42
43 -- | Lookup dependencies of a 'Package' in the vanilla Stage1 context.
44 stage1Dependencies :: Package -> Action [Package]
45 stage1Dependencies =
46 fmap (map Context.package) . contextDependencies . vanillaContext Stage1
47
48 -- | Given a library 'Package' this action computes all of its targets. See
49 -- 'packageTargets' for the explanation of the @includeGhciLib@ parameter.
50 libraryTargets :: Bool -> Context -> Action [FilePath]
51 libraryTargets includeGhciLib context = do
52 confFile <- pkgConfFile context
53 libFile <- pkgLibraryFile context
54 lib0File <- pkgLibraryFile0 context
55 lib0 <- buildDll0 context
56 ghciLib <- pkgGhciLibraryFile context
57 ghciFlag <- if includeGhciLib
58 then interpretInContext context $ getPkgData BuildGhciLib
59 else return "NO"
60 let ghci = ghciFlag == "YES" && (stage context == Stage1 || stage1Only)
61 return $ [ confFile, libFile ] ++ [ lib0File | lib0 ] ++ [ ghciLib | ghci ]
62
63 -- | Coarse-grain 'need': make sure all given libraries are fully built.
64 needLibrary :: [Context] -> Action ()
65 needLibrary cs = need =<< concatMapM (libraryTargets True) cs
66
67 -- HACK (izgzhen), see https://github.com/snowleopard/hadrian/issues/344.
68 -- | Topological sort of packages according to their dependencies.
69 topsortPackages :: [Package] -> Action [Package]
70 topsortPackages pkgs = do
71 elems <- mapM (\p -> (p,) <$> stage1Dependencies p) pkgs
72 return $ map fst $ topSort elems
73 where
74 annotateInDeg es e =
75 (foldr (\e' s -> if fst e' `elem` snd e then s + 1 else s) (0 :: Int) es, e)
76 topSort [] = []
77 topSort es =
78 let annotated = map (annotateInDeg es) es
79 inDegZero = map snd $ filter ((== 0). fst) annotated
80 in inDegZero ++ topSort (es \\ inDegZero)