282dcefa601d6a8dd15db90915c30d74e82d474e
[alexxy/gromacs.git] / cmake / gmxVersionInfo.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2014,2015, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source 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
35 # Sets version information variables and provides CMake functions for
36 # generating files based on them
37 #
38 # This script provides the following basic version variables that need to be
39 # maintained manually:
40 #   GMX_VERSION_MAJOR      Major version number.
41 #   GMX_VERSION_MINOR      Minor version number.
42 #   GMX_VERSION_PATCH      Patch version number.
43 #       Should always be defined: zero for, e.g., 5.0.
44 #   GMX_VERSION_SUFFIX     String suffix to add to numeric version string.
45 #       "-dev" is automatically added when not building from a source package,
46 #       and does not need to be kept here. This mechanism is not quite enough
47 #       for building a tarball, but setting the CMake cache variable
48 #       GMX_BUILD_TARBALL=on will suppress the addition of "-dev" to the
49 #       version string.
50 #   LIBRARY_SOVERSION_MAJOR so major version for the built libraries.
51 #       Should be increased for each binary incompatible release. In GROMACS,
52 #       the typical policy is to increase it at the start of the development
53 #       cycle for each major/minor version change, but not for patch releases,
54 #       even if the latter may not always be fully binary compatible.
55 #       Table of historical values
56 #         GROMACS     5.0    0
57 #         GROMACS     5.1    1
58 #   LIBRARY_SOVERSION_MINOR so minor version for the built libraries.
59 #       Should be increased for each release that changes only the implementation.
60 #       In GROMACS, the typical policy is to increase it for each patch version
61 #       change, even if they may not always be fully binary compatible.
62 #       If it is somehow clear that the ABI implementation has not changed
63 #       in a patch release, this variable should not increase. Release candidate
64 #       and beta versions will not increase this number, since nobody should
65 #       write code against such versions.
66 #   LIBRARY_VERSION        Full library version.
67 #   REGRESSIONTEST_BRANCH  For builds not from source packages, name of the
68 #       regressiontests branch at gerrit.gromacs.org whose HEAD can test this
69 #       code, *if* this code is recent enough (i.e., contains all changes from
70 #       the corresponding code branch that affects the regression test
71 #       results). Even after a release branch is forked for the source
72 #       repository, the correct regressiontests branch can still be master,
73 #       because we do not fork it until behaviour needs to change.
74 #   REGRESSIONTEST_MD5SUM
75 #       The MD5 checksum of the regressiontest tarball. Only used when building
76 #       from a source package.
77 # They are collected into a single section below.
78 # The following variables are set based on these:
79 #   GMX_VERSION            String composed from GMX_VERSION_* numeric variables
80 #       above. Example: 4.6.1, 5.0
81 #   GMX_VERSION_STRING     String with GMX_VERSION suffixed with the given
82 #       suffix and possibly "-dev" for builds not from a source package.
83 #   GMX_VERSION_NUMERIC    Numeric version number (e.g., 40601 for 4.6.1).
84 #   GMX_API_VERSION        Numeric API version.
85 #       This is currently set automatically to GMX_VERSION_NUMERIC, but may
86 #       become manually maintained in the future if there will be releases
87 #       where the API does not change, but programs/libraries do.
88 #       In such a case, this should be the first version where the current API
89 #       appeared.
90 #   REGRESSIONTEST_VERSION For source packages, version number of the
91 #       matching regressiontests tarball.  Not used for builds not from source
92 #       packages.
93 # The latter two are used to generate gromacs/version.h to allow software
94 # written against the GROMACS API to provide some #ifdef'ed code to support
95 # multiple GROMACS versions.
96 #
97 # The following variables are defined without manual intervention:
98 #   SOURCE_IS_SOURCE_DISTRIBUTION  The source tree is from a source tarball.
99 #   SOURCE_IS_GIT_REPOSITORY       The source tree is a git repository.
100 # Note that both can be false if the tree has been extracted, e.g., as a
101 # tarball directly from git.
102 # Additionally, the following variable is defined:
103 #   BUILD_IS_INSOURCE              The build is happening in-source.
104 #
105 # This script also declares machinery to generate and obtain version
106 # information from a git repository.  This is enabled by default if the source
107 # tree is a git, but can be disabled with
108 #   GMX_GIT_VERSION_INFO           Advanced CMake variable to disable git
109 #                                  version info generation.
110 # The main interface to this machinery is the gmx_configure_version_file()
111 # CMake function.  The signature is
112 #   gmx_configure_version_file(<input> <output>
113 #                              [REMOTE_HASH] [SOURCE_FILE]
114 #                              [TARGET <target>]
115 #                              [COMMENT <comment>])
116 #   <input>      Specify the input and output files as for configure_file().
117 #   <output>     The configuration is done with configure_file(... @ONLY) with
118 #                the following variables defined (as well as all the
119 #                GMX_VERSION* variables from above):
120 #                  GMX_VERSION_STRING_FULL
121 #                  GMX_VERSION_FULL_HASH
122 #                  GMX_VERSION_CENTRAL_BASE_HASH
123 #                The output file is created during build time, so any dependent
124 #                targets should specify it as a dependency.
125 #   REMOTE_HASH  Currently, this has no effect, but it signifies that the
126 #                <input> file is using the CENTRAL_BASE_HASH variable.
127 #                This variable is much more expensive to initialize than the
128 #                others, so this allows local changes in this file to only
129 #                compute that value when required if that becomes necessary.
130 #   SOURCE_FILE  Signals that <output> will be used as a source file.
131 #                The function will set properties for the source file
132 #                appropriately to signify that it is generated.
133 #   TARGET       By default, this function uses add_custom_command() to
134 #                generate the output file.  If TARGET is specified, then
135 #                add_custom_target() is used to create a target with the given
136 #                name <target> that runs this custom command.  Use this if
137 #                the same file will be used for multiple downstream targets,
138 #                or if the explicit target for the file is otherwise
139 #                necessary.
140 #   COMMENT      Set a custom comment to be shown when building the rule
141 #                (see add_custom_command(... COMMENT <comment>)).
142 # As an alternative to using this script, also the following variables are
143 # provided (can be useful when generating more complex CMake scripts that do
144 # build-time tasks):
145 #   VERSION_INFO_CMAKE_SCRIPT
146 #       Absolute path to a CMake script that can be included using include()
147 #       to declare the GMX_VERSION_* variables documented for
148 #       gmx_configure_version_file().
149 #   VERSION_INFO_DEPS
150 #       If a custom command depends on VERSION_INFO_CMAKE_SCRIPT, then it
151 #       should add ${VERSION_INFO_DEPS} to its DEPENDS list to get the
152 #       appropriate dependencies.
153 # TODO: If someone wants to add a custom target that depends on
154 # VERSION_INFO_CMAKE_SCRIPT, a separate variable may be needed for those
155 # dependencies.
156 #
157 # The version string printed by 'gmx -version' (and also printed in the startup
158 # header) can provide useful information for, e.g., diagnosing bug reports and
159 # identifying what exact version the user was using.  The following formats are
160 # possible (with examples given for a particular version):
161 #   4.6.1       Plain version number without any suffix signifies a build from
162 #               a released source tarball.
163 #   4.6.1-dev   '-dev' suffix signifies all other builds. If there is no other
164 #               information, either the user built the code outside any git
165 #               repository, or disabled the version info generation.
166 #   4.6.1-dev-YYYYMMDD-1234abc
167 #               The YYYYMMDD part shows the commit date (not author date) of
168 #               the HEAD commit from which the code was built.  The abbreviated
169 #               hash is the hash of that commit (the full hash is available in
170 #               'gmx -version' output).
171 #               If the HEAD hash is not identified as coming from branches in
172 #               "authoritative" GROMACS repositories, 'gmx -version' will show
173 #               the nearest ancestor commit that is identified as such (but see
174 #               the '-local' and '-unknown' suffixes below).
175 #   4.6.1-dev-YYYYMMDD-1234abc-dirty
176 #               As above, but there were local modifications in the source tree
177 #               when the code was built.
178 #   4.6.1-dev-YYYYMMDD-1234abc-unknown
179 #               As above, but there were no remotes in the repository that
180 #               could be identified as "authoritative" GROMACS repositories.
181 #               This happens if the code is not cloned from git.gromacs.org
182 #               or gerrit.gromacs.org.
183 #   4.6.1-dev-YYYYMMDD-1234abc-local
184 #               As above, but there were no commits in the recent history of
185 #               the branch that could be identified as coming from
186 #               "authoritative" GROMACS repositories.  This should be
187 #               relatively rare.
188 #
189 # Other variables set here are not intended for use outside this file.
190 # The scripts gmxGenerateVersionInfo.cmake and gmxConfigureVersionInfo.cmake
191 # are used internally by this machinery, as well as VersionInfo.cmake.cmakein.
192
193 #####################################################################
194 # Basic nature of the source tree
195
196 set(SOURCE_IS_GIT_REPOSITORY OFF)
197 set(SOURCE_IS_SOURCE_DISTRIBUTION OFF)
198 if (EXISTS "${PROJECT_SOURCE_DIR}/.git")
199     set(SOURCE_IS_GIT_REPOSITORY ON)
200 endif()
201 # This file is excluded from CPack source packages, but part of the repository,
202 # so it should get included everywhere else.
203 if (NOT EXISTS "${PROJECT_SOURCE_DIR}/admin/.isreposource")
204     set(SOURCE_IS_SOURCE_DISTRIBUTION ON)
205 endif()
206 set(BUILD_IS_INSOURCE OFF)
207 if ("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
208     set(BUILD_IS_INSOURCE ON)
209 endif()
210
211 #####################################################################
212 # Manually maintained version info
213
214 # The GROMACS convention is that these are the version number of the next
215 # release that is going to be made from this branch.
216 set(GMX_VERSION_MAJOR 5)
217 set(GMX_VERSION_MINOR 2)
218 set(GMX_VERSION_PATCH 0)
219 # The suffix, on the other hand, is used mainly for betas and release
220 # candidates, where it signifies the most recent such release from
221 # this branch; it will be empty before the first such release, as well
222 # as after the final release is out.
223 set(GMX_VERSION_SUFFIX "")
224
225 # Conventionally with libtool, any ABI change must change the major
226 # version number, the minor version number should change if it's just
227 # the implementation that has been altered, and the third number
228 # counts the number of old major versions that will still run if
229 # linked to this library (i.e. it is not a patch number). See the
230 # above descriptions of LIBRARY_SOVERSION_* for policy for changes
231 # here. The important thing is to minimize the chance of third-party
232 # code being able to dynamically link with a version of libgromacs
233 # that might not work.
234 set(LIBRARY_SOVERSION_MAJOR 2)
235 set(LIBRARY_SOVERSION_MINOR 0)
236 set(LIBRARY_VERSION ${LIBRARY_SOVERSION_MAJOR}.${LIBRARY_SOVERSION_MINOR}.0)
237
238 #####################################################################
239 # General version management based on manually set numbers
240
241 if (GMX_VERSION_PATCH)
242     set(GMX_VERSION "${GMX_VERSION_MAJOR}.${GMX_VERSION_MINOR}.${GMX_VERSION_PATCH}")
243 else()
244     set(GMX_VERSION "${GMX_VERSION_MAJOR}.${GMX_VERSION_MINOR}")
245 endif()
246 set(GMX_VERSION_STRING "${GMX_VERSION}${GMX_VERSION_SUFFIX}")
247 option(GMX_BUILD_TARBALL "Build tarball without -dev version suffix" OFF)
248 mark_as_advanced(GMX_BUILD_TARBALL)
249 if (NOT SOURCE_IS_SOURCE_DISTRIBUTION AND NOT GMX_BUILD_TARBALL)
250     set(GMX_VERSION_STRING "${GMX_VERSION_STRING}-dev")
251 endif()
252
253 set(REGRESSIONTEST_VERSION "${GMX_VERSION_STRING}")
254 set(REGRESSIONTEST_BRANCH "refs/heads/master")
255 # TODO Find some way of ensuring that this is bumped appropriately for
256 # each release. It's hard to test because it is only used for
257 # REGRESSIONTEST_DOWNLOAD, which doesn't work until that tarball has
258 # been placed on the server.
259 set(REGRESSIONTEST_MD5SUM "614a74e9b143bda5476f87f4ce08eec0" CACHE INTERNAL "MD5 sum of the regressiontests tarball")
260
261 math(EXPR GMX_VERSION_NUMERIC
262      "${GMX_VERSION_MAJOR}*10000 + ${GMX_VERSION_MINOR}*100 + ${GMX_VERSION_PATCH}")
263 set(GMX_API_VERSION ${GMX_VERSION_NUMERIC})
264
265 #####################################################################
266 # git version info management
267
268 # There can be clusters where git and CMake can run on nodes where the other is
269 # not available, accessing the same source tree.
270 # Should be unlikely, but doesn't hurt to check.
271 set(_git_info_default OFF)
272 if (SOURCE_IS_GIT_REPOSITORY)
273     find_package(Git)
274     if (GIT_FOUND)
275         set(_git_info_default ON)
276     endif()
277 endif()
278 option(GMX_GIT_VERSION_INFO "Generate git version information" ${_git_info_default})
279 mark_as_advanced(GMX_GIT_VERSION_INFO)
280 # Detect preconditions for version info generation if it is requested.
281 if (GMX_GIT_VERSION_INFO)
282     if (NOT SOURCE_IS_GIT_REPOSITORY)
283         message(FATAL_ERROR
284             "Cannot generate git version information from source tree not under git. "
285             "Set GMX_GIT_VERSION_INFO=OFF to proceed.")
286     endif()
287     # We need at least git v1.5.3 be able to parse git's date output.
288     if (NOT GIT_FOUND OR GIT_VERSION_STRING VERSION_LESS "1.5.3")
289         message(FATAL_ERROR
290             "No compatible git version found (>= 1.5.3 required). "
291             "Won't be able to generate development version information. "
292             "Set GMX_GIT_VERSION_INFO=OFF to proceed.")
293     endif()
294 endif()
295
296 include(gmxCustomCommandUtilities)
297
298 # The first two are also for use outside this file, encapsulating the details
299 # of how to use the generated VersionInfo.cmake.
300 set(VERSION_INFO_CMAKE_FILE   ${PROJECT_BINARY_DIR}/VersionInfo.cmake)
301 set(VERSION_INFO_DEPS         ${VERSION_INFO_CMAKE_FILE})
302 # Capture the location of the necessary files in internal variables for use in
303 # the function below.
304 set(VERSION_INFO_CMAKEIN_FILE     ${CMAKE_CURRENT_LIST_DIR}/VersionInfo.cmake.cmakein)
305 set(VERSION_INFO_CONFIGURE_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/gmxConfigureVersionInfo.cmake)
306
307 # Rules to create the VersionInfo.cmake file.
308 # For git info, the sequence is:
309 #   1. (configure time) VersionInfo.cmake.cmakein -> VersionInfo-partial.cmake.cmakein
310 #        - Set all variables that are known at configure time.
311 #   2. (build time)     VersionInfo-partial.cmake.cmakein -> VersionInfo.cmake
312 #        - Set variables that may change as result of repository state changes
313 #          (i.e., everything that requires running git).
314 #        - Runs every time as a git-version-info target, but the output file
315 #          timestamp only changes if its contents actually change.
316 #        - Depending on the native build system, this may run once per build
317 #          or once per each time it is required for step 3.
318 #   3. (build time)     VersionInfo.cmake -> other files
319 #        - Set variables in files specified with gmx_configure_version_file()
320 #          using the values generated in step 2.
321 #        - Each file runs as a custom command that depends on the previous
322 #          steps, and runs only if the VersionInfo.cmake file is newer than the
323 #          output file.
324 # Without git info, the sequence is:
325 #  1. (configure time) VersionInfo.cmake.cmakein -> VersionInfo.cmake
326 #        - Everything is known at configure time, so the output is generated
327 #          immediately with all variables set (git info will be empty).
328 #  2. (build time)     VersionInfo.cmake -> other files
329 #        - As with git info, processes files from gmx_configure_version_file().
330 #        - These are again custom commands that depend on the output from
331 #          step 1, so they get regenerated only when the static version info
332 #          changes.
333 if (GMX_GIT_VERSION_INFO)
334     # Configure information known at this time into a partially filled
335     # version info file.
336     set(VERSION_INFO_CMAKEIN_FILE_PARTIAL
337         ${PROJECT_BINARY_DIR}/VersionInfo-partial.cmake.cmakein)
338     # Leave these to be substituted by the custom target below.
339     set(GMX_VERSION_STRING_FULL       "\@GMX_VERSION_STRING_FULL\@")
340     set(GMX_VERSION_FULL_HASH         "\@GMX_VERSION_FULL_HASH\@")
341     set(GMX_VERSION_CENTRAL_BASE_HASH "\@GMX_VERSION_CENTRAL_BASE_HASH\@")
342     configure_file(${VERSION_INFO_CMAKEIN_FILE}
343                    ${VERSION_INFO_CMAKEIN_FILE_PARTIAL}
344                    @ONLY)
345     # If generating the version info, create a target that runs on every build
346     # and does the actual git calls, storing the results into a CMake script.
347     # This needs to be run at build time to update the version information
348     # properly when the git hash changes, but the build system does not.
349     # All targets added by gmx_configure_version_file() use the information
350     # from this script to get their variables from, removing the need to run
351     # git multiple times and simplifying reuse for other purposes.
352     gmx_add_custom_output_target(git-version-info RUN_ALWAYS
353         OUTPUT ${VERSION_INFO_CMAKE_FILE}
354         COMMAND ${CMAKE_COMMAND}
355             -D GIT_EXECUTABLE=${GIT_EXECUTABLE}
356             -D PROJECT_VERSION=${GMX_VERSION_STRING}
357             -D PROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}
358             -D VERSION_CMAKEIN=${VERSION_INFO_CMAKEIN_FILE_PARTIAL}
359             -D VERSION_OUT=${VERSION_INFO_CMAKE_FILE}
360             -P ${CMAKE_CURRENT_LIST_DIR}/gmxGenerateVersionInfo.cmake
361         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
362         COMMENT "Generating git version information")
363     list(APPEND VERSION_INFO_DEPS git-version-info)
364 else()
365     # If the version info is static, just generate the CMake script with the
366     # version variables during the CMake run.
367     set(GMX_VERSION_STRING_FULL       ${GMX_VERSION_STRING})
368     set(GMX_VERSION_FULL_HASH         "")
369     set(GMX_VERSION_CENTRAL_BASE_HASH "")
370     configure_file(${VERSION_INFO_CMAKEIN_FILE} ${VERSION_INFO_CMAKE_FILE})
371 endif()
372 unset(GMX_VERSION_STRING_FULL)
373 unset(GMX_VERSION_FULL_HASH)
374 unset(GMX_VERSION_CENTRAL_BASE_HASH)
375
376 # The main user-visible interface to the machinery.
377 # See documentation at the top of the script.
378 function (gmx_configure_version_file INFILE OUTFILE)
379     include(CMakeParseArguments)
380     set(_options REMOTE_HASH SOURCE_FILE)
381     set(_one_value_args COMMENT TARGET)
382     set(_multi_value_args EXTRA_VARS)
383     cmake_parse_arguments(
384         ARG "${_options}" "${_one_value_args}" "${_multi_value_args}" ${ARGN})
385     if (ARG_UNPARSED_ARGUMENTS)
386         message(FATAL_ERROR "Unknown arguments: ${ARG_UNPARSED_ARGUMENTS}")
387     endif()
388     # Some callers may pass partial paths that do not really make sense,
389     # so create a default comment that only contains the actual file name.
390     get_filename_component(_basename ${OUTFILE} NAME)
391     set(_comment "Generating ${_basename}")
392     if (ARG_COMMENT)
393         set(_comment ${ARG_COMMENT})
394     endif()
395     # Mimic configure_file()
396     if (NOT IS_ABSOLUTE ${INFILE})
397         set(INFILE ${CMAKE_CURRENT_SOURCE_DIR}/${INFILE})
398     endif()
399     # Create command-line definitions for the requested variables
400     set(_extra_var_defines)
401     foreach(_var ${ARG_EXTRA_VARS})
402         list(APPEND _extra_var_defines -D "${_var}=${${_var}}")
403     endforeach()
404     # The touch command is necessary to ensure that after the target is run,
405     # the timestamp is newer than in the input files.
406     add_custom_command(OUTPUT ${OUTFILE}
407         COMMAND ${CMAKE_COMMAND}
408             -D VERSION_VARIABLES=${VERSION_INFO_CMAKE_FILE}
409             -D VERSION_CMAKEIN=${INFILE}
410             -D VERSION_OUT=${OUTFILE}
411             ${_extra_var_defines}
412             -P ${VERSION_INFO_CONFIGURE_SCRIPT}
413         COMMAND ${CMAKE_COMMAND} -E touch ${OUTFILE}
414         WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
415         DEPENDS ${INFILE} ${VERSION_INFO_DEPS} ${VERSION_INFO_CONFIGURE_SCRIPT}
416         COMMENT "${_comment}"
417         VERBATIM)
418     if (ARG_TARGET)
419         add_custom_target(${ARG_TARGET} DEPENDS ${OUTFILE} VERBATIM)
420         gmx_set_custom_target_output(${ARG_TARGET} ${OUTFILE})
421     endif()
422     if (ARG_SOURCE_FILE)
423         set_source_files_properties(${OUTFILE} PROPERTIES GENERATED true)
424     endif()
425 endfunction()