[llvm-commits] [llvm] r77954 - in /llvm/trunk/utils: FileUpdate/ FileUpdate/CMakeLists.txt FileUpdate/FileUpdate.cpp FileUpdate/Makefile Makefile

Daniel Dunbar daniel at zuster.org
Sun Aug 2 22:12:16 PDT 2009


Author: ddunbar
Date: Mon Aug  3 00:12:16 2009
New Revision: 77954

URL: http://llvm.org/viewvc/llvm-project?rev=77954&view=rev
Log:
Add FileUpdate tool, conditionally updates its output based on its input.
 - Gratuitous and unused, but possibly useful one day.

Added:
    llvm/trunk/utils/FileUpdate/   (with props)
    llvm/trunk/utils/FileUpdate/CMakeLists.txt
    llvm/trunk/utils/FileUpdate/FileUpdate.cpp
    llvm/trunk/utils/FileUpdate/Makefile
Modified:
    llvm/trunk/utils/Makefile

Propchange: llvm/trunk/utils/FileUpdate/

------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Aug  3 00:12:16 2009
@@ -0,0 +1,3 @@
+Debug
+Release-Asserts
+Release

Added: llvm/trunk/utils/FileUpdate/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/FileUpdate/CMakeLists.txt?rev=77954&view=auto

==============================================================================
--- llvm/trunk/utils/FileUpdate/CMakeLists.txt (added)
+++ llvm/trunk/utils/FileUpdate/CMakeLists.txt Mon Aug  3 00:12:16 2009
@@ -0,0 +1,11 @@
+add_executable(FileUpdate
+  FileUpdate.cpp
+  )
+
+target_link_libraries(FileUpdate LLVMSupport LLVMSystem)
+if( MINGW )
+  target_link_libraries(FileUpdate imagehlp psapi)
+endif( MINGW )
+if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
+  target_link_libraries(FileUpdate pthread)
+endif()

Added: llvm/trunk/utils/FileUpdate/FileUpdate.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/FileUpdate/FileUpdate.cpp?rev=77954&view=auto

==============================================================================
--- llvm/trunk/utils/FileUpdate/FileUpdate.cpp (added)
+++ llvm/trunk/utils/FileUpdate/FileUpdate.cpp Mon Aug  3 00:12:16 2009
@@ -0,0 +1,86 @@
+//===- FileUpdate.cpp - Conditionally update a file -----------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// FileUpdate is a utility for conditionally updating a file from its input
+// based on whether the input differs from the output. It is used to avoid
+// unnecessary modifications in a build system.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/System/Signals.h"
+using namespace llvm;
+
+static cl::opt<bool>
+Quiet("quiet", cl::desc("Don't print unnecessary status information"),
+      cl::init(false));
+
+static cl::opt<std::string>
+InputFilename("input-file", cl::desc("Input file (defaults to stdin)"),
+              cl::init("-"), cl::value_desc("filename"));
+
+static cl::opt<std::string>
+OutputFilename(cl::Positional, cl::desc("<output-file>"), cl::Required);
+
+int main(int argc, char **argv) {
+  sys::PrintStackTraceOnErrorSignal();
+  PrettyStackTraceProgram X(argc, argv);
+  cl::ParseCommandLineOptions(argc, argv);
+
+  // Get the input data.
+  std::string ErrorStr;
+  MemoryBuffer *In =
+    MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
+  if (In == 0) {
+    errs() << argv[0] << ": error: Unable to get input '"
+           << InputFilename << "': " << ErrorStr << '\n';
+    return 1;
+  }
+
+  // Get the output data.
+  MemoryBuffer *Out = MemoryBuffer::getFile(OutputFilename.c_str(), &ErrorStr);
+
+  // If the output exists and the contents match, we are done.
+  if (Out && In->getBufferSize() == Out->getBufferSize() &&
+      memcmp(In->getBufferStart(), Out->getBufferStart(),
+             Out->getBufferSize()) == 0) {
+    if (!Quiet)
+      outs() << argv[0] << ": Not updating '" << OutputFilename
+             << "', contents match input.\n";
+    return 0;
+  }
+
+  delete Out;
+
+  // Otherwise, overwrite the output.
+  if (!Quiet)
+    outs() << argv[0] << ": Updating '" << OutputFilename
+           << "', contents changed.\n";
+  raw_fd_ostream OutStream(OutputFilename.c_str(), /*Binary=*/true,
+                           /*Force=*/true, ErrorStr);
+  if (!ErrorStr.empty()) {
+    errs() << argv[0] << ": Unable to write output '"
+           << OutputFilename << "': " << ErrorStr << '\n';
+    return 1;
+  }
+
+  OutStream.write(In->getBufferStart(), In->getBufferSize());
+  OutStream.close();
+
+  if (OutStream.has_error()) {
+    errs() << argv[0] << ": Could not open output file '"
+           << OutputFilename << "': " << ErrorStr << '\n';
+    return 1;
+  }
+
+  return 0;
+}

Added: llvm/trunk/utils/FileUpdate/Makefile
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/FileUpdate/Makefile?rev=77954&view=auto

==============================================================================
--- llvm/trunk/utils/FileUpdate/Makefile (added)
+++ llvm/trunk/utils/FileUpdate/Makefile Mon Aug  3 00:12:16 2009
@@ -0,0 +1,18 @@
+##===- utils/FileUpdate/Makefile ---------------------------*- Makefile -*-===##
+# 
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+# 
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../..
+TOOLNAME = FileUpdate
+USEDLIBS = LLVMSupport.a LLVMSystem.a
+
+# This tool has no plugins, optimize startup time.
+TOOL_NO_EXPORTS = 1
+
+include $(LEVEL)/Makefile.common
+

Modified: llvm/trunk/utils/Makefile
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/Makefile?rev=77954&r1=77953&r2=77954&view=diff

==============================================================================
--- llvm/trunk/utils/Makefile (original)
+++ llvm/trunk/utils/Makefile Mon Aug  3 00:12:16 2009
@@ -8,7 +8,7 @@
 ##===----------------------------------------------------------------------===##
 
 LEVEL = ..
-PARALLEL_DIRS := TableGen fpcmp PerfectShuffle FileCheck unittest
+PARALLEL_DIRS := TableGen fpcmp PerfectShuffle FileCheck FileUpdate unittest
 
 EXTRA_DIST := cgiplotNLT.pl check-each-file codegen-diff countloc.sh cvsupdate \
               DSAclean.py DSAextract.py emacs findsym.pl GenLibDeps.pl \





More information about the llvm-commits mailing list