function(generateVersionHeader VERSION_FILE_PATH VERSION_HEADER_PATH)

file(READ ${VERSION_FILE_PATH} VERSION_FILE_CONTENTS)

set(SEMVER_REGEX "-.*$|[0-9]+")
string(REGEX MATCHALL ${SEMVER_REGEX} PARTIAL_VERSION_LIST ${VERSION_FILE_CONTENTS})

# Set the version numbers
list(GET PARTIAL_VERSION_LIST 0 VERSION_MAJOR)
list(GET PARTIAL_VERSION_LIST 1 VERSION_MINOR)
list(GET PARTIAL_VERSION_LIST 2 VERSION_PATCH)

# The tweak part is optional, so check if the list contains it
list(LENGTH PARTIAL_VERSION_LIST PARTIAL_VERSION_LIST_LEN)
if (PARTIAL_VERSION_LIST_LEN GREATER 3)
    list(GET PARTIAL_VERSION_LIST 3 VERSION_TWEAK)
    string(SUBSTRING ${VERSION_TWEAK} 1 -1 VERSION_TWEAK)
endif()
unset(PARTIAL_VERSION_LIST)

file(WRITE ${VERSION_HEADER_PATH} "#pragma once\n#define VERSION_STRING \"${VERSION_FILE_CONTENTS}\"")

endfunction()

