[PATCH] D42326: [COFF] don't replace import library if contents are unchanged
Bob Haarman via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 19 19:54:11 PST 2018
inglorion updated this revision to Diff 130745.
inglorion marked 2 inline comments as done.
inglorion added a comment.
replaced more autos with actual types
https://reviews.llvm.org/D42326
Files:
lld/COFF/Driver.cpp
lld/test/COFF/unchanged-importlib.test
Index: lld/test/COFF/unchanged-importlib.test
===================================================================
--- /dev/null
+++ lld/test/COFF/unchanged-importlib.test
@@ -0,0 +1,7 @@
+# RUN: yaml2obj < %p/Inputs/export.yaml > %t.obj
+# RUN: lld-link -out:%t.dll -dll %t.obj
+# RUN: touch -t 198002011200.00 %t.lib
+# RUN: lld-link -out:%t.dll -dll %t.obj
+# RUN: ls -l %t.lib | FileCheck --check-prefix=CHECK %s
+
+# CHECK: Feb 1 1980
Index: lld/COFF/Driver.cpp
===================================================================
--- lld/COFF/Driver.cpp
+++ lld/COFF/Driver.cpp
@@ -533,10 +533,39 @@
Exports.push_back(E2);
}
- auto E = writeImportLibrary(getImportName(AsLib), getImplibPath(), Exports,
- Config->Machine, false);
- handleAllErrors(std::move(E),
- [&](ErrorInfoBase &EIB) { error(EIB.message()); });
+ auto HandleError = [](Error &&E) {
+ handleAllErrors(std::move(E),
+ [](ErrorInfoBase &EIB) { error(EIB.message()); });
+ };
+ std::string LibName = getImportName(AsLib);
+ std::string Path = getImplibPath();
+
+ // If the import library already exists, replace it only if the contents
+ // have changed.
+ ErrorOr<std::unique_ptr<MemoryBuffer>> OldBuf = MemoryBuffer::getFile(Path);
+ if (!OldBuf) {
+ HandleError(
+ writeImportLibrary(LibName, Path, Exports, Config->Machine, false));
+ return;
+ }
+
+ SmallString<128> TmpName;
+ if (std::error_code EC =
+ sys::fs::createUniqueFile(Path + ".tmp-%%%%%%%%.lib", TmpName))
+ fatal("cannot create temporary file for import library " + Path + ": " +
+ EC.message());
+
+ if (Error E = writeImportLibrary(LibName, TmpName, Exports, Config->Machine,
+ false)) {
+ HandleError(std::move(E));
+ return;
+ }
+
+ std::unique_ptr<MemoryBuffer> NewBuf = check(MemoryBuffer::getFile(TmpName));
+ if ((*OldBuf)->getBuffer() != NewBuf->getBuffer()) {
+ OldBuf->reset();
+ HandleError(errorCodeToError(sys::fs::rename(TmpName, Path)));
+ }
}
static void parseModuleDefs(StringRef Path) {
@@ -634,8 +663,8 @@
log("Creating a temporary archive for " + Path + " to remove bitcode files");
SmallString<128> S;
- if (auto EC = sys::fs::createTemporaryFile("lld-" + sys::path::stem(Path),
- ".lib", S))
+ if (std::error_code EC = sys::fs::createTemporaryFile(
+ "lld-" + sys::path::stem(Path), ".lib", S))
fatal("cannot create a temporary file: " + EC.message());
std::string Temp = S.str();
TemporaryFiles.push_back(Temp);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42326.130745.patch
Type: text/x-patch
Size: 2631 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180120/051b64b1/attachment.bin>
More information about the llvm-commits
mailing list