[llvm-ar] Fix when llvm-ar fails to replace existing members when updating a thin archive

As seen in https://github.com/llvm/llvm-project/issues/55023 when a thin
archive is updated when not in the CWD, replacement does not work as
expected. This change fixes the relative file path comparison so the
correct files are updated.

Differential Revision: https://reviews.llvm.org/D138218
This commit is contained in:
gbreynoo 2022-11-18 14:37:56 +00:00
parent 2e02f007a2
commit dcbf61b352
2 changed files with 29 additions and 2 deletions

View File

@ -0,0 +1,19 @@
RUN: rm -rf %t && mkdir -p %t/foo %t/bar
RUN: yaml2obj %S/Inputs/elf.yaml -o %t/foo/elf.o
RUN: cp %t/foo/elf.o %t/bar/elf.o
RUN: cd %t
Test a case in which CWD does not contain the archive, ensure replacement behaves as expected
RUN: llvm-ar cr --thin foo/lib.a foo/elf.o
RUN: llvm-ar t foo/lib.a | FileCheck %s --check-prefix=FOO --implicit-check-not {{.}}
RUN: llvm-ar cr --thin foo/lib.a bar/elf.o
RUN: llvm-ar t foo/lib.a | FileCheck %s --check-prefixes=FOO,BAR --implicit-check-not {{.}}
RUN: llvm-ar cr --thin foo/lib.a foo/elf.o
RUN: llvm-ar t foo/lib.a | FileCheck %s --check-prefixes=FOO,BAR --implicit-check-not {{.}}
FOO: foo/elf.o
BAR: foo/../bar/elf.o

View File

@ -875,8 +875,16 @@ static InsertAction computeInsertAction(ArchiveOperation Operation,
if (Operation == QuickAppend || Members.empty())
return IA_AddOldMember;
auto MI = find_if(
Members, [Name](StringRef Path) { return comparePaths(Name, Path); });
auto MI = find_if(Members, [Name](StringRef Path) {
if (Thin && !sys::path::is_absolute(Path)) {
Expected<std::string> PathOrErr =
computeArchiveRelativePath(ArchiveName, Path);
return comparePaths(Name, PathOrErr ? *PathOrErr : Path);
} else {
return comparePaths(Name, Path);
}
});
if (MI == Members.end())
return IA_AddOldMember;