Data.hs 2.54 KB
Newer Older
1
2
{-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving #-}

3
4
5
6
7
8
module Rules.Data (
    cabalSettings, ghcPkgSettings, buildPackageData
    ) where

import Base hiding (arg, args, Args)
import Package
9
import Expression hiding (when, liftIO)
10
11
import Oracles.Flag (when)
import Oracles.Builder
12
13
import Settings.GhcPkg
import Settings.GhcCabal
14
import Settings.TargetDirectory
15
import Rules.Util
16
import Util
17
import Ways
18
19

-- Build package-data.mk by using GhcCabal to process pkgCabal file
20
21
22
23
buildPackageData :: Target -> Rules ()
buildPackageData target =
    let stage = getStage target
        pkg   = getPackage target
Andrey Mokhov's avatar
Andrey Mokhov committed
24
        path  = targetPath stage pkg
25
    in
Andrey Mokhov's avatar
Andrey Mokhov committed
26
    (path </>) <$>
27
28
29
30
31
32
33
34
35
    [ "package-data.mk"
    , "haddock-prologue.txt"
    , "inplace-pkg-config"
    , "setup-config"
    , "build" </> "autogen" </> "cabal_macros.h"
    -- TODO: Is this needed? Also check out Paths_cpsa.hs.
    -- , "build" </> "autogen" </> ("Paths_" ++ name) <.> "hs"
    ] &%> \_ -> do
        let configure = pkgPath pkg </> "configure"
Andrey Mokhov's avatar
Andrey Mokhov committed
36
            -- TODO: 1) how to automate this? 2) handle multiple files?
37
38
            newTarget = target { getFile = path </> "package-data.mk"
                               , getWay = vanilla } -- TODO: think
39
        -- GhcCabal will run the configure script, so we depend on it
Andrey Mokhov's avatar
Andrey Mokhov committed
40
41
        need [pkgPath pkg </> pkgCabal pkg]
        -- We still don't know who built the configure script from configure.ac
42
        when (doesFileExist $ configure <.> "ac") $ need [configure]
43
        build $ newTarget { getBuilder = GhcCabal }
44
        -- TODO: when (registerPackage settings) $
45
        build $ newTarget { getBuilder = GhcPkg stage }
Andrey Mokhov's avatar
Andrey Mokhov committed
46
        postProcessPackageData $ path </> "package-data.mk"
47

Andrey Mokhov's avatar
Andrey Mokhov committed
48
49
50
51
52
53
54
55
56
-- Prepare a given 'packaga-data.mk' file for parsing by readConfigFile:
-- 1) Drop lines containing '$'
-- For example, get rid of
-- libraries/Win32_dist-install_CMM_SRCS  := $(addprefix cbits/,$(notdir ...
-- Reason: we don't need them and we can't parse them.
-- 2) Replace '/' and '\' with '_' before '='
-- For example libraries/deepseq/dist-install_VERSION = 1.4.0.0
-- is replaced by libraries_deepseq_dist-install_VERSION = 1.4.0.0
-- Reason: Shake's built-in makefile parser doesn't recognise slashes
Andrey Mokhov's avatar
Andrey Mokhov committed
57

Andrey Mokhov's avatar
Andrey Mokhov committed
58
59
60
61
62
63
64
65
postProcessPackageData :: FilePath -> Action ()
postProcessPackageData file = do
    pkgData <- (filter ('$' `notElem`) . lines) <$> liftIO (readFile file)
    length pkgData `seq` writeFileLines file $ map processLine pkgData
      where
        processLine line = replaceSeparators '_' prefix ++ suffix
          where
            (prefix, suffix) = break (== '=') line