[llvm] r300160 - Support: Add a VCSRevision.h header file.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 12 18:26:12 PDT 2017


Author: pcc
Date: Wed Apr 12 20:26:12 2017
New Revision: 300160

URL: http://llvm.org/viewvc/llvm-project?rev=300160&view=rev
Log:
Support: Add a VCSRevision.h header file.

This is a magic header file supported by the build system that provides a
single definition, LLVM_REVISION, containing an LLVM revision identifier,
if available. This functionality previously lived in the LTO library, but
I am moving it out to lib/Support because I want to also start using it in
lib/Object to create the IR symbol table.

This change also fixes a bug where LLVM_REVISION was never actually being
used in lib/LTO because the macro HAS_LLVM_REVISION was never defined (it
was misspelled as HAVE_SVN_VERSION_INC in lib/LTO/CMakeLists.txt, and was
only being defined in a non-existent file Version.cpp).

I also changed the code to use "git rev-parse --git-dir" to locate the .git
directory, instead of looking for it in the LLVM source root directory,
which makes this compatible with monorepos as well as git worktrees.

Differential Revision: https://reviews.llvm.org/D31985

Added:
    llvm/trunk/include/llvm/Support/CMakeLists.txt
Modified:
    llvm/trunk/cmake/modules/VersionFromVCS.cmake
    llvm/trunk/include/llvm/CMakeLists.txt
    llvm/trunk/lib/LTO/CMakeLists.txt
    llvm/trunk/lib/LTO/LTO.cpp
    llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp

Modified: llvm/trunk/cmake/modules/VersionFromVCS.cmake
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/cmake/modules/VersionFromVCS.cmake?rev=300160&r1=300159&r2=300160&view=diff
==============================================================================
--- llvm/trunk/cmake/modules/VersionFromVCS.cmake (original)
+++ llvm/trunk/cmake/modules/VersionFromVCS.cmake Wed Apr 12 20:26:12 2017
@@ -25,57 +25,65 @@ function(add_version_info_from_vcs VERS)
         set(LLVM_REPOSITORY ${Project_WC_URL} PARENT_SCOPE)
       endif()
     endif()
-  elseif( EXISTS ${SOURCE_DIR}/.git )
-    set(result "${result}git")
-    # Try to get a ref-id
+  else()
     find_program(git_executable NAMES git git.exe git.cmd)
 
     if( git_executable )
-      if( EXISTS ${SOURCE_DIR}/.git/svn )
-        # Get the repository URL
-        execute_process(COMMAND
-          ${git_executable} svn info
-          WORKING_DIRECTORY ${SOURCE_DIR}
-          TIMEOUT 5
-          RESULT_VARIABLE git_result
-          OUTPUT_VARIABLE git_output)
-        if( git_result EQUAL 0 )
-          string(REGEX MATCH "URL: ([^ \n]*)" svn_url ${git_output})
-          if(svn_url)
-            set(LLVM_REPOSITORY ${CMAKE_MATCH_1} PARENT_SCOPE)
+      # Run from a subdirectory to force git to print an absoute path.
+      execute_process(COMMAND ${git_executable} rev-parse --git-dir
+        WORKING_DIRECTORY ${SOURCE_DIR}/cmake
+        RESULT_VARIABLE git_result
+        OUTPUT_VARIABLE git_dir)
+      if(git_result EQUAL 0)
+        # Try to get a ref-id
+        string(STRIP "${git_dir}" git_dir)
+        set(result "${result}git")
+        if( EXISTS ${git_dir}/svn )
+          # Get the repository URL
+          execute_process(COMMAND
+            ${git_executable} svn info
+            WORKING_DIRECTORY ${SOURCE_DIR}
+            TIMEOUT 5
+            RESULT_VARIABLE git_result
+            OUTPUT_VARIABLE git_output)
+          if( git_result EQUAL 0 )
+            string(REGEX MATCH "URL: ([^ \n]*)" svn_url ${git_output})
+            if(svn_url)
+              set(LLVM_REPOSITORY ${CMAKE_MATCH_1} PARENT_SCOPE)
+            endif()
+          endif()
+
+          # Get the svn revision number for this git commit if one exists.
+          execute_process(COMMAND ${git_executable} svn find-rev HEAD
+            WORKING_DIRECTORY ${SOURCE_DIR}
+            TIMEOUT 5
+            RESULT_VARIABLE git_result
+            OUTPUT_VARIABLE git_head_svn_rev_number
+            OUTPUT_STRIP_TRAILING_WHITESPACE)
+          if( git_result EQUAL 0 AND git_output)
+            set(SVN_REVISION ${git_head_svn_rev_number} PARENT_SCOPE)
+            set(git_svn_rev "-svn-${git_head_svn_rev_number}")
+          else()
+            set(git_svn_rev "")
           endif()
         endif()
 
