[llvm] 4d53b99 - [llvm-ar] Improve tool selection heuristic

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 10 17:34:25 PST 2019


Author: Fangrui Song
Date: 2019-12-10T17:32:50-08:00
New Revision: 4d53b99c5df2c83172d23521d1b1ab8361d6de92

URL: https://github.com/llvm/llvm-project/commit/4d53b99c5df2c83172d23521d1b1ab8361d6de92
DIFF: https://github.com/llvm/llvm-project/commit/4d53b99c5df2c83172d23521d1b1ab8361d6de92.diff

LOG: [llvm-ar] Improve tool selection heuristic

If llvm-ar is installed at arm-pokymllib32-linux-gnueabi-llvm-ar, it may
think it is llvm-lib due to the "lib" substring.

Improve the heuristic to make all the following work as intended:

llvm-ar-9 (llvm-9 package on Debian)
llvm-ranlib.exe
Lib.exe (reported by D44808)
arm-pokymllib32-linux-gnueabi-llvm-ar (reported by D71030)

Reviewed By: raj.khem, rupprecht

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

Added: 
    llvm/test/tools/llvm-ar/tool-name.test
    llvm/test/tools/llvm-dlltool/tool-name.test
    llvm/test/tools/llvm-lib/tool-name.test
    llvm/test/tools/llvm-ranlib/tool-name.test

Modified: 
    llvm/tools/llvm-ar/llvm-ar.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-ar/tool-name.test b/llvm/test/tools/llvm-ar/tool-name.test
new file mode 100644
index 000000000000..cb656d3b47fe
--- /dev/null
+++ b/llvm/test/tools/llvm-ar/tool-name.test
@@ -0,0 +1,15 @@
+## Don't make symlinks on Windows.
+# UNSUPPORTED: system-windows
+
+# RUN: rm -rf %t
+# RUN: mkdir %t
+# RUN: ln -s llvm-ar %t/llvm-ar-9
+# RUN: ln -s llvm-ar %t/ar.exe
+# RUN: ln -s llvm-ar %t/arm-pokymllib32-linux-gnueabi-llvm-ar-9
+
+# RUN: llvm-ar h | FileCheck %s
+# RUN: %t/llvm-ar-9 h | FileCheck %s
+# RUN: %t/ar.exe h | FileCheck %s
+# RUN: %t/arm-pokymllib32-linux-gnueabi-llvm-ar-9 h | FileCheck %s
+
+# CHECK: USAGE: llvm-ar

diff  --git a/llvm/test/tools/llvm-dlltool/tool-name.test b/llvm/test/tools/llvm-dlltool/tool-name.test
new file mode 100644
index 000000000000..e9d8680322d7
--- /dev/null
+++ b/llvm/test/tools/llvm-dlltool/tool-name.test
@@ -0,0 +1,13 @@
+## Don't make symlinks on Windows.
+# UNSUPPORTED: system-windows
+
+# RUN: rm -rf %t
+# RUN: mkdir %t
+# RUN: ln -s llvm-dlltool %t/dlltool.exe
+# RUN: ln -s llvm-dlltool %t/dlltool-10
+
+# RUN: not llvm-dlltool 2>&1 | FileCheck %s
+# RUN: not %t/dlltool.exe 2>&1 | FileCheck %s
+# RUN: not %t/dlltool-10 2>&1 | FileCheck %s
+
+# CHECK: USAGE: llvm-dlltool

diff  --git a/llvm/test/tools/llvm-lib/tool-name.test b/llvm/test/tools/llvm-lib/tool-name.test
new file mode 100644
index 000000000000..9c0f7b5de73c
--- /dev/null
+++ b/llvm/test/tools/llvm-lib/tool-name.test
@@ -0,0 +1,14 @@
+## Don't make symlinks on Windows.
+# UNSUPPORTED: system-windows
+
+# RUN: rm -rf %t
+# RUN: mkdir %t
+## See D44808, MSBuild runs Lib.exe
+# RUN: ln -s llvm-lib %t/Lib.exe
+# RUN: ln -s llvm-lib %t/llvm-lib-10
+
+# RUN: llvm-lib '/?' | FileCheck %s
+# RUN: %t/Lib.exe '/?' | FileCheck %s
+# RUN: %t/llvm-lib-10 '/?' | FileCheck %s
+
+# CHECK: USAGE: llvm-lib

diff  --git a/llvm/test/tools/llvm-ranlib/tool-name.test b/llvm/test/tools/llvm-ranlib/tool-name.test
new file mode 100644
index 000000000000..bd2b49653315
--- /dev/null
+++ b/llvm/test/tools/llvm-ranlib/tool-name.test
@@ -0,0 +1,13 @@
+## Don't make symlinks on Windows.
+# UNSUPPORTED: system-windows
+
+# RUN: rm -rf %t
+# RUN: mkdir %t
+# RUN: ln -s llvm-ranlib %t/llvm-ranlib-9
+# RUN: ln -s llvm-ranlib %t/ranlib.exe
+
+# RUN: llvm-ranlib -h | FileCheck %s
+# RUN: %t/llvm-ranlib-9 -h | FileCheck %s
+# RUN: %t/ranlib.exe -h | FileCheck %s
+
+# CHECK: USAGE: llvm-ranlib

diff  --git a/llvm/tools/llvm-ar/llvm-ar.cpp b/llvm/tools/llvm-ar/llvm-ar.cpp
index 1fdc433047f7..d1830c0762c8 100644
--- a/llvm/tools/llvm-ar/llvm-ar.cpp
+++ b/llvm/tools/llvm-ar/llvm-ar.cpp
@@ -11,6 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/ADT/Triple.h"
 #include "llvm/IR/LLVMContext.h"
@@ -1179,16 +1180,25 @@ int main(int argc, char **argv) {
   llvm::InitializeAllAsmParsers();
 
   Stem = sys::path::stem(ToolName);
-  if (Stem.contains_lower("dlltool"))
+  auto Is = [](StringRef Tool) {
+    // We need to recognize the following filenames.
+    //
+    // Lib.exe -> lib (see D44808, MSBuild runs Lib.exe)
+    // dlltool.exe -> dlltool
+    // arm-pokymllib32-linux-gnueabi-llvm-ar-10 -> ar
+    auto I = Stem.rfind_lower(Tool);
+    return I != StringRef::npos &&
+           (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
+  };
+
+  if (Is("dlltool"))
     return dlltoolDriverMain(makeArrayRef(argv, argc));
-
-  if (Stem.contains_lower("ranlib"))
+  if (Is("ranlib"))
     return ranlib_main(argc, argv);
-
-  if (Stem.contains_lower("lib"))
+  if (Is("lib"))
     return libDriverMain(makeArrayRef(argv, argc));
-
-  if (Stem.contains_lower("ar"))
+  if (Is("ar"))
     return ar_main(argc, argv);
+
   fail("not ranlib, ar, lib or dlltool");
 }


        


More information about the llvm-commits mailing list