|
| 1 | +# This CMake module is used to generate semantic version information for the AWS IoT Secure Tunneling Local Proxy |
| 2 | +# and inject it into our source code, making the version information available to the compiled binary |
| 3 | +# so that it can be written to the logs for debugging purposes. To increment the major/minor versions |
| 4 | +# of the Secure Tunneling Local Proxy, this module expects to find a git tag in the form of "v1.0", where the first number |
| 5 | +# is the major version and the second number is the minor version. This module will search starting at HEAD |
| 6 | +# until it finds the latest versioned tag - git tags that do not start with "v" will be ignored. |
| 7 | +# |
| 8 | +# Additionally, the PATCH version of the version number is automatically incremented based on the number of commits |
| 9 | +# that we see between the current revision and the latest Git tag. For more information on Semantic Versioning, |
| 10 | +# check out https://semver.org/ and for more information on Git tags, check out https://git-scm.com/book/en/v2/Git-Basics-Tagging |
| 11 | + |
| 12 | +cmake_minimum_required(VERSION 3.10) |
| 13 | + |
| 14 | +# Marking Secure Tunneling Local Proxy directory safe |
| 15 | +execute_process(COMMAND git config --global --add safe.directory ${CMAKE_CURRENT_SOURCE_DIR}) |
| 16 | + |
| 17 | +# Check to make sure we have Git info for this package |
| 18 | +execute_process(COMMAND git log --pretty=format:'%h' -n 1 |
| 19 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 20 | + OUTPUT_VARIABLE GIT_INFO) |
| 21 | + |
| 22 | +function (load_version_from_file) |
| 23 | + # Git is not available (this is the case if the source is packaged as an archive), get version from file |
| 24 | + file(STRINGS ${CMAKE_SOURCE_DIR}/.version ${PROJECT_NAME}_VERSION_LIST) |
| 25 | + string(REPLACE "*" ";" ${PROJECT_NAME}_VERSION_LIST ${${PROJECT_NAME}_VERSION_LIST}) |
| 26 | + # Set partial versions |
| 27 | + list(GET ${PROJECT_NAME}_VERSION_LIST 0 ${PROJECT_NAME}_VERSION_STRING_FULL) |
| 28 | + list(GET ${PROJECT_NAME}_VERSION_LIST 1 ${PROJECT_NAME}_VERSION_STRING) |
| 29 | + list(GET ${PROJECT_NAME}_VERSION_LIST 2 ${PROJECT_NAME}_VERSION_MAJOR) |
| 30 | + list(GET ${PROJECT_NAME}_VERSION_LIST 3 ${PROJECT_NAME}_VERSION_MINOR) |
| 31 | + list(GET ${PROJECT_NAME}_VERSION_LIST 4 ${PROJECT_NAME}_VERSION_PATCH) |
| 32 | + list(GET ${PROJECT_NAME}_VERSION_LIST 5 ${PROJECT_NAME}_VERSION_AHEAD) |
| 33 | + list(GET ${PROJECT_NAME}_VERSION_LIST 6 ${PROJECT_NAME}_VERSION_GIT_SHA) |
| 34 | + unset(${PROJECT_NAME}_VERSION_LIST) |
| 35 | + |
| 36 | + message("-- Failed to infer patch version from git, loaded AWS IoT Secure Tunneling Local Proxy version from file: ${${PROJECT_NAME}_VERSION_STRING_FULL}") |
| 37 | +endfunction() |
| 38 | + |
| 39 | +if (GIT_VERSION AND NOT ${GIT_INFO} STREQUAL "") |
| 40 | + message("-- Using Git to calculate AWS IoT Secure Tunneling Local Proxy version information...") |
| 41 | + |
| 42 | + # Get last tag from git - this only matches tags starting with v, so we ignore non-versioning tags |
| 43 | + execute_process(COMMAND ${GIT_EXECUTABLE} describe --abbrev=0 --tags --match "v[0-9]*" |
| 44 | + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 45 | + OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_STRING |
| 46 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 47 | + RESULT_VARIABLE exit) |
| 48 | + |
| 49 | + if (NOT exit EQUAL 0) |
| 50 | + load_version_from_file() |
| 51 | + return() |
| 52 | + endif() |
| 53 | + |
| 54 | + # Determine how many commits since last tag |
| 55 | + execute_process(COMMAND ${GIT_EXECUTABLE} rev-list ${${PROJECT_NAME}_VERSION_STRING}..HEAD --count |
| 56 | + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 57 | + OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_AHEAD |
| 58 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 59 | + RESULT_VARIABLE exit) |
| 60 | + |
| 61 | + if (NOT exit EQUAL 0) |
| 62 | + load_version_from_file() |
| 63 | + return() |
| 64 | + endif() |
| 65 | + |
| 66 | + # Get current commit SHA from git |
| 67 | + execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD |
| 68 | + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 69 | + OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_GIT_SHA |
| 70 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 71 | + RESULT_VARIABLE exit) |
| 72 | + |
| 73 | + if (NOT exit EQUAL 0) |
| 74 | + load_version_from_file() |
| 75 | + return() |
| 76 | + endif() |
| 77 | + |
| 78 | + # Collect the partial versions into a list |
| 79 | + string(REGEX MATCHALL "[0-9]+" ${PROJECT_NAME}_PARTIAL_VERSION_LIST |
| 80 | + ${${PROJECT_NAME}_VERSION_STRING}) |
| 81 | + |
| 82 | + # Set the version numbers |
| 83 | + list(GET ${PROJECT_NAME}_PARTIAL_VERSION_LIST |
| 84 | + 0 ${PROJECT_NAME}_VERSION_MAJOR) |
| 85 | + list(GET ${PROJECT_NAME}_PARTIAL_VERSION_LIST |
| 86 | + 1 ${PROJECT_NAME}_VERSION_MINOR) |
| 87 | + set(${PROJECT_NAME}_VERSION_PATCH ${${PROJECT_NAME}_VERSION_AHEAD}) |
| 88 | + |
| 89 | + # Unset the list |
| 90 | + unset(${PROJECT_NAME}_PARTIAL_VERSION_LIST) |
| 91 | + |
| 92 | + # Set full project version string |
| 93 | + set(${PROJECT_NAME}_VERSION_STRING_FULL |
| 94 | + v${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}.${${PROJECT_NAME}_VERSION_PATCH}-${${PROJECT_NAME}_VERSION_GIT_SHA}) |
| 95 | + |
| 96 | + |
| 97 | + message("-- Generated AWS IoT Secure Tunneling Local Proxy version: ${${PROJECT_NAME}_VERSION_STRING_FULL}") |
| 98 | + # Save version to file (which will be used when Git is not available |
| 99 | + # or VERSION_UPDATE_FROM_GIT is disabled) |
| 100 | + file(WRITE ${CMAKE_SOURCE_DIR}/.version ${${PROJECT_NAME}_VERSION_STRING_FULL} |
| 101 | + "*" ${${PROJECT_NAME}_VERSION_STRING} |
| 102 | + "*" ${${PROJECT_NAME}_VERSION_MAJOR} |
| 103 | + "*" ${${PROJECT_NAME}_VERSION_MINOR} |
| 104 | + "*" ${${PROJECT_NAME}_VERSION_PATCH} |
| 105 | + "*" ${${PROJECT_NAME}_VERSION_AHEAD} |
| 106 | + "*" ${${PROJECT_NAME}_VERSION_GIT_SHA}) |
| 107 | + |
| 108 | + # exit from cmake processing |
| 109 | + return() |
| 110 | +endif() |
0 commit comments