Don't use 'using std::error_code' in include/llvm.
[oota-llvm.git] / utils / FileUpdate / FileUpdate.cpp
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/FileSystem.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/PrettyStackTrace.h"
20 #include "llvm/Support/Signals.h"
21 #include "llvm/Support/ToolOutputFile.h"
22 #include <system_error>
23 using namespace llvm;
24 using std::error_code;
25
26 static cl::opt<bool>
27 Quiet("quiet", cl::desc("Don't print unnecessary status information"),
28       cl::init(false));
29
30 static cl::opt<std::string>
31 InputFilename("input-file", cl::desc("Input file (defaults to stdin)"),
32               cl::init("-"), cl::value_desc("filename"));
33
34 static cl::opt<std::string>
35 OutputFilename(cl::Positional, cl::desc("<output-file>"), cl::Required);
36
37 int main(int argc, char **argv) {
38   sys::PrintStackTraceOnErrorSignal();
39   PrettyStackTraceProgram X(argc, argv);
40   cl::ParseCommandLineOptions(argc, argv);
41
42   if (OutputFilename == "-") {
43     errs() << argv[0] << ": error: Can't update standard output\n";
44     return 1;
45   }
46
47   // Get the input data.
48   std::unique_ptr<MemoryBuffer> In;
49   if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, In)) {
50     errs() << argv[0] << ": error: Unable to get input '"
51            << InputFilename << "': " << ec.message() << '\n';
52     return 1;
53   }
54
55   // Get the output data.
56   std::unique_ptr<MemoryBuffer> Out;
57   MemoryBuffer::getFile(OutputFilename.c_str(), Out);
58
59   // If the output exists and the contents match, we are done.
60   if (Out && In->getBufferSize() == Out->getBufferSize() &&
61       memcmp(In->getBufferStart(), Out->getBufferStart(),
62              Out->getBufferSize()) == 0) {
63     if (!Quiet)
64       errs() << argv[0] << ": Not updating '" << OutputFilename
65              << "', contents match input.\n";
66     return 0;
67   }
68
69   // Otherwise, overwrite the output.
70   if (!Quiet)
71     errs() << argv[0] << ": Updating '" << OutputFilename
72            << "', contents changed.\n";
73   std::string ErrorStr;
74   tool_output_file OutStream(OutputFilename.c_str(), ErrorStr,
75                              sys::fs::F_None);
76   if (!ErrorStr.empty()) {
77     errs() << argv[0] << ": Unable to write output '"
78            << OutputFilename << "': " << ErrorStr << '\n';
79     return 1;
80   }
81
82   OutStream.os().write(In->getBufferStart(), In->getBufferSize());
83
84   // Declare success.
85   OutStream.keep();
86
87   return 0;
88 }