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

import Data.Version
4
import Distribution.Package as DP
5
6
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
Andrey Mokhov's avatar
Andrey Mokhov committed
7
import Distribution.Verbosity
8
9

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

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