From 0e1e36098005b34ef7df3ca16310ae396483464a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin@martin.st>
Date: Tue, 11 May 2021 09:19:52 +0300
Subject: [PATCH] [llvm-nm] Support the -V option, print that the tool is
 compatible with GNU nm

This unlocks some codepaths in libtool.
---
 llvm/test/tools/llvm-nm/libtool-version.test |  5 +++++
 llvm/tools/llvm-nm/llvm-nm.cpp               | 13 +++++++++++++
 2 files changed, 18 insertions(+)
 create mode 100644 llvm/test/tools/llvm-nm/libtool-version.test

diff --git llvm/test/tools/llvm-nm/libtool-version.test llvm/test/tools/llvm-nm/libtool-version.test
new file mode 100644
index 0000000000000..8d32634fed615
--- /dev/null
+++ llvm/test/tools/llvm-nm/libtool-version.test
@@ -0,0 +1,5 @@
+RUN: llvm-nm -V | FileCheck %s
+RUN: llvm-nm --version | FileCheck %s
+Check that the output of llvm-nm -V (and --version) contains the text
+"GNU" somewhere, to let libtool know that it is compatible with GNU nm.
+CHECK: GNU
diff --git llvm/tools/llvm-nm/llvm-nm.cpp llvm/tools/llvm-nm/llvm-nm.cpp
index 914b0a14bfedc..93ed3273c4a4e 100644
--- llvm/tools/llvm-nm/llvm-nm.cpp
+++ llvm/tools/llvm-nm/llvm-nm.cpp
@@ -224,6 +224,8 @@ cl::opt<bool> AddInlinedInfo("add-inlinedinfo",
                                       "TBD(Mach-O) only"),
                              cl::cat(NMCat));
 
+cl::opt<bool> Version("V", cl::desc("Print version info"), cl::cat(NMCat));
+
 cl::extrahelp HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
 
 bool PrintAddress = true;
@@ -2230,11 +2232,22 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
   }
 }
 
+static void printExtraVersionInfo(raw_ostream &outs) {
+  outs << "llvm-nm, compatible with GNU nm\n";
+}
+
 int main(int argc, char **argv) {
   InitLLVM X(argc, argv);
   cl::HideUnrelatedOptions(NMCat);
+  cl::AddExtraVersionPrinter(printExtraVersionInfo);
   cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
 
+  if (Version) {
+    printExtraVersionInfo(outs());
+    cl::PrintVersionMessage();
+    return 0;
+  }
+
   // llvm-nm only reads binary files.
   if (error(sys::ChangeStdinToBinary()))
     return 1;
