├── GetVersionFromGitTag.cmake └── README.md /GetVersionFromGitTag.cmake: -------------------------------------------------------------------------------- 1 | # 2 | # This cmake module sets the project version and partial version 3 | # variables by analysing the git tag and commit history. It expects git 4 | # tags defined with semantic versioning 2.0.0 (http://semver.org/). 5 | # 6 | # The module expects the PROJECT_NAME variable to be set, and recognizes 7 | # the GIT_FOUND, GIT_EXECUTABLE and VERSION_UPDATE_FROM_GIT variables. 8 | # If Git is found and VERSION_UPDATE_FROM_GIT is set to boolean TRUE, 9 | # the project version will be updated using information fetched from the 10 | # most recent git tag and commit. Otherwise, the module will try to read 11 | # a VERSION file containing the full and partial versions. The module 12 | # will update this file each time the project version is updated. 13 | # 14 | # Once done, this module will define the following variables: 15 | # 16 | # ${PROJECT_NAME}_VERSION_STRING - Version string without metadata 17 | # such as "v2.0.0" or "v1.2.41-beta.1". This should correspond to the 18 | # most recent git tag. 19 | # ${PROJECT_NAME}_VERSION_STRING_FULL - Version string with metadata 20 | # such as "v2.0.0+3.a23fbc" or "v1.3.1-alpha.2+4.9c4fd1" 21 | # ${PROJECT_NAME}_VERSION - Same as ${PROJECT_NAME}_VERSION_STRING, 22 | # without the preceding 'v', e.g. "2.0.0" or "1.2.41-beta.1" 23 | # ${PROJECT_NAME}_VERSION_MAJOR - Major version integer (e.g. 2 in v2.3.1-RC.2+21.ef12c8) 24 | # ${PROJECT_NAME}_VERSION_MINOR - Minor version integer (e.g. 3 in v2.3.1-RC.2+21.ef12c8) 25 | # ${PROJECT_NAME}_VERSION_PATCH - Patch version integer (e.g. 1 in v2.3.1-RC.2+21.ef12c8) 26 | # ${PROJECT_NAME}_VERSION_TWEAK - Tweak version string (e.g. "RC.2" in v2.3.1-RC.2+21.ef12c8) 27 | # ${PROJECT_NAME}_VERSION_AHEAD - How many commits ahead of last tag (e.g. 21 in v2.3.1-RC.2+21.ef12c8) 28 | # ${PROJECT_NAME}_VERSION_GIT_SHA - The git sha1 of the most recent commit (e.g. the "ef12c8" in v2.3.1-RC.2+21.ef12c8) 29 | # 30 | # This module is public domain, use it as it fits you best. 31 | # 32 | # Author: Nuno Fachada 33 | 34 | # Check if git is found... 35 | if (GIT_FOUND AND VERSION_UPDATE_FROM_GIT) 36 | 37 | # Get last tag from git 38 | execute_process(COMMAND ${GIT_EXECUTABLE} describe --abbrev=0 --tags 39 | WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} 40 | OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_STRING 41 | OUTPUT_STRIP_TRAILING_WHITESPACE) 42 | 43 | #How many commits since last tag 44 | execute_process(COMMAND ${GIT_EXECUTABLE} rev-list master ${${PROJECT_NAME}_VERSION_STRING}^..HEAD --count 45 | WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} 46 | OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_AHEAD 47 | OUTPUT_STRIP_TRAILING_WHITESPACE) 48 | 49 | # Get current commit SHA from git 50 | execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD 51 | WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} 52 | OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_GIT_SHA 53 | OUTPUT_STRIP_TRAILING_WHITESPACE) 54 | 55 | # Get partial versions into a list 56 | string(REGEX MATCHALL "-.*$|[0-9]+" ${PROJECT_NAME}_PARTIAL_VERSION_LIST 57 | ${${PROJECT_NAME}_VERSION_STRING}) 58 | 59 | # Set the version numbers 60 | list(GET ${PROJECT_NAME}_PARTIAL_VERSION_LIST 61 | 0 ${PROJECT_NAME}_VERSION_MAJOR) 62 | list(GET ${PROJECT_NAME}_PARTIAL_VERSION_LIST 63 | 1 ${PROJECT_NAME}_VERSION_MINOR) 64 | list(GET ${PROJECT_NAME}_PARTIAL_VERSION_LIST 65 | 2 ${PROJECT_NAME}_VERSION_PATCH) 66 | 67 | # The tweak part is optional, so check if the list contains it 68 | list(LENGTH ${PROJECT_NAME}_PARTIAL_VERSION_LIST 69 | ${PROJECT_NAME}_PARTIAL_VERSION_LIST_LEN) 70 | if (${PROJECT_NAME}_PARTIAL_VERSION_LIST_LEN GREATER 3) 71 | list(GET ${PROJECT_NAME}_PARTIAL_VERSION_LIST 3 ${PROJECT_NAME}_VERSION_TWEAK) 72 | string(SUBSTRING ${${PROJECT_NAME}_VERSION_TWEAK} 1 -1 ${PROJECT_NAME}_VERSION_TWEAK) 73 | endif() 74 | 75 | # Unset the list 76 | unset(${PROJECT_NAME}_PARTIAL_VERSION_LIST) 77 | 78 | # Set full project version string 79 | set(${PROJECT_NAME}_VERSION_STRING_FULL 80 | ${${PROJECT_NAME}_VERSION_STRING}+${${PROJECT_NAME}_VERSION_AHEAD}.${${PROJECT_NAME}_VERSION_GIT_SHA}) 81 | 82 | # Save version to file (which will be used when Git is not available 83 | # or VERSION_UPDATE_FROM_GIT is disabled) 84 | file(WRITE ${CMAKE_SOURCE_DIR}/VERSION ${${PROJECT_NAME}_VERSION_STRING_FULL} 85 | "*" ${${PROJECT_NAME}_VERSION_STRING} 86 | "*" ${${PROJECT_NAME}_VERSION_MAJOR} 87 | "*" ${${PROJECT_NAME}_VERSION_MINOR} 88 | "*" ${${PROJECT_NAME}_VERSION_PATCH} 89 | "*" ${${PROJECT_NAME}_VERSION_TWEAK} 90 | "*" ${${PROJECT_NAME}_VERSION_AHEAD} 91 | "*" ${${PROJECT_NAME}_VERSION_GIT_SHA}) 92 | 93 | else() 94 | 95 | # Git not available, get version from file 96 | file(STRINGS ${CMAKE_SOURCE_DIR}/VERSION ${PROJECT_NAME}_VERSION_LIST) 97 | string(REPLACE "*" ";" ${PROJECT_NAME}_VERSION_LIST ${${PROJECT_NAME}_VERSION_LIST}) 98 | # Set partial versions 99 | list(GET ${PROJECT_NAME}_VERSION_LIST 0 ${PROJECT_NAME}_VERSION_STRING_FULL) 100 | list(GET ${PROJECT_NAME}_VERSION_LIST 1 ${PROJECT_NAME}_VERSION_STRING) 101 | list(GET ${PROJECT_NAME}_VERSION_LIST 2 ${PROJECT_NAME}_VERSION_MAJOR) 102 | list(GET ${PROJECT_NAME}_VERSION_LIST 3 ${PROJECT_NAME}_VERSION_MINOR) 103 | list(GET ${PROJECT_NAME}_VERSION_LIST 4 ${PROJECT_NAME}_VERSION_PATCH) 104 | list(GET ${PROJECT_NAME}_VERSION_LIST 5 ${PROJECT_NAME}_VERSION_TWEAK) 105 | list(GET ${PROJECT_NAME}_VERSION_LIST 6 ${PROJECT_NAME}_VERSION_AHEAD) 106 | list(GET ${PROJECT_NAME}_VERSION_LIST 7 ${PROJECT_NAME}_VERSION_GIT_SHA) 107 | 108 | endif() 109 | 110 | 111 | # Set project version (without the preceding 'v') 112 | set(${PROJECT_NAME}_VERSION ${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}.${${PROJECT_NAME}_VERSION_PATCH}) 113 | if (${PROJECT_NAME}_VERSION_TWEAK) 114 | set(${PROJECT_NAME}_VERSION ${${PROJECT_NAME}_VERSION}-${${PROJECT_NAME}_VERSION_TWEAK}) 115 | endif() 116 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Summary 2 | 3 | This [CMake](http://www.cmake.org/) module sets the project version and 4 | partial version variables by analysing the git tag and commit history. 5 | It expects git tags defined with [semantic versioning 2.0.0](http://semver.org/). 6 | 7 | ### Usage 8 | 9 | The module expects the `PROJECT_NAME` variable to be set, and recognizes 10 | the `GIT_FOUND`, `GIT_EXECUTABLE` and `VERSION_UPDATE_FROM_GIT` 11 | variables. 12 | 13 | If Git is found and `VERSION_UPDATE_FROM_GIT` is set to boolean TRUE, 14 | the project version will be updated using information fetched from the 15 | most recent git tag and commit. Otherwise, the module will try to read 16 | a `VERSION` file containing the full and partial versions. The module 17 | will update this file each time the project version is updated from git. 18 | 19 | Once done, this module will define the following variables: 20 | 21 | * `${PROJECT_NAME}_VERSION_STRING` - Version string without metadata 22 | such as "v2.0.0" or "v1.2.41-beta.1". This should correspond to the 23 | most recent git tag. 24 | * `${PROJECT_NAME}_VERSION_STRING_FULL` - Version string with metadata 25 | such as "v2.0.0+3.a23fbc" or "v1.3.1-alpha.2+4.9c4fd1" 26 | * `${PROJECT_NAME}_VERSION` - Same as `${PROJECT_NAME}_VERSION_STRING`, without the preceding 27 | 'v', e.g. "2.0.0" or "1.2.41-beta.1" 28 | * `${PROJECT_NAME}_VERSION_MAJOR` - Major version integer (e.g. 2 in v2.3.1-RC.2+21.ef12c8) 29 | * `${PROJECT_NAME}_VERSION_MINOR` - Minor version integer (e.g. 3 in v2.3.1-RC.2+21.ef12c8) 30 | * `${PROJECT_NAME}_VERSION_PATCH` - Patch version integer (e.g. 1 in v2.3.1-RC.2+21.ef12c8) 31 | * `${PROJECT_NAME}_VERSION_TWEAK` - Tweak version string (e.g. "RC.2" in v2.3.1-RC.2+21.ef12c8) 32 | * `${PROJECT_NAME}_VERSION_AHEAD` - How many commits ahead of last tag (e.g. 21 in v2.3.1-RC.2+21.ef12c8) 33 | * `${PROJECT_NAME}_VERSION_GIT_SHA` - The git sha1 of the most recent commit (e.g. the "ef12c8" in v2.3.1-RC.2+21.ef12c8) 34 | 35 | ### License 36 | 37 | This module is public domain. 38 | 39 | --------------------------------------------------------------------------------