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

quchen's avatar
quchen committed
3
import Data.Foldable
4
5

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

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

-- | 'need' all top-level build targets
27
28
29
30
31
32
33
34
topLevelTargets :: Rules ()
topLevelTargets = do

    want $ Rules.Generate.installTargets

    -- TODO: do we want libffiLibrary to be a top-level target?

    action $ do -- TODO: Add support for all rtsWays
35
36
        rtsLib    <- pkgLibraryFile Stage1 rts vanilla
        rtsThrLib <- pkgLibraryFile Stage1 rts threaded
37
38
39
40
        need [ rtsLib, rtsThrLib ]

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

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

quchen's avatar
quchen committed
63
    for_ allStages $ \stage ->
64
65
        for_ knownPackages $ \package -> do
            let context = vanillaContext stage package
66
67
68
69
70
71
72
73
            compilePackage            readPackageDb  context
            buildPackageData                         context
            buildPackageDependencies  readPackageDb  context
            buildPackageDocumentation                context
            generatePackageCode                      context
            buildPackageLibrary                      context
            buildProgram                             context
            registerPackage           writePackageDb context
74
75

buildRules :: Rules ()
76
buildRules = do
Andrey Mokhov's avatar
Andrey Mokhov committed
77
78
79
80
81
82
83
84
    cabalRules
    generateRules
    copyRules
    gmpRules
    libffiRules
    perlScriptRules
    setupRules
    packageRules