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

import Data.Version
4
5
6
7
import Distribution.Package
import Distribution.Verbosity
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
8
9
10
import Expression
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
32
            need [pkgCabalFile pkg]
            pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
            let deps     = collectDeps . condLibrary $ pd
33
                depNames = [ name | Dependency (PackageName name) _ <- deps ]
Andrey Mokhov's avatar
Andrey Mokhov committed
34
            return . unwords $ Package.pkgName pkg : sort depNames
Andrey Mokhov's avatar
Andrey Mokhov committed
35
        writeFileChanged out . unlines $ pkgDeps
36
37
38
39
40
41

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