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

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

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

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

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