Rules.hs 3.23 KB
Newer Older
1
module Rules (topLevelTargets, buildRules) where
2

3
import Base
Andrey Mokhov's avatar
Andrey Mokhov committed
4
import Context
Andrey Mokhov's avatar
Andrey Mokhov committed
5
import Expression
Andrey Mokhov's avatar
Andrey Mokhov committed
6
import Flavour
7
import GHC
8
9
10
11
12
13
14
15
16
17
18
19
20
import qualified Rules.Compile
import qualified Rules.Data
import qualified Rules.Dependencies
import qualified Rules.Documentation
import qualified Rules.Generate
import qualified Rules.Cabal
import qualified Rules.Configure
import qualified Rules.Gmp
import qualified Rules.Libffi
import qualified Rules.Library
import qualified Rules.Perl
import qualified Rules.Program
import qualified Rules.Register
Andrey Mokhov's avatar
Andrey Mokhov committed
21
import Settings
22
import Settings.Path
23

quchen's avatar
quchen committed
24
allStages :: [Stage]
quchen's avatar
quchen committed
25
allStages = [minBound ..]
quchen's avatar
quchen committed
26

Andrey Mokhov's avatar
Andrey Mokhov committed
27
-- | This rule 'need' all top-level build targets.
28
29
30
31
32
topLevelTargets :: Rules ()
topLevelTargets = do

    want $ Rules.Generate.installTargets

Andrey Mokhov's avatar
Andrey Mokhov committed
33
    -- TODO: Do we want libffiLibrary to be a top-level target?
34
35

    action $ do -- TODO: Add support for all rtsWays
Andrey Mokhov's avatar
Andrey Mokhov committed
36
37
        rtsLib    <- pkgLibraryFile $ rtsContext { way = vanilla  }
        rtsThrLib <- pkgLibraryFile $ rtsContext { way = threaded }
38
39
        need [ rtsLib, rtsThrLib ]

Andrey Mokhov's avatar
Andrey Mokhov committed
40
41
    forM_ allStages $ \stage ->
        forM_ (knownPackages \\ [rts, libffi]) $ \pkg -> action $ do
42
43
            let context = vanillaContext stage pkg
            activePackages <- interpretInContext context getPackages
44
45
46
            when (pkg `elem` activePackages) $
                if isLibrary pkg
                then do -- build a library
47
                    ways <- interpretInContext context getLibraryWays
48
                    libs <- mapM (pkgLibraryFile . Context stage pkg) ways
Andrey Mokhov's avatar
Andrey Mokhov committed
49
                    docs <- interpretInContext context $ buildHaddock flavour
Andrey Mokhov's avatar
Andrey Mokhov committed
50
                    need $ libs ++ [ pkgHaddockFile context | docs && stage == Stage1 ]
51
                else do -- otherwise build a program
Andrey Mokhov's avatar
Andrey Mokhov committed
52
                    need =<< maybeToList <$> programPath context
quchen's avatar
quchen committed
53

54
packageRules :: Rules ()
55
packageRules = do
56
57
    -- We cannot register multiple GHC packages in parallel. Also we cannot run
    -- GHC when the package database is being mutated by "ghc-pkg". This is a
58
59
60
61
62
63
    -- classic concurrent read exclusive write (CREW) conflict.
    let maxConcurrentReaders = 1000
    packageDb <- newResource "package-db" maxConcurrentReaders
    let readPackageDb  = [(packageDb, 1)]
        writePackageDb = [(packageDb, maxConcurrentReaders)]

64
    -- TODO: not all build rules make sense for all stage/package combinations
Andrey Mokhov's avatar
Andrey Mokhov committed
65
66
    let contexts        = liftM3 Context        allStages knownPackages allWays
        vanillaContexts = liftM2 vanillaContext allStages knownPackages
67

Andrey Mokhov's avatar
Andrey Mokhov committed
68
    forM_ contexts $ mconcat
69
70
        [ Rules.Compile.compilePackage readPackageDb
        , Rules.Library.buildPackageLibrary ]
71

Andrey Mokhov's avatar
Andrey Mokhov committed
72
    forM_ vanillaContexts $ mconcat
73
74
75
76
77
78
79
        [ Rules.Data.buildPackageData
        , Rules.Dependencies.buildPackageDependencies readPackageDb
        , Rules.Documentation.buildPackageDocumentation
        , Rules.Library.buildPackageGhciLibrary
        , Rules.Generate.generatePackageCode
        , Rules.Program.buildProgram readPackageDb
        , Rules.Register.registerPackage writePackageDb ]
80
81

buildRules :: Rules ()
82
buildRules = do
83
84
85
86
87
88
    Rules.Cabal.cabalRules
    Rules.Configure.configureRules
    Rules.Generate.copyRules
    Rules.Generate.generateRules
    Rules.Gmp.gmpRules
    Rules.Libffi.libffiRules
Andrey Mokhov's avatar
Andrey Mokhov committed
89
    packageRules
90
    Rules.Perl.perlScriptRules