[CMake] Interrogate header files without TRY_RUN

Alexander Neundorf a.neundorf-work at gmx.net
Thu Jun 16 12:58:24 EDT 2011


On Thursday 16 June 2011, David Gobbi wrote:
> Hi All,
> 
> Is there a way for CMake to search for macro definitions in header
> files without doing a try_run?  The reason I'm asking is that
> eventually I'll require FindPython.cmake to report the version of
> python that it finds.  But I don't want to do this by checking the
> directory names (which is unreliable) or by doing a try_run (for the
> sake of cross-compilation).  Running the python executable itself to
> find the version is also out, due to cross-compilation.
> 
> For the VTK wrappers I've written a pseudo-preprocessor
> (vtkWrapPreprocess.c) that does essentially what I need, but not at
> configure time.  I'm thinking of the same thing for CMake... a simple
> built-in preprocessor that can emulate a compiler and give cmake
> access to macro definitions without the need for a try_run.  Or
> equivalently the real compiler could run its preprocessor stage and
> then present the results to cmake.  The main thing is to avoid the
> try_run.

You could simply read the header file and try to grep fpor the version 
numbers. Works quite good in many cases.

Or you could try to mess around with try_compile(), and search in the produced 
binary for the version strings. This may need some clever tricks to actually 
create such a string in the executable.
CMake does that for a few things, e.g. to determine the sizes of types, 
compiler ID, and I think more, see e.g. Modules/CheckTypeSize.*.

Alex


More information about the CMake mailing list