90e3752aca22d4b185f180f8eb971e8bfeb9353f
[alexxy/gromacs.git] / cmake / gmxGenerateVersionInfo.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
5 # David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 # others, as listed in the AUTHORS file in the top-level source
7 # directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34 # Generate Gromacs development build version information.
35
36 # This script generates version information for a build from a development
37 # source tree based on git repository information.
38 # It is assumed that by default the script is run in cmake script mode.
39 # If *not* called in script mode but used in generating cache variables,
40 # GEN_VERSION_INFO_INTERNAL has to be set ON.
41 #
42 # The following variables have to be previously defined:
43 # GIT_EXECUTABLE         - path to git binary (must be >=1.5.3)
44 # PROJECT_VERSION        - hard-coded version string (generated info is appended)
45 # PROJECT_SOURCE_DIR     - top level source directory (which has to be in git)
46 # VERSION_CMAKEIN        - path to an input template file
47 # VERSION_OUT            - path to the output file
48 # VERSION_NO_REMOTE_HASH - if set, GMX_GIT_REMOTE_HASH is not generated
49 #
50 # Output:
51 # i)  Script mode: VERSION_OUT is configured from the input VERSION_CMAKEIN
52 # using the variables listed below.
53 # ii) Cache variable mode: the variables below are set in cache.
54 #
55 # GMX_PROJECT_VERSION_STR   - version string
56 # GMX_GIT_HEAD_HASH         - git hash of current local HEAD
57 # GMX_GIT_REMOTE_HASH       - git hash of the first ancestor commit from the
58 #                             main Gromacs repository
59 #
60 # Szilard Pall (pszilard@cbr.su.se)
61 # Teemu Murtola (teemu.murtola@gmail.com)
62
63 if("${PROJECT_VERSION}" STREQUAL "")
64     message(FATAL_ERROR "PROJECT_VERSION undefined!")
65 endif()
66
67 # if we're generating variables for cache unset the variables
68 if(GEN_VERSION_INFO_INTERNAL)
69     set(GMX_PROJECT_VERSION_STR)
70     set(GMX_GIT_HEAD_HASH)
71     set(GMX_GIT_REMOTE_HASH)
72 endif()
73
74 # bail if the source tree is not in a git repository
75 if(NOT EXISTS "${PROJECT_SOURCE_DIR}/.git")
76     message(FATAL_ERROR "Project source directory ${PROJECT_SOURCE_DIR} not in git")
77 endif()
78
79 # If git executable exists, build the development version string.
80 if(EXISTS "${GIT_EXECUTABLE}")
81     # refresh git index
82     execute_process(COMMAND ${GIT_EXECUTABLE} update-index -q --refresh
83         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
84         TIMEOUT 5
85         OUTPUT_QUIET
86         ERROR_VARIABLE EXEC_ERR
87         OUTPUT_STRIP_TRAILING_WHITESPACE
88     )
89
90     # get the full hash of the current HEAD
91     execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
92         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
93         OUTPUT_VARIABLE HEAD_HASH
94         ERROR_VARIABLE EXEC_ERR
95         OUTPUT_STRIP_TRAILING_WHITESPACE
96     )
97     set(GMX_GIT_HEAD_HASH ${HEAD_HASH})
98     # extract the shortened hash (7 char)
99     execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
100         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
101         OUTPUT_VARIABLE HEAD_HASH_SHORT
102         ERROR_VARIABLE EXEC_ERR
103         OUTPUT_STRIP_TRAILING_WHITESPACE
104     )
105
106     # if there are local uncommitted changes, the build gets labeled "dirty"
107     execute_process(COMMAND ${GIT_EXECUTABLE} diff-index --name-only HEAD
108         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
109         OUTPUT_VARIABLE SRC_LOCAL_CHANGES
110         ERROR_VARIABLE EXEC_ERR
111         OUTPUT_STRIP_TRAILING_WHITESPACE
112     )
113     if(NOT "${SRC_LOCAL_CHANGES}" STREQUAL "")
114         set(DIRTY_STR "-dirty")
115         set(GMX_GIT_HEAD_HASH "${GMX_GIT_HEAD_HASH} (dirty)")
116     endif()
117
118     # get the date of the HEAD commit
119     execute_process(COMMAND ${GIT_EXECUTABLE} rev-list -n1 "--pretty=format:%ci" HEAD
120         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
121         OUTPUT_VARIABLE HEAD_DATE
122         ERROR_VARIABLE EXEC_ERR
123         OUTPUT_STRIP_TRAILING_WHITESPACE
124     )
125     string(REGEX REPLACE "\n| " ";" HEAD_DATE "${HEAD_DATE}")
126     list(GET HEAD_DATE 2 HEAD_DATE)
127     string(REGEX REPLACE "-" "" HEAD_DATE "${HEAD_DATE}")
128
129     # compile the version string suffix
130     set(VERSION_STR_SUFFIX "${HEAD_DATE}-${HEAD_HASH_SHORT}${DIRTY_STR}")
131
132     if (DEFINED VERSION_NO_REMOTE_HASH)
133         set(GMX_REMOTES "")
134     else()
135         # find the names of remotes that are located on the official gromacs
136         # git/gerrit servers
137         execute_process(COMMAND ${GIT_EXECUTABLE} config --get-regexp
138                         "remote\\..*\\.url" "\\.gromacs\\.org[:/].*gromacs(\\.git)?$"
139             WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
140             OUTPUT_VARIABLE GMX_REMOTES
141             ERROR_VARIABLE EXEC_ERR
142             OUTPUT_STRIP_TRAILING_WHITESPACE
143         )
144     endif()
145
146     # if there are remotes from the gromacs git servers, try to find ancestor
147     # commits of the current HEAD from this remote;
148     # otherwise, label the build "unknown"
149     if("${GMX_REMOTES}" STREQUAL "")
150         if (NOT DEFINED VERSION_NO_REMOTE_HASH)
151             set(VERSION_STR_SUFFIX "${VERSION_STR_SUFFIX}-unknown")
152         endif()
153         set(GMX_GIT_REMOTE_HASH "unknown")
154     else()
155         string(REPLACE "\n" ";" GMX_REMOTES ${GMX_REMOTES})
156         # construct a command pipeline that produces a reverse-time-ordered
157         # list of commits and their annotated names in GMX_REMOTES
158         # the max-count limit is there to put an upper bound on the execution time
159         set(BASEREVCOMMAND "COMMAND ${GIT_EXECUTABLE} rev-list --max-count=1000 HEAD")
160         foreach(REMOTE ${GMX_REMOTES})
161             string(REGEX REPLACE "remote\\.(.*)\\.url.*" "\\1" REMOTE ${REMOTE})
162             set(BASEREVCOMMAND "${BASEREVCOMMAND} COMMAND ${GIT_EXECUTABLE} name-rev --stdin --refs=refs/remotes/${REMOTE}/*")
163         endforeach(REMOTE)
164         # this is necessary for CMake to properly split the variable into
165         # parameters for execute_process().
166         string(REPLACE " " ";" BASEREVCOMMAND ${BASEREVCOMMAND})
167         # find the first ancestor in the list provided by rev-list (not
168         # necessarily the last though) which is in GMX_REMOTE, extract the
169         # hash and the number of commits HEAD is ahead with
170         execute_process(${BASEREVCOMMAND}
171             WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
172             OUTPUT_VARIABLE ANCESTOR_LIST
173         )
174         string(REGEX REPLACE "\n" ";" ANCESTOR_LIST "${ANCESTOR_LIST}")
175
176         set(AHEAD 0)
177         set(GMX_GIT_REMOTE_HASH "")
178         foreach(ANCESTOR ${ANCESTOR_LIST})
179             string(REPLACE "\n" "" HASH_AND_REVNAMES "${ANCESTOR}")
180             string(REPLACE " " ";" HASH_AND_REVNAMES "${HASH_AND_REVNAMES}")
181             list(LENGTH HASH_AND_REVNAMES COUNT)
182             # stop and set the hash if we have a hit, otherwise loop and count
183             # how far ahead is the local repo
184             if(COUNT GREATER 1)
185                 LIST(GET HASH_AND_REVNAMES 0 GMX_GIT_REMOTE_HASH)
186                 break()
187             endif()
188             math(EXPR AHEAD ${AHEAD}+1)
189         endforeach(ANCESTOR)
190         # mark the build "local" if didn't find any commits that are from
191         # remotes/${GMX_REMOTE}/*
192         if("${GMX_GIT_REMOTE_HASH}" STREQUAL "")
193             set(GMX_GIT_REMOTE_HASH "unknown")
194             set(VERSION_STR_SUFFIX "${VERSION_STR_SUFFIX}-local")
195         # don't print the remote hash if there are no local commits
196         elseif("${GMX_GIT_REMOTE_HASH}" STREQUAL "${HEAD_HASH}")
197             set(GMX_GIT_REMOTE_HASH "")
198         else()
199             set(GMX_GIT_REMOTE_HASH "${GMX_GIT_REMOTE_HASH} (${AHEAD} newer local commits)")
200         endif()
201     endif()
202
203     # compile final version string
204     set(GMX_PROJECT_VERSION_STR "${PROJECT_VERSION}-${VERSION_STR_SUFFIX}")
205 else()
206     # the version has to be defined - if not we're not using gitversion.h/.c;
207     # set the GIT related information to "unknown"
208     message(WARNING "Source tree seems to be a repository, but no compatible git is available, using hard-coded version string")
209     set(GMX_PROJECT_VERSION_STR "${PROJECT_VERSION}")
210     set(GMX_GIT_HEAD_HASH "unknown")
211     set(GMX_GIT_REMOTE_HASH "unknown")
212 endif()
213
214 # if we're generating cache variables set these
215 # otherwise it's assumed that it's called in script mode to generate a file
216 if(GEN_VERSION_INFO_INTERNAL)
217     set(GMX_PROJECT_VERSION_STR ${GMX_PROJECT_VERSION_STR}
218         CACHE STRING "Gromacs version string" FORCE)
219     set(GMX_GIT_HEAD_HASH ${GMX_GIT_HEAD_HASH}${DIRTY_STR}
220         CACHE STRING "Current git HEAD commit object" FORCE)
221     set(GMX_GIT_REMOTE_HASH ${GMX_GIT_REMOTE_HASH}
222         CACHE STRING "Commmit object of the nearest ancestor present in the Gromacs git repository" FORCE)
223     mark_as_advanced(GMX_GIT_HEAD_HASH GMX_GIT_REMOTE_HASH)
224 else()
225     if("${VERSION_CMAKEIN}" STREQUAL "")
226         message(FATAL_ERROR "Missing input parameter VERSION_CMAKEIN!")
227     endif()
228     if("${VERSION_OUT}" STREQUAL "")
229         message(FATAL_ERROR "Missing input parameter VERSION_OUT!")
230     endif()
231     # Generate the output file.
232     configure_file(${VERSION_CMAKEIN} ${VERSION_OUT})
233 endif()