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

import Stage
Andrey Mokhov's avatar
Andrey Mokhov committed
4
import Package hiding (library)
Andrey Mokhov's avatar
Andrey Mokhov committed
5
import Expression
6
import Settings
7
import Data.Version
8
9
10
11
import Distribution.Package
import Distribution.Verbosity
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
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
        pkgs <- interpretWithStage Stage0 getPackages
18
        constraints <- forM (sort pkgs) $ \pkg -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
19
20
21
            need [pkgCabalFile pkg]
            pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
            let identifier       = package . packageDescription $ pd
22
                version          = showVersion . pkgVersion $ identifier
Andrey Mokhov's avatar
Andrey Mokhov committed
23
                PackageName name = Distribution.Package.pkgName identifier
24
            return $ name ++ " == " ++ version
Andrey Mokhov's avatar
Andrey Mokhov committed
25
        writeFileChanged out . unlines $ constraints
26
27

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

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