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

3
import Distribution.Package as DP
4
5
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
Andrey Mokhov's avatar
Andrey Mokhov committed
6
import Distribution.Text
Andrey Mokhov's avatar
Andrey Mokhov committed
7
import Distribution.Verbosity
8
9

import Base
Andrey Mokhov's avatar
Andrey Mokhov committed
10
11
import Expression
import GHC
12
import Settings
Andrey Mokhov's avatar
Andrey Mokhov committed
13
import Settings.Paths
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
        bootPkgs <- interpretInContext (stageContext Stage0) getPackages
Andrey Mokhov's avatar
Andrey Mokhov committed
20
        let pkgs = filter (\p -> p /= compiler && isLibrary p) bootPkgs
21
        constraints <- forM (sort pkgs) $ \pkg -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
22
23
            need [pkgCabalFile pkg]
            pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
24
            let identifier          = package . packageDescription $ pd
25
                version             = display . pkgVersion $ identifier
Andrey Mokhov's avatar
Andrey Mokhov committed
26
            return $ unPackageName (DP.pkgName identifier) ++ " == " ++ version
Andrey Mokhov's avatar
Andrey Mokhov committed
27
        writeFileChanged out . unlines $ constraints
28

Andrey Mokhov's avatar
Andrey Mokhov committed
29
    -- Cache package dependencies.
Andrey Mokhov's avatar
Andrey Mokhov committed
30
    packageDependencies %> \out -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
31
32
        pkgDeps <- forM (sort knownPackages) $ \pkg ->
            if pkg `elem` [hp2ps, libffi, rts, touchy, unlit]
Andrey Mokhov's avatar
Andrey Mokhov committed
33
34
35
36
            then return $ pkgNameString pkg
            else do
                need [pkgCabalFile pkg]
                pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
37
                let depsLib  = collectDeps $ condLibrary pd
Andrey Mokhov's avatar
Andrey Mokhov committed
38
39
                    depsExes = map (collectDeps . Just . snd) $ condExecutables pd
                    deps     = concat $ depsLib : depsExes
Andrey Mokhov's avatar
Andrey Mokhov committed
40
                    depNames = [ unPackageName name | Dependency name _ <- deps ]
Andrey Mokhov's avatar
Andrey Mokhov committed
41
                return . unwords $ pkgNameString pkg : sort depNames
Andrey Mokhov's avatar
Andrey Mokhov committed
42
        writeFileChanged out . unlines $ pkgDeps
43
44
45
46
47
48

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