Clean up git version generation.
authorTeemu Murtola <teemu.murtola@gmail.com>
Mon, 22 Oct 2012 04:41:40 +0000 (07:41 +0300)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Sat, 27 Oct 2012 04:13:18 +0000 (06:13 +0200)
- Rename USE_VERSION_H to GMX_GIT_VERSION_INFO. This was particularly
  confusing now that there was a separate version.h file for API version
  numbers.
- Move the version.h/version.c files to src/gromacs/utility/ and rename
  them to gitversion.*, again to avoid confusion with the API version.h
  header.
- Remove unnecessary suffix stripping from gmxGenerateVersionInfo.cmake:
  when this functionality was added, the version string was of the form
  X.Y-dev-YYYYMMDD, and the script was stripping the date off. Now that
  the date is gone, it is clearer to just append the generated version
  with no extra logic.
- Change the logic for handling GMX_GIT_VERSION_INFO in CMake such that
  user-provided cache values are never changed under the hood.

Change-Id: I372a4e2fdf11b1799dd22997bc6adb4faf3db93d

CMakeLists.txt
cmake/gmxGenerateVersionInfo.cmake
src/config.h.cmakein
src/gromacs/CMakeLists.txt
src/gromacs/gmxlib/copyrite.c
src/gromacs/utility/.gitignore
src/gromacs/utility/gitversion.c.cmakein [moved from src/gromacs/version.c.cmakein with 82% similarity]
src/gromacs/utility/gitversion.h [moved from src/gromacs/gmxlib/version.h with 93% similarity]

