[Orc] LLVMLinkInOrcMCJITReplacement shouldn't be in the anonymous namespace.
[oota-llvm.git] / lib / TableGen / Main.cpp
1 //===- Main.cpp - Top-Level TableGen implementation -----------------------===//
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 // TableGen is a tool which can be used to build up a description of something,
11 // then invoke one or more "tablegen backends" to emit information about the
12 // description in some predefined format.  In practice, this is used by the LLVM
13 // code generators to automate generation of a code generator through a
14 // high-level description of the target.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "TGParser.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/ToolOutputFile.h"
23 #include "llvm/TableGen/Error.h"
24 #include "llvm/TableGen/Main.h"
25 #include "llvm/TableGen/Record.h"
26 #include <algorithm>
27 #include <cstdio>
28 #include <system_error>
29 using namespace llvm;
30
31 namespace {
32   cl::opt<std::string>
33   OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
34                  cl::init("-"));
35
36   cl::opt<std::string>
37   DependFilename("d",
38                  cl::desc("Dependency filename"),
39                  cl::value_desc("filename"),
40                  cl::init(""));
41
42   cl::opt<std::string>
43   InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
44
45   cl::list<std::string>
46   IncludeDirs("I", cl::desc("Directory of include files"),
47               cl::value_desc("directory"), cl::Prefix);
48 }
49
50 /// \brief Create a dependency file for `-d` option.
51 ///
52 /// This functionality is really only for the benefit of the build system.
53 /// It is similar to GCC's `-M*` family of options.
54 static int createDependencyFile(const TGParser &Parser, const char *argv0) {
55   if (OutputFilename == "-") {
56     errs() << argv0 << ": the option -d must be used together with -o\n";
57     return 1;
58   }
59   std::error_code EC;
60   tool_output_file DepOut(DependFilename, EC, sys::fs::F_Text);
61   if (EC) {
62     errs() << argv0 << ": error opening " << DependFilename << ":"
63            << EC.message() << "\n";
64     return 1;
65   }
66   DepOut.os() << OutputFilename << ":";
67   for (const auto &Dep : Parser.getDependencies()) {
68     DepOut.os() << ' ' << Dep.first;
69   }
70   DepOut.os() << "\n";
71   DepOut.keep();
72   return 0;
73 }
74
75 namespace llvm {
76
77 int TableGenMain(char *argv0, TableGenMainFn *MainFn) {
78   RecordKeeper Records;
79
80   // Parse the input file.
81   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
82       MemoryBuffer::getFileOrSTDIN(InputFilename);
83   if (std::error_code EC = FileOrErr.getError()) {
84     errs() << "Could not open input file '" << InputFilename
85            << "': " << EC.message() << "\n";
86     return 1;
87   }
88
89   // Tell SrcMgr about this buffer, which is what TGParser will pick up.
90   SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
91
92   // Record the location of the include directory so that the lexer can find
93   // it later.
94   SrcMgr.setIncludeDirs(IncludeDirs);
95
96   TGParser Parser(SrcMgr, Records);
97
98   if (Parser.ParseFile())
99     return 1;
100
101   std::error_code EC;
102   tool_output_file Out(OutputFilename, EC, sys::fs::F_Text);
103   if (EC) {
104     errs() << argv0 << ": error opening " << OutputFilename << ":"
105            << EC.message() << "\n";
106     return 1;
107   }
108   if (!DependFilename.empty()) {
109     if (int Ret = createDependencyFile(Parser, argv0))
110       return Ret;
111   }
112
113   if (MainFn(Out.os(), Records))
114     return 1;
115
116   if (ErrorsPrinted > 0) {
117     errs() << argv0 << ": " << ErrorsPrinted << " errors.\n";
118     return 1;
119   }
120
121   // Declare success.
122   Out.keep();
123   return 0;
124 }
125
126 }