如何以编程方式检索 GHC 包信息?
更具体地说,给定任意包名称,我需要检索相同的 library-dirs
字段,该字段可以通过运行的 Haskell 程序内部的 ghc-pkg describe
命令获取。
More specifically, given an arbritary package name I need to retrieve the same library-dirs
field that can be obtained with the ghc-pkg describe
command from inside a running Haskell program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是我通过查看 ghc-pkg 源代码得出的结论。
getPkgInfos 函数返回所有已安装软件包的软件包定义(希望包括用户安装的软件包)。有了这个,您就可以检索库目录和其他包信息。请参阅文档了解细节。
GHC_PKGCONF
变量需要指向不位于通常位置的系统的全局包配置文件。例如,ghc-pkg 通过在 Ubuntu 中通过包装脚本接收命令行标志来解决这个问题。我自己编写了这段代码,但它很大程度上受到 ghc-pkg 的启发(其中一些部分逐字复制)。原始代码是根据 BSD 风格的许可证获得许可的,我认为这可以根据所有 Stackoverflow 内容所在的 cc-wiki 许可证进行分发,但我不太确定。无论如何,与其他任何事情一样,我做了一些初步测试,它似乎有效,但使用它需要您自担风险。
Here's what I could come up with by peeking into the
ghc-pkg
source code.The
getPkgInfos
function returns the package definitions for all installed packages (hopefully including user-installed packages). With this in your hands, you can retrieve the library directories and other package information. See the documentation for details.The
GHC_PKGCONF
variable needs to point to the global package config file for systems where it isn't located at the usual place.ghc-pkg
solves this problem by receiving a command line flag via a wrapper script in Ubuntu, for instance.I wrote this code myself, but it was largely inspired by ghc-pkg (with some pieces copied verbatim). The original code was licensed under a BSD-style license, I think this can be distributed under the cc-wiki license all Stackoverflow content is under, but I'm not really sure. Anyway, as anything else, I did some initial testing and it seems to work, but use it at your own risk.
已安装软件包数据库的格式为
Distribution.InstalledPackageInfo
。这不遵循用户的包配置,但应该是一个开始。
The format of the installed packages database is
Distribution.InstalledPackageInfo
.This doesn't obey the user's package configuration, but should be a start.
在 haskell-cafe@ 或 cabal 邮件列表上询问 Duncan Coutts。 (我是认真的。这是一个比堆栈溢出更好的解决阴谋集团问题的论坛)。
有时您只需将人们指向不同的论坛即可。
Ask Duncan Coutts on the haskell-cafe@ or cabal mailing lists. (I'm serious. That is a better forum for Cabal questions than stack overflow).
Sometimes you just have to point people at a different forum.
如果您使用 cabal 来配置和构建您的程序/库,您可以使用自动生成的 Paths_* 模块。
例如,如果您有一个
foo.cabal
文件,cabal 将生成一个Paths_foo
模块(请参阅dist/build/autogen
下的源代码)您可以导入。该模块导出一个函数getLibDir :: IO FilePath
,其中包含您要查找的值。If you're using cabal to configure and build your program/library you can used the autogenerated Paths_* module.
For example, if you have a
foo.cabal
file, cabal will generate aPaths_foo
module (see its source underdist/build/autogen
) which you can import. This module exports a functiongetLibDir :: IO FilePath
which has the value you're looking for.