Cabal.hs 2.21 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
7
import Distribution.Types.CondTree
Andrey Mokhov's avatar
Andrey Mokhov committed
8
import Distribution.Types.Dependency
Andrey Mokhov's avatar
Andrey Mokhov committed
9
import Distribution.Verbosity
10
11

import Base
Andrey Mokhov's avatar
Andrey Mokhov committed
12
13
import Expression
import GHC
14
import Settings
15
import Settings.Path
16
import UserSettings
17
18

cabalRules :: Rules ()
19
cabalRules = do
20
    -- Cache boot package constraints (to be used in 'cabalArgs').
Andrey Mokhov's avatar
Andrey Mokhov committed
21
    bootPackageConstraints %> \out -> do
22
        bootPkgs <- stagePackages Stage0
Andrey Mokhov's avatar
Andrey Mokhov committed
23
        let pkgs = filter (\p -> p /= compiler && isLibrary p) bootPkgs
24
        constraints <- forM (sort pkgs) $ \pkg -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
25
            need [pkgCabalFile pkg]
26
            pd <- liftIO . readGenericPackageDescription silent $ pkgCabalFile pkg
27
            let identifier          = package . packageDescription $ pd
28
                version             = display . pkgVersion $ identifier
Andrey Mokhov's avatar
Andrey Mokhov committed
29
            return $ unPackageName (DP.pkgName identifier) ++ " == " ++ version
Andrey Mokhov's avatar
Andrey Mokhov committed
30
        writeFileChanged out . unlines $ constraints
31
        putSuccess $ "| Successfully computed boot package constraints"
32

Andrey Mokhov's avatar
Andrey Mokhov committed
33
    -- Cache package dependencies.
Andrey Mokhov's avatar
Andrey Mokhov committed
34
    packageDependencies %> \out -> do
35
36
37
        pkgDeps <- forM (sort knownPackages) $ \pkg -> do
            exists <- doesFileExist $ pkgCabalFile pkg
            if not exists then return $ pkgNameString pkg
Andrey Mokhov's avatar
Andrey Mokhov committed
38
39
            else do
                need [pkgCabalFile pkg]
40
                pd <- liftIO . readGenericPackageDescription silent $ pkgCabalFile pkg
41
                let depsLib  = collectDeps $ condLibrary pd
Andrey Mokhov's avatar
Andrey Mokhov committed
42
43
                    depsExes = map (collectDeps . Just . snd) $ condExecutables pd
                    deps     = concat $ depsLib : depsExes
Andrey Mokhov's avatar
Andrey Mokhov committed
44
                    depNames = [ unPackageName name | Dependency name _ <- deps ]
Andrey Mokhov's avatar
Andrey Mokhov committed
45
                return . unwords $ pkgNameString pkg : sort depNames
46
47
        writeFileChanged out $ unlines pkgDeps
        putSuccess $ "| Successfully computed package dependencies"
48
49
50
51
52

collectDeps :: Maybe (CondTree v [Dependency] a) -> [Dependency]
collectDeps Nothing = []
collectDeps (Just (CondNode _ deps ifs)) = deps ++ concatMap f ifs
  where
53
    f (CondBranch _ t mt) = collectDeps (Just t) ++ collectDeps mt