Use Cabal directly in place of ghc-cabal + make build root configurable (#531)
[hadrian.git] / src / Rules / Register.hs
1 module Rules.Register (registerPackages) where
2
3 import Base
4 import Context
5 import GHC
6 import Settings
7 import Target
8 import Utilities
9
10 import Distribution.ParseUtils
11 import qualified Distribution.Compat.ReadP as Parse
12 import Distribution.Version (Version)
13
14 import Hadrian.Expression
15 import Hadrian.Haskell.Cabal.Parse as Cabal
16
17 parseCabalName :: String -> Maybe (String, Version)
18 parseCabalName = readPToMaybe parse
19 where parse = (,) <$> (parsePackageName <* Parse.char '-') <*> parseOptVersion
20
21 -- | Build rules for registering packages and initialising package databases
22 -- by running the @ghc-pkg@ utility.
23 registerPackages :: [(Resource, Int)] -> Context -> Rules ()
24 registerPackages rs context@Context {..} = do
25 root <- buildRootRules
26 root -/- relativePackageDbPath stage %>
27 buildStamp rs context
28
29 root -/- relativePackageDbPath stage -/- packageDbStamp %> \stamp ->
30 writeFileLines stamp []
31
32 root -/- relativePackageDbPath stage -/- "*.conf" %> \conf -> do
33 settings <- libPath context <&> (-/- "settings")
34 platformConstants <- libPath context <&> (-/- "platformConstants")
35 need [settings, platformConstants]
36 let Just pkgName | takeBaseName conf == "rts" = Just "rts"
37 | otherwise = fst <$> parseCabalName (takeBaseName conf)
38 let Just pkg = findPackageByName pkgName
39 bootLibs <- filter isLibrary <$> stagePackages Stage0
40 case stage of
41 Stage0 | pkg `notElem` bootLibs -> copyConf rs (context { package = pkg }) conf
42 _ -> buildConf rs (context { package = pkg }) conf
43
44
45 buildConf :: [(Resource, Int)] -> Context -> FilePath -> Action ()
46 buildConf _ context@Context {..} _conf = do
47 depPkgIds <- cabalDependencies context
48
49 -- setup-config, triggers `ghc-cabal configure`
50 -- everything of a package should depend on that
51 -- in the first place.
52 setupConfig <- (contextPath context) <&> (-/- "setup-config")
53 need [setupConfig]
54 need =<< mapM (\pkgId -> packageDbPath stage <&> (-/- pkgId <.> "conf")) depPkgIds
55
56 ways <- interpretInContext context (getLibraryWays <> if package == rts then getRtsWays else mempty)
57 need =<< concatMapM (libraryTargets True) [ context { way = w } | w <- ways ]
58
59 -- might need some package-db resource to limit read/write,
60 -- see packageRules
61 bldPath <- buildPath context
62
63 -- special package cases (these should ideally be rolled into cabal one way or the other)
64 when (package == rts) $
65 -- iif cabal new about "generated-headers", we could read them from the configuredCabal
66 -- information, and just "need" them here.
67 need [ bldPath -/- "DerivedConstants.h"
68 , bldPath -/- "ghcautoconf.h"
69 , bldPath -/- "ghcplatform.h"
70 , bldPath -/- "ghcversion.h"
71 , bldPath -/- "ffi.h"
72 ]
73
74 when (package == integerGmp) $
75 need [bldPath -/- "ghc-gmp.h"]
76
77 -- copy and register the package
78 copyPackage context
79 registerPackage context
80
81 copyConf :: [(Resource, Int)] -> Context -> FilePath -> Action ()
82 copyConf rs context@Context {..} conf = do
83 depPkgIds <- fmap stdOutToPkgIds . askWithResources rs $
84 target context (GhcPkg Dependencies stage) [pkgName package] []
85 need =<< mapM (\pkgId -> packageDbPath stage <&> (-/- pkgId <.> "conf")) depPkgIds
86 buildWithResources rs $
87 target context (GhcPkg Clone stage) [pkgName package] [conf]
88
89 where
90 stdOutToPkgIds :: String -> [String]
91 stdOutToPkgIds = drop 1 . concatMap words . lines
92
93 buildStamp :: [(Resource, Int)] -> Context -> FilePath -> Action ()
94 buildStamp rs Context {..} path = do
95 buildWithResources rs $
96 target (vanillaContext stage ghc) (GhcPkg Init stage) [] [path]
97 putSuccess $ "| Successfully initialised " ++ path