PackageData.hs 4.32 KB
Newer Older
1
{-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving #-}
2
3

module Oracles.PackageData (
4
5
    PackageData (..), PackageDataList (..),
    pkgData, pkgDataList, packageDataOracle
6
7
    ) where

8
import Base
9
import Util
10
import Data.List
11
import Data.Maybe
12
13
import Control.Applicative
import qualified Data.HashMap.Strict as Map
14

15
16
-- For each (PackageData path) the file 'path/package-data.mk' contains
-- a line of the form 'path_VERSION = 1.2.3.4'.
17
-- pkgData $ PackageData path is an action that consults the file and
18
19
-- returns "1.2.3.4".
--
20
-- PackageDataList is used for multiple string options separated by spaces,
21
-- such as 'path_MODULES = Data.Array Data.Array.Base ...'.
22
-- pkgListData Modules therefore returns ["Data.Array", "Data.Array.Base", ...]
23
24
25
26
data PackageData = Version      FilePath
                 | PackageKey   FilePath
                 | Synopsis     FilePath
                 | BuildGhciLib FilePath
27

28
data PackageDataList = Modules        FilePath
29
                     | HiddenModules  FilePath
30
31
32
33
34
35
36
37
38
39
                     | SrcDirs        FilePath
                     | IncludeDirs    FilePath
                     | Deps           FilePath
                     | DepKeys        FilePath
                     | DepNames       FilePath
                     | CppArgs        FilePath
                     | HsArgs         FilePath
                     | CcArgs         FilePath
                     | CSrcs          FilePath
                     | DepIncludeDirs FilePath
40
41

newtype PackageDataKey = PackageDataKey (FilePath, String)
42
    deriving (Show, Typeable, Eq, Hashable, Binary, NFData)
43

44
-- TODO: is this needed?
45
46
47
askPackageData :: FilePath -> String -> Action String
askPackageData path key = do
    let fullKey = replaceSeparators '_' $ path ++ "_" ++ key
48
        pkgData = path -/- "package-data.mk"
49
50
51
52
    value <- askOracle $ PackageDataKey (pkgData, fullKey)
    return $ fromMaybe
        (error $ "No key '" ++ key ++ "' in " ++ pkgData ++ ".") value

53
54
55
pkgData :: PackageData -> Action String
pkgData packageData = do
    let (key, path) = case packageData of
56
57
58
59
           Version      path -> ("VERSION"       , path)
           PackageKey   path -> ("PACKAGE_KEY"   , path)
           Synopsis     path -> ("SYNOPSIS"      , path)
           BuildGhciLib path -> ("BUILD_GHCI_LIB", path)
60
        fullKey = replaceSeparators '_' $ path ++ "_" ++ key
61
        pkgData = path -/- "package-data.mk"
62
63
    res <- askOracle $ PackageDataKey (pkgData, fullKey)
    return $ fromMaybe
64
        (error $ "No key '" ++ key ++ "' in " ++ pkgData ++ ".") res
65

66
67
pkgDataList :: PackageDataList -> Action [String]
pkgDataList packageData = do
68
69
    let (key, path, defaultValue) = case packageData of
           Modules        path -> ("MODULES"                       , path, "" )
70
           HiddenModules  path -> ("HIDDEN_MODULES"                , path, "" )
71
72
73
74
75
76
77
78
79
80
81
           SrcDirs        path -> ("HS_SRC_DIRS"                   , path, ".")
           IncludeDirs    path -> ("INCLUDE_DIRS"                  , path, ".")
           Deps           path -> ("DEPS"                          , path, "" )
           DepKeys        path -> ("DEP_KEYS"                      , path, "" )
           DepNames       path -> ("DEP_NAMES"                     , path, "" )
           CppArgs        path -> ("CPP_OPTS"                      , path, "" )
           HsArgs         path -> ("HC_OPTS"                       , path, "" )
           CcArgs         path -> ("CC_OPTS"                       , path, "" )
           CSrcs          path -> ("C_SRCS"                        , path, "" )
           DepIncludeDirs path -> ("DEP_INCLUDE_DIRS_SINGLE_QUOTED", path, "" )
        fullKey = replaceSeparators '_' $ path ++ "_" ++ key
82
        pkgData = path -/- "package-data.mk"
83
84
85
        unquote = dropWhile (== '\'') . dropWhileEnd (== '\'')
    res <- askOracle $ PackageDataKey (pkgData, fullKey)
    return $ map unquote $ words $ case res of
86
        Nothing    -> error $ "No key '" ++ key ++ "' in " ++ pkgData ++ "."
87
88
        Just ""    -> defaultValue
        Just value -> value
89
90
91
92
93
94
95
96

-- Oracle for 'package-data.mk' files
packageDataOracle :: Rules ()
packageDataOracle = do
    pkgData <- newCache $ \file -> do
        need [file]
        putOracle $ "Reading " ++ file ++ "..."
        liftIO $ readConfigFile file
Andrey Mokhov's avatar
Andrey Mokhov committed
97
    addOracle $ \(PackageDataKey (file, key)) ->
98
99
        Map.lookup key <$> pkgData (unifyPath file)
    return ()