index 4290c921ff3839d9c0f536f4f5c525fde7e3a0e7..e1cf04b076685182012dc4cabdbc8f6abacc32db 100644 (file)
@@ -93,6 +93,11 @@ SET(CPACK_SOURCE_INSTALLED_DIRECTORIES "${CMAKE_SOURCE_DIR};/;${CMAKE_BINARY_DIR
 #must come after all cpack settings!
 include(CPack)
 
+set(SOURCE_IS_GIT_REPOSITORY OFF)
+if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
+    set(SOURCE_IS_GIT_REPOSITORY ON)
+endif()
+
 ########################################################################
 # Check and warn if cache generated on a different host is being reused
 ########################################################################
@@ -193,8 +198,8 @@ mark_as_advanced(GMX_LOAD_PLUGINS)
 option(GMX_GPU  "Enable GPU acceleration" ON)
 option(GMX_OPENMP "Enable OpenMP-based multithreading" ON)
 
-option(USE_VERSION_H "Generate development version string/information" ON)
-mark_as_advanced(USE_VERSION_H)
+option(GMX_GIT_VERSION_INFO "Generate git version information" ${SOURCE_IS_GIT_REPOSITORY})
+mark_as_advanced(GMX_GIT_VERSION_INFO)
 
 option(GMX_DEFAULT_SUFFIX "Use default suffixes for GROMACS binaries and libs (_d for double, _mpi for MPI; rerun cmake after changing to see relevant options)" ON)
 
@@ -557,29 +562,32 @@ if(WIN32 AND NOT CYGWIN)
 endif()
 
 # only bother with finding git and using version.h if the source is a git repo
-if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
-    if(USE_VERSION_H)
-        # We need at least git v1.5.3 be able to parse git's date output. If not
-        # found or the version is too small, we can't generate version information.
-        find_package(Git)
-
-       # Find out the git version
-       if(GIT_FOUND AND NOT GIT_VERSION)
-         execute_process(COMMAND ${GIT_EXECUTABLE} "--version"
-            OUTPUT_VARIABLE _exec_out
-            OUTPUT_STRIP_TRAILING_WHITESPACE)
-         string(REGEX REPLACE "git version (.*)" "\\1" GIT_VERSION ${_exec_out})
-         set(GIT_VERSION ${GIT_VERSION} CACHE STRING "Git version")
-         mark_as_advanced(GIT_VERSION)
-       endif()
-
-        if(NOT GIT_FOUND OR GIT_VERSION VERSION_LESS "1.5.3")
-          message("No compatible git version found, won't be able to generate proper development version information.")
-          set(USE_VERSION_H OFF)
-        endif()
+if(GMX_GIT_VERSION_INFO)
+    if (NOT SOURCE_IS_GIT_REPOSITORY)
+        message(FATAL_ERROR
+            "Cannot generate git version information from source tree not under git. "
+            "Set GMX_GIT_VERSION_INFO=OFF to proceed.")
+    endif ()
+    # We need at least git v1.5.3 be able to parse git's date output. If not
+    # found or the version is too small, we can't generate version information.
+    find_package(Git)
+
+    # Find out the git version
+    if(GIT_FOUND AND NOT GIT_VERSION)
+      execute_process(COMMAND ${GIT_EXECUTABLE} "--version"
+        OUTPUT_VARIABLE _exec_out
+        OUTPUT_STRIP_TRAILING_WHITESPACE)
+      string(REGEX REPLACE "git version (.*)" "\\1" GIT_VERSION ${_exec_out})
+      set(GIT_VERSION ${GIT_VERSION} CACHE STRING "Git version")
+      mark_as_advanced(GIT_VERSION)
+    endif()
+
+    if(NOT GIT_FOUND OR GIT_VERSION VERSION_LESS "1.5.3")
+        message(FATAL_ERROR
+            "No compatible git version found (>= 1.5.3 required). "
+            "Won't be able to generate development version information. "
+            "Set GMX_GIT_VERSION_INFO=OFF to proceed.")
     endif()
-else()
-    set(USE_VERSION_H OFF)
 endif()
 
 option(GMX_INTERNAL_BOOST "Use minimal internal version of boost" OFF)
index 8d65b676dd0185f0ae5d1f3560d69de45a82a829..c94a22786d3580637a6613b0e3f642403906d6dc 100644 (file)
@@ -9,16 +9,14 @@
 # The following variables have to be previously defined:
 # GIT_EXECUTABLE        - path to git binary
 # GIT_VERSION           - git version (if not defined it's assumed that >=1.5.3)
-# PROJECT_VERSION       - hard-coded version string, should have the following structure:
-#                       VERSION[-dev-SUFFIX] where the VERSION can have any form and the suffix
-#                       is optional but should start with -dev
+# PROJECT_VERSION       - hard-coded version string (generated info is appended)
 # PROJECT_SOURCE_DIR    - top level source directory (which has to be in git)
-# VERSION_C_CMAKEIN     - path to the version.c.cmakein file
-# VERSION_C_OUT         - path to the version.c output file
+# VERSION_C_CMAKEIN     - path to the gitversion.c.cmakein file
+# VERSION_C_OUT         - path to the gitversion.c output file
 #
 # Output:
-# i)  Script mode: version.c configured from the input version.c.cmakein using
-# the variables listed below.
+# i)  Script mode: gitversion.c configured from the input gitversion.c.cmakein
+# using the variables listed below.
 # ii) Cache variable mode: the varables below are set in cache.
 #
 # GMX_PROJECT_VERSION_STR   - version string
@@ -31,7 +29,6 @@
 if("${PROJECT_VERSION}" STREQUAL "")
     message(FATAL_ERROR "PROJECT_VERSION undefined!")
 endif()
-set(VER ${PROJECT_VERSION})
 
 # if we're generating variables for cache unset the variables
 if(GEN_VERSION_INFO_INTERNAL)
@@ -163,13 +160,11 @@ if(EXISTS "${GIT_EXECUTABLE}" AND NOT GIT_VERSION VERSION_LESS "1.5.3")
         endif()
     endif()
 
-    # compile final version string, if there is already a -dev suffix in VER
-    # remove everything after this and replace it with the generated suffix
-    string(REGEX REPLACE "(.*)-dev.*" "\\1" VER "${VER}")
-    set(GMX_PROJECT_VERSION_STR "${VER}-dev-${VERSION_STR_SUFFIX}")
+    # compile final version string
+    set(GMX_PROJECT_VERSION_STR "${PROJECT_VERSION}-${VERSION_STR_SUFFIX}")
 else()
-    # the version has to be defined - if not we're not using version.h/.c and set
-    # the GIT related information to "unknown"
+    # the version has to be defined - if not we're not using gitversion.h/.c;
+    # set the GIT related information to "unknown"
     message(WARNING "Source tree seems to be a repository, but no compatible git is available, using hard-coded version string")
     set(GMX_PROJECT_VERSION_STR "${PROJECT_VERSION}")
     set(GMX_GIT_HEAD_HASH "unknown")
@@ -177,7 +172,7 @@ else()
 endif()
 
 # if we're generating cache variables set these
-# otherwise it's assumed that it's called in script mode to generate version.c
+# otherwise it's assumed that it's called in script mode to generate gitversion.c
 if(GEN_VERSION_INFO_INTERNAL)
     set(GMX_PROJECT_VERSION_STR ${GMX_PROJECT_VERSION_STR}
         CACHE STRING "Gromacs version string" FORCE)
@@ -193,6 +188,6 @@ else()
     if("${VERSION_C_OUT}" STREQUAL "")
         message(FATAL_ERROR "Missing input parameter VERSION_C_OUT!")
     endif()
-    # generate version.c
+    # generate gitversion.c
     configure_file(${VERSION_C_CMAKEIN} ${VERSION_C_OUT})
 endif()
index 1e0bf611d15cc0810aa1a3b7db5e9ef426807982..e5895c753ac095de939a01bebf7ac21b9a1bbc85 100644 (file)
@@ -28,8 +28,8 @@
 /* Version number of package (translate from cmake to autoconf macro name) */
 #define VERSION  "@PROJECT_VERSION@"
 
-/* Use the version string from generated version.h */
-#cmakedefine USE_VERSION_H
+/* Use extra version information generated with git */
+#cmakedefine GMX_GIT_VERSION_INFO
 
 /* Default location of data files */
 #define GMXLIBDIR "@GMXLIBDIR@"
index 457ae58fc44567e239583605ef27c39ad7459d97..06171b854d32e414ce8944841ff71cfa4bc67b93 100644 (file)
@@ -23,25 +23,29 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/version.h.cmakein ${CMAKE_CURRENT_BIN
 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/version.h
     DESTINATION ${INCL_INSTALL_DIR}/gromacs
     COMPONENT development)
-# add target that generates version.c every time a make is run
-# only do this if we generate the version
-if (USE_VERSION_H)
+
+# Add target that generates gitversion.c every time make is run
+# if git version info is requested
+# This code is here instead of utility/CMakeLists.txt because CMake
+# ignores set_source_file_properties from subdirectories.
+if (GMX_GIT_VERSION_INFO)
+    set(GENERATED_VERSION_FILE ${CMAKE_CURRENT_BINARY_DIR}/utility/gitversion.c)
     add_custom_target(gmx_version ALL
-            COMMAND ${CMAKE_COMMAND} 
+            COMMAND ${CMAKE_COMMAND}
                 -D GIT_EXECUTABLE="${GIT_EXECUTABLE}"
                 -D GIT_VERSION="${GIT_VERSION}"
                 -D PROJECT_VERSION="${PROJECT_VERSION}"
                 -D PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}"
-                -D VERSION_C_CMAKEIN="${CMAKE_CURRENT_SOURCE_DIR}/version.c.cmakein"
-                -D VERSION_C_OUT="${CMAKE_CURRENT_BINARY_DIR}/version.c"
-                -P ${CMAKE_SOURCE_DIR}/cmake/gmxGenerateVersionInfo.cmake 
-            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/gmxlib 
-            DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/version.c.cmakein
-            COMMENT "Generating version information")
-    list(APPEND LIBGROMACS_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/version.c) # auto-generated
-    set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/version.c 
+                -D VERSION_C_CMAKEIN="${CMAKE_CURRENT_SOURCE_DIR}/utility/gitversion.c.cmakein"
+                -D VERSION_C_OUT=${GENERATED_VERSION_FILE}
+                -P ${CMAKE_SOURCE_DIR}/cmake/gmxGenerateVersionInfo.cmake
+            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+            DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/utility/gitversion.c.cmakein
+            COMMENT "Generating git version information")
+    set_source_files_properties(${GENERATED_VERSION_FILE}
                                 PROPERTIES GENERATED true)
