Skip to content

Commit

Permalink
Include datadirs of a component's dependencies in the Paths_* module
Browse files Browse the repository at this point in the history
The added constant is `dependencies_datadirs :: [(String, FilePath)]`,
which is a mapping from the dependency's package name to the data
directory of the installed depenency.
  • Loading branch information
mmhat committed Feb 7, 2025
1 parent 595d023 commit b6c7d39
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Cabal/src/Distribution/Simple/Build/PathsModule.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ import Distribution.Package
import Distribution.PackageDescription
import Distribution.Simple.Compiler
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.PackageIndex (allPackagesForUnitIds)
import Distribution.Simple.Utils (shortRelativePath)
import Distribution.System
import Distribution.Version

import qualified Data.Set as Set
import qualified Distribution.Simple.Build.PathsModule.Z as Z
import qualified Distribution.Types.InstalledPackageInfo as InstalledPackageInfo
import qualified Distribution.Types.LocalBuildConfig as LocalBuildConfig
import qualified System.FilePath as FilePath

-- ------------------------------------------------------------

Expand Down Expand Up @@ -61,6 +66,7 @@ generatePathsModule pkg_descr lbi clbi =
, Z.zDatadir = zDatadir
, Z.zLibexecdir = zLibexecdir
, Z.zSysconfdir = zSysconfdir
, Z.zDependenciesDatadirs = zDependenciesDatadirs
}
where
supports_cpp = supports_language_pragma
Expand Down Expand Up @@ -148,6 +154,29 @@ generatePathsModule pkg_descr lbi clbi =
flat_libexecdir_reloc = shortRelativePath flat_prefix flat_libexecdir
flat_sysconfdir_reloc = shortRelativePath flat_prefix flat_sysconfdir

zDependenciesDatadirs :: [(PackageName, FilePath)]
zDependenciesDatadirs =
[ (packageName info, directory)
| info <- dependenciesPackageInfos
, let directory = InstalledPackageInfo.dataDir info
, FilePath.isAbsolute directory
]

dependenciesPackageInfos =
allPackagesForUnitIds dependenciesUnitIds installedPackageIndex

dependenciesUnitIds =
Set.fromList
. map fst
. componentPackageDeps
$ clbi

installedPackageIndex =
LocalBuildConfig.installedPkgs
. LocalBuildConfig.componentBuildDescr
. localBuildDescr
$ lbi

-- | Generates the name of the environment variable controlling the path
-- component of interest.
--
Expand Down
18 changes: 17 additions & 1 deletion Cabal/src/Distribution/Simple/Build/PathsModule/Z.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{- FOURMOLU_DISABLE -}
{-# LANGUAGE DeriveGeneric #-}

module Distribution.Simple.Build.PathsModule.Z (render, Z(..)) where

import Distribution.ZinzaPrelude

import qualified Data.List as List

data Z
= Z {zPackageName :: PackageName,
zVersionDigits :: String,
Expand All @@ -20,9 +25,11 @@ data Z
zDatadir :: FilePath,
zLibexecdir :: FilePath,
zSysconfdir :: FilePath,
zDependenciesDatadirs :: [(PackageName, FilePath)],
zNot :: (Bool -> Bool),
zManglePkgName :: (PackageName -> String)}
deriving Generic

render :: Z -> String
render z_root = execWriter $ do
if (zSupportsCpp z_root)
Expand Down Expand Up @@ -58,7 +65,7 @@ render z_root = execWriter $ do
tell " (\n"
tell " version,\n"
tell " getBinDir, getLibDir, getDynLibDir, getDataDir, getLibexecDir,\n"
tell " getDataFileName, getSysconfDir\n"
tell " getDataFileName, getSysconfDir, dependencies_datadirs\n"
tell " ) where\n"
tell "\n"
if (zNot z_root (zAbsolute z_root))
Expand Down Expand Up @@ -111,6 +118,15 @@ render z_root = execWriter $ do
tell " dir <- getDataDir\n"
tell " return (dir `joinFileName` name)\n"
tell "\n"
tell "dependencies_datadirs :: [(String, FilePath)]\n"
tell "dependencies_datadirs =\n"
tell " [ "
tell
$ List.intercalate " , "
$ map (\(name, directory) -> "(\"" ++ prettyShow name ++ "\", \"" ++ directory ++ "\")\n")
$ zDependenciesDatadirs z_root
tell " ]\n"
tell "\n"
tell "getBinDir, getLibDir, getDynLibDir, getDataDir, getLibexecDir, getSysconfDir :: IO FilePath\n"
tell "\n"
let
Expand Down
6 changes: 6 additions & 0 deletions Cabal/src/Distribution/Simple/PackageIndex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ module Distribution.Simple.PackageIndex
, allPackagesByName
, allPackagesBySourcePackageId
, allPackagesBySourcePackageIdAndLibName
, allPackagesForUnitIds

-- ** Special queries
, brokenPackages
Expand Down Expand Up @@ -424,6 +425,11 @@ allPackagesBySourcePackageIdAndLibName index =
, ipkgs@(ipkg : _) <- Map.elems pvers
]

-- | Get all packages from the index that match one of the provided `UnitId`s.
allPackagesForUnitIds :: Set UnitId -> PackageIndex a -> [a]
allPackagesForUnitIds unitIds (PackageIndex pids _pnames) =
Map.elems (Map.restrictKeys pids unitIds)

--

-- * Lookups
Expand Down

0 comments on commit b6c7d39

Please sign in to comment.