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

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

    -- Cache package dependencies
Andrey Mokhov's avatar
Andrey Mokhov committed
29
    packageDependencies %> \out -> do
30
        pkgs <- interpretWithStage Stage1 getPackages
31
        pkgDeps <- forM (sort pkgs) $ \pkg -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
32
33
34
            need [pkgCabalFile pkg]
            pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
            let deps     = collectDeps . condLibrary $ pd
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