1# cmakefile executed within a makefile target
2
3# If we find ReleaseInfo.cmake we use the info from there and don't need Git to be installed
4find_file(REL_INFO_FILE ReleaseInfo.cmake PATHS "${PROJECT_SOURCE_DIR}" "${PROJECT_BINARY_DIR}" NO_DEFAULT_PATH)
5if(REL_INFO_FILE STREQUAL REL_INFO_FILE-NOTFOUND)
6    if(EXISTS "${PROJECT_SOURCE_DIR}/.git")
7        # this is a git repo
8
9        # we look for the git command in this paths by order of preference
10        if(WIN32)
11            find_program(GIT_CMD git.exe HINTS ENV Path PATH_SUFFIXES ../)
12        elseif(APPLE)
13            find_program(GIT_CMD git PATHS "/opt/local/bin" "/usr/local/bin" "/usr/bin")
14            find_program(GIT_CMD git)
15            set(SHELL "/bin/bash")
16        else() # Linux
17            find_program(GIT_CMD git)
18            set(SHELL "/bin/bash")
19        endif()
20
21        # Fail if Git is not installed
22        if(GIT_CMD STREQUAL GIT_CMD-NOTFOUND)
23            message(FATAL_ERROR "git command not found!")
24        else()
25            message(STATUS "git command found: ${GIT_CMD}")
26        endif()
27
28        # Get version description.
29        # Depending on whether you checked out a branch (dev) or a tag (release),
30        # "git describe" will return "5.0-gtk2-2-g12345678" or "5.0-gtk2", respectively.
31        execute_process(COMMAND ${GIT_CMD} describe --tags --always OUTPUT_VARIABLE GIT_DESCRIBE OUTPUT_STRIP_TRAILING_WHITESPACE WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
32
33        # Get branch name.
34        # Will return empty if you checked out a commit or tag. Empty string handled later.
35        execute_process(COMMAND ${GIT_CMD} symbolic-ref --short -q HEAD OUTPUT_VARIABLE GIT_BRANCH OUTPUT_STRIP_TRAILING_WHITESPACE WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
36
37        # Get commit hash.
38        execute_process(COMMAND ${GIT_CMD} rev-parse --short --verify HEAD OUTPUT_VARIABLE GIT_COMMIT OUTPUT_STRIP_TRAILING_WHITESPACE WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
39
40        # Get commit date, YYYY-MM-DD.
41        execute_process(COMMAND ${GIT_CMD} show -s --format=%cd --date=format:%Y-%m-%d OUTPUT_VARIABLE GIT_COMMIT_DATE OUTPUT_STRIP_TRAILING_WHITESPACE WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
42
43        # Get number of commits since tagging. This is what "GIT_DESCRIBE" uses.
44        # Works when checking out branch, tag or commit.
45        # Get a list of all tags in repo:
46        execute_process(COMMAND ${GIT_CMD} tag --merged HEAD OUTPUT_VARIABLE GIT_TAG WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
47        # Replace newlines with semicolons so that it can be split:
48        string(REPLACE "\n" ";" GIT_TAG_LIST "${GIT_TAG}")
49        execute_process(COMMAND ${GIT_CMD} rev-list --count HEAD --not ${GIT_TAG_LIST} OUTPUT_VARIABLE GIT_COMMITS_SINCE_TAG OUTPUT_STRIP_TRAILING_WHITESPACE WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
50
51        # Get number of commits since branching.
52        # Works when checking out branch, tag or commit.
53        execute_process(COMMAND ${GIT_CMD} rev-list --count HEAD --not --tags OUTPUT_VARIABLE GIT_COMMITS_SINCE_BRANCH OUTPUT_STRIP_TRAILING_WHITESPACE WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
54    elseif(EXISTS "${PROJECT_SOURCE_DIR}/.hg")
55        # a hg-git repo (i.e. Alberto's one :-)
56
57        # we look for the hg command in this paths by order of preference
58        if(WIN32)
59            find_program(HG_CMD hg.exe HINTS ENV Path PATH_SUFFIXES ../)
60        elseif(APPLE)
61            find_program(HG_CMD hg PATHS "/opt/local/bin" "/usr/local/bin" "/usr/bin")
62            find_program(HG_CMD hg)
63            set(SHELL "/bin/bash")
64        else() # Linux
65            find_program(HG_CMD hg)
66            set(SHELL "/bin/bash")
67        endif()
68
69        # Fail if Mercurial is not installed
70        if(HG_CMD STREQUAL HG_CMD-NOTFOUND)
71            message(FATAL_ERROR "hg command not found!")
72        else()
73            message(STATUS "hg command found: ${HG_CMD}")
74        endif()
75
76        # we emulate the behaviour of git with Mercurial
77        execute_process(COMMAND ${HG_CMD} log -r .
78            #--template "{latesttag('re:.*v?[0-9.]+(rc)?[0-9]+$') % '{sub('^.*/.*:', '', tag)}{ifeq(distance, 0, '', '-')}{ifeq(distance, 0, '', distance)}{ifeq(distance, 0, '', '-g')}{ifeq(distance, 0, '', short(gitnode))}'}"
79            --template "{latesttag('re:.*v?[0-9.]+(rc)?[0-9]+$') % '{sub('^.*/.*:', '', tag)}{ifeq(distance, 0, '', '-')}{ifeq(distance, 0, '', count(revset('ancestors(\".\") and descendants(last(tag(r\"re:^v?[0-9]+[.][0-9.]+(rc[0-9]+)?$\"), 1))'))-1)}{ifeq(distance, 0, '', '-g')}{ifeq(distance, 0, '', short(gitnode))}'}"
80            OUTPUT_VARIABLE GIT_DESCRIBE
81            OUTPUT_STRIP_TRAILING_WHITESPACE
82            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
83
84        execute_process(COMMAND ${HG_CMD} log -r . --template "{activebookmark}"
85            OUTPUT_VARIABLE GIT_BRANCH
86            OUTPUT_STRIP_TRAILING_WHITESPACE
87            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
88
89        execute_process(COMMAND ${HG_CMD} log -r . --template "{short(gitnode)}"
90            OUTPUT_VARIABLE GIT_COMMIT
91            OUTPUT_STRIP_TRAILING_WHITESPACE
92            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
93
94        execute_process(COMMAND ${HG_CMD} log -r . --template "{date|shortdate}"
95            OUTPUT_VARIABLE GIT_COMMIT_DATE
96            OUTPUT_STRIP_TRAILING_WHITESPACE
97            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
98
99        execute_process(COMMAND #${HG_CMD} log -r . --template "{latesttag('re:.*v?[0-9.]+(rc)?[0-9]+$') % '{distance}'}"
100            ${HG_CMD} log -r . --template "{count(revset('ancestors(\".\") and descendants(last(tag(r\"re:^v?[0-9]+[.][0-9.]+(rc[0-9]+)?$\"), 1))'))-1}"
101            OUTPUT_VARIABLE GIT_COMMITS_SINCE_TAG
102            OUTPUT_STRIP_TRAILING_WHITESPACE
103            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
104
105        execute_process(COMMAND ${HG_CMD} log -r . --template "{count(revset('.::bookmark()'))-1}"
106            OUTPUT_VARIABLE GIT_COMMITS_SINCE_BRANCH
107            OUTPUT_STRIP_TRAILING_WHITESPACE
108            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
109    else()
110        message(WARNING "not inside a repository -- info will be bogus!")
111    endif()
112
113    # If user checked-out something which is not a branch, use the description as branch name.
114    if(GIT_BRANCH STREQUAL "")
115        set(GIT_BRANCH "${GIT_DESCRIBE}")
116    endif()
117
118    # Create numeric version.
119    # This version is nonsense, either don't use it at all or use it only where you have no other choice, e.g. Inno Setup's VersionInfoVersion.
120    # Strip everything after hyphen, e.g. "5.0-gtk2" -> "5.0", "5.1-rc1" -> "5.1" (ergo BS).
121    if(GIT_COMMITS_SINCE_TAG STREQUAL "")
122        set(GIT_NUMERIC_VERSION_BS "0.0.0")
123    else()
124        string(REGEX REPLACE "-.*" "" GIT_NUMERIC_VERSION_BS "${GIT_DESCRIBE}")
125        set(GIT_NUMERIC_VERSION_BS "${GIT_NUMERIC_VERSION_BS}.${GIT_COMMITS_SINCE_TAG}")
126    endif()
127
128    string(TIMESTAMP BUILDINFO_DATE UTC)
129
130    message(STATUS "Git checkout information:")
131    message(STATUS "    Commit description: ${GIT_DESCRIBE}")
132    message(STATUS "    Branch: ${GIT_BRANCH}")
133    message(STATUS "    Commit: ${GIT_COMMIT}")
134    message(STATUS "    Commit date: ${GIT_COMMIT_DATE}")
135    message(STATUS "    Commits since tag: ${GIT_COMMITS_SINCE_TAG}")
136    message(STATUS "    Commits since branch: ${GIT_COMMITS_SINCE_BRANCH}")
137    message(STATUS "    Version (unreliable): ${GIT_NUMERIC_VERSION_BS}")
138    message(STATUS "Build information:")
139    message(STATUS "    Build OS: ${BUILDINFO_OS}")
140    message(STATUS "    Build date: ${BUILDINFO_DATE}")
141
142    if(NOT DEFINED CACHE_NAME_SUFFIX)
143        set(CACHE_NAME_SUFFIX "${GIT_DESCRIBE}")
144        message(STATUS "CACHE_NAME_SUFFIX was not defined, it is now \"${CACHE_NAME_SUFFIX}\"")
145    else()
146        message(STATUS "CACHE_NAME_SUFFIX is \"${CACHE_NAME_SUFFIX}\"")
147    endif()
148
149    file(WRITE
150        "${CMAKE_BINARY_DIR}/ReleaseInfo.cmake.in"
151        "set(GIT_DESCRIBE \"${GIT_DESCRIBE}\")
152set(GIT_BRANCH \"${GIT_BRANCH}\")
153set(GIT_COMMIT \"${GIT_COMMIT}\")
154set(GIT_COMMIT_DATE \"${GIT_COMMIT_DATE}\")
155set(GIT_COMMITS_SINCE_TAG \"${GIT_COMMITS_SINCE_TAG}\")
156set(GIT_COMMITS_SINCE_BRANCH \"${GIT_COMMITS_SINCE_BRANCH}\")
157set(GIT_NUMERIC_VERSION_BS \"${GIT_NUMERIC_VERSION_BS}\")
158")
159else()
160    include(${REL_INFO_FILE})
161endif()
162
163if(WIN32)
164    if(BIT_DEPTH EQUAL 4)
165        set(BUILD_BIT_DEPTH 32)
166        # 32 bits builds has to be installable on 64 bits system, to support WinXP/64.
167        set(ARCHITECTURE_ALLOWED "x86 x64 ia64")
168        # installing in 32 bits mode even on 64 bits OS and architecture
169        set(INSTALL_MODE "")
170    elseif(BIT_DEPTH EQUAL 8)
171        set(BUILD_BIT_DEPTH 64)
172        # Restricting the 64 bits builds to 64 bits systems only
173        set(ARCHITECTURE_ALLOWED "x64 ia64")
174        # installing in 64 bits mode for all 64 bits processors, even for itanium architecture
175        set(INSTALL_MODE "x64 ia64")
176    endif(BIT_DEPTH EQUAL 4)
177    # set part of the output archive name
178    set(SYSTEM_NAME "WinVista")
179
180    configure_file("${PROJECT_SOURCE_DIR}/tools/win/InnoSetup/WindowsInnoSetup.iss.in" "${CMAKE_BINARY_DIR}/rtdata/WindowsInnoSetup.iss")
181endif(WIN32)
182
183# build version.h from template
184configure_file("${PROJECT_SOURCE_DIR}/rtgui/version.h.in" "${CMAKE_BINARY_DIR}/rtgui/version.h")
185# build AboutThisBuild.txt from template
186configure_file("${PROJECT_SOURCE_DIR}/AboutThisBuild.txt.in" "${CMAKE_BINARY_DIR}/AboutThisBuild.txt")
187