Rules.hs 2.98 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
topLevelTargets :: Rules ()
topLevelTargets = do
30
    want $ Rules.Generate.inplaceLibCopyTargets
31

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

46
packageRules :: Rules ()
47
packageRules = do
48
49
    -- 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
50
51
52
53
54
55
    -- classic concurrent read exclusive write (CREW) conflict.
    let maxConcurrentReaders = 1000
    packageDb <- newResource "package-db" maxConcurrentReaders
    let readPackageDb  = [(packageDb, 1)]
        writePackageDb = [(packageDb, maxConcurrentReaders)]

Andrey Mokhov's avatar
Andrey Mokhov committed
56
57
    let contexts        = liftM3 Context        allStages knownPackages allWays
        vanillaContexts = liftM2 vanillaContext allStages knownPackages
Andrey Mokhov's avatar
Andrey Mokhov committed
58
        programContexts = liftM2 programContext allStages knownPackages
59

Andrey Mokhov's avatar
Andrey Mokhov committed
60
    forM_ contexts $ mconcat
61
62
        [ Rules.Compile.compilePackage readPackageDb
        , Rules.Library.buildPackageLibrary ]
63

Andrey Mokhov's avatar
Andrey Mokhov committed
64
65
    forM_ programContexts $ Rules.Program.buildProgram readPackageDb

Andrey Mokhov's avatar
Andrey Mokhov committed
66
    forM_ vanillaContexts $ mconcat
67
68
69
70
71
72
        [ Rules.Data.buildPackageData
        , Rules.Dependencies.buildPackageDependencies readPackageDb
        , Rules.Documentation.buildPackageDocumentation
        , Rules.Library.buildPackageGhciLibrary
        , Rules.Generate.generatePackageCode
        , Rules.Register.registerPackage writePackageDb ]
73
74

buildRules :: Rules ()
75
buildRules = do
76
77
78
79
80
81
    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
82
    packageRules
83
    Rules.Perl.perlScriptRules