-endif (USE_VERSION_H)
+    list(APPEND LIBGROMACS_SOURCES ${GENERATED_VERSION_FILE})
+endif()
 
 # apply gcc 4.4.x bug workaround
 if(GMX_USE_GCC44_BUG_WORKAROUND)
@@ -52,9 +56,9 @@ if(GMX_USE_GCC44_BUG_WORKAROUND)
 endif()
 
 add_library(libgromacs ${LIBGROMACS_SOURCES})
-if (USE_VERSION_H)
+if (GMX_GIT_VERSION_INFO)
     add_dependencies(libgromacs gmx_version)
-endif (USE_VERSION_H)
+endif ()
 
 target_link_libraries(libgromacs ${GMX_GPU_LIBRARIES}
                       ${GMX_EXTRA_LIBRARIES} ${FFT_LIBRARIES} ${XML_LIBRARIES}
index f898b3d4c592820a21c35728790947f0340082ff..7915fbe57f66604fa67b158e19e0e93560996aee 100644 (file)
@@ -621,9 +621,9 @@ void please_cite(FILE *fp,const char *key)
   fflush(fp);
 }
 
-#ifdef USE_VERSION_H
+#ifdef GMX_GIT_VERSION_INFO
 /* Version information generated at compile time. */
-#include "version.h"
+#include "gromacs/utility/gitversion.h"
 #else
 /* Fall back to statically defined version. */
 static const char _gmx_ver_string[]="VERSION " VERSION;
