[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 17:57:47 PST 2018


inglorion updated this revision to Diff 130737.
inglorion marked an inline comment as done.
inglorion retitled this revision from "[COFF] add option to avoid overwriting unchanged import libraries" to "[COFF] don't replace import library if contents are unchanged".
inglorion edited the summary of this revision.
inglorion added a reviewer: pcc.
inglorion removed a subscriber: pcc.
inglorion added a comment.

Made this the new behavior and removed the options to enable/disable it.

Made the code changes suggested by @ruiu.


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()); });
+  };
+  auto LibName = getImportName(AsLib);
+  auto Path = getImplibPath();
+
+  // If the import library already exists, replace it only if the contents
+  // have changed.
+  llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> OldBuf =
+      MemoryBuffer::getFile(Path);
+  if (OldBuf) {
+    SmallString<128> TmpName;
+    if (auto 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));
+    } else {
+      auto NewBuf = check(MemoryBuffer::getFile(TmpName));
+      if ((*OldBuf)->getBuffer() != NewBuf->getBuffer()) {
+        OldBuf->reset();
+        if (auto EC = sys::fs::rename(TmpName, Path))
+          HandleError(errorCodeToError(EC));
+      }
+    }
+  } else {
+    Error E =
+        writeImportLibrary(LibName, Path, Exports, Config->Machine, false);
+    HandleError(std::move(E));
+  }
 }
 
 static void parseModuleDefs(StringRef Path) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42326.130737.patch
Type: text/x-patch
Size: 2178 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180120/4bb2af85/attachment-0001.bin>


More information about the llvm-commits mailing list