[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
This commit is contained in:
Fangrui Song 2019-12-10 13:45:02 -08:00
parent 935d41e4bd
commit 4d53b99c5d
5 changed files with 72 additions and 7 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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");
}