-        # Get the svn revision number for this git commit if one exists.
-        execute_process(COMMAND ${git_executable} svn find-rev HEAD
+        # Get the git ref id
+        execute_process(COMMAND
+          ${git_executable} rev-parse --short HEAD
           WORKING_DIRECTORY ${SOURCE_DIR}
           TIMEOUT 5
           RESULT_VARIABLE git_result
-          OUTPUT_VARIABLE git_head_svn_rev_number
+          OUTPUT_VARIABLE git_ref_id
           OUTPUT_STRIP_TRAILING_WHITESPACE)
-        if( git_result EQUAL 0 AND git_output)
-          set(SVN_REVISION ${git_head_svn_rev_number} PARENT_SCOPE)
-          set(git_svn_rev "-svn-${git_head_svn_rev_number}")
+
+        if( git_result EQUAL 0 )
+          set(GIT_COMMIT ${git_ref_id} PARENT_SCOPE)
+          set(result "${result}${git_svn_rev}-${git_ref_id}")
         else()
-          set(git_svn_rev "")
+          set(result "${result}${git_svn_rev}")
         endif()
       endif()
-
-      # Get the git ref id
-      execute_process(COMMAND
-        ${git_executable} rev-parse --short HEAD
-        WORKING_DIRECTORY ${SOURCE_DIR}
-        TIMEOUT 5
-        RESULT_VARIABLE git_result
-        OUTPUT_VARIABLE git_ref_id
-        OUTPUT_STRIP_TRAILING_WHITESPACE)
-
-      if( git_result EQUAL 0 )
-        set(GIT_COMMIT ${git_ref_id} PARENT_SCOPE)
-        set(result "${result}${git_svn_rev}-${git_ref_id}")
-      else()
-        set(result "${result}${git_svn_rev}")
-      endif()
     endif()
   endif()
   set(${VERS} ${result} PARENT_SCOPE)

Modified: llvm/trunk/include/llvm/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CMakeLists.txt?rev=300160&r1=300159&r2=300160&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CMakeLists.txt (original)
+++ llvm/trunk/include/llvm/CMakeLists.txt Wed Apr 12 20:26:12 2017
@@ -1,4 +1,5 @@
 add_subdirectory(IR)
+add_subdirectory(Support)
 
 # If we're doing an out-of-tree build, copy a module map for generated
 # header files into the build area.

Added: llvm/trunk/include/llvm/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/CMakeLists.txt?rev=300160&view=auto
==============================================================================
--- llvm/trunk/include/llvm/Support/CMakeLists.txt (added)
+++ llvm/trunk/include/llvm/Support/CMakeLists.txt Wed Apr 12 20:26:12 2017
@@ -0,0 +1,54 @@
+# Figure out if we can track VC revisions.
+function(find_first_existing_file out_var)
+  foreach(file ${ARGN})
+    if(EXISTS "${file}")
+      set(${out_var} "${file}" PARENT_SCOPE)
+      return()
+    endif()
+  endforeach()
+endfunction()
+
+macro(find_first_existing_vc_file out_var path)
+  find_program(git_executable NAMES git git.exe git.cmd)
+  # Run from a subdirectory to force git to print an absolute path.
+  execute_process(COMMAND ${git_executable} rev-parse --git-dir
+    WORKING_DIRECTORY ${path}/cmake
+    RESULT_VARIABLE git_result
+    OUTPUT_VARIABLE git_dir)
+  if(git_result EQUAL 0)
+    string(STRIP "${git_dir}" git_dir)
+    set(${out_var} "${git_dir}/logs/HEAD")
+  else()
+    find_first_existing_file(${out_var}
+      "${path}/.svn/wc.db"   # SVN 1.7
+      "${path}/.svn/entries" # SVN 1.6
+    )
+  endif()
+endmacro()
+
+find_first_existing_vc_file(llvm_vc "${LLVM_MAIN_SRC_DIR}")
+
+# The VC revision include that we want to generate.
+set(version_inc "${CMAKE_CURRENT_BINARY_DIR}/VCSRevision.h")
+
+set(get_svn_script "${LLVM_CMAKE_PATH}/GenerateVersionFromCVS.cmake")
+
+if(DEFINED llvm_vc)
+  # Create custom target to generate the VC revision include.
+  add_custom_command(OUTPUT "${version_inc}"
+    DEPENDS "${llvm_vc}" "${get_svn_script}"
+    COMMAND
+    ${CMAKE_COMMAND} "-DSOURCE_DIR=${LLVM_MAIN_SRC_DIR}"
+                     "-DNAME=LLVM_REVISION"
+                     "-DHEADER_FILE=${version_inc}"
+                     -P "${get_svn_script}")
+
+  # Mark the generated header as being generated.
+  set_source_files_properties("${version_inc}"
+    PROPERTIES GENERATED TRUE
+               HEADER_FILE_ONLY TRUE)
+else()
+  file(WRITE "${version_inc}" "")
+endif()
+
+add_custom_target(llvm_vcsrevision_h DEPENDS "${version_inc}")

Modified: llvm/trunk/lib/LTO/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/CMakeLists.txt?rev=300160&r1=300159&r2=300160&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/CMakeLists.txt (original)
+++ llvm/trunk/lib/LTO/CMakeLists.txt Wed Apr 12 20:26:12 2017
@@ -1,52 +1,3 @@
-# Figure out if we can track VC revisions.
-function(find_first_existing_file out_var)
-  foreach(file ${ARGN})
-    if(EXISTS "${file}")
-      set(${out_var} "${file}" PARENT_SCOPE)
-      return()
-    endif()
-  endforeach()
-endfunction()
-
-macro(find_first_existing_vc_file out_var path)
-  find_first_existing_file(${out_var}
-    "${path}/.git/logs/HEAD" # Git
-    "${path}/.svn/wc.db"     # SVN 1.7
-    "${path}/.svn/entries"   # SVN 1.6
-    )
-endmacro()
-
-find_first_existing_vc_file(llvm_vc "${LLVM_MAIN_SRC_DIR}")
-
-# The VC revision include that we want to generate.
-set(version_inc "${CMAKE_CURRENT_BINARY_DIR}/LLVMLTORevision.h")
-
-set(get_svn_script "${LLVM_CMAKE_PATH}/GenerateVersionFromCVS.cmake")
-
-if(DEFINED llvm_vc)
-  # Create custom target to generate the VC revision include.
-  add_custom_command(OUTPUT "${version_inc}"
-    DEPENDS "${llvm_vc}" "${get_svn_script}"
-    COMMAND
-    ${CMAKE_COMMAND} "-DSOURCE_DIR=${LLVM_MAIN_SRC_DIR}"
-                     "-DNAME=LLVM_REVISION"
-                     "-DHEADER_FILE=${version_inc}"
-                     -P "${get_svn_script}")
-
-  # Mark the generated header as being generated.
-  set_source_files_properties("${version_inc}"
-    PROPERTIES GENERATED TRUE
-               HEADER_FILE_ONLY TRUE)
-
-  # Tell Version.cpp that it needs to build with -DHAVE_SVN_VERSION_INC.
-  set_source_files_properties(Version.cpp
-    PROPERTIES COMPILE_DEFINITIONS "HAVE_SVN_VERSION_INC")
-else()
-  # Not producing a VC revision include.
-  set(version_inc)
-endif()
-
-
 add_llvm_library(LLVMLTO
   Caching.cpp
   LTO.cpp
@@ -55,11 +6,11 @@ add_llvm_library(LLVMLTO
   LTOCodeGenerator.cpp
   UpdateCompilerUsed.cpp
   ThinLTOCodeGenerator.cpp
-  ${version_inc}
 
   ADDITIONAL_HEADER_DIRS
   ${LLVM_MAIN_INCLUDE_DIR}/llvm/LTO
 
   DEPENDS
   intrinsics_gen
+  llvm_vcsrevision_h
 )

Modified: llvm/trunk/lib/LTO/LTO.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTO.cpp?rev=300160&r1=300159&r2=300160&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/LTO.cpp (original)
+++ llvm/trunk/lib/LTO/LTO.cpp Wed Apr 12 20:26:12 2017
@@ -35,6 +35,7 @@
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/ThreadPool.h"
 #include "llvm/Support/Threading.h"
+#include "llvm/Support/VCSRevision.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
@@ -74,7 +75,7 @@ static void computeCacheKey(
 
   // Start with the compiler revision
   Hasher.update(LLVM_VERSION_STRING);
-#ifdef HAVE_LLVM_REVISION
+#ifdef LLVM_REVISION
   Hasher.update(LLVM_REVISION);
 #endif
 

Modified: llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp?rev=300160&r1=300159&r2=300160&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp (original)
+++ llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp Wed Apr 12 20:26:12 2017
@@ -14,10 +14,6 @@
 
 #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
 
-#ifdef HAVE_LLVM_REVISION
-#include "LLVMLTORevision.h"
-#endif
-
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
@@ -47,6 +43,7 @@
 #include "llvm/Support/ThreadPool.h"
 #include "llvm/Support/Threading.h"
 #include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/VCSRevision.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Transforms/IPO.h"
 #include "llvm/Transforms/IPO/FunctionImport.h"
@@ -304,7 +301,7 @@ public:
 
     // Start with the compiler revision
     Hasher.update(LLVM_VERSION_STRING);
-#ifdef HAVE_LLVM_REVISION
+#ifdef LLVM_REVISION
     Hasher.update(LLVM_REVISION);
 #endif
 




More information about the llvm-commits mailing list