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

Andrey Mokhov's avatar
Andrey Mokhov committed
3
import Expression
4
import Data.Version
Andrey Mokhov's avatar
Andrey Mokhov committed
5
import Distribution.Package hiding (Package)
6
7
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
Andrey Mokhov's avatar
Andrey Mokhov committed
8
import Distribution.Verbosity
9
10
import Package hiding (library)
import Settings
11
12

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

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

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