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