Skip to content

Commit 350c89f

Browse files
cjacekmstorsjo
authored andcommitted
[llvm-lib] Write object files in reversed order.
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
1 parent 3d570a5 commit 350c89f

File tree

6 files changed

+23
-10
lines changed

6 files changed

+23
-10
lines changed

llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@ static void doList(opt::InputArgList& Args) {
133133
object::Archive Archive(B.get()->getMemBufferRef(), Err);
134134
fatalOpenError(std::move(Err), B->getBufferIdentifier());
135135

136+
std::vector<StringRef> Names;
136137
for (auto &C : Archive.children(Err)) {
137138
Expected<StringRef> NameOrErr = C.getName();
138139
fatalOpenError(NameOrErr.takeError(), B->getBufferIdentifier());
139-
StringRef Name = NameOrErr.get();
140-
llvm::outs() << Name << '\n';
140+
Names.push_back(NameOrErr.get());
141141
}
142+
for (auto Name : reverse(Names))
143+
llvm::outs() << Name << '\n';
142144
fatalOpenError(std::move(Err), B->getBufferIdentifier());
143145
}
144146

@@ -392,6 +394,9 @@ int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) {
392394
}
393395
}
394396

397+
// For compatibility with MSVC, reverse member vector after de-duplication.
398+
std::reverse(Members.begin(), Members.end());
399+
395400
if (Error E =
396401
writeArchive(OutputPath, Members,
397402
/*WriteSymtab=*/true, object::Archive::K_GNU,

llvm/test/tools/llvm-lib/Inputs/abc.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.globl c
2+
c:
3+
.globl a
4+
a:
5+
.globl b
6+
b:

llvm/test/tools/llvm-lib/duplicate.test

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ RUN: mkdir -p %t
66

77
RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %t/foo.o %S/Inputs/a.s
88
RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %t/bar.o %S/Inputs/b.s
9-
RUN: llvm-lib -out:%t/foo.lib %t/foo.o %t/foo.o %t/bar.o
9+
RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %t/abc.o %S/Inputs/abc.s
10+
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
1011

1112
RUN: llvm-ar t %t/foo.lib | FileCheck %s
12-
CHECK: foo.o
13-
CHECK-NOT: foo.o
1413
CHECK: bar.o
14+
CHECK-NEXT: abc.o
15+
CHECK-NEXT: foo.o
16+
CHECK-NOT: foo.o

llvm/test/tools/llvm-lib/nest.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ RUN: llvm-lib -out:%t/foo.lib %t/foo.o
1010
RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %t/bar.o %S/Inputs/b.s
1111
RUN: llvm-lib -out:%t/bar.lib %t/foo.lib %t/bar.o
1212

13-
RUN: llvm-ar t %t/bar.lib | FileCheck %s
13+
RUN: llvm-lib -list %t/bar.lib | FileCheck %s
1414
CHECK: foo.o
1515
CHECK: bar.o

llvm/test/tools/llvm-lib/use-paths.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ RUN: llvm-lib -out:foo.lib foo/a.obj foo/b.obj
1313
RUN: llvm-ar t foo.lib | FileCheck %s
1414

1515
FIXME: We should probably use backslashes on Windows to better match MSVC tools.
16-
CHECK: foo/a.obj
1716
CHECK: foo/b.obj
17+
CHECK: foo/a.obj
1818

1919
Do it again with absolute paths and see that we get something.
2020
RUN: llvm-lib -out:foo.lib %t/foo/a.obj %t/foo/b.obj
2121
RUN: llvm-ar t foo.lib | FileCheck %s --check-prefix=ABS
2222

23-
ABS: {{.*}}/foo/a.obj
2423
ABS: {{.*}}/foo/b.obj
24+
ABS: {{.*}}/foo/a.obj

llvm/test/tools/llvm-lib/xfghashmap-list.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# RUN: rm -rf %t && mkdir -p %t && cd %t
22
# RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o a.obj %S/Inputs/a.s
33
# RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o b.obj %S/Inputs/b.s
4-
# RUN: llvm-lib /out:xfghashmap.lib a.obj b.obj
4+
# RUN: llvm-lib /out:xfghashmap.lib b.obj a.obj
55

66
## Replace a section in the library file with /<XFGHASHMAP>/ emulating
77
## a library from the Windows SDK for Windows 11.
@@ -10,9 +10,9 @@
1010
## This should print the /<XFGHASHMAP>/ section as well as an .obj one.
1111
# RUN: llvm-lib /list %t/xfghashmap.lib | FileCheck %s
1212

13-
# CHECK: a.obj
1413
# CHECK: /<XFGHASHMAP>/
1514
# CHECK-NOT: b.obj
15+
# CHECK: a.obj
1616

1717
import sys
1818

0 commit comments

Comments
 (0)