[llvm-ranlib] Handle -D and -U command line flag

I have been trying to build CheriBSD (a fork for FreeBSD for the CHERI
CPU) with LLVM binutils instead of the default elftoolchain utilities.
I noticed that building static archives was failing because ranlib is
invoked with the -D flag. This failed with llvm-ranlib since it parses
the -D flag as the archive path and reports an error that more than one
archive has been passed.

This fixes https://llvm.org/PR41707

Reviewed By: rupprecht
Differential Revision: https://reviews.llvm.org/D71554
This commit is contained in:
Alex Richardson 2020-01-02 13:44:02 +01:00
parent 8188c998ff
commit 535b3c6b2f
3 changed files with 84 additions and 8 deletions

View File

@ -0,0 +1,45 @@
## Test the -D and -U flags of llvm-ranlib
## Create an archive with timestamps but without symbol table
## Important: all `llvm-ar tv` calls must use TZ=UTC to produce identical values
# RUN: yaml2obj %S/../llvm-ar/Inputs/add-lib1.yaml -o %t.o
# RUN: env TZ=UTC touch -t 200001020304 %t.o
# RUN: rm -f %t.a %t-no-index.a && llvm-ar cqSU %t-no-index.a %t.o
## Check that the intial listing has real values:
# RUN: env TZ=UTC llvm-ar tv %t-no-index.a | FileCheck %s --check-prefix=REAL-VALUES
## Check that the -D flag clears the timestamps:
# RUN: cp %t-no-index.a %t.a && llvm-ranlib -D %t.a
# RUN: env TZ=UTC llvm-ar tv %t.a | FileCheck %s --check-prefix=DETERMINISTIC-VALUES
## Check that the -U flag maintains the timestamps:
# RUN: cp %t-no-index.a %t.a && llvm-ranlib -U %t.a
# RUN: env TZ=UTC llvm-ar tv %t.a | FileCheck %s --check-prefix=REAL-VALUES
## Check that we accept multiple values and the last one wins:
# RUN: cp %t-no-index.a %t.a && llvm-ranlib -UDU %t.a
# RUN: env TZ=UTC llvm-ar tv %t.a | FileCheck %s --check-prefix=REAL-VALUES
# RUN: cp %t-no-index.a %t.a && llvm-ranlib -UUD %t.a
# RUN: env TZ=UTC llvm-ar tv %t.a | FileCheck %s --check-prefix=DETERMINISTIC-VALUES
## Check arguments can be passed before and after the file name
# RUN: cp %t-no-index.a %t.a && llvm-ranlib -U %t.a -D -U
# RUN: env TZ=UTC llvm-ar tv %t.a | FileCheck %s --check-prefix=REAL-VALUES
## Check that the -D/-U option is only accepted with a single dash. This matches
## the GNU ranlib behaviour.
# RUN: not llvm-ranlib --D %t.a 2>&1 | FileCheck %s --check-prefix=BAD-OPT-D
# BAD-OPT-D: llvm-ranlib: error: Invalid option: '--D'
# RUN: not llvm-ranlib --U %t.a 2>&1 | FileCheck %s --check-prefix=BAD-OPT-U
# BAD-OPT-U: llvm-ranlib: error: Invalid option: '--U'
# RUN: not llvm-ranlib -x %t.a 2>&1 | FileCheck %s --check-prefix=BAD-OPT-x
# BAD-OPT-x: llvm-ranlib: error: Invalid option: '-x'
## If the first argument starts with value flags, the error message only shows
## the remainder of the parsed string:
# RUN: not llvm-ranlib -Dx %t.a 2>&1 | FileCheck %s --check-prefix=BAD-OPT-x
# RUN: not llvm-ranlib -DxD %t.a 2>&1 | FileCheck %s --check-prefix=BAD-OPT-xD
# BAD-OPT-xD: llvm-ranlib: error: Invalid option: '-xD'
# DETERMINISTIC-VALUES: rw-r--r-- 0/0 712 Jan 1 00:00 1970 D-flag.test.tmp.o
# REAL-VALUES: rw-r--r-- {{[0-9]+}}/{{[0-9]+}} 712 Jan 2 03:04 2000 D-flag.test.tmp.o

View File

@ -1,8 +1,17 @@
## Show that the help message for llvm-ranlib can be printed with either the
## long flag -help.
# RUN: llvm-ranlib -h | FileCheck %s
# RUN: llvm-ranlib -help | FileCheck %s
# RUN: llvm-ranlib --help | FileCheck %s
# RUN: llvm-ranlib -h | FileCheck %s --check-prefix=HELP
# RUN: llvm-ranlib -help | FileCheck %s --check-prefix=HELP
# RUN: llvm-ranlib --help | FileCheck %s --check-prefix=HELP
# RUN: llvm-ranlib --version | FileCheck %s --check-prefix=VERSION
# RUN: llvm-ranlib -version | FileCheck %s --check-prefix=VERSION
# RUN: llvm-ranlib -v | FileCheck %s --check-prefix=VERSION
# CHECK: USAGE: llvm-ranlib
## Also check combined options (first -h/-v flag wins)
# RUN: llvm-ranlib -Dh | FileCheck %s --check-prefix=HELP
# RUN: llvm-ranlib -Dvh | FileCheck %s --check-prefix=VERSION
# RUN: llvm-ranlib -Dhv | FileCheck %s --check-prefix=HELP
# HELP: USAGE: llvm-ranlib
# VERSION: LLVM version

View File

@ -65,7 +65,9 @@ USAGE: llvm-ranlib <archive-file>
OPTIONS:
-h --help - Display available options
--version - Display the version of this program
-v --version - Display the version of this program
-D - Use zero for timestamps and uids/gids (default)
-U - Use actual timestamps and uids/gids
)";
const char ArHelp[] = R"(OVERVIEW: LLVM Archiver
@ -1156,13 +1158,33 @@ static int ar_main(int argc, char **argv) {
static int ranlib_main(int argc, char **argv) {
bool ArchiveSpecified = false;
for (int i = 1; i < argc; ++i) {
if (handleGenericOption(argv[i])) {
StringRef arg(argv[i]);
if (handleGenericOption(arg)) {
return 0;
} else if (arg.consume_front("-")) {
// Handle the -D/-U flag
while (!arg.empty()) {
if (arg.front() == 'D') {
Deterministic = true;
} else if (arg.front() == 'U') {
Deterministic = false;
} else if (arg.front() == 'h') {
printHelpMessage();
return 0;
} else if (arg.front() == 'v') {
cl::PrintVersionMessage();
return 0;
} else {
// TODO: GNU ranlib also supports a -t flag
fail("Invalid option: '-" + arg + "'");
}
arg = arg.drop_front(1);
}
} else {
if (ArchiveSpecified)
fail("exactly one archive should be specified");
ArchiveSpecified = true;
ArchiveName = argv[i];
ArchiveName = arg.str();
}
}
if (!ArchiveSpecified) {