From 535b3c6b2f1c81ed91942ebd9ea06a1022dc59a1 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Thu, 2 Jan 2020 13:44:02 +0100 Subject: [PATCH] [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 --- llvm/test/tools/llvm-ranlib/D-flag.test | 45 +++++++++++++++++++ llvm/test/tools/llvm-ranlib/help-message.test | 17 +++++-- llvm/tools/llvm-ar/llvm-ar.cpp | 30 +++++++++++-- 3 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 llvm/test/tools/llvm-ranlib/D-flag.test diff --git a/llvm/test/tools/llvm-ranlib/D-flag.test b/llvm/test/tools/llvm-ranlib/D-flag.test new file mode 100644 index 000000000000..01714cfe229e --- /dev/null +++ b/llvm/test/tools/llvm-ranlib/D-flag.test @@ -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 diff --git a/llvm/test/tools/llvm-ranlib/help-message.test b/llvm/test/tools/llvm-ranlib/help-message.test index 885638a53fcd..61620accd6d3 100644 --- a/llvm/test/tools/llvm-ranlib/help-message.test +++ b/llvm/test/tools/llvm-ranlib/help-message.test @@ -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 diff --git a/llvm/tools/llvm-ar/llvm-ar.cpp b/llvm/tools/llvm-ar/llvm-ar.cpp index d1830c0762c8..c339dfe1f33e 100644 --- a/llvm/tools/llvm-ar/llvm-ar.cpp +++ b/llvm/tools/llvm-ar/llvm-ar.cpp @@ -64,8 +64,10 @@ const char RanlibHelp[] = R"(OVERVIEW: LLVM Ranlib (llvm-ranlib) USAGE: llvm-ranlib OPTIONS: - -h --help - Display available options - --version - Display the version of this program + -h --help - Display available options + -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) {