File: get_git_rev.cmake

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,184 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (53 lines) | stat: -rw-r--r-- 1,886 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Function that returns the last git tag matching the prefix passed as parameter
# tag name if you are exactly on (annotated) tag,
# or <tag>-<n>-g<shortened sha-1> if not
# where <n> is number of commits since <tag>
function(get_last_git_tag PREFIX)

    if(PREFIX)
        execute_process(
            COMMAND git describe --tags --match "${PREFIX}*"
            WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
            OUTPUT_VARIABLE LAST_TAG
            OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET
        )

        # if the prefix does not succeed, try lower case
        string(TOLOWER ${PREFIX} LOWER_CASE_PREFIX)
        if(NOT LAST_TAG)
            execute_process(
                COMMAND git describe --tags --match "${LOWER_CASE_PREFIX}*"
                WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
                OUTPUT_VARIABLE LAST_TAG
                OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET
            )
        endif()

        if(LAST_TAG)
            string(TOLOWER ${LAST_TAG} LOWER_CASE_LAST_TAG)
            string(REPLACE "${LOWER_CASE_PREFIX}/" "" LAST_TAG ${LOWER_CASE_LAST_TAG})
        endif()
    endif()

    # if the prefix does not succeed, default to the last tag matching a strictly numerical scheme
    if(NOT LAST_TAG)
        execute_process(
            COMMAND git describe --tags --match "[0-9]*"
            WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
            OUTPUT_VARIABLE LAST_TAG
            OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET
        )
    endif()

    # Well, we found nothing... In this case, we just use the last commit hash
    if(NOT LAST_TAG)
        execute_process(
            COMMAND git describe --always
            WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
            OUTPUT_VARIABLE LAST_TAG
            OUTPUT_STRIP_TRAILING_WHITESPACE
        )
    endif()

    set(GIT_TAG ${LAST_TAG} PARENT_SCOPE)
endfunction()