82edb3a2e41f283cce21269da602920d2ffefdd2
[ghc.git] / src / Rules / Cabal.hs
1 module Rules.Cabal (cabalRules) where
2
3 import Data.Version
4 import Distribution.Package as DP
5 import Distribution.PackageDescription
6 import Distribution.PackageDescription.Parse
7 import Distribution.Verbosity
8
9 import Base
10 import Expression
11 import GHC
12 import Settings
13
14 cabalRules :: Rules ()
15 cabalRules = do
16 -- Cache boot package constraints (to be used in cabalArgs)
17 bootPackageConstraints %> \out -> do
18 bootPkgs <- interpretInContext (stageContext Stage0) getPackages
19 let pkgs = filter (\p -> p /= compiler && isLibrary p) bootPkgs
20 constraints <- forM (sort pkgs) $ \pkg -> do
21 need [pkgCabalFile pkg]
22 pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
23 let identifier = package . packageDescription $ pd
24 version = showVersion . pkgVersion $ identifier
25 DP.PackageName name = DP.pkgName identifier
26 return $ name ++ " == " ++ version
27 writeFileChanged out . unlines $ constraints
28
29 -- Cache package dependencies
30 packageDependencies %> \out -> do
31 let pkgs = knownPackages \\ [hp2ps, libffi, touchy, unlit]
32 pkgDeps <- forM (sort pkgs) $ \pkg ->
33 if pkg == rts
34 then return $ pkgNameString pkg
35 else do
36 need [pkgCabalFile pkg]
37 pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
38 let depsLib = collectDeps $ condLibrary pd
39 depsExes = map (collectDeps . Just . snd) $ condExecutables pd
40 deps = concat $ depsLib : depsExes
41 depNames = [ name | Dependency (DP.PackageName name) _ <- deps ]
42 return . unwords $ pkgNameString pkg : sort depNames
43 writeFileChanged out . unlines $ pkgDeps
44
45 collectDeps :: Maybe (CondTree v [Dependency] a) -> [Dependency]
46 collectDeps Nothing = []
47 collectDeps (Just (CondNode _ deps ifs)) = deps ++ concatMap f ifs
48 where
49 f (_, t, mt) = collectDeps (Just t) ++ collectDeps mt