082d50915f4ba63b9dbd3f62e81727e3cbac3e02
[hadrian.git] / src / Rules.hs
1 module Rules (topLevelTargets, buildRules) where
2
3 import Base
4 import Context
5 import Expression
6 import Flavour
7 import GHC
8 import qualified Rules.Compile
9 import qualified Rules.Data
10 import qualified Rules.Dependencies
11 import qualified Rules.Documentation
12 import qualified Rules.Generate
13 import qualified Rules.Cabal
14 import qualified Rules.Configure
15 import qualified Rules.Gmp
16 import qualified Rules.Libffi
17 import qualified Rules.Library
18 import qualified Rules.Perl
19 import qualified Rules.Program
20 import qualified Rules.Register
21 import Settings
22 import Settings.Path
23
24 allStages :: [Stage]
25 allStages = [minBound ..]
26
27 -- | This rule 'need' all top-level build targets.
28 topLevelTargets :: Rules ()
29 topLevelTargets = do
30 want $ Rules.Generate.inplaceLibCopyTargets
31
32 forM_ allStages $ \stage ->
33 forM_ (knownPackages \\ [rts, libffi]) $ \pkg -> action $ do
34 let context = vanillaContext stage pkg
35 activePackages <- interpretInContext context getPackages
36 when (pkg `elem` activePackages) $
37 if isLibrary pkg
38 then do -- build a library
39 ways <- interpretInContext context getLibraryWays
40 libs <- mapM (pkgLibraryFile . Context stage pkg) ways
41 docs <- interpretInContext context $ buildHaddock flavour
42 need $ libs ++ [ pkgHaddockFile context | docs && stage == Stage1 ]
43 else -- otherwise build a program
44 need =<< maybeToList <$> programPath (programContext stage pkg)
45
46 packageRules :: Rules ()
47 packageRules = do
48 -- We cannot register multiple GHC packages in parallel. Also we cannot run
49 -- GHC when the package database is being mutated by "ghc-pkg". This is a
50 -- classic concurrent read exclusive write (CREW) conflict.
51 let maxConcurrentReaders = 1000
52 packageDb <- newResource "package-db" maxConcurrentReaders
53 let readPackageDb = [(packageDb, 1)]
54 writePackageDb = [(packageDb, maxConcurrentReaders)]
55
56 let contexts = liftM3 Context allStages knownPackages allWays
57 vanillaContexts = liftM2 vanillaContext allStages knownPackages
58 programContexts = liftM2 programContext allStages knownPackages
59
60 forM_ contexts $ mconcat
61 [ Rules.Compile.compilePackage readPackageDb
62 , Rules.Library.buildPackageLibrary ]
63
64 forM_ programContexts $ Rules.Program.buildProgram readPackageDb
65
66 forM_ vanillaContexts $ mconcat
67 [ Rules.Data.buildPackageData
68 , Rules.Dependencies.buildPackageDependencies readPackageDb
69 , Rules.Documentation.buildPackageDocumentation
70 , Rules.Library.buildPackageGhciLibrary
71 , Rules.Generate.generatePackageCode
72 , Rules.Register.registerPackage writePackageDb ]
73
74 buildRules :: Rules ()
75 buildRules = do
76 Rules.Cabal.cabalRules
77 Rules.Configure.configureRules
78 Rules.Generate.copyRules
79 Rules.Generate.generateRules
80 Rules.Gmp.gmpRules
81 Rules.Libffi.libffiRules
82 packageRules
83 Rules.Perl.perlScriptRules