Expression: Don't re-export Base
[hadrian.git] / src / Rules / Data.hs
1 module Rules.Data (buildPackageData) where
2
3 import Base
4 import Expression
5 import GHC
6 import Oracles
7 import Predicates (registerPackage)
8 import Rules.Actions
9 import Rules.Resources
10 import Settings
11 import Settings.Builders.GhcCabal
12
13 -- Build package-data.mk by using GhcCabal to process pkgCabal file
14 buildPackageData :: Resources -> PartialTarget -> Rules ()
15 buildPackageData rs target @ (PartialTarget stage pkg) = do
16 let cabalFile = pkgCabalFile pkg
17 configure = pkgPath pkg -/- "configure"
18 dataFile = pkgDataFile stage pkg
19
20 dataFile %> \mk -> do
21 -- GhcCabal may run the configure script, so we depend on it
22 -- We don't know who built the configure script from configure.ac
23 whenM (doesFileExist $ configure <.> "ac") $ need [configure]
24
25 -- We configure packages in the order of their dependencies
26 deps <- packageDeps pkg
27 pkgs <- interpretPartial target getPackages
28 let depPkgs = matchPackageNames (sort pkgs) deps
29 need $ map (pkgDataFile stage) depPkgs
30
31 need [cabalFile]
32 buildWithResources [(resGhcCabal rs, 1)] $
33 fullTarget target GhcCabal [cabalFile] [mk]
34
35 -- ghc-pkg produces inplace-pkg-config when run on packages with
36 -- library components only
37 when (isLibrary pkg) .
38 whenM (interpretPartial target registerPackage) .
39 buildWithResources [(resGhcPkg rs, 1)] $
40 fullTarget target (GhcPkg stage) [cabalFile] [mk]
41
42 postProcessPackageData dataFile
43
44 -- TODO: PROGNAME was $(CrossCompilePrefix)hp2ps
45 -- TODO: code duplication around ghcIncludeDirs
46 priority 2.0 $ do
47 when (pkg == hp2ps) $ dataFile %> \mk -> do
48 let prefix = "utils_hp2ps_stage" ++ show (fromEnum stage) ++ "_"
49 cSrcs = [ "AreaBelow.c", "Curves.c", "Error.c", "Main.c"
50 , "Reorder.c", "TopTwenty.c", "AuxFile.c"
51 , "Deviation.c", "HpFile.c", "Marks.c", "Scale.c"
52 , "TraceElement.c", "Axes.c", "Dimensions.c", "Key.c"
53 , "PsFile.c", "Shade.c", "Utilities.c" ]
54 contents = unlines $ map (prefix++)
55 [ "PROGNAME = hp2ps"
56 , "C_SRCS = " ++ unwords cSrcs
57 , "INSTALL = YES"
58 , "INSTALL_INPLACE = YES"
59 , "DEP_EXTRA_LIBS = m"
60 , "CC_OPTS = " ++ unwords (map ("-I"++) ghcIncludeDirs) ]
61 writeFileChanged mk contents
62 putSuccess $ "| Successfully generated '" ++ mk ++ "'."
63
64 -- Bootstrapping `ghcCabal`: although `ghcCabal` is a proper cabal
65 -- package, we cannot generate the corresponding `package-data.mk` file
66 -- by running by running `ghcCabal`, because it has not yet been built.
67 when (pkg == ghcCabal && stage == Stage0) $ dataFile %> \mk -> do
68 let contents = unlines
69 [ "utils_ghc-cabal_stage0_PROGNAME = ghc-cabal"
70 , "utils_ghc-cabal_stage0_MODULES = Main"
71 , "utils_ghc-cabal_stage0_SYNOPSIS = Bootstrapped ghc-cabal utility."
72 , "utils_ghc-cabal_stage0_HS_SRC_DIRS = ." ]
73 writeFileChanged mk contents
74 putSuccess $ "| Successfully generated '" ++ mk ++ "'."
75
76 -- Prepare a given 'packaga-data.mk' file for parsing by readConfigFile:
77 -- 1) Drop lines containing '$'
78 -- For example, get rid of
79 -- libraries/Win32_dist-install_CMM_SRCS := $(addprefix cbits/,$(notdir ...
80 -- Reason: we don't need them and we can't parse them.
81 -- 2) Replace '/' and '\' with '_' before '='
82 -- For example libraries/deepseq/dist-install_VERSION = 1.4.0.0
83 -- is replaced by libraries_deepseq_dist-install_VERSION = 1.4.0.0
84 -- Reason: Shake's built-in makefile parser doesn't recognise slashes
85 postProcessPackageData :: FilePath -> Action ()
86 postProcessPackageData file = do
87 contents <- fmap (filter ('$' `notElem`) . lines) . liftIO $ readFile file
88 length contents `seq` writeFileLines file $ map processLine contents
89 where
90 processLine line = replaceSeparators '_' prefix ++ suffix
91 where
92 (prefix, suffix) = break (== '=') line