|
| 1 | +# Calculate the version from the current git repository. |
| 2 | + |
| 3 | +# This script is copied from/maintained at https://github.com/nabto/nabto-embedded-sdk |
| 4 | +# Script version 2020-12-08 |
| 5 | + |
| 6 | +function(nabto_version version_var error_var) |
| 7 | + |
| 8 | + find_program(GIT "git") |
| 9 | + if (NOT GIT) |
| 10 | + set(${error_var} "git executable not found" PARENT_SCOPE) |
| 11 | + return() |
| 12 | + endif() |
| 13 | + |
| 14 | + execute_process( |
| 15 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 16 | + COMMAND git rev-parse --is-inside-work-tree |
| 17 | + RESULT_VARIABLE IS_GIT_REPOSITORY) |
| 18 | + |
| 19 | + if (NOT IS_GIT_REPOSITORY EQUAL 0) |
| 20 | + set(${error_var} "directory is not a git repository" PARENT_SCOPE) |
| 21 | + return() |
| 22 | + endif() |
| 23 | + # A git repo, generate version and put it into version_var |
| 24 | + |
| 25 | + execute_process( |
| 26 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 27 | + COMMAND git diff --quiet --exit-code |
| 28 | + RESULT_VARIABLE GIT_DIRTY) |
| 29 | + execute_process( |
| 30 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 31 | + COMMAND git describe --exact-match --tags |
| 32 | + OUTPUT_VARIABLE GIT_TAG ERROR_QUIET) |
| 33 | + execute_process( |
| 34 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 35 | + COMMAND git rev-parse --abbrev-ref HEAD |
| 36 | + OUTPUT_VARIABLE GIT_BRANCH) |
| 37 | + execute_process( |
| 38 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 39 | + COMMAND git rev-list --count HEAD |
| 40 | + OUTPUT_VARIABLE GIT_COUNT ERROR_QUIET) |
| 41 | + execute_process( |
| 42 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 43 | + COMMAND git rev-parse --short HEAD |
| 44 | + OUTPUT_VARIABLE GIT_HASH ERROR_QUIET) |
| 45 | + |
| 46 | + if (GIT_DIRTY EQUAL 0) |
| 47 | + set(GIT_DIRTY "") |
| 48 | + else() |
| 49 | + set(GIT_DIRTY ".dirty") |
| 50 | + endif() |
| 51 | + |
| 52 | + |
| 53 | + string(STRIP "${GIT_DIRTY}" GIT_DIRTY) |
| 54 | + string(STRIP "${GIT_TAG}" GIT_TAG) |
| 55 | + string(STRIP "${GIT_BRANCH}" GIT_BRANCH) |
| 56 | + string(STRIP "${GIT_COUNT}" GIT_COUNT) |
| 57 | + string(STRIP "${GIT_HASH}" GIT_HASH) |
| 58 | + |
| 59 | + message("version.cmake variables: GIT_DIRTY ${GIT_DIRTY}, GIT_TAG ${GIT_TAG}, GIT_BRANCH ${GIT_BRANCH}, GIT_COUNT ${GIT_COUNT}, GIT_HASH: ${GIT_HASH}") |
| 60 | + |
| 61 | + if (GIT_TAG AND NOT GIT_DIRTY) |
| 62 | + # string v4.5.6 -> 4.5.6 |
| 63 | + string(SUBSTRING ${GIT_TAG} 1 -1 VERSION) |
| 64 | + else() |
| 65 | + # A feature branch |
| 66 | + set(VERSION "0.0.0-branch.${GIT_BRANCH}.commits.${GIT_COUNT}+${GIT_HASH}${GIT_DIRTY}") |
| 67 | + endif() |
| 68 | + |
| 69 | + message("Generated the version: ${VERSION} based on the git repository information") |
| 70 | + set(${version_var} ${VERSION} PARENT_SCOPE) |
| 71 | +endfunction() |
0 commit comments