Cabal.hs 1.73 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
9
import Settings.Packages
import Data.List
import Data.Version
10
11
12
13
import Distribution.Package
import Distribution.Verbosity
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
14
15

cabalRules :: Rules ()
16
cabalRules = do
17
    -- Cache boot package constraints (to be used in cabalArgs)
Andrey Mokhov's avatar
Andrey Mokhov committed
18
    bootPackageConstraints %> \out -> do
19
        pkgs <- interpret (stageTarget Stage0) getPackages
20
        constraints <- forM (sort pkgs) $ \pkg -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
21
22
23
            need [pkgCabalFile pkg]
            pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
            let identifier       = package . packageDescription $ pd
24
                version          = showVersion . pkgVersion $ identifier
Andrey Mokhov's avatar
Andrey Mokhov committed
25
                PackageName name = Distribution.Package.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
        pkgs <- interpret (stageTarget Stage1) getPackages
32
        pkgDeps <- forM (sort pkgs) $ \pkg -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
33
34
35
            need [pkgCabalFile pkg]
            pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
            let deps     = collectDeps . condLibrary $ pd
36
                depNames = [ name | Dependency (PackageName name) _ <- deps ]
Andrey Mokhov's avatar
Andrey Mokhov committed
37
            return . unwords $ Package.pkgName pkg : sort depNames
Andrey Mokhov's avatar
Andrey Mokhov committed
38
        writeFileChanged out . unlines $ pkgDeps
39
40
41
42
43
44

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