[llvm] f2f96eb - [llvm-objcopy] Improve tool selection logic to recognize llvm-strip-$major as strip

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 23 13:49:38 PDT 2020


Author: Fangrui Song
Date: 2020-03-23T13:49:26-07:00
New Revision: f2f96eb605bc770e4da400dbcc7a6d2526ec1fd4

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

LOG: [llvm-objcopy] Improve tool selection logic to recognize llvm-strip-$major as strip

Debian and some other distributions install llvm-strip as llvm-strip-$major (e.g. `/usr/bin/llvm-strip-9`)

D54193 made it work with llvm-strip-$major but did not add a test.
The behavior was regressed by D69146.

Fixes https://github.com/ClangBuiltLinux/linux/issues/940

Reviewed By: alexshap

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

Added: 
    llvm/test/tools/llvm-objcopy/tool-name.test

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

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-objcopy/tool-name.test b/llvm/test/tools/llvm-objcopy/tool-name.test
new file mode 100644
index 000000000000..a273375f109e
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/tool-name.test
@@ -0,0 +1,33 @@
+## Don't make symlinks on Windows.
+# UNSUPPORTED: system-windows
+
+# RUN: rm -rf %t
+# RUN: mkdir %t
+
+# RUN: ln -s llvm-objcopy %t/llvm-objcopy-11.exe
+# RUN: ln -s llvm-objcopy %t/powerpc64-unknown-freebsd13-objcopy
+
+# RUN: llvm-objcopy --help | FileCheck --check-prefix=OBJCOPY %s
+# RUN: %t/llvm-objcopy-11.exe --help | FileCheck --check-prefix=OBJCOPY %s
+# RUN: %t/powerpc64-unknown-freebsd13-objcopy --help | FileCheck --check-prefix=OBJCOPY %s
+
+# OBJCOPY: OVERVIEW: llvm-objcopy tool
+
+# RUN: ln -s llvm-strip %t/strip.exe
+# RUN: ln -s llvm-strip %t/gnu-llvm-strip-10
+
+# RUN: llvm-strip --help | FileCheck --check-prefix=STRIP %s
+# RUN: %t/strip.exe --help | FileCheck --check-prefix=STRIP %s
+# RUN: %t/gnu-llvm-strip-10 --help | FileCheck --check-prefix=STRIP %s
+
+# STRIP: OVERVIEW: llvm-strip tool
+
+## This driver emulates install_name_tool on macOS.
+# RUN: ln -s llvm-install-name-tool %t/llvm-install-name-tool-10
+# RUN: ln -s llvm-install-name-tool %t/install_name_tool.exe
+
+# RUN: llvm-install-name-tool --help | FileCheck --check-prefix=INSTALL %s
+# RUN: %t/llvm-install-name-tool-10 --help | FileCheck --check-prefix=INSTALL %s
+# RUN: %t/install_name_tool.exe --help | FileCheck --check-prefix=INSTALL %s
+
+# INSTALL: OVERVIEW: llvm-install-name-tool tool

diff  --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
index 8e95ebb73331..69b23b6cf975 100644
--- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
+++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -327,11 +327,25 @@ enum class ToolType { Objcopy, Strip, InstallNameTool };
 int main(int argc, char **argv) {
   InitLLVM X(argc, argv);
   ToolName = argv[0];
-  ToolType Tool = StringSwitch<ToolType>(sys::path::stem(ToolName))
-                      .EndsWith("strip", ToolType::Strip)
-                      .EndsWith("install-name-tool", ToolType::InstallNameTool)
-                      .EndsWith("install_name_tool", ToolType::InstallNameTool)
-                      .Default(ToolType::Objcopy);
+
+  StringRef Stem = sys::path::stem(ToolName);
+  auto Is = [=](StringRef Tool) {
+    // We need to recognize the following filenames:
+    //
+    // llvm-objcopy -> objcopy
+    // strip-10.exe -> strip
+    // powerpc64-unknown-freebsd13-objcopy -> objcopy
+    // llvm-install-name-tool -> install-name-tool
+    auto I = Stem.rfind_lower(Tool);
+    return I != StringRef::npos &&
+           (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
+  };
+  ToolType Tool = ToolType::Objcopy;
+  if (Is("strip"))
+    Tool = ToolType::Strip;
+  else if (Is("install-name-tool") || Is("install_name_tool"))
+    Tool = ToolType::InstallNameTool;
+
   // Expand response files.
   // TODO: Move these lines, which are copied from lib/Support/CommandLine.cpp,
   // into a separate function in the CommandLine library and call that function


        


More information about the llvm-commits mailing list