[CMake] How to assign version numbers from a source header to CMake variables?

SF Markus Elfring elfring at users.sourceforge.net
Mon Jan 17 11:30:17 EST 2011


> Here is the code:

I have found and implemented a solution that can extract data from a header file 
with the "help" from evaluation of regular expressions.

file(STRINGS "${CPPCHECK_SOURCE_DIR}/lib/library_version.h"
      CPPCHECK_BUILD_SPECIFICATION REGEX "^[ \t]*#define[ 
\t]+CPPCHECK_LIBRARY_VERSION_[A-Z]+[ \t]+[0-9]+.*$")

if(CPPCHECK_BUILD_SPECIFICATION)
    message(STATUS "${CPPCHECK_BUILD_SPECIFICATION}")
    foreach(item IN ITEMS MAJOR MINOR PATCH)
       string(REGEX REPLACE ".*#define[ \t]+CPPCHECK_LIBRARY_VERSION_${item}[ 
\t]+([0-9]+).*"
              "\\1" XYZ ${CPPCHECK_BUILD_SPECIFICATION})
       set("CPPCHECK_BUILD_VERSION_${item}" ${XYZ} CACHE STRING "Version number 
for the build of the Cppcheck software")
    endforeach()
else()
    message(FATAL_ERROR "Data were not found for the required build specification.")
endif()


How do you think about any optimisation opportunities like it is mentioned in 
the feature request "Directly reading data from a file into variables"?
http://public.kitware.com/Bug/view.php?id=11714

Regards,
Markus


More information about the CMake mailing list