Skip to content

Commit

Permalink
Monitor the pkg-config db for changes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dcoutts committed Apr 12, 2016
1 parent 9aab26e commit f968b41
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
49 changes: 46 additions & 3 deletions cabal-install/Distribution/Client/PkgConfigDb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@
-- Read the list of packages available to pkg-config.
-----------------------------------------------------------------------------
module Distribution.Client.PkgConfigDb
(
PkgConfigDb
( PkgConfigDb
, readPkgConfigDb
, pkgConfigDbFromList
, pkgConfigPkgIsPresent
, getPkgConfigDbDirs
) where

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>))
import Control.Applicative ((<$>), (<*>))
#endif

import Control.Exception (IOException, handle)
import Data.Char (isSpace)
import qualified Data.Map as M
import Data.Version (parseVersion)
import Text.ParserCombinators.ReadP (readP_to_S)
import System.FilePath (splitSearchPath)

import Distribution.Package
( PackageName(..) )
Expand All @@ -35,6 +36,8 @@ import Distribution.Verbosity
import Distribution.Version
( Version, VersionRange, withinRange )

import Distribution.Compat.Environment
( lookupEnv )
import Distribution.Simple.Program
( ProgramConfiguration, pkgConfigProgram, getProgramOutput,
requireProgram )
Expand Down Expand Up @@ -101,3 +104,43 @@ pkgConfigPkgIsPresent (PkgConfigDb db) pn vr =
-- executed later on, but we have no grounds for rejecting the plan at
-- this stage.
pkgConfigPkgIsPresent NoPkgConfigDb _ _ = True


-- | Query pkg-config for the locations of pkg-config's package files. Use this
-- to monitor for changes in the pkg-config DB.
--
getPkgConfigDbDirs :: Verbosity -> ProgramConfiguration -> IO [FilePath]
getPkgConfigDbDirs verbosity conf =
(++) <$> getEnvPath <*> getDefPath
where
-- According to @man pkg-config@:
--
-- PKG_CONFIG_PATH
-- A colon-separated (on Windows, semicolon-separated) list of directories
-- to search for .pc files. The default directory will always be searched
-- after searching the path
--
getEnvPath = maybe [] parseSearchPath
<$> lookupEnv "PKG_CONFIG_PATH"

-- Again according to @man pkg-config@:
--
-- pkg-config can be used to query itself for the default search path,
-- version number and other information, for instance using:
--
-- > pkg-config --variable pc_path pkg-config
--
getDefPath = handle ioErrorHandler $ do
(pkgConfig, _) <- requireProgram verbosity pkgConfigProgram conf
parseSearchPath <$>
getProgramOutput verbosity pkgConfig
["--variable", "pc_path", "pkg-config"]

parseSearchPath str =
case lines str of
[p] | not (null p) -> splitSearchPath p
_ -> []

ioErrorHandler :: IOException -> IO [FilePath]
ioErrorHandler _e = return []

13 changes: 11 additions & 2 deletions cabal-install/Distribution/Client/ProjectPlanning.hs
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,7 @@ rebuildInstallPlan verbosity
compiler progdb platform
corePackageDbs
sourcePkgDb <- getSourcePackages verbosity withRepoCtx
pkgConfigDB <- liftIO $
readPkgConfigDb verbosity progdb
pkgConfigDB <- getPkgConfigDb verbosity progdb

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


getPkgConfigDb :: Verbosity -> ProgramDb -> Rebuild PkgConfigDb
getPkgConfigDb verbosity progdb = do
dirs <- liftIO $ getPkgConfigDbDirs verbosity progdb
monitorFiles (map monitorDirectory dirs)
-- Just monitor the dirs so we'll notice new .pc files.
-- Alternatively we could monitor all the .pc files too.

liftIO $ readPkgConfigDb verbosity progdb


recreateDirectory :: Verbosity -> Bool -> FilePath -> Rebuild ()
recreateDirectory verbosity createParents dir = do
liftIO $ createDirectoryIfMissingVerbose verbosity createParents dir
Expand Down

0 comments on commit f968b41

Please sign in to comment.