1 //===- Main.cpp - Top-Level TableGen implementation -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
16 //===----------------------------------------------------------------------===//
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"
28 #include <system_error>
33 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
38 cl::desc("Dependency filename"),
39 cl::value_desc("filename"),
43 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
46 IncludeDirs("I", cl::desc("Directory of include files"),
47 cl::value_desc("directory"), cl::Prefix);
50 /// \brief Create a dependency file for `-d` option.
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";
60 tool_output_file DepOut(DependFilename, EC, sys::fs::F_Text);
62 errs() << argv0 << ": error opening " << DependFilename << ":"
63 << EC.message() << "\n";
66 DepOut.os() << OutputFilename << ":";
67 const TGLexer::DependenciesMapTy &Dependencies = Parser.getDependencies();
68 for (TGLexer::DependenciesMapTy::const_iterator I = Dependencies.begin(),
69 E = Dependencies.end();
71 DepOut.os() << " " << I->first;
80 int TableGenMain(char *argv0, TableGenMainFn *MainFn) {
83 // Parse the input file.
84 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
85 MemoryBuffer::getFileOrSTDIN(InputFilename);
86 if (std::error_code EC = FileOrErr.getError()) {
87 errs() << "Could not open input file '" << InputFilename
88 << "': " << EC.message() << "\n";
92 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
93 SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
95 // Record the location of the include directory so that the lexer can find
97 SrcMgr.setIncludeDirs(IncludeDirs);
99 TGParser Parser(SrcMgr, Records);
101 if (Parser.ParseFile())
105 tool_output_file Out(OutputFilename, EC, sys::fs::F_Text);
107 errs() << argv0 << ": error opening " << OutputFilename << ":"
108 << EC.message() << "\n";
111 if (!DependFilename.empty()) {
112 if (int Ret = createDependencyFile(Parser, argv0))
116 if (MainFn(Out.os(), Records))
119 if (ErrorsPrinted > 0) {
120 errs() << argv0 << ": " << ErrorsPrinted << " errors.\n";