[lld] [lld][ELF] Support LLVM repository and LLVM revision information (PR #97323)

Hongyu Chen via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 2 15:05:34 PDT 2024


https://github.com/yugier updated https://github.com/llvm/llvm-project/pull/97323

>From f50fd37f3d3e5a292f6f0068e31745a797894476 Mon Sep 17 00:00:00 2001
From: Hongyu Chen <hongyuchy at google.com>
Date: Mon, 1 Jul 2024 16:25:31 +0000
Subject: [PATCH 1/5] [lld][ELF] Support LLVM repository and LLVM revision
 information with --version

Added LLVM repository and LLVM revision information for ld.lld
--version. The getLLVMRepositoryPath and getLLVMRevision are under
lld/common so it should be availble for other drivers to use too. This
change is only to ld.lld.
---
 lld/Common/Version.cpp           | 18 +++++++++++++++++-
 lld/ELF/Driver.cpp               |  7 +++++--
 lld/include/lld/Common/Version.h |  2 ++
 lld/test/ELF/version.test        |  2 +-
 4 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/lld/Common/Version.cpp b/lld/Common/Version.cpp
index ec6eda6a6748f..da7485a4f28bb 100644
--- a/lld/Common/Version.cpp
+++ b/lld/Common/Version.cpp
@@ -11,8 +11,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "lld/Common/Version.h"
-
 #include "VCSVersion.inc"
+#include "llvm/Support/VCSRevision.h"
 
 // Returns a version string, e.g.:
 // LLD 14.0.0 (https://github.com/llvm/llvm-project.git
@@ -26,3 +26,19 @@ std::string lld::getLLDVersion() {
   return LLD_VENDOR_DISPLAY "LLD " LLD_VERSION_STRING;
 #undef LLD_VENDOR_DISPLAY
 }
+
+std::string lld::getLLVMRepositoryPath() {
+#ifdef LLVM_REPOSITORY
+  return LLVM_REPOSITORY;
+#else
+  return "";
+#endif
+}
+
+std::string lld::getLLVMRevision() {
+#ifdef LLVM_REVISION
+  return LLVM_REVISION;
+#else
+  return "";
+#endif
+}
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index ed773f5e69f77..c8b658c9d2cd2 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -164,6 +164,7 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
 
   return errorCount() == 0;
 }
+
 } // namespace elf
 } // namespace lld
 
@@ -630,8 +631,10 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   // lot of "configure" scripts out there that are generated by old version
   // of Libtool. We cannot convince every software developer to migrate to
   // the latest version and re-generate scripts. So we have this hack.
-  if (args.hasArg(OPT_v) || args.hasArg(OPT_version))
-    message(getLLDVersion() + " (compatible with GNU linkers)");
+  if (args.hasArg(OPT_v) || args.hasArg(OPT_version)) {
+    message(getLLDVersion() + " (compatible with GNU linkers) " +
+            getLLVMRepositoryPath() + " " + getLLVMRevision());
+  }
 
   if (const char *path = getReproduceOption(args)) {
     // Note that --reproduce is a debug option so you can ignore it
diff --git a/lld/include/lld/Common/Version.h b/lld/include/lld/Common/Version.h
index 9571aa2743e5b..ec1bd590647f4 100644
--- a/lld/include/lld/Common/Version.h
+++ b/lld/include/lld/Common/Version.h
@@ -19,6 +19,8 @@
 namespace lld {
 /// Retrieves a string representing the complete lld version.
 std::string getLLDVersion();
+std::string getLLVMRepositoryPath();
+std::string getLLVMRevision();
 }
 
 #endif // LLD_VERSION_H
diff --git a/lld/test/ELF/version.test b/lld/test/ELF/version.test
index cdeeb4795e185..ae4b145329767 100644
--- a/lld/test/ELF/version.test
+++ b/lld/test/ELF/version.test
@@ -7,4 +7,4 @@
 # RUN: ld.lld -V 2>&1 | FileCheck %s
 # RUN: not ld.lld -V %t/not-exist 2>&1 | FileCheck %s
 
-# CHECK: LLD {{.*}} (compatible with GNU linkers)
+# CHECK: LLD {{.*}} (compatible with GNU linkers) {{.*}} {{.*}}

>From 77f9b543114b8a699a0e5ec93524349132a0bf3c Mon Sep 17 00:00:00 2001
From: Hongyu Chen <hongyuchy at google.com>
Date: Mon, 1 Jul 2024 20:31:53 +0000
Subject: [PATCH 2/5] [lld] updated getLLDVersion

getLLDVersion didn't contain LLVM repository and LLVM revision
information which does not match its comment. This commit updates it so
it should return strings like `LLD 19.0.0
(https://github.com/yugier/llvm-project.git
4134b33c6a362cb462b335177d6d9e8235f04309) compatible with GNU linkers`
---
 lld/Common/Version.cpp           | 20 ++++----------------
 lld/ELF/Driver.cpp               |  7 ++-----
 lld/include/lld/Common/Version.h |  2 --
 lld/test/ELF/version.test        |  2 +-
 4 files changed, 7 insertions(+), 24 deletions(-)

diff --git a/lld/Common/Version.cpp b/lld/Common/Version.cpp
index da7485a4f28bb..06d995d0077db 100644
--- a/lld/Common/Version.cpp
+++ b/lld/Common/Version.cpp
@@ -22,23 +22,11 @@ std::string lld::getLLDVersion() {
 #define LLD_VENDOR_DISPLAY LLD_VENDOR " "
 #else
 #define LLD_VENDOR_DISPLAY
+#endif
+#if defined(LLVM_REPOSITORY) || defined(LLVM_REVISION)
+  return LLD_VENDOR_DISPLAY "LLD " LLD_VERSION_STRING " (" LLVM_REPOSITORY
+                            " " LLVM_REVISION ")";
 #endif
   return LLD_VENDOR_DISPLAY "LLD " LLD_VERSION_STRING;
 #undef LLD_VENDOR_DISPLAY
 }
-
-std::string lld::getLLVMRepositoryPath() {
-#ifdef LLVM_REPOSITORY
-  return LLVM_REPOSITORY;
-#else
-  return "";
-#endif
-}
-
-std::string lld::getLLVMRevision() {
-#ifdef LLVM_REVISION
-  return LLVM_REVISION;
-#else
-  return "";
-#endif
-}
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index c8b658c9d2cd2..cd61ae52afe70 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -164,7 +164,6 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
 
   return errorCount() == 0;
 }
