Build deriveConstants utility.
[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 -- TODO: Track dependency on this generated file
49 -- TODO: Use a cabal file instead of manual hacks?
50 priority 2.0 $ path -/- "package-data.mk" %> \mk -> do
51 let contents = unlines
52 [ "utils_deriveConstants_dist-boot_MODULES = DeriveConstants"
53 , "utils_deriveConstants_dist-boot_PROGNAME = deriveConstants"
54 , "utils_deriveConstants_dist-boot_HS_SRC_DIRS = ."
55 , "utils_deriveConstants_dist-boot_INSTALL_INPLACE = YES"
56 , "utils_deriveConstants_dist-boot_HC_OPTS = -package process -package containers" ]
57 writeFileChanged mk contents
58
59 -- Prepare a given 'packaga-data.mk' file for parsing by readConfigFile:
60 -- 1) Drop lines containing '$'
61 -- For example, get rid of
62 -- libraries/Win32_dist-install_CMM_SRCS := $(addprefix cbits/,$(notdir ...
63 -- Reason: we don't need them and we can't parse them.
64 -- 2) Replace '/' and '\' with '_' before '='
65 -- For example libraries/deepseq/dist-install_VERSION = 1.4.0.0
66 -- is replaced by libraries_deepseq_dist-install_VERSION = 1.4.0.0
67 -- Reason: Shake's built-in makefile parser doesn't recognise slashes
68 postProcessPackageData :: FilePath -> Action ()
69 postProcessPackageData file = do
70 contents <- fmap (filter ('$' `notElem`) . lines) . liftIO $ readFile file
71 length contents `seq` writeFileLines file $ map processLine contents
72 where
73 processLine line = replaceSeparators '_' prefix ++ suffix
74 where
75 (prefix, suffix) = break (== '=') line