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