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

Alexander Richardson via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 16 08:43:32 PST 2019


arichardson created this revision.
arichardson added a reviewer: rupprecht.
Herald added subscribers: llvm-commits, cfe-commits, krytarowski, emaste.
Herald added projects: clang, LLVM.
arichardson updated this revision to Diff 234082.
arichardson added a comment.

Drop unrelated change


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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71554

Files:
  llvm/test/tools/llvm-ranlib/D-flag.test
  llvm/tools/llvm-ar/llvm-ar.cpp


Index: llvm/tools/llvm-ar/llvm-ar.cpp
===================================================================
--- llvm/tools/llvm-ar/llvm-ar.cpp
+++ llvm/tools/llvm-ar/llvm-ar.cpp
@@ -64,8 +64,10 @@
 USAGE: llvm-ranlib <archive-file>
 
 OPTIONS:
-  -h --help                         - Display available options
-  --version                         - Display the version of this program
+  -h --help             - Display available options
+  --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,27 @@
 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 {
+          // 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) {
Index: llvm/test/tools/llvm-ranlib/D-flag.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-ranlib/D-flag.test
@@ -0,0 +1,47 @@
+## Test the -D and -U flags of llvm-ranlib
+## Create an archive with timestamps but without symbol table
+# RUN: yaml2obj %S/../llvm-ar/Inputs/add-lib1.yaml -o %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: 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: llvm-ar tv %t.a
+# RUN: llvm-ar tv %t.a | FileCheck %s --check-prefix=DETERMINISTIC-VALUES
+
+## Check that the -U flag maintains the timestamps:
+# RUN: llvm-ar tv %t-no-index.a | FileCheck %s --check-prefix=REAL-VALUES
+# RUN: cp %t-no-index.a %t.a && llvm-ranlib -U %t.a
+# RUN: 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: 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: 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: 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 01:00 1970 D-flag.test.tmp.o
+# REAL-VALUES: rw-r--r-- {{[0-9]+}}/{{[0-9]+}} 712
+# REAL-VALUES-NOT: 1970
+# REAL-VALUES-SAME: D-flag.test.tmp.o


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71554.234082.patch
Type: text/x-patch
Size: 4187 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191216/6c675362/attachment-0001.bin>


More information about the cfe-commits mailing list