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

3
module Rules.Data (
4
    cabalArgs, ghcPkgArgs, buildPackageData
5 6
    ) where

7
import Way
8
import Base
9
import Package
Andrey Mokhov's avatar
Andrey Mokhov committed
10
import Builder
11 12
import Expression
import Control.Monad.Extra
13 14
import Settings.GhcPkg
import Settings.GhcCabal
15
import Settings.TargetDirectory
Andrey Mokhov's avatar
Andrey Mokhov committed
16
import Rules.Actions
17 18 19
import Util

-- Build package-data.mk by using GhcCabal to process pkgCabal file
20
buildPackageData :: StagePackageTarget -> Rules ()
21 22 23
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 36
    [ "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"
        -- GhcCabal will run the configure script, so we depend on it
Andrey Mokhov's avatar
Andrey Mokhov committed
37 38
        need [pkgPath pkg </> pkgCabal pkg]
        -- We still don't know who built the configure script from configure.ac
39
        whenM (doesFileExist $ configure <.> "ac") $ need [configure]
40 41
        -- TODO: 1) automate? 2) mutliple files 3) vanilla?
        build $ fullTarget target (path </> "package-data.mk") GhcCabal vanilla
42
        -- TODO: when (registerPackage settings) $
43
        build $ fullTarget target (path </> "package-data.mk") (GhcPkg stage) vanilla
Andrey Mokhov's avatar
Andrey Mokhov committed
44
        postProcessPackageData $ path </> "package-data.mk"
45

Andrey Mokhov's avatar
Andrey Mokhov committed
46 47 48 49 50 51 52 53 54
-- 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
55

Andrey Mokhov's avatar
Andrey Mokhov committed
56 57 58 59 60 61 62 63
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