[CMake] External package version number

Eric Noulard eric.noulard at gmail.com
Mon May 18 06:08:12 EDT 2009


2009/5/18  <laurent at marzu.org>:
> Hello,
>
> I would like to retrieve the version number of an external package (=
> libxml2 or xerces-c++ or any other XML parser available).
>
> And, to format the version number in a "standard way" (at least for my
> package using either libxml2, xerces-c++ etc).
>
> So my first thought was to use "CheckCSourceRuns" to compile a test program
> and "execute_process" which will print on standard output the version number
> in order
>
> to get it in a cmake variable. But it seems that CheckCSourceRuns does not
> provide an "executable" to me in order to "give" it to "execute_process".

Right I think, CheckCSourceRuns, only want to do what it does,
telling you whether the source code you gave it was able to run.

> And I would not like to build a target just for this ..

OK.

> Is there a way to do such thing with cmake or should I add another target to
> my "cmake" in order to compile an executable doing the extraction
> of the version number ?

I would say you'd better try with

try_run  CMake builtin macro

which should precisely do what you want:

" Try compiling a srcfile.  Return TRUE or FALSE for success or failure
       in COMPILE_RESULT_VAR.  Then if the compile succeeded, run the
       executable and return its exit code in RUN_RESULT_VAR.  If the
       executable was built, but failed to run, then RUN_RESULT_VAR will be
       set to FAILED_TO_RUN.  COMPILE_OUTPUT_VARIABLE specifies the variable
       where the output from the compile step goes.  RUN_OUTPUT_VARIABLE
       specifies the variable where the output from the running executable
       goes."

note that CheckCSourceRuns uses try_run , but the former
does not gives you full control over the run just like try_run does.

Another way to go would be to propose a patch to CheckCSourceRuns,
with some (optional) interface enrichment
which may be uses to obtain the output of the run.

Note that another to go if you are targeting Linux platform may be to use
FindPkgConfig.cmake which makes use of pkg-config in order to retrieve
information about package.

I've said "if your are targeting Linux" because there has been several
discussion
here on the list regarding the usage of pkg-config on other platforms.
Obviously there are pros and cons.

I'm neither because I currently do not use pkg-config,
however you may search the list for that: site: http://tinyurl.com/peyjtg

-- 
Erk


More information about the CMake mailing list