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

import Data.Version
Andrey Mokhov's avatar
Andrey Mokhov committed
4
import Distribution.Package hiding (Package)
5
6
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
Andrey Mokhov's avatar
Andrey Mokhov committed
7
import Distribution.Verbosity
Andrey Mokhov's avatar
Andrey Mokhov committed
8
9
import Expression
import GHC
10
11
import Package hiding (library)
import Settings
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
Andrey Mokhov's avatar
Andrey Mokhov committed
17
18
        bootPkgs <- interpretWithStage Stage0 getPackages
        let pkgs = filter (\p -> p /= compiler && isLibrary p) bootPkgs
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
            need [pkgCabalFile pkg]
            pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
Andrey Mokhov's avatar
Andrey Mokhov committed
34
35
36
            let depsLib  = collectDeps $ condLibrary pd
                depsExes = map (collectDeps . Just . snd) $ condExecutables pd
                deps     = concat $ depsLib : depsExes
37
                depNames = [ name | Dependency (PackageName name) _ <- deps ]
Andrey Mokhov's avatar
Andrey Mokhov committed
38
            return . unwords $ Package.pkgName pkg : sort depNames
Andrey Mokhov's avatar
Andrey Mokhov committed
39
        writeFileChanged out . unlines $ pkgDeps
40
41
42
43
44
45

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