Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 3, 2024

Conversation

hongyu-dev
Copy link
Contributor

@hongyu-dev hongyu-dev commented Jul 1, 2024

Added LLVM repository and LLVM revision information for lld::getLLDVersion()

Before this change:

hongyuchy@hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0 (compatible with GNU linkers) 

After this change with LLVM_APPEND_VC_REV=on

hongyuchy@hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0 (https://github.com/yugier/llvm-project.git 4134b33c6a362cb462b335177d6d9e8235f04309), compatible with GNU linkers

with LLVM_APPEND_VC_REV=off

hongyuchy@hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0, compatible with GNU linkers

--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.
@llvmbot
Copy link
Member

llvmbot commented Jul 1, 2024

@llvm/pr-subscribers-lld
@llvm/pr-subscribers-lld-wasm
@llvm/pr-subscribers-lld-macho

@llvm/pr-subscribers-lld-coff

Author: Hongyu Chen (yugier)

Changes

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.

Before this change:

hongyuchy@<!-- -->hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0 (compatible with GNU linkers) 

After this change:

hongyuchy@<!-- -->hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0 (compatible with GNU linkers) https://github.com/yugier/llvm-project.git 4134b33c6a362cb462b335177d6d9e8235f04309

Full diff: https://github.com/llvm/llvm-project/pull/97323.diff

4 Files Affected:

  • (modified) lld/Common/Version.cpp (+17-1)
  • (modified) lld/ELF/Driver.cpp (+5-2)
  • (modified) lld/include/lld/Common/Version.h (+2)
  • (modified) lld/test/ELF/version.test (+1-1)
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) {{.*}} {{.*}}

@@ -164,6 +164,7 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,

return errorCount() == 0;
}

Copy link
Contributor

@Prabhuk Prabhuk Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove empty line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just did!

Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also update wasm-ld?

@tschuett tschuett requested a review from MaskRay July 1, 2024 17:25
@petrhosek
Copy link
Member

I think this change should instead be made to https://github.com/llvm/llvm-project/blob/f9efc295089217425d8220af892dcc5ede9eb9f7/lld/Common/Version.cpp so it applies to all LLD drivers uniformly.

Specifically, the comment above getLLDVersion says:

// Returns a version string, e.g.:
// LLD 14.0.0 (https://github.com/llvm/llvm-project.git
// 2d9759c7902c5cbc9a7e3ab623321d5578d51687)

That's not the case though since the string doesn't include the repository or the revision information, so I'd change the implementation to match the comment.

I'd also consider removing the parenthesis around the compatible with GNU linkers part in the ELF driver so we end up with:

