views:

100

answers:

2

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.

A: 

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

Paul McGuire
that would work, but i'm looking for a more generic solution to access configure-time symbols from python, rather than explicitly reading the config.h file at runtime.
tmatth
+1  A: 

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.

ptomato
perfect! great answer, exactly what I was looking for and applicable in other contexts (i.e. non-python) as well. thanks.
tmatth