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

3
import Distribution.Package as DP
4
5
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
Andrey Mokhov's avatar
Andrey Mokhov committed
6
import Distribution.Text
Andrey Mokhov's avatar
Andrey Mokhov committed
7
import Distribution.Verbosity
Andrey Mokhov's avatar
Andrey Mokhov committed
8
import Text.PrettyPrint
9
10

import Base
Andrey Mokhov's avatar
Andrey Mokhov committed
11
12
import Expression
import GHC
13
import Settings
Andrey Mokhov's avatar
Andrey Mokhov committed
14
import Settings.Paths
15
16

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

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

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