Improve infrastructure for Cabal file parsing
[ghc.git] / src / Oracles / PackageData.hs
1 module Oracles.PackageData (
2 PackageData (..), PackageDataList (..), pkgData, pkgDataList
3 ) where
4
5 import Hadrian.Oracles.TextFile
6
7 import Base
8
9 data PackageData = BuildGhciLib FilePath
10 | ComponentId FilePath
11 | Synopsis FilePath
12 | Version FilePath
13
14 data PackageDataList = AsmSrcs FilePath
15 | CcArgs FilePath
16 | CSrcs FilePath
17 | CmmSrcs FilePath
18 | CppArgs FilePath
19 | DepCcArgs FilePath
20 | DepExtraLibs FilePath
21 | DepIds FilePath
22 | DepIncludeDirs FilePath
23 | DepLdArgs FilePath
24 | DepLibDirs FilePath
25 | DepNames FilePath
26 | Deps FilePath
27 | HiddenModules FilePath
28 | HsArgs FilePath
29 | IncludeDirs FilePath
30 | LdArgs FilePath
31 | Modules FilePath
32 | SrcDirs FilePath
33
34 askPackageData :: FilePath -> String -> Action String
35 askPackageData path = lookupValueOrEmpty (path -/- "package-data.mk")
36
37 -- | For each @PackageData path@ the file 'path/package-data.mk' contains a line
38 -- of the form 'path_VERSION = 1.2.3.4'. @pkgData (PackageData path)@ is an
39 -- Action that consults the file and returns "1.2.3.4".
40 pkgData :: PackageData -> Action String
41 pkgData packageData = case packageData of
42 BuildGhciLib path -> askPackageData path "BUILD_GHCI_LIB"
43 ComponentId path -> askPackageData path "COMPONENT_ID"
44 Synopsis path -> askPackageData path "SYNOPSIS"
45 Version path -> askPackageData path "VERSION"
46
47 -- | @PackageDataList path@ is used for multiple string options separated by
48 -- spaces, such as @path_MODULES = Data.Array Data.Array.Base ...@.
49 -- @pkgListData Modules@ therefore returns ["Data.Array", "Data.Array.Base", ...]
50 pkgDataList :: PackageDataList -> Action [String]
51 pkgDataList packageData = fmap (map unquote . words) $ case packageData of
52 AsmSrcs path -> askPackageData path "S_SRCS"
53 CcArgs path -> askPackageData path "CC_OPTS"
54 CSrcs path -> askPackageData path "C_SRCS"
55 CmmSrcs path -> askPackageData path "CMM_SRCS"
56 CppArgs path -> askPackageData path "CPP_OPTS"
57 DepCcArgs path -> askPackageData path "DEP_CC_OPTS"
58 DepExtraLibs path -> askPackageData path "DEP_EXTRA_LIBS"
59 DepIds path -> askPackageData path "DEP_IPIDS"
60 DepIncludeDirs path -> askPackageData path "DEP_INCLUDE_DIRS_SINGLE_QUOTED"
61 DepLibDirs path -> askPackageData path "DEP_LIB_DIRS_SINGLE_QUOTED"
62 DepLdArgs path -> askPackageData path "DEP_LD_OPTS"
63 DepNames path -> askPackageData path "DEP_NAMES"
64 Deps path -> askPackageData path "DEPS"
65 HiddenModules path -> askPackageData path "HIDDEN_MODULES"
66 HsArgs path -> askPackageData path "HC_OPTS"
67 IncludeDirs path -> askPackageData path "INCLUDE_DIRS"
68 LdArgs path -> askPackageData path "LD_OPTS"
69 Modules path -> askPackageData path "MODULES"
70 SrcDirs path -> askPackageData path "HS_SRC_DIRS"
71 where
72 unquote = dropWhile (== '\'') . dropWhileEnd (== '\'')