Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 1 | //===- FileUpdate.cpp - Conditionally update a file -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // FileUpdate is a utility for conditionally updating a file from its input |
| 11 | // based on whether the input differs from the output. It is used to avoid |
| 12 | // unnecessary modifications in a build system. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/Support/CommandLine.h" |
| 17 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame^] | 18 | #include "llvm/ADT/OwningPtr.h" |
Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 19 | #include "llvm/Support/PrettyStackTrace.h" |
Dan Gohman | e4f1a9b | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ToolOutputFile.h" |
Michael J. Spencer | 5fae2f1 | 2010-11-29 18:33:08 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Signals.h" |
Michael J. Spencer | 7a8aab3 | 2010-12-09 17:48:55 +0000 | [diff] [blame] | 22 | #include "llvm/Support/system_error.h" |
Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
| 25 | static cl::opt<bool> |
| 26 | Quiet("quiet", cl::desc("Don't print unnecessary status information"), |
| 27 | cl::init(false)); |
| 28 | |
| 29 | static cl::opt<std::string> |
| 30 | InputFilename("input-file", cl::desc("Input file (defaults to stdin)"), |
| 31 | cl::init("-"), cl::value_desc("filename")); |
| 32 | |
| 33 | static cl::opt<std::string> |
| 34 | OutputFilename(cl::Positional, cl::desc("<output-file>"), cl::Required); |
| 35 | |
| 36 | int main(int argc, char **argv) { |
| 37 | sys::PrintStackTraceOnErrorSignal(); |
| 38 | PrettyStackTraceProgram X(argc, argv); |
| 39 | cl::ParseCommandLineOptions(argc, argv); |
| 40 | |
Dan Gohman | e7b67d0 | 2010-08-20 16:56:11 +0000 | [diff] [blame] | 41 | if (OutputFilename == "-") { |
| 42 | errs() << argv[0] << ": error: Can't update standard output\n"; |
| 43 | return 1; |
| 44 | } |
| 45 | |
Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 46 | // Get the input data. |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame^] | 47 | OwningPtr<MemoryBuffer> In; |
| 48 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), In)) { |
Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 49 | errs() << argv[0] << ": error: Unable to get input '" |
Michael J. Spencer | 7a8aab3 | 2010-12-09 17:48:55 +0000 | [diff] [blame] | 50 | << InputFilename << "': " << ec.message() << '\n'; |
Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | // Get the output data. |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame^] | 55 | OwningPtr<MemoryBuffer> Out; |
| 56 | MemoryBuffer::getFile(OutputFilename.c_str(), Out); |
Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 57 | |
| 58 | // If the output exists and the contents match, we are done. |
| 59 | if (Out && In->getBufferSize() == Out->getBufferSize() && |
| 60 | memcmp(In->getBufferStart(), Out->getBufferStart(), |
| 61 | Out->getBufferSize()) == 0) { |
| 62 | if (!Quiet) |
Dan Gohman | 7a4575e | 2010-08-20 16:54:27 +0000 | [diff] [blame] | 63 | errs() << argv[0] << ": Not updating '" << OutputFilename |
Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 64 | << "', contents match input.\n"; |
| 65 | return 0; |
| 66 | } |
| 67 | |
Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 68 | // Otherwise, overwrite the output. |
| 69 | if (!Quiet) |
Dan Gohman | 7a4575e | 2010-08-20 16:54:27 +0000 | [diff] [blame] | 70 | errs() << argv[0] << ": Updating '" << OutputFilename |
Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 71 | << "', contents changed.\n"; |
Michael J. Spencer | 7a8aab3 | 2010-12-09 17:48:55 +0000 | [diff] [blame] | 72 | std::string ErrorStr; |
Dan Gohman | 7a4575e | 2010-08-20 16:54:27 +0000 | [diff] [blame] | 73 | tool_output_file OutStream(OutputFilename.c_str(), ErrorStr, |
| 74 | raw_fd_ostream::F_Binary); |
Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 75 | if (!ErrorStr.empty()) { |
| 76 | errs() << argv[0] << ": Unable to write output '" |
| 77 | << OutputFilename << "': " << ErrorStr << '\n'; |
| 78 | return 1; |
| 79 | } |
| 80 | |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 81 | OutStream.os().write(In->getBufferStart(), In->getBufferSize()); |
Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 82 | |
Dan Gohman | 7a4575e | 2010-08-20 16:54:27 +0000 | [diff] [blame] | 83 | // Declare success. |
| 84 | OutStream.keep(); |
Daniel Dunbar | 0508471 | 2009-08-03 05:12:16 +0000 | [diff] [blame] | 85 | |
| 86 | return 0; |
| 87 | } |