Add Base.hs with Shake imports and build paths.
[hadrian.git] / src / Rules / Data.hs
1 module Rules.Data (buildPackageData) where
2
3 import Base
4 import Util
5 import Package
6 import Builder
7 import Switches
8 import Expression
9 import qualified Target
10 import Settings.TargetDirectory
11 import Rules.Actions
12 import Control.Applicative
13 import Control.Monad.Extra
14
15 -- TODO: Add ordering between packages? (see ghc.mk)
16 -- Build package-data.mk by using GhcCabal to process pkgCabal file
17 buildPackageData :: StagePackageTarget -> Rules ()
18 buildPackageData target = do
19 let stage = Target.stage target
20 pkg = Target.package target
21 path = targetPath stage pkg
22 cabal = pkgPath pkg -/- pkgCabal pkg
23 configure = pkgPath pkg -/- "configure"
24
25 -- We do not allow parallel invokations of ghc-pkg (they don't work)
26 ghcPkg <- newResource "ghc-pkg" 1
27
28 (path -/-) <$>
29 [ "package-data.mk"
30 , "haddock-prologue.txt"
31 , "inplace-pkg-config"
32 , "setup-config"
33 , "build" -/- "autogen" -/- "cabal_macros.h"
34 -- TODO: Is this needed? Also check out Paths_cpsa.hs.
35 -- , "build" -/- "autogen" -/- ("Paths_" ++ name) <.> "hs"
36 ] &%> \files -> do
37 -- GhcCabal may run the configure script, so we depend on it
38 -- We don't know who built the configure script from configure.ac
39 whenM (doesFileExist $ configure <.> "ac") $ need [configure]
40 buildWithResources [(ghcPkg, 1)] $ -- GhcCabal calls ghc-pkg too
41 fullTarget target [cabal] GhcCabal files
42 whenM (interpretExpr target registerPackage) .
43 buildWithResources [(ghcPkg, 1)] $
44 fullTarget target [cabal] (GhcPkg stage) files
45 postProcessPackageData $ path -/- "package-data.mk"
46
47 -- Prepare a given 'packaga-data.mk' file for parsing by readConfigFile:
48 -- 1) Drop lines containing '$'
49 -- For example, get rid of
50 -- libraries/Win32_dist-install_CMM_SRCS := $(addprefix cbits/,$(notdir ...
51 -- Reason: we don't need them and we can't parse them.
52 -- 2) Replace '/' and '\' with '_' before '='
53 -- For example libraries/deepseq/dist-install_VERSION = 1.4.0.0
54 -- is replaced by libraries_deepseq_dist-install_VERSION = 1.4.0.0
55 -- Reason: Shake's built-in makefile parser doesn't recognise slashes
56
57 postProcessPackageData :: FilePath -> Action ()
58 postProcessPackageData file = do
59 pkgData <- (filter ('$' `notElem`) . lines) <$> liftIO (readFile file)
60 length pkgData `seq` writeFileLines file $ map processLine pkgData
61 where
62 processLine line = replaceSeparators '_' prefix ++ suffix
63 where
64 (prefix, suffix) = break (== '=') line