This is an automated email from the ASF dual-hosted git repository.

tillt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 7b09ad960e722043bdc8eef0d46f0b10a845869a
Author: Till Toenshoff <[email protected]>
AuthorDate: Wed Mar 6 18:07:29 2019 +0100

    Updated build specific artefact generation.
    
    For autotools, we extracted additional build info like the git branch
    and sha during the automake phase handing them into libbuild via
    commandline defines.
    
    CMake builds however used a configuration file for this purpose.
    
    This patch updates both build systems to make use of
    build_git_config.hpp.in for build specific git information.
    
    Review: https://reviews.apache.org/r/70047/
---
 cmake/CompilationConfigure.cmake                   | 84 +++++++++++++++-------
 configure.ac                                       | 59 ++++++++++++++-
 src/Makefile.am                                    | 21 +-----
 src/common/build.cpp                               |  2 +
 src/common/build_config.hpp.in                     |  4 --
 .../{build_config.hpp.in => git_version.hpp.in}    | 22 +++---
 6 files changed, 128 insertions(+), 64 deletions(-)

diff --git a/cmake/CompilationConfigure.cmake b/cmake/CompilationConfigure.cmake
index c330324..d9c1e40 100644
--- a/cmake/CompilationConfigure.cmake
+++ b/cmake/CompilationConfigure.cmake
@@ -603,35 +603,65 @@ string(REPLACE "\"" "\\\"" BUILD_FLAGS 
"${BUILD_FLAGS_RAW}")
 
 set(BUILD_JAVA_JVM_LIBRARY ${JAVA_JVM_LIBRARY})
 
-# When building from source, from a git clone, emit some extra build info.
-if (IS_DIRECTORY "${CMAKE_SOURCE_DIR}/.git")
-  execute_process(
-    COMMAND git rev-parse HEAD
-    OUTPUT_VARIABLE BUILD_GIT_SHA
-    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
-    ERROR_QUIET
-    OUTPUT_STRIP_TRAILING_WHITESPACE)
-
-  execute_process(
-    COMMAND git symbolic-ref HEAD
-    OUTPUT_VARIABLE BUILD_GIT_BRANCH
-    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
-    ERROR_QUIET
-    OUTPUT_STRIP_TRAILING_WHITESPACE)
-
-  execute_process(
-    COMMAND git describe --exact --tags
-    OUTPUT_VARIABLE BUILD_GIT_TAG
-    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
-    ERROR_QUIET
-    OUTPUT_STRIP_TRAILING_WHITESPACE)
-endif ()
-
-# Emit the BUILD_DATE, BUILD_TIME, and BUILD_USER variables into a file.
-# When building from a git clone, the variables BUILD_GIT_SHA,
-# BUILD_GIT_BRANCH, and BUILD_GIT_TAG will also be emitted.
+# Emit the BUILD_DATE, BUILD_TIME and BUILD_USER definitions into a file.
 # This will be updated each time `cmake` is run.
 configure_file(
   "${CMAKE_SOURCE_DIR}/src/common/build_config.hpp.in"
   "${CMAKE_BINARY_DIR}/src/common/build_config.hpp"
   @ONLY)
