Skip to content

Commit 50d7e5d

Browse files
committed
[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 ClangBuiltLinux/linux#940 Reviewed By: alexshap Differential Revision: https://reviews.llvm.org/D76562 (cherry picked from commit f2f96eb)
1 parent 489a735 commit 50d7e5d

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Don't make symlinks on Windows.
2+
# UNSUPPORTED: system-windows
3+
4+
# RUN: rm -rf %t
5+
# RUN: mkdir %t
6+
7+
# RUN: ln -s llvm-objcopy %t/llvm-objcopy-11.exe
8+
# RUN: ln -s llvm-objcopy %t/powerpc64-unknown-freebsd13-objcopy
9+
10+
# RUN: llvm-objcopy --help | FileCheck --check-prefix=OBJCOPY %s
11+
# RUN: %t/llvm-objcopy-11.exe --help | FileCheck --check-prefix=OBJCOPY %s
12+
# RUN: %t/powerpc64-unknown-freebsd13-objcopy --help | FileCheck --check-prefix=OBJCOPY %s
13+
14+
# OBJCOPY: OVERVIEW: llvm-objcopy tool
15+
16+
# RUN: ln -s llvm-strip %t/strip.exe
17+
# RUN: ln -s llvm-strip %t/gnu-llvm-strip-10
18+
19+
# RUN: llvm-strip --help | FileCheck --check-prefix=STRIP %s
20+
# RUN: %t/strip.exe --help | FileCheck --check-prefix=STRIP %s
21+
# RUN: %t/gnu-llvm-strip-10 --help | FileCheck --check-prefix=STRIP %s
22+
23+
# STRIP: OVERVIEW: llvm-strip tool
24+
25+
## This driver emulates install_name_tool on macOS.
26+
# RUN: ln -s llvm-install-name-tool %t/llvm-install-name-tool-10
27+
# RUN: ln -s llvm-install-name-tool %t/install_name_tool.exe
28+
29+
# RUN: llvm-install-name-tool --help | FileCheck --check-prefix=INSTALL %s
30+
# RUN: %t/llvm-install-name-tool-10 --help | FileCheck --check-prefix=INSTALL %s
31+
# RUN: %t/install_name_tool.exe --help | FileCheck --check-prefix=INSTALL %s
32+
33+
# INSTALL: OVERVIEW: llvm-install-name-tool tool

llvm/tools/llvm-objcopy/llvm-objcopy.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,25 @@ enum class ToolType { Objcopy, Strip, InstallNameTool };
322322
int main(int argc, char **argv) {
323323
InitLLVM X(argc, argv);
324324
ToolName = argv[0];
325-
ToolType Tool = StringSwitch<ToolType>(sys::path::stem(ToolName))
326-
.EndsWith("strip", ToolType::Strip)
327-
.EndsWith("install-name-tool", ToolType::InstallNameTool)
328-
.EndsWith("install_name_tool", ToolType::InstallNameTool)
329-
.Default(ToolType::Objcopy);
325+
326+
StringRef Stem = sys::path::stem(ToolName);
327+
auto Is = [=](StringRef Tool) {
328+
// We need to recognize the following filenames:
329+
//
330+
// llvm-objcopy -> objcopy
331+
// strip-10.exe -> strip
332+
// powerpc64-unknown-freebsd13-objcopy -> objcopy
333+
// llvm-install-name-tool -> install-name-tool
334+
auto I = Stem.rfind_lower(Tool);
335+
return I != StringRef::npos &&
336+
(I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
337+
};
338+
ToolType Tool = ToolType::Objcopy;
339+
if (Is("strip"))
340+
Tool = ToolType::Strip;
341+
else if (Is("install-name-tool") || Is("install_name_tool"))
342+
Tool = ToolType::InstallNameTool;
343+
330344
// Expand response files.
331345
// TODO: Move these lines, which are copied from lib/Support/CommandLine.cpp,
332346
// into a separate function in the CommandLine library and call that function

0 commit comments

Comments
 (0)