ref: bd7bd9b30650ca74cf4997a5e1b3d8b7bc6e5a6b cisco/defs/git/CMakeLists.txt -rw-r--r-- 2.5 KiB View raw
                                                                                
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
macro(get_git_infos)
    execute_process(COMMAND git log --pretty=format:'%h' -n 1
                OUTPUT_VARIABLE GIT_REV
                ERROR_QUIET)

    # Check whether we got any revision (which isn't
    # always the case, e.g. when someone downloaded a zip
    # file from Github instead of a checkout)
    if ("${GIT_REV}" STREQUAL "")
        set(GIT_REV "N/A")
        set(GIT_DIFF "")
        set(GIT_TAG "N/A")
        set(GIT_BRANCH "N/A")
    else()
        execute_process(
            COMMAND bash -c "git diff --quiet --exit-code || echo +"
            OUTPUT_VARIABLE GIT_DIFF)
        execute_process(
            COMMAND git describe --abbrev=0
            OUTPUT_VARIABLE GIT_TAG ERROR_QUIET)
        execute_process(
            COMMAND git rev-parse --abbrev-ref HEAD
            OUTPUT_VARIABLE GIT_BRANCH)

        execute_process(
            COMMAND bash -c "git describe --exact-match --tags 2>/dev/null || git rev-parse --short HEAD"
            OUTPUT_VARIABLE GIT_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)

        string(STRIP "${GIT_REV}" GIT_REV)
        string(SUBSTRING "${GIT_REV}" 1 7 GIT_REV)
        string(STRIP "${GIT_DIFF}" GIT_DIFF)
        string(STRIP "${GIT_TAG}" GIT_TAG)
        string(STRIP "${GIT_BRANCH}" GIT_BRANCH)
    endif()
endmacro(get_git_infos)

function(generate_git_c_version NAME)
    get_git_infos()

    set(VERSION "const char *GIT_REV=\"${GIT_REV}${GIT_DIFF}\";
    const char *GIT_TAG=\"${GIT_TAG}\";
    const char *GIT_BRANCH=\"${GIT_BRANCH}\";
    const char *GIT_VERSION=\"${GIT_VERSION}\";
    ")

    if(EXISTS ${NAME})
        file(READ ${NAME} VERSION_)
    else()
        set(VERSION_ "")
    endif()

    if (NOT "${VERSION}" STREQUAL "${VERSION_}")
        file(WRITE ${NAME} "${VERSION}")
    endif()
endfunction(generate_git_c_version)

function(generate_git_go_version NAME PKG)
    get_git_infos()

    set(GO_VERSION "package ${PKG}

    const GITREV     = \"${GIT_REV}${GIT_DIFF}\"
    const GITTAG     = \"${GIT_TAG}\"
    const GITBRANCH  = \"${GIT_BRANCH}\"
    const GITVERSION = \"${GIT_VERSION}\"

    func GitVersion() string {
        return GITVERSION
    }

    func GitTag() string {
        return GITTAG
    }

    func GitBranch() string {
        return GITBRANCH
    }

    func GitRev() string {
        return GITREV
    }
    ")

    if(EXISTS ${NAME})
        file(READ ${NAME} GO_VERSION_)
    else()
        set(GO_VERSION_ "")
    endif()

    if (NOT "${GO_VERSION}" STREQUAL "${GO_VERSION_}")
        file(WRITE ${NAME} "${GO_VERSION}")
    endif()
endfunction(generate_git_go_version)