PackageData.hs 3.93 KB
Newer Older
1
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2
module Oracles.PackageData (
Andrey Mokhov's avatar
Andrey Mokhov committed
3
    PackageData (..), PackageDataList (..), pkgData, pkgDataList, packageDataOracle
4
5
    ) where

6
import Development.Shake.Config
7
import qualified Data.HashMap.Strict as Map
8

Andrey Mokhov's avatar
Andrey Mokhov committed
9
10
import Base

11
data PackageData = BuildGhciLib FilePath
12
                 | ComponentId  FilePath
13
                 | Synopsis     FilePath
14
                 | Version      FilePath
15

16
17
data PackageDataList = AsmSrcs        FilePath
                     | CcArgs         FilePath
Andrey Mokhov's avatar
Andrey Mokhov committed
18
                     | CSrcs          FilePath
19
                     | CmmSrcs        FilePath
Andrey Mokhov's avatar
Andrey Mokhov committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
                     | CppArgs        FilePath
                     | DepCcArgs      FilePath
                     | DepExtraLibs   FilePath
                     | DepIds         FilePath
                     | DepIncludeDirs FilePath
                     | DepLdArgs      FilePath
                     | DepLibDirs     FilePath
                     | DepNames       FilePath
                     | Deps           FilePath
                     | HiddenModules  FilePath
                     | HsArgs         FilePath
                     | IncludeDirs    FilePath
                     | LdArgs         FilePath
                     | Modules        FilePath
                     | SrcDirs        FilePath
35
36

newtype PackageDataKey = PackageDataKey (FilePath, String)
Andrey Mokhov's avatar
Andrey Mokhov committed
37
    deriving (Binary, Eq, Hashable, NFData, Show, Typeable)
38

39
askPackageData :: FilePath -> String -> Action String
40
41
askPackageData path key = fromMaybe "" <$>
    askOracle (PackageDataKey (path -/- "package-data.mk", key))
42

Andrey Mokhov's avatar
Andrey Mokhov committed
43
44
45
-- | For each @PackageData path@ the file 'path/package-data.mk' contains a line
-- of the form 'path_VERSION = 1.2.3.4'. @pkgData (PackageData path)@ is an
-- Action that consults the file and returns "1.2.3.4".
46
pkgData :: PackageData -> Action String
47
pkgData packageData = case packageData of
48
    BuildGhciLib path -> askPackageData path "BUILD_GHCI_LIB"
49
    ComponentId  path -> askPackageData path "COMPONENT_ID"
50
    Synopsis     path -> askPackageData path "SYNOPSIS"
51
    Version      path -> askPackageData path "VERSION"
52

Andrey Mokhov's avatar
Andrey Mokhov committed
53
54
55
-- | @PackageDataList path@ is used for multiple string options separated by
-- spaces, such as @path_MODULES = Data.Array Data.Array.Base ...@.
-- @pkgListData Modules@ therefore returns ["Data.Array", "Data.Array.Base", ...]
56
pkgDataList :: PackageDataList -> Action [String]
57
pkgDataList packageData = fmap (map unquote . words) $ case packageData of
58
    AsmSrcs        path -> askPackageData path "S_SRCS"
Andrey Mokhov's avatar
Andrey Mokhov committed
59
60
    CcArgs         path -> askPackageData path "CC_OPTS"
    CSrcs          path -> askPackageData path "C_SRCS"
61
    CmmSrcs        path -> askPackageData path "CMM_SRCS"
Andrey Mokhov's avatar
Andrey Mokhov committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    CppArgs        path -> askPackageData path "CPP_OPTS"
    DepCcArgs      path -> askPackageData path "DEP_CC_OPTS"
    DepExtraLibs   path -> askPackageData path "DEP_EXTRA_LIBS"
    DepIds         path -> askPackageData path "DEP_IPIDS"
    DepIncludeDirs path -> askPackageData path "DEP_INCLUDE_DIRS_SINGLE_QUOTED"
    DepLibDirs     path -> askPackageData path "DEP_LIB_DIRS_SINGLE_QUOTED"
    DepLdArgs      path -> askPackageData path "DEP_LD_OPTS"
    DepNames       path -> askPackageData path "DEP_NAMES"
    Deps           path -> askPackageData path "DEPS"
    HiddenModules  path -> askPackageData path "HIDDEN_MODULES"
    HsArgs         path -> askPackageData path "HC_OPTS"
    IncludeDirs    path -> askPackageData path "INCLUDE_DIRS"
    LdArgs         path -> askPackageData path "LD_OPTS"
    Modules        path -> askPackageData path "MODULES"
    SrcDirs        path -> askPackageData path "HS_SRC_DIRS"
77
78
  where
    unquote = dropWhile (== '\'') . dropWhileEnd (== '\'')
79

Andrey Mokhov's avatar
Andrey Mokhov committed
80
-- | Oracle for 'package-data.mk' files.
81
packageDataOracle :: Rules ()
Andrey Mokhov's avatar
Andrey Mokhov committed
82
packageDataOracle = void $ do
83
    keys <- newCache $ \file -> do
84
        need [file]
85
        putLoud $ "Reading " ++ file ++ "..."
86
        liftIO $ readConfigFile file
Andrey Mokhov's avatar
Andrey Mokhov committed
87
    addOracle $ \(PackageDataKey (file, key)) -> Map.lookup key <$> keys file