[llvm-lib] Write object files in reversed order.
authorJacek Caban <jacek@codeweavers.com>
Thu, 9 Feb 2023 22:46:20 +0000 (00:46 +0200)
committerMartin Storsjö <martin@martin.st>
Thu, 9 Feb 2023 22:51:07 +0000 (00:51 +0200)
This isn't strictly needed, but this matches how MSVC lib.exe writes to
archives, so this makes llvm-lib more compatible and simplifies comparing
output between tools.

Reviewed By: hans

Differential Revision: https://reviews.llvm.org/D143536

llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp
llvm/test/tools/llvm-lib/Inputs/abc.s [new file with mode: 0644]
llvm/test/tools/llvm-lib/duplicate.test
llvm/test/tools/llvm-lib/nest.test
llvm/test/tools/llvm-lib/use-paths.test
llvm/test/tools/llvm-lib/xfghashmap-list.test

index ade753a..f92ff3d 100644 (file)
@@ -133,12 +133,14 @@ static void doList(opt::InputArgList& Args) {
   object::Archive Archive(B.get()->getMemBufferRef(), Err);
   fatalOpenError(std::move(Err), B->getBufferIdentifier());
 
+  std::vector<StringRef> Names;
   for (auto &C : Archive.children(Err)) {
     Expected<StringRef> NameOrErr = C.getName();
     fatalOpenError(NameOrErr.takeError(), B->getBufferIdentifier());
-    StringRef Name = NameOrErr.get();
-    llvm::outs() << Name << '\n';
+    Names.push_back(NameOrErr.get());
   }
+  for (auto Name : reverse(Names))
+    llvm::outs() << Name << '\n';
   fatalOpenError(std::move(Err), B->getBufferIdentifier());
 }
 
@@ -392,6 +394,9 @@ int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) {
     }
   }
 
+  // For compatibility with MSVC, reverse member vector after de-duplication.
+  std::reverse(Members.begin(), Members.end());
+
   if (Error E =
           writeArchive(OutputPath, Members,
                        /*WriteSymtab=*/true, object::Archive::K_GNU,
diff --git a/llvm/test/tools/llvm-lib/Inputs/abc.s b/llvm/test/tools/llvm-lib/Inputs/abc.s
new file mode 100644 (file)
index 0000000..1950ef9
--- /dev/null
@@ -0,0 +1,6 @@
+.globl c
+c:
+.globl a
+a:
+.globl b
+b:
index 3c503ca..098858d 100644 (file)
@@ -6,9 +6,11 @@ RUN: mkdir -p %t
 
 RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %t/foo.o %S/Inputs/a.s
 RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %t/bar.o %S/Inputs/b.s
-RUN: llvm-lib -out:%t/foo.lib %t/foo.o %t/foo.o %t/bar.o
+RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %t/abc.o %S/Inputs/abc.s
+RUN: llvm-lib -out:%t/foo.lib %t/foo.o %t/foo.o %t/abc.o %t/bar.o  %t/foo.o %t/foo.o
 
 RUN: llvm-ar t %t/foo.lib | FileCheck %s
-CHECK: foo.o
-CHECK-NOT: foo.o
 CHECK: bar.o
+CHECK-NEXT: abc.o
+CHECK-NEXT: foo.o
+CHECK-NOT: foo.o
index 627c847..3b1c3b0 100644 (file)
@@ -10,6 +10,6 @@ RUN: llvm-lib -out:%t/foo.lib %t/foo.o
 RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %t/bar.o %S/Inputs/b.s
 RUN: llvm-lib -out:%t/bar.lib %t/foo.lib %t/bar.o
 
-RUN: llvm-ar t %t/bar.lib | FileCheck %s
+RUN: llvm-lib -list %t/bar.lib | FileCheck %s
 CHECK: foo.o
 CHECK: bar.o
index 971c216..bc75ed8 100644 (file)
@@ -13,12 +13,12 @@ RUN: llvm-lib -out:foo.lib foo/a.obj foo/b.obj
 RUN: llvm-ar t foo.lib | FileCheck %s
 
 FIXME: We should probably use backslashes on Windows to better match MSVC tools.
-CHECK: foo/a.obj
 CHECK: foo/b.obj
+CHECK: foo/a.obj
 
 Do it again with absolute paths and see that we get something.
 RUN: llvm-lib -out:foo.lib %t/foo/a.obj %t/foo/b.obj
 RUN: llvm-ar t foo.lib | FileCheck %s --check-prefix=ABS
 
-ABS: {{.*}}/foo/a.obj
 ABS: {{.*}}/foo/b.obj
+ABS: {{.*}}/foo/a.obj
index 333fc77..9cd2b73 100644 (file)
@@ -1,7 +1,7 @@
 # RUN: rm -rf %t && mkdir -p %t && cd %t
 # RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o a.obj %S/Inputs/a.s
 # RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o b.obj %S/Inputs/b.s
-# RUN: llvm-lib /out:xfghashmap.lib a.obj b.obj
+# RUN: llvm-lib /out:xfghashmap.lib b.obj a.obj
 
 ## Replace a section in the library file with /<XFGHASHMAP>/ emulating
 ## a library from the Windows SDK for Windows 11.
@@ -10,9 +10,9 @@
 ## This should print the /<XFGHASHMAP>/ section as well as an .obj one.
 # RUN: llvm-lib /list %t/xfghashmap.lib | FileCheck %s
 
-# CHECK: a.obj
 # CHECK: /<XFGHASHMAP>/
 # CHECK-NOT: b.obj
+# CHECK: a.obj
 
 import sys