在python中访问autoconf定义的符号

发布于 2024-08-20 05:51:07 字数 322 浏览 10 评论 0原文

我正在开发一个用 C++ 和 python 编写的项目。我的configure.ac中有以下行:

AC_INIT(MILHOUSE, 0.3.6)

这意味着在运行configure生成的config.h中,我有以下定义行:

/* Define to the version of this package. */
#define PACKAGE_VERSION "0.3.6"  

我只是想知道是否有一个现有的模块用于解析这样的配置符号或在至少有一种在 python 中访问这些定义的标准方法。

I'm working on a project that is written in both C++ and python. I have the following line in my configure.ac:

AC_INIT(MILHOUSE, 0.3.6)

which means that in the config.h generated by running configure, i have the following define line:

/* Define to the version of this package. */
#define PACKAGE_VERSION "0.3.6"  

I just wanted to know if there was an existing module for parsing configure symbols like this or at least a standard way of accessing these defines in python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灼痛 2024-08-27 05:51:07

AC_INIT 不仅定义了预处理器符号,还定义了输出变量。当您列出一个文件时,我们将其称为 somefile,在您的 AC_CONFIG_FILES 宏中,您的 configure 脚本会查找名为 somefile 的文件。 in,并将@符号之间的任何输出变量的名称替换为其值,调用结果somefile

因此,要在 Python 文件 somescript.py 中访问这些定义,请在 configure.ac 中添加如下内容:

AC_INIT(MILHOUSE, 0.3.6)
...blah blah...
AC_CONFIG_FILES([
  some/Makefile
  some/other/Makefile
  somescript.py
])

然后将您的 Python 文件命名为 somescript.py。 in 并访问 PACKAGE_VERSION 输出变量,如下所示:

version = '''@PACKAGE_VERSION@'''

三引号可能是明智的,因为您永远不知道输出变量何时可能包含引号。

AC_INIT not only defines preprocessor symbols, it also defines output variables. When you list a file, let's call it somefile, in your AC_CONFIG_FILES macro, your configure script looks for a file called somefile.in, and replaces the names of any output variables between @-signs with their values, calling the result somefile.

So, to access these definitions in a Python file somescript.py, put something like this in your configure.ac:

AC_INIT(MILHOUSE, 0.3.6)
...blah blah...
AC_CONFIG_FILES([
  some/Makefile
  some/other/Makefile
  somescript.py
])

Then name your Python file somescript.py.in and access the PACKAGE_VERSION output variable like this:

version = '''@PACKAGE_VERSION@'''

The triple quotes are probably wise, because you never know when an output variable might contain a quote.

逐鹿 2024-08-27 05:51:07

pyparsing wiki 的示例页面包括 此宏扩展器示例。以下是它处理的示例代码:

#def A 100
#def ALEN A+1

char Astring[ALEN];
char AA[A];
typedef char[ALEN] Acharbuf;

因此它还将处理根据其他宏定义的宏。将“#def”更改为“#define”应该不难。

The examples page of the pyparsing wiki includes this example of a macro expander. Here is the sample code that it processes:

#def A 100
#def ALEN A+1

char Astring[ALEN];
char AA[A];
typedef char[ALEN] Acharbuf;

So it will also handle macros that are defined in terms of other macros. Should not be difficult to change '#def' to '#define'.

肥爪爪 2024-08-27 05:51:07

添加到接受的答案:如果您对自定义定义的变量感兴趣,请务必使用AC_SUBST 除了 AC_DEFINE[_UNQUOTED] 以外,您的配置文件中不会替换任何内容。使用这个其他答案中的提示,我将其添加到我的configure.ac中,

AC_DEFUN([AX_DEFINE_SUBST], [
AC_DEFINE_UNQUOTED([$1], [$2], [$3])
AC_SUBST([$1], [$2])
])
...
AX_DEFINE_SUBST([OUTPUT_DIRECTORY], "$with_output", [output directory])

因此在< code>config.h 我得到

/* output directory */
#define OUTPUT_DIRECTORY "/some/directory/"

并且 config.py.in 从转换

output_directory = '''@OUTPUT_DIRECTORY@'''

config.py

output_directory = '''/some/directory/'''

Adding to the accepted answer: if you are interested in a custom defined variable, be sure to use AC_SUBST in addition to AC_DEFINE[_UNQUOTED] else nothing is replaced in your config files. Using the hints from this other answer, I added this to my configure.ac

AC_DEFUN([AX_DEFINE_SUBST], [
AC_DEFINE_UNQUOTED([$1], [$2], [$3])
AC_SUBST([$1], [$2])
])
...
AX_DEFINE_SUBST([OUTPUT_DIRECTORY], "$with_output", [output directory])

so in config.h I get

/* output directory */
#define OUTPUT_DIRECTORY "/some/directory/"

and config.py.in is converted from

output_directory = '''@OUTPUT_DIRECTORY@'''

to config.py

output_directory = '''/some/directory/'''
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文