+
+# Create 'src/common/git_version.hpp' only if we did not do so before.
+# This protects the results from getting overwritten by additional cmake
+# runs outside the reach of the git repository.
+if(NOT EXISTS "${CMAKE_BINARY_DIR}/src/common/git_version.hpp")
+  # When building from a git clone, the definitions BUILD_GIT_SHA,
+  # BUILD_GIT_BRANCH and BUILD_GIT_TAG will be emitted.
+  if (IS_DIRECTORY "${CMAKE_SOURCE_DIR}/.git")
+    # Optionally set BUILD_GIT_SHA.
+    set(DEFINE_BUILD_GIT_SHA "")
+
+    execute_process(
+      COMMAND git rev-parse HEAD
+      OUTPUT_VARIABLE BUILD_GIT_SHA
+      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+      ERROR_QUIET
+      OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+    if (NOT BUILD_GIT_SHA STREQUAL "")
+      set(DEFINE_BUILD_GIT_SHA "#define BUILD_GIT_SHA \"${BUILD_GIT_SHA}\"")
+    endif()
+
+    # Optionally set BUILD_GIT_BRANCH.
+    set(DEFINE_BUILD_GIT_BRANCH "")
+
+    execute_process(
+      COMMAND git symbolic-ref HEAD
+      OUTPUT_VARIABLE BUILD_GIT_BRANCH
+      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+      ERROR_QUIET
+      OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+    if (NOT BUILD_GIT_BRANCH STREQUAL "")
+      set(DEFINE_BUILD_GIT_BRANCH "#define BUILD_GIT_BRANCH 
\"${BUILD_GIT_BRANCH}\"")
+    endif()
+
+    # Optionally set BUILD_GIT_TAG.
+    set(DEFINE_BUILD_GIT_TAG "")
+
+    execute_process(
+      COMMAND git describe --exact --tags
+      OUTPUT_VARIABLE BUILD_GIT_TAG
+      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+      ERROR_QUIET
+      OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+    if (NOT BUILD_GIT_TAG STREQUAL "")
+      set(DEFINE_BUILD_GIT_TAG "#define BUILD_GIT_TAG \"${BUILD_GIT_TAG}\"")
+    endif()
+  endif ()
+
+  configure_file(
+    "${CMAKE_SOURCE_DIR}/src/common/git_version.hpp.in"
+    "${CMAKE_BINARY_DIR}/src/common/git_version.hpp"
+    @ONLY)
+endif()
diff --git a/configure.ac b/configure.ac
index ee29fc7..1f45d5c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -818,7 +818,6 @@ AM_CONDITIONAL([DISABLE_LIBTOOL_WRAPPERS],
 AM_CONDITIONAL([OS_LINUX], [test "x$OS_NAME" = "xlinux"])
 AM_CONDITIONAL([OS_FREEBSD], [test "x$OS_NAME" = "xfreebsd"])
 
-AM_CONDITIONAL([GIT_REPO], [test -d ${srcdir}"/.git"])
 AM_CONDITIONAL([HAS_GPERFTOOLS], [test "x$enable_perftools" = "xyes"])
 
 AM_CONDITIONAL([INSTALL_MODULE_DEPENDENCIES],
@@ -2816,6 +2815,64 @@ AC_SUBST(MESOS_MINOR_VERSION, 
m4_car(m4_shift(major_minor_patch)))
 AC_SUBST(MESOS_PATCH_VERSION, m4_car(m4_shift2(major_minor_patch)))
 
 
+###############################################################################
+# Configure libbuild definitions passed into 'src/common/git_version.hpp'.
+###############################################################################
+
+git_dir="${srcdir}/.git/"
+git_config=src/common/git_version.hpp
+
+# Create 'src/common/git_version.hpp' only if we did not do so before. This
+# protects the results from getting overwritten by additional configure runs
+# outside the reach of the git repository, as for example done by
+# 'support/packaging/centos/build_rpm.sh'.
+
+AC_MSG_CHECKING([${git_config} presence])
+
+AS_IF([test -f ${git_config}], [AC_MSG_RESULT([yes])], [
+  AC_MSG_RESULT([no])
+
+  AC_MSG_NOTICE([generating ${git_config}])
+
+  # When building from a git clone, the definitions BUILD_GIT_SHA,
+  # BUILD_GIT_BRANCH, and BUILD_GIT_TAG will be emitted.
+
+  AS_IF([test -d ${git_dir}], [
+    # Optionally set BUILD_GIT_SHA.
+    DEFINE_BUILD_GIT_SHA=""
+
+    git_sha="$(git --git-dir=\"${git_dir}\" rev-parse HEAD 2>/dev/null)"
+
+    AS_IF([test $? = 0], [
+      DEFINE_BUILD_GIT_SHA="#define BUILD_GIT_SHA \"${git_sha}\""
+    ])
+    AC_SUBST([DEFINE_BUILD_GIT_SHA])
+
+    # Optionally set BUILD_GIT_BRANCH.
+    DEFINE_BUILD_GIT_BRANCH=""
+
+    git_branch="$(git --git-dir=\"${git_dir}\" symbolic-ref HEAD 2>/dev/null)"
+
+    AS_IF([test $? = 0], [
+      DEFINE_BUILD_GIT_BRANCH="#define BUILD_GIT_BRANCH \"${git_branch}\""
+    ])
+    AC_SUBST([DEFINE_BUILD_GIT_BRANCH])
+
+    # Optionally set BUILD_GIT_TAG.
+    DEFINE_BUILD_GIT_TAG=""
+
+    git_tag="$(git --git-dir=\"${git_dir}\" describe --exact --tags 
2>/dev/null)"
+
+    AS_IF([test $? = 0], [
+      DEFINE_BUILD_GIT_TAG="#define BUILD_GIT_TAG \"${git_tag}\""
+    ])
+    AC_SUBST([DEFINE_BUILD_GIT_TAG])
+  ])
+
+  AC_CONFIG_FILES([${git_config}])
+])
+
+
 AC_OUTPUT
 
 AC_MSG_NOTICE([Build option summary:
diff --git a/src/Makefile.am b/src/Makefile.am
index cf61a33..d451d7c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1525,7 +1525,9 @@ libmesos_no_3rdparty_la_LIBADD = # Initialized to enable 
using +=.
 
 # Convenience library that *always* gets rebuilt to ensure accurate info.
 noinst_LTLIBRARIES += libbuild.la
-libbuild_la_SOURCES = common/build.cpp
+libbuild_la_SOURCES =                                                  \
+  common/build.cpp                                                     \
+  common/git_version.hpp
 libbuild_la_CPPFLAGS = $(AM_CPPFLAGS)
 libbuild_la_CPPFLAGS += $(MESOS_CPPFLAGS)
 libbuild_la_CPPFLAGS += -DBUILD_DATE="\"$$(date '+%Y-%m-%d %H:%M:%S')\""
@@ -1533,23 +1535,6 @@ libbuild_la_CPPFLAGS += -DBUILD_TIME="\"$$(date '+%s')\""
 libbuild_la_CPPFLAGS += -DBUILD_USER="\"$$USER\""
 libbuild_la_CPPFLAGS += -DBUILD_JAVA_JVM_LIBRARY=\"$(JAVA_JVM_LIBRARY)\"
 
-if GIT_REPO
-BUILD_GIT_SHA=$$(sh -c 'cd $(top_srcdir); \
-  SHA=`git log -n 1 --format=%H 2> /dev/null` && \
-  echo -DBUILD_GIT_SHA=\"$$SHA\"')
-libbuild_la_CPPFLAGS += $(BUILD_GIT_SHA)
-
-BUILD_GIT_BRANCH=$$(sh -c 'cd $(top_srcdir); \
-  BRANCH=`git symbolic-ref HEAD 2> /dev/null` && \
-  echo -DBUILD_GIT_BRANCH=\"$$BRANCH\"')
-libbuild_la_CPPFLAGS += $(BUILD_GIT_BRANCH)
-
-BUILD_GIT_TAG=$$(sh -c 'cd $(top_srcdir); \
-  TAG=`git describe --exact --tags 2> /dev/null` && \
-  echo -DBUILD_GIT_TAG=\"$$TAG\"')
-libbuild_la_CPPFLAGS += $(BUILD_GIT_TAG)
-endif
-
 # We need to escape the build flags properly.
 BUILD_FLAGS = $(echo $(MESOS_CPPFLAGS) $(CPPFLAGS) | $(SED) 's/\"/\\\"/g')     
\
               $(echo $(AM_CFLAGS) $(CFLAGS) | $(SED) 's/\"/\\\"/g')            
\
diff --git a/src/common/build.cpp b/src/common/build.cpp
index f5271d8..f919c79 100644
--- a/src/common/build.cpp
+++ b/src/common/build.cpp
@@ -32,6 +32,8 @@
 #include "common/build_config.hpp"
 #endif // USE_CMAKE_BUILD_CONFIG
 
+#include "common/git_version.hpp"
+
 using std::string;
 
 namespace mesos {
diff --git a/src/common/build_config.hpp.in b/src/common/build_config.hpp.in
index 4cce240..db08117 100644
--- a/src/common/build_config.hpp.in
+++ b/src/common/build_config.hpp.in
@@ -26,8 +26,4 @@
 #cmakedefine BUILD_FLAGS "@BUILD_FLAGS@"
 #cmakedefine BUILD_JAVA_JVM_LIBRARY "@BUILD_JAVA_JVM_LIBRARY@"
 
-#cmakedefine BUILD_GIT_SHA "@BUILD_GIT_SHA@"
-#cmakedefine BUILD_GIT_BRANCH "@BUILD_GIT_BRANCH@"
-#cmakedefine BUILD_GIT_TAG "@BUILD_GIT_TAG@"
-
 #endif // __COMMON_BUILD_CONFIG_HPP__
diff --git a/src/common/build_config.hpp.in b/src/common/git_version.hpp.in
similarity index 53%
copy from src/common/build_config.hpp.in
copy to src/common/git_version.hpp.in
index 4cce240..bfcf4a7 100644
--- a/src/common/build_config.hpp.in
+++ b/src/common/git_version.hpp.in
@@ -14,20 +14,14 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#ifndef __COMMON_BUILD_CONFIG_HPP__
-#define __COMMON_BUILD_CONFIG_HPP__
+#ifndef __COMMON_GIT_VERSION_HPP__
+#define __COMMON_GIT_VERSION_HPP__
 
-// NOTE: The quotes in these definitions are necessary. Without them, the
-// preprocessor will interpret the symbols as (e.g.) int literals and unquoted
-// identifiers, rather than the string values our code expects.
-#cmakedefine BUILD_DATE "@BUILD_DATE@"
-#cmakedefine BUILD_TIME "@BUILD_TIME@"
-#cmakedefine BUILD_USER "@BUILD_USER@"
-#cmakedefine BUILD_FLAGS "@BUILD_FLAGS@"
-#cmakedefine BUILD_JAVA_JVM_LIBRARY "@BUILD_JAVA_JVM_LIBRARY@"
+// This file contains artifacts gathered by an initial configuration
+// phase but never overwritten if populated already.
 
-#cmakedefine BUILD_GIT_SHA "@BUILD_GIT_SHA@"
-#cmakedefine BUILD_GIT_BRANCH "@BUILD_GIT_BRANCH@"
-#cmakedefine BUILD_GIT_TAG "@BUILD_GIT_TAG@"
+@DEFINE_BUILD_GIT_SHA@
+@DEFINE_BUILD_GIT_BRANCH@
+@DEFINE_BUILD_GIT_TAG@
 
-#endif // __COMMON_BUILD_CONFIG_HPP__
+#endif // __COMMON_GIT_VERSION_HPP__

Reply via email to