LLD 19.0.0 (https://github.com/yugier/llvm-project.git 4134b33c6a362cb462b335177d6d9e8235f04309) compatible with GNU linkers

I think this is a bit cleaner than:

LLD 19.0.0 (https://github.com/yugier/llvm-project.git 4134b33c6a362cb462b335177d6d9e8235f04309) (compatible with GNU linkers)

@compnerd
Copy link
Member

compnerd commented Jul 1, 2024

I think that we should check autotools before changing the "(compatible with GNU linkers)" as it may have the parenthesis hardcoded.

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
4134b33) compatible with GNU linkers`
@brad0
Copy link
Contributor

brad0 commented Jul 2, 2024

I think that we should check autotools before changing the "(compatible with GNU linkers)" as it may have the parenthesis hardcoded.

The rest of what it says does not matter. It is just checking for the string GNU.

      case `"$lt_cv_path_LD" -v 2>&1 </dev/null` in
      *GNU* | *'with BFD'*)

Comment on lines 26 to 30
#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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#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;
#if defined(LLVM_REPOSITORY) || defined(LLVM_REVISION)
return LLD_VENDOR_DISPLAY "LLD " LLD_VERSION_STRING " (" LLVM_REPOSITORY
" " LLVM_REVISION ")";
#else
return LLD_VENDOR_DISPLAY "LLD " LLD_VERSION_STRING;
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

Copy link
Member

@petrhosek petrhosek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wait for @MaskRay to take a look as well.

Copy link

github-actions bot commented Jul 2, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@MaskRay
Copy link
Member

MaskRay commented Jul 2, 2024

The description (which will be used as the default commit message when "Squash and merge" is clicked) is probably about
LLVM_APPEND_VC_REV=on.

I'd also consider removing the parenthesis around the compatible with GNU linkers part in the ELF driver so we end up with:

In the LLVM_APPEND_VC_REV=off configuration,

% fld.lld --version
LLD 19.0.0 compatible with GNU linkers

Without parentheses, "compatible ..." might be parsed as part of the version string.

Should parentheses be added back? We could also use a leading comma or something else, but lld/MinGW/Driver.cpp should be updated as well for consistency.

@petrhosek
Copy link
Member

The description (which will be used as the default commit message when "Squash and merge" is clicked) is probably about LLVM_APPEND_VC_REV=on.

I'd also consider removing the parenthesis around the compatible with GNU linkers part in the ELF driver so we end up with:

In the LLVM_APPEND_VC_REV=off configuration,

% fld.lld --version
LLD 19.0.0 compatible with GNU linkers

Without parentheses, "compatible ..." might be parsed as part of the version string.

Should parentheses be added back? We could also use a leading comma or something else, but lld/MinGW/Driver.cpp should be updated as well for consistency.

Looks like mold uses the following format:

$ ld.lld --version
LLD 19.0.0 (https://github.com/yugier/llvm-project.git 4134b33c6a362cb462b335177d6d9e8235f04309; compatible with GNU ld)

Should we use the same format for consistency?

@MaskRay
Copy link
Member

MaskRay commented Jul 2, 2024

We could use a comma to be consistent with other LLVM binary utilities: llvm-objcopy, compatible with GNU objcopy/
llvm-nm, compatible with GNU nm.

@hongyu-dev
Copy link
Contributor Author

We could use a comma to be consistent with other LLVM binary utilities: llvm-objcopy, compatible with GNU objcopy/ llvm-nm, compatible with GNU nm.

Do you prefer we have

$ ld.lld --version
LLD 19.0.0 (https://github.com/yugier/llvm-project.git 4134b33c6a362cb462b335177d6d9e8235f04309), compatible with GNU ld

or

$ ld.lld --version
LLD 19.0.0, compatible with GNU ld (https://github.com/yugier/llvm-project.git 4134b33c6a362cb462b335177d6d9e8235f04309)

Thank you!

@MaskRay
Copy link
Member

MaskRay commented Jul 2, 2024

LLD 19.0.0 (https://github.com/yugier/llvm-project.git 4134b33c6a362cb462b335177d6d9e8235f04309), compatible with GNU ld to keep the version parts together.

Used a comma to seperate version information and "compatible with GNU
linkers" in ELF and MinGW for being consistent with other LLVM binary
utilities.
@hongyu-dev
Copy link
Contributor Author

LLD 19.0.0 (https://github.com/yugier/llvm-project.git 4134b33c6a362cb462b335177d6d9e8235f04309), compatible with GNU ld to keep the version parts together.

No problem! Updated! Please let me know if there is anything else that should be changed for this.

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The After this change: part in the description should be adjusted.
The output is probably for LLVM_APPEND_VC_REV=on

@hongyu-dev
Copy link
Contributor Author

LGTM. The After this change: part in the description should be adjusted. The output is probably for LLVM_APPEND_VC_REV=on

I just did! Thank you for your fast response and help! :)

@hongyu-dev hongyu-dev merged commit 0f9fbbb into llvm:main Jul 3, 2024
7 checks passed
@hongyu-dev hongyu-dev deleted the ld.lld-llvm-repo-revision-info branch July 3, 2024 03:33
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
…m#97323)

Added LLVM repository and LLVM revision information for
`lld::getLLDVersion()`

Before this change:
```
hongyuchy@hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0 (compatible with GNU linkers) 
```

After this change with LLVM_APPEND_VC_REV=on
```
hongyuchy@hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0 (https://github.com/yugier/llvm-project.git 4134b33), compatible with GNU linkers
```

with LLVM_APPEND_VC_REV=off
```
hongyuchy@hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0, compatible with GNU linkers
```
@mstorsjo
Copy link
Member

mstorsjo commented Jul 4, 2024

Sorry, but this change has broken how Meson detects and disambiguates between the ld.lld and lld-link interfaces for Windows targets. See https://github.com/mesonbuild/meson/blob/1.4.1/mesonbuild/linkers/detect.py#L67 - Meson looks for literally the string (compatible with GNU linkers) including the parentheses.

I would request reinstating that form.

We should of course request Meson to match a less strict pattern here as well, but even then, we should work with the variety of existing Meson versions out there as well, at least for a couple years after that has been changed.

@mstorsjo
Copy link
Member

mstorsjo commented Jul 4, 2024

As this check in Meson only seems to be used to differentiate between the ld.lld and lld-link interface for the Windows targets, it should be enough to only adjust the printout in lld/MinGW/Driver.cpp, e.g. with this diff:

diff --git a/lld/MinGW/Driver.cpp b/lld/MinGW/Driver.cpp
index 1fd120ad3601..35fd478a2190 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.

mstorsjo added a commit to mstorsjo/meson that referenced this pull request Jul 4, 2024
Don't require the string to be enclosed in parentheses.
llvm/llvm-project#97323 changed the LLD
version printout to no longer be enclosed in parentheses, which
made Meson fail to detect the linker style used here.

The LLD change is being reverted in
llvm/llvm-project#97698 in order to fix
building with existing Meson versions, but for the future, loosen
the check slightly, to not require the parentheses.
@mstorsjo
Copy link
Member

mstorsjo commented Jul 4, 2024

I posted such a change in #97698, and made a PR to Meson to loosen the check now in mesonbuild/meson#13383.

eli-schwartz pushed a commit to mesonbuild/meson that referenced this pull request Jul 4, 2024
Don't require the string to be enclosed in parentheses.
llvm/llvm-project#97323 changed the LLD
version printout to no longer be enclosed in parentheses, which
made Meson fail to detect the linker style used here.

The LLD change is being reverted in
llvm/llvm-project#97698 in order to fix
building with existing Meson versions, but for the future, loosen
the check slightly, to not require the parentheses.
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
…m#97323)

Added LLVM repository and LLVM revision information for
`lld::getLLDVersion()`

Before this change:
```
hongyuchy@hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0 (compatible with GNU linkers) 
```

After this change with LLVM_APPEND_VC_REV=on
```
hongyuchy@hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0 (https://github.com/yugier/llvm-project.git 4134b33), compatible with GNU linkers
```

with LLVM_APPEND_VC_REV=off
```
hongyuchy@hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0, compatible with GNU linkers
```
@yamader
Copy link

yamader commented Jul 7, 2024

This change breaks scripts that want to detect the LLD version when LLLVM_APPEND_VC_REV=off.
For example, this change broke a very basic Linux config script.
It expects the second block of --version to contain only a period-separated numeric version string.
https://github.com/torvalds/linux/blob/e24b3ff/scripts/ld-version.sh#L50

I don't know if the Linux ones, etc. should be changed, but I would comment in passing that it can cause problems.

MaskRay added a commit that referenced this pull request Jul 7, 2024
With `LLVM_APPEND_VC_REV=off`, the new version message after #97323
looks like:
```
% /tmp/out/custom2/bin/ld.lld --version
LLD 19.0.0, compatible with GNU linkers
```

A trailing comma after the version string might cause issues with
version detection tools that don't strip it, as seen in the Linux
kernel's scripts/ld-version.sh script.

Pull Request: #97942
@@ -23,6 +23,11 @@ std::string lld::getLLDVersion() {
#else
#define LLD_VENDOR_DISPLAY
#endif
#if defined(LLVM_REPOSITORY) || defined(LLVM_REVISION)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant && here.

eli-schwartz pushed a commit to mesonbuild/meson that referenced this pull request Jul 10, 2024
Don't require the string to be enclosed in parentheses.
llvm/llvm-project#97323 changed the LLD
version printout to no longer be enclosed in parentheses, which
made Meson fail to detect the linker style used here.

The LLD change is being reverted in
llvm/llvm-project#97698 in order to fix
building with existing Meson versions, but for the future, loosen
the check slightly, to not require the parentheses.

(cherry picked from commit 1ca002a)
Volker-Weissmann pushed a commit to Volker-Weissmann/meson that referenced this pull request May 1, 2025
Don't require the string to be enclosed in parentheses.
llvm/llvm-project#97323 changed the LLD
version printout to no longer be enclosed in parentheses, which
made Meson fail to detect the linker style used here.

The LLD change is being reverted in
llvm/llvm-project#97698 in order to fix
building with existing Meson versions, but for the future, loosen
the check slightly, to not require the parentheses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.