-
 } // namespace elf
 } // namespace lld
 
@@ -631,10 +630,8 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   // lot of "configure" scripts out there that are generated by old version
   // of Libtool. We cannot convince every software developer to migrate to
   // the latest version and re-generate scripts. So we have this hack.
-  if (args.hasArg(OPT_v) || args.hasArg(OPT_version)) {
-    message(getLLDVersion() + " (compatible with GNU linkers) " +
-            getLLVMRepositoryPath() + " " + getLLVMRevision());
-  }
+  if (args.hasArg(OPT_v) || args.hasArg(OPT_version))
+    message(getLLDVersion() + " compatible with GNU linkers");
 
   if (const char *path = getReproduceOption(args)) {
     // Note that --reproduce is a debug option so you can ignore it
diff --git a/lld/include/lld/Common/Version.h b/lld/include/lld/Common/Version.h
index ec1bd590647f4..9571aa2743e5b 100644
--- a/lld/include/lld/Common/Version.h
+++ b/lld/include/lld/Common/Version.h
@@ -19,8 +19,6 @@
 namespace lld {
 /// Retrieves a string representing the complete lld version.
 std::string getLLDVersion();
-std::string getLLVMRepositoryPath();
-std::string getLLVMRevision();
 }
 
 #endif // LLD_VERSION_H
diff --git a/lld/test/ELF/version.test b/lld/test/ELF/version.test
index ae4b145329767..bf7c5ecf7d40c 100644
--- a/lld/test/ELF/version.test
+++ b/lld/test/ELF/version.test
@@ -7,4 +7,4 @@
 # RUN: ld.lld -V 2>&1 | FileCheck %s
 # RUN: not ld.lld -V %t/not-exist 2>&1 | FileCheck %s
 
-# CHECK: LLD {{.*}} (compatible with GNU linkers) {{.*}} {{.*}}
+# CHECK: LLD {{.*}} compatible with GNU linkers

>From 2056f24b3d5c1f862e6f593ab0dafe0cb2061ce4 Mon Sep 17 00:00:00 2001
From: Hongyu Chen <hongyuchy at google.com>
Date: Tue, 2 Jul 2024 18:09:28 +0000
Subject: [PATCH 3/5] [lld] updated getLLDVersion #else

---
 lld/Common/Version.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lld/Common/Version.cpp b/lld/Common/Version.cpp
index 06d995d0077db..13d077851e84c 100644
--- a/lld/Common/Version.cpp
+++ b/lld/Common/Version.cpp
@@ -26,7 +26,8 @@ std::string lld::getLLDVersion() {
 #if defined(LLVM_REPOSITORY) || defined(LLVM_REVISION)
   return LLD_VENDOR_DISPLAY "LLD " LLD_VERSION_STRING " (" LLVM_REPOSITORY
                             " " LLVM_REVISION ")";
-#endif
+#else 
   return LLD_VENDOR_DISPLAY "LLD " LLD_VERSION_STRING;
+#endif 
 #undef LLD_VENDOR_DISPLAY
 }

>From 79e83e8c86ccaf8d4c5431da56b94f38fc0dfbf1 Mon Sep 17 00:00:00 2001
From: Hongyu Chen <hongyuchy at google.com>
Date: Tue, 2 Jul 2024 18:20:27 +0000
Subject: [PATCH 4/5] [lld] getLLDVersion clang format fix

---
 lld/Common/Version.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lld/Common/Version.cpp b/lld/Common/Version.cpp
index 13d077851e84c..78f7c6b69b505 100644
--- a/lld/Common/Version.cpp
+++ b/lld/Common/Version.cpp
@@ -26,8 +26,8 @@ std::string lld::getLLDVersion() {
 #if defined(LLVM_REPOSITORY) || defined(LLVM_REVISION)
   return LLD_VENDOR_DISPLAY "LLD " LLD_VERSION_STRING " (" LLVM_REPOSITORY
                             " " LLVM_REVISION ")";
-#else 
+#else
   return LLD_VENDOR_DISPLAY "LLD " LLD_VERSION_STRING;
-#endif 
+#endif
 #undef LLD_VENDOR_DISPLAY
 }

>From dc1c7192f14341143fc85df1c3da6b8eec225fad Mon Sep 17 00:00:00 2001
From: Hongyu Chen <hongyuchy at google.com>
Date: Tue, 2 Jul 2024 22:02:41 +0000
Subject: [PATCH 5/5] [lld] updated ELF and MinGW version format

Used a comma to seperate version information and "compatible with GNU
linkers" in ELF and MinGW for being consistent with other LLVM binary
utilities.
---
 lld/ELF/Driver.cpp         | 2 +-
 lld/MinGW/Driver.cpp       | 2 +-
 lld/test/ELF/version.test  | 2 +-
 lld/test/MinGW/driver.test | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index cd61ae52afe70..afa394369f82a 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -631,7 +631,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   // of Libtool. We cannot convince every software developer to migrate to
   // the latest version and re-generate scripts. So we have this hack.
   if (args.hasArg(OPT_v) || args.hasArg(OPT_version))
-    message(getLLDVersion() + " compatible with GNU linkers");
+    message(getLLDVersion() + ", compatible with GNU linkers");
 
   if (const char *path = getReproduceOption(args)) {
     // Note that --reproduce is a debug option so you can ignore it
diff --git a/lld/MinGW/Driver.cpp b/lld/MinGW/Driver.cpp
index 35fd478a21905..1fd120ad3601d 100644
--- a/lld/MinGW/Driver.cpp
+++ b/lld/MinGW/Driver.cpp
@@ -199,7 +199,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
   // a GNU compatible linker. As long as an output for the -v option
   // contains "GNU" or "with BFD", they recognize us as GNU-compatible.
   if (args.hasArg(OPT_v) || args.hasArg(OPT_version))
-    message(getLLDVersion() + " (compatible with GNU linkers)");
+    message(getLLDVersion() + ", compatible with GNU linkers");
 
   // The behavior of -v or --version is a bit strange, but this is
   // needed for compatibility with GNU linkers.
diff --git a/lld/test/ELF/version.test b/lld/test/ELF/version.test
index bf7c5ecf7d40c..383c1ac976d96 100644
--- a/lld/test/ELF/version.test
+++ b/lld/test/ELF/version.test
@@ -7,4 +7,4 @@
 # RUN: ld.lld -V 2>&1 | FileCheck %s
 # RUN: not ld.lld -V %t/not-exist 2>&1 | FileCheck %s
 
-# CHECK: LLD {{.*}} compatible with GNU linkers
+# CHECK: LLD {{.*}}, compatible with GNU linkers
diff --git a/lld/test/MinGW/driver.test b/lld/test/MinGW/driver.test
index b723c0ad98749..44ec58818e0bf 100644
--- a/lld/test/MinGW/driver.test
+++ b/lld/test/MinGW/driver.test
@@ -268,7 +268,7 @@ APPCONTAINER: -appcontainer
 RUN: ld.lld -m i386pep --version 2>&1 | FileCheck -check-prefix=VERSION %s
 RUN: ld.lld -m i386pep -v 2>&1 | FileCheck -check-prefix=VERSION %s
 RUN: not ld.lld -m i386pep -v xyz 2>&1 | FileCheck -check-prefix=VERSION %s
-VERSION: LLD {{.*}} (compatible with GNU linkers)
+VERSION: LLD {{.*}}, compatible with GNU linkers
 
 RUN: ld.lld -m i386pep --help 2>&1 | FileCheck -check-prefix=HELP %s
 HELP: USAGE:



More information about the llvm-commits mailing list