11
11
-- Read the list of packages available to pkg-config.
12
12
-----------------------------------------------------------------------------
13
13
module Distribution.Client.PkgConfigDb
14
- (
15
- PkgConfigDb
14
+ ( PkgConfigDb
16
15
, readPkgConfigDb
17
16
, pkgConfigDbFromList
18
17
, pkgConfigPkgIsPresent
18
+ , getPkgConfigDbDirs
19
19
) where
20
20
21
21
#if !MIN_VERSION_base(4,8,0)
22
- import Control.Applicative ((<$>) )
22
+ import Control.Applicative ((<$>) , (<*>) )
23
23
#endif
24
24
25
25
import Control.Exception (IOException , handle )
26
26
import Data.Char (isSpace )
27
27
import qualified Data.Map as M
28
28
import Data.Version (parseVersion )
29
29
import Text.ParserCombinators.ReadP (readP_to_S )
30
+ import System.FilePath (splitSearchPath )
30
31
31
32
import Distribution.Package
32
33
( PackageName (.. ) )
@@ -35,6 +36,8 @@ import Distribution.Verbosity
35
36
import Distribution.Version
36
37
( Version , VersionRange , withinRange )
37
38
39
+ import Distribution.Compat.Environment
40
+ ( lookupEnv )
38
41
import Distribution.Simple.Program
39
42
( ProgramConfiguration , pkgConfigProgram , getProgramOutput ,
40
43
requireProgram )
@@ -101,3 +104,43 @@ pkgConfigPkgIsPresent (PkgConfigDb db) pn vr =
101
104
-- executed later on, but we have no grounds for rejecting the plan at
102
105
-- this stage.
103
106
pkgConfigPkgIsPresent NoPkgConfigDb _ _ = True
107
+
108
+
109
+ -- | Query pkg-config for the locations of pkg-config's package files. Use this
110
+ -- to monitor for changes in the pkg-config DB.
111
+ --
112
+ getPkgConfigDbDirs :: Verbosity -> ProgramConfiguration -> IO [FilePath ]
113
+ getPkgConfigDbDirs verbosity conf =
114
+ (++) <$> getEnvPath <*> getDefPath
115
+ where
116
+ -- According to @man pkg-config@:
117
+ --
118
+ -- PKG_CONFIG_PATH
119
+ -- A colon-separated (on Windows, semicolon-separated) list of directories
120
+ -- to search for .pc files. The default directory will always be searched
121
+ -- after searching the path
122
+ --
123
+ getEnvPath = maybe [] parseSearchPath
124
+ <$> lookupEnv " PKG_CONFIG_PATH"
125
+
126
+ -- Again according to @man pkg-config@:
127
+ --
128
+ -- pkg-config can be used to query itself for the default search path,
129
+ -- version number and other information, for instance using:
130
+ --
131
+ -- > pkg-config --variable pc_path pkg-config
132
+ --
133
+ getDefPath = handle ioErrorHandler $ do
134
+ (pkgConfig, _) <- requireProgram verbosity pkgConfigProgram conf
135
+ parseSearchPath <$>
136
+ getProgramOutput verbosity pkgConfig
137
+ [" --variable" , " pc_path" , " pkg-config" ]
138
+
139
+ parseSearchPath str =
140
+ case lines str of
141
+ [p] | not (null p) -> splitSearchPath p
142
+ _ -> []
143
+
144
+ ioErrorHandler :: IOException -> IO [FilePath ]
145
+ ioErrorHandler _e = return []
146
+
0 commit comments