[PATCH] D92902: [llvm-elfabi] Add flag to keep timestamp when output is the same

Haowei Wu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 8 17:51:48 PST 2020


haowei created this revision.
haowei added reviewers: mcgrathr, jhenderson, MaskRay, phosek, grimar.
haowei requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This change adds '--keep-timestamp' flag to llvm-elfabi tool. When enabled, llvm-elfabi will not update the existing tbe file's timestamp if the content of the file will not be changed.

This flag make it easier to avoid re-linking in the build if the ABI of shared library in deps is not changed.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92902

Files:
  llvm/test/tools/llvm-elfabi/keep-timestamp.test
  llvm/tools/llvm-elfabi/llvm-elfabi.cpp


Index: llvm/tools/llvm-elfabi/llvm-elfabi.cpp
===================================================================
--- llvm/tools/llvm-elfabi/llvm-elfabi.cpp
+++ llvm/tools/llvm-elfabi/llvm-elfabi.cpp
@@ -57,22 +57,37 @@
                clEnumValN(ELFTarget::ELF64BE, "elf64-big",
                           "64-bit big-endian ELF stub")));
 cl::opt<std::string> BinaryOutputFilePath(cl::Positional, cl::desc("output"));
+cl::opt<bool> KeepOutputFileTimeStamp(
+    "keep-timestamp", cl::desc("Keep the timestamp of the output file "
+                               "unchanged if the content is not changed"));
 
 /// writeTBE() writes a Text-Based ELF stub to a file using the latest version
 /// of the YAML parser.
 static Error writeTBE(StringRef FilePath, ELFStub &Stub) {
   std::error_code SysErr;
-
-  // Open file for writing.
-  raw_fd_ostream Out(FilePath, SysErr);
-  if (SysErr)
-    return createStringError(SysErr, "Couldn't open `%s` for writing",
-                             FilePath.data());
-  // Write file.
-  Error YAMLErr = writeTBEToOutputStream(Out, Stub);
+  // Write TBE to memory first.
+  std::string TBEStr;
+  raw_string_ostream OutStr(TBEStr);
+  Error YAMLErr = writeTBEToOutputStream(OutStr, Stub);
   if (YAMLErr)
     return YAMLErr;
-
+  OutStr.str();
+
+  if (KeepOutputFileTimeStamp) {
+    ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrError =
+        MemoryBuffer::getFile(FilePath);
+    if (BufOrError) {
+      // Compare TBE output with existing TBE file.
+      std::unique_ptr<MemoryBuffer> FileReadBuffer = std::move(*BufOrError);
+      if (FileReadBuffer->getBuffer().equals(TBEStr)) {
+        // TBE file unchanged, abort updating.
+        return Error::success();
+      }
+    }
+  }
+  // Open TBE file for writing.
+  raw_fd_ostream Out(FilePath, SysErr);
+  Out << TBEStr;
   return Error::success();
 }
 
Index: llvm/test/tools/llvm-elfabi/keep-timestamp.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-elfabi/keep-timestamp.test
@@ -0,0 +1,6 @@
+# RUN: llvm-elfabi --elf %p/Inputs/gnu_hash.so --emit-tbe=%t --keep-timestamp
+# RUN: touch -m -d "1970-01-01 00:00:00" %t
+# RUN: llvm-elfabi --elf %p/Inputs/gnu_hash.so --emit-tbe=%t --keep-timestamp
+# RUN: stat %t | FileCheck %s
+
+# CHECK: Modify: 1970-01-01 00:00:00.000000000 -0800


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92902.310395.patch
Type: text/x-patch
Size: 2359 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201209/1fcf8df0/attachment.bin>


More information about the llvm-commits mailing list