开发者

Accessing autoconf defined symbols in 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开发者_Go百科.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.


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.


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'.


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/'''
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