blob: 3514d0f21571bf79c2bb3b77ad2275656b1e5de7 [file] [log] [blame]
Daniel Dunbar05084712009-08-03 05:12:16 +00001//===- 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"
18#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000019#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer5fae2f12010-11-29 18:33:08 +000020#include "llvm/Support/Signals.h"
Michael J. Spencer7a8aab32010-12-09 17:48:55 +000021#include "llvm/Support/system_error.h"
Daniel Dunbar05084712009-08-03 05:12:16 +000022using namespace llvm;
23
24static cl::opt<bool>
25Quiet("quiet", cl::desc("Don't print unnecessary status information"),
26 cl::init(false));
27
28static cl::opt<std::string>
29InputFilename("input-file", cl::desc("Input file (defaults to stdin)"),
30 cl::init("-"), cl::value_desc("filename"));
31
32static cl::opt<std::string>
33OutputFilename(cl::Positional, cl::desc("<output-file>"), cl::Required);
34
35int main(int argc, char **argv) {
36 sys::PrintStackTraceOnErrorSignal();
37 PrettyStackTraceProgram X(argc, argv);
38 cl::ParseCommandLineOptions(argc, argv);
39
Dan Gohmane7b67d02010-08-20 16:56:11 +000040 if (OutputFilename == "-") {
41 errs() << argv[0] << ": error: Can't update standard output\n";
42 return 1;
43 }
44
Daniel Dunbar05084712009-08-03 05:12:16 +000045 // Get the input data.
Michael J. Spencer7a8aab32010-12-09 17:48:55 +000046 error_code ec;
Daniel Dunbar05084712009-08-03 05:12:16 +000047 MemoryBuffer *In =
Michael J. Spencer7a8aab32010-12-09 17:48:55 +000048 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
Daniel Dunbar05084712009-08-03 05:12:16 +000049 if (In == 0) {
50 errs() << argv[0] << ": error: Unable to get input '"
Michael J. Spencer7a8aab32010-12-09 17:48:55 +000051 << InputFilename << "': " << ec.message() << '\n';
Daniel Dunbar05084712009-08-03 05:12:16 +000052 return 1;
53 }
54
55 // Get the output data.
Michael J. Spencer7a8aab32010-12-09 17:48:55 +000056 MemoryBuffer *Out = MemoryBuffer::getFile(OutputFilename.c_str(), ec);
Daniel Dunbar05084712009-08-03 05:12:16 +000057
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 Gohman7a4575e2010-08-20 16:54:27 +000063 errs() << argv[0] << ": Not updating '" << OutputFilename
Daniel Dunbar05084712009-08-03 05:12:16 +000064 << "', contents match input.\n";
65 return 0;
66 }
67
68 delete Out;
69
70 // Otherwise, overwrite the output.
71 if (!Quiet)
Dan Gohman7a4575e2010-08-20 16:54:27 +000072 errs() << argv[0] << ": Updating '" << OutputFilename
Daniel Dunbar05084712009-08-03 05:12:16 +000073 << "', contents changed.\n";
Michael J. Spencer7a8aab32010-12-09 17:48:55 +000074 std::string ErrorStr;
Dan Gohman7a4575e2010-08-20 16:54:27 +000075 tool_output_file OutStream(OutputFilename.c_str(), ErrorStr,
76 raw_fd_ostream::F_Binary);
Daniel Dunbar05084712009-08-03 05:12:16 +000077 if (!ErrorStr.empty()) {
78 errs() << argv[0] << ": Unable to write output '"
79 << OutputFilename << "': " << ErrorStr << '\n';
80 return 1;
81 }
82
Dan Gohmand4c45432010-09-01 14:20:41 +000083 OutStream.os().write(In->getBufferStart(), In->getBufferSize());
Daniel Dunbar05084712009-08-03 05:12:16 +000084
Dan Gohman7a4575e2010-08-20 16:54:27 +000085 // Declare success.
86 OutStream.keep();
Daniel Dunbar05084712009-08-03 05:12:16 +000087
88 return 0;
89}