24fc3e83c6062f1514688038026ce6c298d0d3e9
[alexxy/gromacs.git] / cmake / gmxVersionInfo.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2014,2015,2016,2017,2018 by the GROMACS development team.
5 # Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
6 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 # and including many others, as listed in the AUTHORS file in the
8 # top-level source directory and at http://www.gromacs.org.
9 #
10 # GROMACS is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public License
12 # as published by the Free Software Foundation; either version 2.1
13 # of the License, or (at your option) any later version.
14 #
15 # GROMACS is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with GROMACS; if not, see
22 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24 #
25 # If you want to redistribute modifications to GROMACS, please
26 # consider that scientific software is very special. Version
27 # control is crucial - bugs must be traceable. We will be happy to
28 # consider code for inclusion in the official distribution, but
29 # derived work must not be called official GROMACS. Details are found
30 # in the README & COPYING files - if they are missing, get the
31 # official version at http://www.gromacs.org.
32 #
33 # To help us fund GROMACS development, we humbly ask that you cite
34 # the research papers on the package. Check out http://www.gromacs.org.
35
36 # Sets version information variables and provides CMake functions for
37 # generating files based on them
38 #
39 # The following variables are derived from variables initialized by
40 # https://cmake.org/cmake/help/latest/command/project.html#command:project
41 #   GMX_VERSION_MAJOR      Major version number.
42 #   GMX_VERSION_PATCH      Patch version number.
43 #       Should always be defined: zero for, e.g., 2016.
44 #
45 # This script provides the following basic version variables that need to be
46 # maintained manually:
47 #   GMX_VERSION_SUFFIX     String suffix to add to numeric version string.
48 #       "-dev" is automatically added when not building from a source package,
49 #       and does not need to be kept here. This mechanism is not quite enough
50 #       for building a tarball, but setting the CMake cache variable
51 #       GMX_BUILD_TARBALL=on will suppress the addition of "-dev" to the
52 #       version string.
53 #   LIBRARY_SOVERSION_MAJOR so major version for the built libraries.
54 #       Should be increased for each binary incompatible release. In GROMACS,
55 #       the typical policy is to increase it at the start of the development
56 #       cycle for each major/minor version change, but not for patch releases,
57 #       even if the latter may not always be fully binary compatible.
58 #       Table of historical values
59 #         GROMACS     5.0    0
60 #         GROMACS     5.1    1
61 #         GROMACS     2016   2
62 #         GROMACS     2018   3
63 #         GROMACS     2019   4
64 #         GROMACS     2020   5
65 #         GROMACS     2021   6
66 #         GROMACS     2022   7
67 #   LIBRARY_SOVERSION_MINOR so minor version for the built libraries.
68 #       Should be increased for each release that changes only the implementation.
69 #       In GROMACS, the typical policy is to increase it for each patch version
70 #       change, even if they may not always be fully binary compatible.
71 #       If it is somehow clear that the ABI implementation has not changed
72 #       in a patch release, this variable should not increase. Release candidate
73 #       and beta versions will not increase this number, since nobody should
74 #       write code against such versions.
75 #   LIBRARY_VERSION        Full library version.
76 #   REGRESSIONTEST_BRANCH  For builds not from source packages, name of the
77 #       regressiontests branch at gerrit.gromacs.org whose HEAD can test this
78 #       code, *if* this code is recent enough (i.e., contains all changes from
79 #       the corresponding code branch that affects the regression test
80 #       results). Even after a release branch is forked for the source
81 #       repository, the correct regressiontests branch can still be master,
82 #       because we do not fork it until behaviour needs to change.
83 #   REGRESSIONTEST_MD5SUM
84 #       The MD5 checksum of the regressiontest tarball. Only used when building
85 #       from a source package.
86 #   GMX_SOURCE_DOI_ID
87 #       ID collected from Zenodo connected to the doi for a released version
88 #       used to identify the source when building an official released version.
89 #       This ID is used for the source code tarball.
90 #   GMX_MANUAL_DOI_ID
91 #       Same as above, but for the reference manual.
92 # They are collected into a single section below.
93 # The following variables are set based on these:
94 #   GMX_VERSION            String composed from GMX_VERSION_* numeric variables
95 #       above. Example: 4.6.1, 5.0, 2016
96 #   GMX_VERSION_STRING     String with GMX_VERSION suffixed with the given
97 #       suffix and possibly "-dev" for builds not from a source package.
98 #   GMX_VERSION_NUMERIC    Numeric version number (e.g., 40601 for 4.6.1, 20160001 for 2016.1).
99 #   GMX_API_VERSION        Numeric API version.
100 #       This is currently set automatically to GMX_VERSION_NUMERIC, but may
101 #       become manually maintained in the future if there will be releases
102 #       where the API does not change, but programs/libraries do.
103 #       In such a case, this should be the first version where the current API
104 #       appeared.
105 #   REGRESSIONTEST_VERSION For source packages, version number of the
106 #       matching regressiontests tarball.  Not used for builds not from source
107 #       packages.
108 # The latter two are used to generate gromacs/version.h to allow software
109 # written against the GROMACS API to provide some #ifdef'ed code to support
110 # multiple GROMACS versions.
111 #
112 # This script also declares machinery to generate and obtain version
113 # information from a git repository.  This is enabled by default if the source
114 # tree is a git, but can be disabled with
115 #   GMX_GIT_VERSION_INFO           Advanced CMake variable to disable git
116 #                                  version info generation.
117 # If the version generation is disabled, then the source and manual doi
118 # will be based on the stored values for the ID.
119 # The main interface to this machinery is the gmx_configure_version_file()
120 # CMake function.  The signature is
121 #   gmx_configure_version_file(<input> <output>
122 #                              [REMOTE_HASH]
123 #                              [TARGET <target>]
124 #                              [COMMENT <comment>])
125 #   <input>      Specify the input and output files as for configure_file().
126 #   <output>     The configuration is done with configure_file(... @ONLY) with
127 #                the following variables defined (as well as all the
128 #                GMX_VERSION* variables from above):
129 #                  GMX_VERSION_STRING_FULL
130 #                  GMX_VERSION_FULL_HASH
131 #                  GMX_VERSION_CENTRAL_BASE_HASH
132 #                The output file is created during build time, so any dependent
133 #                targets should specify it as a dependency.
134 #   REMOTE_HASH  Currently, this has no effect, but it signifies that the
135 #                <input> file is using the CENTRAL_BASE_HASH variable.
136 #                This variable is much more expensive to initialize than the
137 #                others, so this allows local changes in this file to only
138 #                compute that value when required if that becomes necessary.
139 #   TARGET       By default, this function uses add_custom_command() to
140 #                generate the output file.  If TARGET is specified, then
141 #                add_custom_target() is used to create a target with the given
142 #                name <target> that runs this custom command.  Use this if
143 #                the same file will be used for multiple downstream targets,
144 #                or if the explicit target for the file is otherwise
145 #                necessary.
146 #   COMMENT      Set a custom comment to be shown when building the rule
147 #                (see add_custom_command(... COMMENT <comment>)).
148 # As an alternative to using this script, also the following variables are
149 # provided (can be useful when generating more complex CMake scripts that do
150 # build-time tasks):
151 #   VERSION_INFO_CMAKE_SCRIPT
152 #       Absolute path to a CMake script that can be included using include()
153 #       to declare the GMX_VERSION_* variables documented for
154 #       gmx_configure_version_file().
155 #   VERSION_INFO_DEPS
156 #       If a custom command depends on VERSION_INFO_CMAKE_SCRIPT, then it
157 #       should add ${VERSION_INFO_DEPS} to its DEPENDS list to get the
158 #       appropriate dependencies.
159 # TODO: If someone wants to add a custom target that depends on
160 # VERSION_INFO_CMAKE_SCRIPT, a separate variable may be needed for those
161 # dependencies.
162 #
163 # The version string printed by 'gmx -version' (and also printed in the startup
164 # header) can provide useful information for, e.g., diagnosing bug reports and
165 # identifying what exact version the user was using.  The following formats are
166 # possible (with examples given for a particular version):
167 #   2018.1       Plain version number without any suffix signifies a build from
168 #                a released source tarball.
169 #   2018.1-dev   '-dev' suffix signifies all other builds. If there is no other
170 #                information, either the user built the code outside any git
171 #                repository, or disabled the version info generation.
172 #   2018.1-dev-YYYYMMDD-1234abc
173 #                The YYYYMMDD part shows the commit date (not author date) of
174 #                the HEAD commit from which the code was built.  The abbreviated
175 #                hash is the hash of that commit (the full hash is available in
176 #                'gmx -version' output).
177 #                If the HEAD hash is not identified as coming from branches in
178 #                "authoritative" GROMACS repositories, 'gmx -version' will show
179 #                the nearest ancestor commit that is identified as such (but see
180 #                the '-local' and '-unknown' suffixes below).
181 #   2018.1-dev-YYYYMMDD-1234abc-dirty
182 #                As above, but there were local modifications in the source tree
183 #                when the code was built.
184 #   2018.1-dev-YYYYMMDD-1234abc-unknown
185 #                As above, but there were no remotes in the repository that
186 #                could be identified as "authoritative" GROMACS repositories.
187 #                This happens if the code is not cloned from git.gromacs.org
188 #                or gerrit.gromacs.org.
189 #   2018.1-dev-YYYYMMDD-1234abc-local
190 #                As above, but there were no commits in the recent history of
191 #                the branch that could be identified as coming from
192 #                "authoritative" GROMACS repositories.  This should be
193 #                relatively rare.
194 #
195 # Other variables set here are not intended for use outside this file.
196 # The scripts gmxGenerateVersionInfo.cmake and gmxConfigureVersionInfo.cmake
197 # are used internally by this machinery, as well as VersionInfo.cmake.cmakein.
198
199 #####################################################################
200 # Derived version info.
201 # Ref https://cmake.org/cmake/help/latest/command/project.html#command:project
202 set(GMX_VERSION_MAJOR ${Gromacs_VERSION_MAJOR})
203 set(GMX_VERSION_PATCH ${Gromacs_VERSION_MINOR})
204
205 #####################################################################
206 # Manually maintained version info
207
208 # The suffix is used mainly for betas and release
209 # candidates, where it signifies the most recent such release from
210 # this branch; it will be empty before the first such release, as well
211 # as after the final release is out.
212 set(GMX_VERSION_SUFFIX "-beta1")
213
214 # Conventionally with libtool, any ABI change must change the major
215 # version number, the minor version number should change if it's just
216 # the implementation that has been altered, and the third number
217 # counts the number of old major versions that will still run if
218 # linked to this library (i.e. it is not a patch number). See the
219 # above descriptions of LIBRARY_SOVERSION_* for policy for changes
220 # here. The important thing is to minimize the chance of third-party
221 # code being able to dynamically link with a version of libgromacs
222 # that might not work.
223 set(LIBRARY_SOVERSION_MAJOR 7)
224 set(LIBRARY_SOVERSION_MINOR 0)
225 set(LIBRARY_VERSION ${LIBRARY_SOVERSION_MAJOR}.${LIBRARY_SOVERSION_MINOR}.0)
226
227 #####################################################################
228 # General version management based on manually set numbers
229
230 if (GMX_VERSION_PATCH)
231     set(GMX_VERSION "${GMX_VERSION_MAJOR}.${GMX_VERSION_PATCH}")
232 else()
233     set(GMX_VERSION "${GMX_VERSION_MAJOR}")
234 endif()
235 set(GMX_VERSION_STRING "${GMX_VERSION}${GMX_VERSION_SUFFIX}")
236
237 set(REGRESSIONTEST_VERSION "${GMX_VERSION_STRING}")
238 set(REGRESSIONTEST_BRANCH "master")
239 # Follow the relevant part of the release checklist at
240 # https://gitlab.com/gromacs/gromacs/-/wikis/Release-checklist#how-to-build-a-regressiontests-tarball
241 # in order to have it build the regressiontests tarball with all the
242 # right version naming. The version number and suffix goes into the
243 # directory name within the regressiontests tarball, which affects the
244 # md5sum of the tarball. The matching md5sum has to go here, and if it
245 # isn't right the real release workflow will report a failure.
246 set(REGRESSIONTEST_MD5SUM "9e122010c6e0fea35ec621a8e6561f7f" CACHE INTERNAL "MD5 sum of the regressiontests tarball for this GROMACS version")
247
248 # If you are making a custom fork of GROMACS, please describe your
249 # fork, perhaps with its version number, in the value of
250 # GMX_VERSION_STRING_OF_FORK here. This string will appear in the
251 # header of log files that mdrun writes. This will help you, your
252 # users, your system administrators, your maintainers and the
253 # maintainers of GROMACS core understand how to troubleshoot and
254 # reproduce potential problems.
255 #
256 # If you are distributing a patch to GROMACS, then this change would
257 # be great as part of your patch. Otherwise for personal use, you can
258 # also just set a CMake cache variable.
259 set(GMX_VERSION_STRING_OF_FORK "" CACHE INTERNAL
260     "Version string for forks of GROMACS to set to describe themselves")
261 mark_as_advanced(GMX_VERSION_STRING_OF_FORK)
262 if (GMX_VERSION_STRING_OF_FORK)
263     set(GMX_VERSION_STRING "${GMX_VERSION_STRING}-${GMX_VERSION_STRING_OF_FORK}")
264 endif()
265
266 option(GMX_BUILD_TARBALL "Build tarball without -dev version suffix" OFF)
267 mark_as_advanced(GMX_BUILD_TARBALL)
268 if (NOT SOURCE_IS_SOURCE_DISTRIBUTION AND
269     NOT GMX_BUILD_TARBALL)
270     set(GMX_VERSION_STRING "${GMX_VERSION_STRING}-dev")
271 endif()
272
273 math(EXPR GMX_VERSION_NUMERIC
274      "${GMX_VERSION_MAJOR}*10000 + ${GMX_VERSION_PATCH}")
275 set(GMX_API_VERSION ${GMX_VERSION_NUMERIC})
276
277 # Set those values only in release versions, after getting the identifiers
278 # from Zenodo for the manual and source code
279 # Has to be done by hand before every final release
280 # Use force to override anything given as a cmake command line input
281 # Actual input depends on the GMX_VERSION_STRING_OF_FORK variable being set or not.
282 # If it is set, we always default to an empty string, otherwise to the value set for the release build.
283 if (GMX_VERSION_STRING_OF_FORK)
284     set(GMX_MANUAL_DOI_INTERNAL "")
285     set(GMX_SOURCE_DOI_INTERNAL "")
286 else()
287     set(GMX_MANUAL_DOI_INTERNAL "") # Set correct doi string here
288     set(GMX_SOURCE_DOI_INTERNAL "") # Set correct doi string here
289 endif()
290 set(GMX_MANUAL_DOI ${GMX_MANUAL_DOI_INTERNAL} CACHE INTERNAL "reserved doi for GROMACS manual" FORCE)
291 set(GMX_SOURCE_DOI ${GMX_SOURCE_DOI_INTERNAL} CACHE INTERNAL "reserved doi for GROMACS source code" FORCE)
292
293 #####################################################################
294 # git version info management
295
296 # There can be clusters where git and CMake can run on nodes where the other is
297 # not available, accessing the same source tree.
298 # Should be unlikely, but doesn't hurt to check.
299 set(_git_info_default OFF)
300 if (SOURCE_IS_GIT_REPOSITORY)
301     find_package(Git)
302     if (GIT_FOUND)
303         set(_git_info_default ON)
304     endif()
305 endif()
306 option(GMX_GIT_VERSION_INFO "Generate git version information" ${_git_info_default})
307 mark_as_advanced(GMX_GIT_VERSION_INFO)
308 # Detect preconditions for version info generation if it is requested.
309 if (GMX_GIT_VERSION_INFO)
310     if (NOT SOURCE_IS_GIT_REPOSITORY)
311         message(FATAL_ERROR
312             "Cannot generate git version information from source tree not under git. "
313             "Set GMX_GIT_VERSION_INFO=OFF to proceed.")
314     endif()
315     # We need at least git v1.5.3 be able to parse git's date output.
316     if (NOT GIT_FOUND OR GIT_VERSION_STRING VERSION_LESS "1.5.3")
317         message(FATAL_ERROR
318             "No compatible git version found (>= 1.5.3 required). "
319             "Won't be able to generate development version information. "
320             "Set GMX_GIT_VERSION_INFO=OFF to proceed.")
321     endif()
322 endif()
323
324 include(gmxCustomCommandUtilities)
325 include(FindPythonModule)
326 # The first two are also for use outside this file, encapsulating the details
327 # of how to use the generated VersionInfo.cmake.
328 set(VERSION_INFO_CMAKE_FILE   ${PROJECT_BINARY_DIR}/VersionInfo.cmake)
329 set(VERSION_INFO_DEPS         ${VERSION_INFO_CMAKE_FILE})
330 # Capture the location of the necessary files in internal variables for use in
331 # the function below.
332 set(VERSION_INFO_CMAKEIN_FILE     ${CMAKE_CURRENT_LIST_DIR}/VersionInfo.cmake.cmakein)
333 set(VERSION_INFO_CONFIGURE_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/gmxConfigureVersionInfo.cmake)
334 # A set of directories to scan for calculating the hash of source files.
335 set(SET_OF_DIRECTORIES_TO_CHECKSUM  "src")
336 list(APPEND SET_OF_DIRECTORIES_TO_CHECKSUM "python_packaging")
337 # Due to the limitations for passing a list as arguments, we make the directories a string here
338 string(REPLACE ";" ":" DIRECTORIES_TO_CHECKSUM_STRING "${SET_OF_DIRECTORIES_TO_CHECKSUM}")
339
340 # Rules to create the VersionInfo.cmake file.
341 # For git info, the sequence is:
342 #   1. (configure time) VersionInfo.cmake.cmakein -> VersionInfo-partial.cmake.cmakein
343 #        - Set all variables that are known at configure time.
344 #   2. (build time)     VersionInfo-partial.cmake.cmakein -> VersionInfo.cmake
345 #        - Set variables that may change as result of repository state changes
346 #          (i.e., everything that requires running git).
347 #        - Runs every time as a git-version-info target, but the output file
348 #          timestamp only changes if its contents actually change.
349 #        - Depending on the native build system, this may run once per build
350 #          or once per each time it is required for step 3.
351 #   3. (build time)     VersionInfo.cmake -> other files
352 #        - Set variables in files specified with gmx_configure_version_file()
353 #          using the values generated in step 2.
354 #        - Each file runs as a custom command that depends on the previous
355 #          steps, and runs only if the VersionInfo.cmake file is newer than the
356 #          output file.
357 # Without git info, the sequence is:
358 #  1. (configure time) VersionInfo.cmake.cmakein -> VersionInfo.cmake
359 #        - Everything is known at configure time, so the output is generated
360 #          immediately with all variables set (git info will be empty).
361 #  2. (build time)     VersionInfo.cmake -> other files
362 #        - As with git info, processes files from gmx_configure_version_file().
363 #        - These are again custom commands that depend on the output from
364 #          step 1, so they get regenerated only when the static version info
365 #          changes.
366 #
367 # Note that VersionInfo-partial.cmake is also used to transfer version
368 # information between GitLab CI jobs for release and documentation builds.
369
370 # Check if we have all necessary python modules available
371 if (Python3_Interpreter_FOUND)
372     set(HAVE_FULL_FUNCTIONING_PYTHON Python3_Interpreter_FOUND)
373     foreach(module argparse hashlib hmac os stat re) # add further modules if necessary
374         find_python_module(${module} QUIET)
375         string(TOUPPER ${module} module_upper)
376         if(NOT PYTHONMODULE_${module_upper})
377             message(STATUS
378                 "Python module ${module} not found - disabling checksum validation")
379             unset(HAVE_FULL_FUNCTIONING_PYTHON)
380         endif()
381     endforeach()
382 endif()
383
384 # Configure information known at this time into a partially filled
385 # version info file.
386 set(VERSION_INFO_CMAKEIN_FILE_PARTIAL
387     ${PROJECT_BINARY_DIR}/VersionInfo-partial.cmake.cmakein)
388 # Leave these to be substituted by the targets below.
389 set(GMX_VERSION_STRING_FULL       "\@GMX_VERSION_STRING_FULL\@")
390
391 if (GMX_GIT_VERSION_INFO)
392     # Leave these to be substituted by the custom target below.
393     # Specific for building from git.
394     set(GMX_VERSION_FULL_HASH         "\@GMX_VERSION_FULL_HASH\@")
395     set(GMX_VERSION_CENTRAL_BASE_HASH "\@GMX_VERSION_CENTRAL_BASE_HASH\@")
396     # If generating the version info, create a target that runs on every build
397     # and does the actual git calls, storing the results into a CMake script.
398     # This needs to be run at build time to update the version information
399     # properly when the git hash changes, but the build system does not.
400     # All targets added by gmx_configure_version_file() use the information
401     # from this script to get their variables from, removing the need to run
402     # git multiple times and simplifying reuse for other purposes.
403     gmx_add_custom_output_target(git-version-info RUN_ALWAYS
404         OUTPUT ${VERSION_INFO_CMAKE_FILE}
405         COMMAND ${CMAKE_COMMAND}
406             -D GIT_EXECUTABLE=${GIT_EXECUTABLE}
407             -D PROJECT_VERSION=${GMX_VERSION_STRING}
408             -D PROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}
409             -D VERSION_CMAKEIN=${VERSION_INFO_CMAKEIN_FILE_PARTIAL}
410             -D VERSION_OUT=${VERSION_INFO_CMAKE_FILE}
411             -P ${CMAKE_CURRENT_LIST_DIR}/gmxGenerateVersionInfo.cmake
412         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
413         COMMENT "Generating git version information")
414     list(APPEND VERSION_INFO_DEPS git-version-info)
415 else()
416     # Leave these to be substituted by the custom target below.
417     # Specific for building from source tarball.
418     set(GMX_RELEASE_SOURCE_FILE_CHECKSUM "\@GMX_RELEASE_SOURCE_FILE_CHECKSUM\@")
419     set(GMX_CURRENT_SOURCE_FILE_CHECKSUM "\@GMX_CURRENT_SOURCE_FILE_CHECKSUM\@")
420     gmx_add_custom_output_target(release-version-info RUN_ALWAYS
421         OUTPUT ${VERSION_INFO_CMAKE_FILE}
422         COMMAND ${CMAKE_COMMAND}
423             -D PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}
424             -D HAVE_FULL_FUNCTIONING_PYTHON=${HAVE_FULL_FUNCTIONING_PYTHON}
425             -D PROJECT_VERSION=${GMX_VERSION_STRING}
426             -D PROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}
427             -D DIRECTORIES_TO_CHECKSUM=${DIRECTORIES_TO_CHECKSUM_STRING}
428             -D VERSION_CMAKEIN=${VERSION_INFO_CMAKEIN_FILE_PARTIAL}
429             -D VERSION_OUT=${VERSION_INFO_CMAKE_FILE}
430             -D VERSION_STRING_OF_FORK=${GMX_VERSION_STRING_OF_FORK}
431             -D SOURCE_IS_SOURCE_DISTRIBUTION=${SOURCE_IS_SOURCE_DISTRIBUTION}
432             -P ${CMAKE_CURRENT_LIST_DIR}/gmxGenerateVersionInfoWithoutGit.cmake
433         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
434         COMMENT "Generating release version information")
435     list(APPEND VERSION_INFO_DEPS release-version-info)
436 endif()
437 configure_file(${VERSION_INFO_CMAKEIN_FILE}
438                ${VERSION_INFO_CMAKEIN_FILE_PARTIAL}
439                @ONLY)
440 unset(GMX_VERSION_STRING_FULL)
441 unset(GMX_VERSION_FULL_HASH)
442 unset(GMX_VERSION_CENTRAL_BASE_HASH)
443 unset(GMX_RELEASE_SOURCE_FILE_CHECKSUM)
444 unset(GMX_CURRENT_SOURCE_FILE_CHECKSUM)
445
446
447 # What file the checksum should be written to
448 set(CHECKSUM_FILE "${PROJECT_SOURCE_DIR}/src/reference_checksum")
449
450 # Target that allows checksumming a source tree when producing a tarball.
451 # Allows verification of builds from the tarball to make sure the source had
452 # not been tampered with.
453 # Note: The RUN_ALWAYS here is to regenerate the hash file only, it does not
454 # mean that the target is run in all builds
455 if (HAVE_FULL_FUNCTIONING_PYTHON)
456     # We need the full path to the directories after passing it through
457     set(FULL_PATH_DIRECTORIES "")
458     foreach(DIR ${SET_OF_DIRECTORIES_TO_CHECKSUM})
459         list(APPEND FULL_PATH_DIRECTORIES "${PROJECT_SOURCE_DIR}/${DIR}")
460     endforeach()
461     gmx_add_custom_output_target(reference_checksum RUN_ALWAYS
462         OUTPUT ${CHECKSUM_FILE}
463         COMMAND ${PYTHON_EXECUTABLE}
464             ${PROJECT_SOURCE_DIR}/admin/createFileHash.py
465             -s ${FULL_PATH_DIRECTORIES}
466             -o ${CHECKSUM_FILE}
467         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
468         COMMENT "Generating reference checksum of source files")
469 else()
470     add_custom_target(reference_checksum
471         COMMAND ${CMAKE_COMMAND} -E echo
472         "Can not checksum files without python3 being available"
473         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
474         COMMENT "Generating reference checksum of source files")
475 endif()
476
477 # The main user-visible interface to the machinery.
478 # See documentation at the top of the script.
479 function (gmx_configure_version_file INFILE OUTFILE)
480     include(CMakeParseArguments)
481     set(_options REMOTE_HASH)
482     set(_one_value_args COMMENT TARGET)
483     set(_multi_value_args EXTRA_VARS)
484     cmake_parse_arguments(
485         ARG "${_options}" "${_one_value_args}" "${_multi_value_args}" ${ARGN})
486     if (ARG_UNPARSED_ARGUMENTS)
487         message(FATAL_ERROR "Unknown arguments: ${ARG_UNPARSED_ARGUMENTS}")
488     endif()
489     # Some callers may pass partial paths that do not really make sense,
490     # so create a default comment that only contains the actual file name.
491     get_filename_component(_basename ${OUTFILE} NAME)
492     set(_comment "Generating ${_basename}")
493     if (ARG_COMMENT)
494         set(_comment ${ARG_COMMENT})
495     endif()
496     # Mimic configure_file()
497     if (NOT IS_ABSOLUTE ${INFILE})
498         set(INFILE ${CMAKE_CURRENT_SOURCE_DIR}/${INFILE})
499     endif()
500     # Create command-line definitions for the requested variables
501     set(_extra_var_defines)
502     foreach(_var ${ARG_EXTRA_VARS})
503         list(APPEND _extra_var_defines -D "${_var}=${${_var}}")
504     endforeach()
505     # The touch command is necessary to ensure that after the target is run,
506     # the timestamp is newer than in the input files.
507     add_custom_command(OUTPUT ${OUTFILE}
508         COMMAND ${CMAKE_COMMAND}
509             -D VERSION_VARIABLES=${VERSION_INFO_CMAKE_FILE}
510             -D VERSION_CMAKEIN=${INFILE}
511             -D VERSION_OUT=${OUTFILE}
512             ${_extra_var_defines}
513             -P ${VERSION_INFO_CONFIGURE_SCRIPT}
514         COMMAND ${CMAKE_COMMAND} -E touch ${OUTFILE}
515         WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
516         DEPENDS ${INFILE} ${VERSION_INFO_DEPS} ${VERSION_INFO_CONFIGURE_SCRIPT}
517         COMMENT "${_comment}"
518         VERBATIM)
519     if (ARG_TARGET)
520         add_custom_target(${ARG_TARGET} DEPENDS ${OUTFILE} VERBATIM)
521         gmx_set_custom_target_output(${ARG_TARGET} ${OUTFILE})
522     endif()
523 endfunction()