Cabal.hs 2.01 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 Settings
12
13

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

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

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