Cabal.hs 2 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
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
18
        bootPkgs <- interpretInContext (stageContext Stage0) getPackages
Andrey Mokhov's avatar
Andrey Mokhov committed
19
        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
        let pkgs = knownPackages \\ [hp2ps, libffi, touchy, unlit]
Andrey Mokhov's avatar
Andrey Mokhov committed
32
33
34
35
36
37
38
39
40
41
42
        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
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