Cabal.hs 2.16 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.Verbosity
9 10

import Base
Andrey Mokhov's avatar
Andrey Mokhov committed
11
import GHC
12
import Settings
13
import Settings.Path
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 <- stagePackages Stage0
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
            need [pkgCabalFile pkg]
23
            pd <- liftIO . readGenericPackageDescription 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
        putSuccess $ "| Successfully computed boot package constraints"
29

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

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