Skip to content

Commit f2f96eb

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
1 parent a650d55 commit f2f96eb

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
@@ -327,11 +327,25 @@ enum class ToolType { Objcopy, Strip, InstallNameTool };
327327
int main(int argc, char **argv) {
328328
InitLLVM X(argc, argv);
329329
ToolName = argv[0];
330-
ToolType Tool = StringSwitch<ToolType>(sys::path::stem(ToolName))
331-
.EndsWith("strip", ToolType::Strip)
332-
.EndsWith("install-name-tool", ToolType::InstallNameTool)
333-
.EndsWith("install_name_tool", ToolType::InstallNameTool)
334-
.Default(ToolType::Objcopy);
330+
331+
StringRef Stem = sys::path::stem(ToolName);
332+
auto Is = [=](StringRef Tool) {
333+
// We need to recognize the following filenames:
334+
//
335+
// llvm-objcopy -> objcopy
336+
// strip-10.exe -> strip
337+
// powerpc64-unknown-freebsd13-objcopy -> objcopy
338+
// llvm-install-name-tool -> install-name-tool
339+
auto I = Stem.rfind_lower(Tool);
340+
return I != StringRef::npos &&
341+
(I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
342+
};
343+
ToolType Tool = ToolType::Objcopy;
344+
if (Is("strip"))
345+
Tool = ToolType::Strip;
346+
else if (Is("install-name-tool") || Is("install_name_tool"))
347+
Tool = ToolType::InstallNameTool;
348+
335349
// Expand response files.
336350
// TODO: Move these lines, which are copied from lib/Support/CommandLine.cpp,
337351
// into a separate function in the CommandLine library and call that function

0 commit comments

Comments
 (0)