You can use the following helper function to automatically have the version component variables set up:
macro (setup_package_version_variables _packageName)
if (DEFINED ${_packageName}_VERSION)
string (REGEX MATCHALL "[0-9]+" _versionComponents "${${_packageName}_VERSION}")
list (LENGTH _versionComponents _len)
if (${_len} GREATER 0)
list(GET _versionComponents 0 ${_packageName}_VERSION_MAJOR)
endif()
if (${_len} GREATER 1)
list(GET _versionComponents 1 ${_packageName}_VERSION_MINOR)
endif()
if (${_len} GREATER 2)
list(GET _versionComponents 2 ${_packageName}_VERSION_PATCH)
endif()
if (${_len} GREATER 3)
list(GET _versionComponents 3 ${_packageName}_VERSION_TWEAK)
endif()
set (${_packageName}_VERSION_COUNT ${_len})
else()
set (${_packageName}_VERSION_COUNT 0)
set (${_packageName}_VERSION "")
endif()
endmacro()
The macro can be invoked in the following way:
set(MY_PROGRAM_VERSION "2.5.1")
setup_package_version_variables(MY_PROGRAM)
The macro also sets MY_PROGRAM_VERSION_COUNT
to the number of version components and MY_PROGRAM_VERSION_TWEAK
if the version number has 4 components (e.g., "2.5.1.0")