# borrowed from https://gitlab.kitware.com/cmake/cmake/commit/7e142c5ac2be11097f7ff905b1606179803043d7#ba1ba9330ed10bb8897f93da52f40125b2d89737_91_137
set(_version_regexp "^(([0-9])+(\\.([0-9]+)(\\.([0-9]+)(\\.([0-9]+))?)?)?)?$")

function(set_version_variables PREFIX COMMAND VERSION_STRING)
  if(${COMMAND} STREQUAL "VERSION")
  else()
    message(FATAL_ERROR "Invalid COMMAND argument: ${COMMAND}")
    return()
  endif(${COMMAND} STREQUAL "VERSION")
  string(REGEX MATCH "${_version_regexp}" _version_match "${VERSION_STRING}")
  if(${CMAKE_MATCH_COUNT} GREATER 7)
    set(${PREFIX}_VERSION_TWEAK ${CMAKE_MATCH_8} PARENT_SCOPE)
  endif(${CMAKE_MATCH_COUNT} GREATER 7)
  if(${CMAKE_MATCH_COUNT} GREATER 5)
    set(${PREFIX}_VERSION_PATCH ${CMAKE_MATCH_6} PARENT_SCOPE)
  endif(${CMAKE_MATCH_COUNT} GREATER 5)
  if(${CMAKE_MATCH_COUNT} GREATER 3)
    set(${PREFIX}_VERSION_MINOR ${CMAKE_MATCH_4} PARENT_SCOPE)
  endif(${CMAKE_MATCH_COUNT} GREATER 3)
  if(${CMAKE_MATCH_COUNT} GREATER 1)
    set(${PREFIX}_VERSION_MAJOR ${CMAKE_MATCH_2} PARENT_SCOPE)
  endif(${CMAKE_MATCH_COUNT} GREATER 1)
  if(${CMAKE_MATCH_COUNT} GREATER 0)
    set(${PREFIX}_VERSION ${CMAKE_MATCH_0} PARENT_SCOPE)
  endif(${CMAKE_MATCH_COUNT} GREATER 0)
endfunction(set_version_variables)
