Cabal.hs 2.53 KB
Newer Older
1
2
module Rules.Cabal (cabalRules) where

Ben Gamari's avatar
Ben Gamari committed
3
import Base
4
import Data.Version
5
import Distribution.Package as DP
6
7
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
Andrey Mokhov's avatar
Andrey Mokhov committed
8
import Distribution.Verbosity
Andrey Mokhov's avatar
Andrey Mokhov committed
9
10
import Expression
import GHC
11
import Rules.Actions
12
import Settings
13
14

cabalRules :: Rules ()
15
cabalRules = do
16
    -- Cache boot package constraints (to be used in cabalArgs)
Andrey Mokhov's avatar
Andrey Mokhov committed
17
    bootPackageConstraints %> \out -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
18
19
        bootPkgs <- interpretWithStage Stage0 getPackages
        let pkgs = filter (\p -> p /= compiler && isLibrary p) bootPkgs
20
        constraints <- forM (sort pkgs) $ \pkg -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
21
22
            need [pkgCabalFile pkg]
            pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
23
24
25
            let identifier          = package . packageDescription $ pd
                version             = showVersion . pkgVersion $ identifier
                DP.PackageName name = DP.pkgName identifier
26
            return $ name ++ " == " ++ version
Andrey Mokhov's avatar
Andrey Mokhov committed
27
        writeFileChanged out . unlines $ constraints
28
29

    -- Cache package dependencies
Andrey Mokhov's avatar
Andrey Mokhov committed
30
    packageDependencies %> \out -> do
31
        pkgs <- interpretWithStage Stage1 getPackages
32
        pkgDeps <- forM (sort pkgs) $ \pkg -> if pkg == rts then return [] else do
Andrey Mokhov's avatar
Andrey Mokhov committed
33
34
            need [pkgCabalFile pkg]
            pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
Andrey Mokhov's avatar
Andrey Mokhov committed
35
36
37
            let depsLib  = collectDeps $ condLibrary pd
                depsExes = map (collectDeps . Just . snd) $ condExecutables pd
                deps     = concat $ depsLib : depsExes
38
39
                depNames = [ name | Dependency (DP.PackageName name) _ <- deps ]
            return . unwords $ pkgNameString pkg : sort depNames
Andrey Mokhov's avatar
Andrey Mokhov committed
40
        writeFileChanged out . unlines $ pkgDeps
41

42
    -- When the file exists, the packageConfiguration has been initialised
43
44
    -- TODO: get rid of an extra file?

45
    forM_ [Stage0, Stage1] $ \stage ->
46
47
48
49
50
51
52
53
54
        packageConfigurationInitialised stage %> \out -> do
            let target  = PartialTarget stage cabal
                pkgConf = packageConfiguration stage
            removeDirectoryIfExists pkgConf
            -- TODO: can we get rid of this fake target?
            build $ fullTarget target (GhcPkg stage) [] [pkgConf]
            let message = "Successfully initialised " ++ pkgConf
            writeFileChanged out message
            putSuccess message
55

56
57
58
59
60
collectDeps :: Maybe (CondTree v [Dependency] a) -> [Dependency]
collectDeps Nothing = []
collectDeps (Just (CondNode _ deps ifs)) = deps ++ concatMap f ifs
  where
    f (_, t, mt) = collectDeps (Just t) ++ collectDeps mt