Fix bug in source verification on Windows
[alexxy/gromacs.git] / cmake / gmxGenerateVersionInfoRelease.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2020, 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 # Generate GROMACS release build version information, as well as build
36 # directory integrity checking.
37
38 # This script generates version information for a build from a release tarball
39 # source tree based on the release information in the released tarball.
40 # It is assumed that by default the script is run in cmake script mode.
41 # If *not* called in script mode but used in generating cache variables,
42 # GEN_VERSION_INFO_INTERNAL has to be set ON.
43 #
44 # The following variables have to be previously defined:
45 # PROJECT_VERSION         - hard-coded version string (generated info is appended)
46 # PROJECT_SOURCE_DIR      - top level source directory
47 # DIRECTORIES_TO_CHECKSUM - List of directories to hash
48 # VERSION_CMAKEIN         - path to an input template file
49 # VERSION_OUT             - path to the output file
50 #
51 # The following a possible additional definitions
52 # PYTHON_EXECUTABLE       - Needed to run checking stage of current tree
53 # VERSION_STRING_OF_FORK  - Possibly defined custom version string to override
54 #                          process of checking source file hashes.
55 # Output:
56 # VERSION_OUT is configured from the input VERSION_CMAKEIN
57 # using the variables listed below.
58 #
59 # GMX_VERSION_STRING_FULL       - version string
60 # GMX_RELEASE_SOURCE_FILE_CHECKSUM - Sha256 hash of source tree files at release
61 # GMX_CURRENT_SOURCE_FILE_CHECKSUM - Sha256 hash of current source tree files
62 #
63 # Paul Bauer (paul.bauer.q@gmail.com)
64 # (authors of git Version of the script that this is based on below)
65 # Szilard Pall (pszilard@cbr.su.se)
66 # Teemu Murtola (teemu.murtola@gmail.com)
67
68 # Check input variables.
69 if("${PROJECT_VERSION}" STREQUAL "")
70     message(FATAL_ERROR "PROJECT_VERSION undefined!")
71 endif()
72 if (NOT EXISTS "${PROJECT_SOURCE_DIR}")
73     message(FATAL_ERROR "Project source directory ${PROJECT_SOURCE_DIR} does not exist")
74 endif()
75 if ("${DIRECTORIES_TO_CHECKSUM}" STREQUAL "")
76     message(FATAL_ERROR "Need list of directories to generate the hash for")
77 endif()
78 if ("${VERSION_CMAKEIN}" STREQUAL "")
79     message(FATAL_ERROR "Missing input parameter VERSION_CMAKEIN!")
80 endif()
81 if ("${VERSION_OUT}" STREQUAL "")
82     message(FATAL_ERROR "Missing input parameter VERSION_OUT!")
83 endif()
84
85 # Prepare version string to populate
86 set(GMX_VERSION_STRING_FULL          ${PROJECT_VERSION})
87
88 # We had to pass the directory list as a string, so now we convert it back to a list
89 string(REPLACE ":" ";" DIRECTORIES_TO_CHECKSUM_LIST ${DIRECTORIES_TO_CHECKSUM})
90
91 # We need the full path to the directories after passing it through
92 set(FULL_PATH_DIRECTORIES "")
93 foreach(DIR ${DIRECTORIES_TO_CHECKSUM_LIST})
94     list(APPEND FULL_PATH_DIRECTORIES "${PROJECT_SOURCE_DIR}/${DIR}")
95 endforeach()
96
97 # Prepare for checking source tree file hashes.
98 # To notify the user during compilation and at runtime that the build source
99 # has not been modified after unpacking the source tarball, the contents are hashed
100 # to be compared to a hash computed during the release process. If the hash matches
101 # all is fine and the user gets a message in the log file indicating that.
102 # If either the release hash file is missing, or if the hash does not match
103 # a different message is printed to indicate that the source has been changed
104 # compared to the version actually released. This is not needed in case a build
105 # is done in git, as we have the information there already.
106 # This is not done if the user has explicitly set an additional custom version string with
107 # -DGMX_VERSION_STRING_OF_FORK, as this indicates that they are knowing that a custom
108 # version of GROMACS is in use.
109 set(RELEASE_CHECKSUM_FILE "${PROJECT_SOURCE_DIR}/src/reference_checksum")
110 if(NOT VERSION_STRING_OF_FORK OR "${VERSION_STRING_OF_FORK}" STREQUAL "")
111     if(EXISTS ${RELEASE_CHECKSUM_FILE} AND PYTHON_EXECUTABLE)
112         file(READ ${RELEASE_CHECKSUM_FILE} GMX_RELEASE_SOURCE_FILE_CHECKSUM)
113         string(STRIP ${GMX_RELEASE_SOURCE_FILE_CHECKSUM} GMX_RELEASE_SOURCE_FILE_CHECKSUM)
114         set(CHECKSUM_RESULT_FILE "${CMAKE_CURRENT_BINARY_DIR}/computed_checksum")
115         execute_process(COMMAND ${PYTHON_EXECUTABLE}
116                                 ${PROJECT_SOURCE_DIR}/admin/createFileHash.py
117                                 -s ${FULL_PATH_DIRECTORIES}
118                                 -o ${CHECKSUM_RESULT_FILE}
119                         WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
120                         OUTPUT_QUIET)
121                     file(READ ${CHECKSUM_RESULT_FILE} GMX_CURRENT_SOURCE_FILE_CHECKSUM)
122         string(STRIP ${GMX_CURRENT_SOURCE_FILE_CHECKSUM} GMX_CURRENT_SOURCE_FILE_CHECKSUM)
123         if(NOT ${GMX_RELEASE_SOURCE_FILE_CHECKSUM} STREQUAL ${GMX_CURRENT_SOURCE_FILE_CHECKSUM})
124             set(GMX_VERSION_STRING_FULL "${GMX_VERSION_STRING_FULL}-MODIFIED")
125             message(STATUS "The source code for this GROMACS installation is different from the officially released version.")
126         endif()
127     elseif(PYTHON_EXECUTABLE)
128         set(GMX_VERSION_STRING_FULL "${GMX_VERSION_STRING_FULL}-UNCHECKED")
129         set(GMX_RELEASE_SOURCE_FILE_CHECKSUM "NoChecksumFile")
130         set(GMX_CURRENT_SOURCE_FILE_CHECKSUM "NoChecksumFile")
131         message(WARNING "Could not valdiate the GROMACS source due to missing reference checksum file.")
132     else()
133         set(GMX_VERSION_STRING_FULL "${GMX_VERSION_STRING_FULL}-UNCHECKED")
134         set(GMX_RELEASE_SOURCE_FILE_CHECKSUM "NoPythonAvailable")
135         set(GMX_CURRENT_SOURCE_FILE_CHECKSUM "NoPythonAvailable")
136         message(STATUS "Could not calculate checksum of source files without Python")
137     endif()
138 endif()
139
140 # Generate the output file.
141 configure_file(${VERSION_CMAKEIN} ${VERSION_OUT})