Skip to content

Commit f968b41

Browse files
committed
Monitor the pkg-config db for changes
We use the pkg-config db as an input for the solver, so we ought to monitor it for changes. The pkg-config tool makes this possible, if not totally trivial.
1 parent 9aab26e commit f968b41

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

cabal-install/Distribution/Client/PkgConfigDb.hs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@
1111
-- Read the list of packages available to pkg-config.
1212
-----------------------------------------------------------------------------
1313
module Distribution.Client.PkgConfigDb
14-
(
15-
PkgConfigDb
14+
( PkgConfigDb
1615
, readPkgConfigDb
1716
, pkgConfigDbFromList
1817
, pkgConfigPkgIsPresent
18+
, getPkgConfigDbDirs
1919
) where
2020

2121
#if !MIN_VERSION_base(4,8,0)
22-
import Control.Applicative ((<$>))
22+
import Control.Applicative ((<$>), (<*>))
2323
#endif
2424

2525
import Control.Exception (IOException, handle)
2626
import Data.Char (isSpace)
2727
import qualified Data.Map as M
2828
import Data.Version (parseVersion)
2929
import Text.ParserCombinators.ReadP (readP_to_S)
30+
import System.FilePath (splitSearchPath)
3031

3132
import Distribution.Package
3233
( PackageName(..) )
@@ -35,6 +36,8 @@ import Distribution.Verbosity
3536
import Distribution.Version
3637
( Version, VersionRange, withinRange )
3738

39+
import Distribution.Compat.Environment
40+
( lookupEnv )
3841
import Distribution.Simple.Program
3942
( ProgramConfiguration, pkgConfigProgram, getProgramOutput,
4043
requireProgram )
@@ -101,3 +104,43 @@ pkgConfigPkgIsPresent (PkgConfigDb db) pn vr =
101104
-- executed later on, but we have no grounds for rejecting the plan at
102105
-- this stage.
103106
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+

cabal-install/Distribution/Client/ProjectPlanning.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,7 @@ rebuildInstallPlan verbosity
413413
compiler progdb platform
414414
corePackageDbs
415415
sourcePkgDb <- getSourcePackages verbosity withRepoCtx
416-
pkgConfigDB <- liftIO $
417-
readPkgConfigDb verbosity progdb
416+
pkgConfigDB <- getPkgConfigDb verbosity progdb
418417

419418
--TODO: [code cleanup] it'd be better if the Compiler contained the
420419
-- ConfiguredPrograms that it needs, rather than relying on the progdb
@@ -630,6 +629,16 @@ createPackageDBIfMissing verbosity compiler progdb packageDbs =
630629
_ -> return ()
631630

632631

632+
getPkgConfigDb :: Verbosity -> ProgramDb -> Rebuild PkgConfigDb
633+
getPkgConfigDb verbosity progdb = do
634+
dirs <- liftIO $ getPkgConfigDbDirs verbosity progdb
635+
monitorFiles (map monitorDirectory dirs)
636+
-- Just monitor the dirs so we'll notice new .pc files.
637+
-- Alternatively we could monitor all the .pc files too.
638+
639+
liftIO $ readPkgConfigDb verbosity progdb
640+
641+
633642
recreateDirectory :: Verbosity -> Bool -> FilePath -> Rebuild ()
634643
recreateDirectory verbosity createParents dir = do
635644
liftIO $ createDirectoryIfMissingVerbose verbosity createParents dir

0 commit comments

Comments
 (0)