Add support for non-library packages.
[hadrian.git] / src / Rules / Data.hs
1 module Rules.Data (buildPackageData) where
2
3 import Expression
4 import Oracles
5 import Predicates (registerPackage)
6 import Rules.Actions
7 import Rules.Resources
8 import Settings
9
10 -- Build package-data.mk by using GhcCabal to process pkgCabal file
11 buildPackageData :: Resources -> PartialTarget -> Rules ()
12 buildPackageData rs target @ (PartialTarget stage pkg) = do
13 let path = targetPath stage pkg
14 cabalFile = pkgCabalFile pkg
15 configure = pkgPath pkg -/- "configure"
16
17 fmap (path -/-)
18 [ "package-data.mk"
19 , "haddock-prologue.txt"
20 , "setup-config"
21 , "build" -/- "autogen" -/- "cabal_macros.h"
22 -- TODO: Is this needed? Also check out Paths_cpsa.hs.
23 -- , "build" -/- "autogen" -/- ("Paths_" ++ name) <.> "hs"
24 ] &%> \outs -> do
25 -- GhcCabal may run the configure script, so we depend on it
26 -- We don't know who built the configure script from configure.ac
27 whenM (doesFileExist $ configure <.> "ac") $ need [configure]
28
29 -- We configure packages in the order of their dependencies
30 deps <- packageDeps pkg
31 pkgs <- interpretPartial target getPackages
32 let cmp p name = compare (pkgName p) name
33 depPkgs = intersectOrd cmp (sort pkgs) deps
34 need [ targetPath stage p -/- "package-data.mk" | p <- depPkgs ]
35
36 need [cabalFile]
37 buildWithResources [(ghcCabal rs, 1)] $
38 fullTarget target GhcCabal [cabalFile] outs
39
40 -- TODO: find out of ghc-cabal can be concurrent with ghc-pkg
41 when (isLibrary pkg) .
42 whenM (interpretPartial target registerPackage) .
43 buildWithResources [(ghcPkg rs, 1)] $
44 fullTarget target (GhcPkg stage) [cabalFile] outs
45
46 postProcessPackageData $ path -/- "package-data.mk"
47
48 -- Prepare a given 'packaga-data.mk' file for parsing by readConfigFile:
49 -- 1) Drop lines containing '$'
50 -- For example, get rid of
51 -- libraries/Win32_dist-install_CMM_SRCS := $(addprefix cbits/,$(notdir ...
52 -- Reason: we don't need them and we can't parse them.
53 -- 2) Replace '/' and '\' with '_' before '='
54 -- For example libraries/deepseq/dist-install_VERSION = 1.4.0.0
55 -- is replaced by libraries_deepseq_dist-install_VERSION = 1.4.0.0
56 -- Reason: Shake's built-in makefile parser doesn't recognise slashes
57 postProcessPackageData :: FilePath -> Action ()
58 postProcessPackageData file = do
59 contents <- fmap (filter ('$' `notElem`) . lines) . liftIO $ readFile file
60 length contents `seq` writeFileLines file $ map processLine contents
61 where
62 processLine line = replaceSeparators '_' prefix ++ suffix
63 where
64 (prefix, suffix) = break (== '=') line