@@ -643,7 +643,7 @@ const char *GromacsVersion()
 void gmx_print_version_info(FILE *fp)
 {
     fprintf(fp, "Version:          %s\n", _gmx_ver_string);
-#ifdef USE_VERSION_H
+#ifdef GMX_GIT_VERSION_INFO
     fprintf(fp, "GIT SHA1 hash:    %s\n", _gmx_full_git_hash);
     /* Only print out the branch information if present.
      * The generating script checks whether the branch point actually
index 4bd114ba343e425b71817ef601e495348e0b7e89..97e9c2eb99a0f265070a2d6c9bf476e0ffb4361f 100644 (file)
@@ -1 +1,2 @@
 gmx_header_config_gen.h
+gitversion.c
similarity index 82%
rename from src/gromacs/version.c.cmakein
rename to src/gromacs/utility/gitversion.c.cmakein
index c59f8b051372ae8969e39108058c2b81605e675b..d33c61ac82aa3ac8727d620e967407168fa12f77 100644 (file)
@@ -1,4 +1,5 @@
-#include "gromacs/gmxlib/version.h"
+#include "gromacs/utility/gitversion.h"
+
 const char _gmx_ver_string[] = "VERSION @GMX_PROJECT_VERSION_STR@";
 const char _gmx_full_git_hash[] = "@GMX_GIT_HEAD_HASH@";
 const char _gmx_central_base_hash[] = "@GMX_GIT_REMOTE_HASH@";
similarity index 93%
rename from src/gromacs/gmxlib/version.h
rename to src/gromacs/utility/gitversion.h
index 937b3c5da0e658214afdc42020eda4a28aae30a5..c1323111e2f3ee89b7b14d0fab9bae46e452c65c 100644 (file)
  *
  * For more info, check our website at http://www.gromacs.org
  */
-#ifndef GMX_GMXLIB_VERSION_H
-#define GMX_GMXLIB_VERSION_H
+#ifndef GMX_UTILITY_GITVERSION_H
+#define GMX_UTILITY_GITVERSION_H
 
 extern const char _gmx_ver_string[];
 extern const char _gmx_full_git_hash[];
 extern const char _gmx_central_base_hash[];
 
-#endif /* GMX_GMXLIB_VERSION_H */
+#endif