blob: 1c66c87aee8f845fe8d6cafd30c4e2ef739d9ee5 [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"
Daniel Dunbar05084712009-08-03 05:12:16 +000020#include "llvm/System/Signals.h"
21using namespace llvm;
22
23static cl::opt<bool>
24Quiet("quiet", cl::desc("Don't print unnecessary status information"),
25 cl::init(false));
26
27static cl::opt<std::string>
28InputFilename("input-file", cl::desc("Input file (defaults to stdin)"),
29 cl::init("-"), cl::value_desc("filename"));
30
31static cl::opt<std::string>
32OutputFilename(cl::Positional, cl::desc("<output-file>"), cl::Required);
33
34int main(int argc, char **argv) {
35 sys::PrintStackTraceOnErrorSignal();
36 PrettyStackTraceProgram X(argc, argv);
37 cl::ParseCommandLineOptions(argc, argv);
38
Dan Gohmane7b67d02010-08-20 16:56:11 +000039 if (OutputFilename == "-") {
40 errs() << argv[0] << ": error: Can't update standard output\n";
41 return 1;
42 }
43
Daniel Dunbar05084712009-08-03 05:12:16 +000044 // Get the input data.
45 std::string ErrorStr;
46 MemoryBuffer *In =
47 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
48 if (In == 0) {
49 errs() << argv[0] << ": error: Unable to get input '"
50 << InputFilename << "': " << ErrorStr << '\n';
51 return 1;
52 }
53
54 // Get the output data.
55 MemoryBuffer *Out = MemoryBuffer::getFile(OutputFilename.c_str(), &ErrorStr);
56
57 // If the output exists and the contents match, we are done.
58 if (Out && In->getBufferSize() == Out->getBufferSize() &&
59 memcmp(In->getBufferStart(), Out->getBufferStart(),
60 Out->getBufferSize()) == 0) {
61 if (!Quiet)
Dan Gohman7a4575e2010-08-20 16:54:27 +000062 errs() << argv[0] << ": Not updating '" << OutputFilename
Daniel Dunbar05084712009-08-03 05:12:16 +000063 << "', contents match input.\n";
64 return 0;
65 }
66
67 delete Out;
68
69 // Otherwise, overwrite the output.
70 if (!Quiet)
Dan Gohman7a4575e2010-08-20 16:54:27 +000071 errs() << argv[0] << ": Updating '" << OutputFilename
Daniel Dunbar05084712009-08-03 05:12:16 +000072 << "', contents changed.\n";
Dan Gohman7a4575e2010-08-20 16:54:27 +000073 tool_output_file OutStream(OutputFilename.c_str(), ErrorStr,
74 raw_fd_ostream::F_Binary);
Daniel Dunbar05084712009-08-03 05:12:16 +000075 if (!ErrorStr.empty()) {
76 errs() << argv[0] << ": Unable to write output '"
77 << OutputFilename << "': " << ErrorStr << '\n';
78 return 1;
79 }
80
Dan Gohmand4c45432010-09-01 14:20:41 +000081 OutStream.os().write(In->getBufferStart(), In->getBufferSize());
Daniel Dunbar05084712009-08-03 05:12:16 +000082
Dan Gohman7a4575e2010-08-20 16:54:27 +000083 // Declare success.
84 OutStream.keep();
Daniel Dunbar05084712009-08-03 05:12:16 +000085
86 return 0;
87}