1 //===- Main.cpp - Top-Level TableGen implementation -----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // TableGen is a tool which can be used to build up a description of something,
10 // then invoke one or more "tablegen backends" to emit information about the
11 // description in some predefined format. In practice, this is used by the LLVM
12 // code generators to automate generation of a code generator through a
13 // high-level description of the target.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/TableGen/Main.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/ToolOutputFile.h"
24 #include "llvm/TableGen/Error.h"
25 #include "llvm/TableGen/Record.h"
28 #include <system_error>
31 static cl::opt
<std::string
>
32 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
35 static cl::opt
<std::string
>
37 cl::desc("Dependency filename"),
38 cl::value_desc("filename"),
41 static cl::opt
<std::string
>
42 InputFilename(cl::Positional
, cl::desc("<input file>"), cl::init("-"));
44 static cl::list
<std::string
>
45 IncludeDirs("I", cl::desc("Directory of include files"),
46 cl::value_desc("directory"), cl::Prefix
);
48 static cl::list
<std::string
>
49 MacroNames("D", cl::desc("Name of the macro to be defined"),
50 cl::value_desc("macro name"), cl::Prefix
);
52 static int reportError(const char *ProgName
, Twine Msg
) {
53 errs() << ProgName
<< ": " << Msg
;
58 /// Create a dependency file for `-d` option.
60 /// This functionality is really only for the benefit of the build system.
61 /// It is similar to GCC's `-M*` family of options.
62 static int createDependencyFile(const TGParser
&Parser
, const char *argv0
) {
63 if (OutputFilename
== "-")
64 return reportError(argv0
, "the option -d must be used together with -o\n");
67 ToolOutputFile
DepOut(DependFilename
, EC
, sys::fs::OF_None
);
69 return reportError(argv0
, "error opening " + DependFilename
+ ":" +
71 DepOut
.os() << OutputFilename
<< ":";
72 for (const auto &Dep
: Parser
.getDependencies()) {
73 DepOut
.os() << ' ' << Dep
.first
;
80 int llvm::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 return reportError(argv0
, "Could not open input file '" + InputFilename
+
88 "': " + EC
.message() + "\n");
90 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
91 SrcMgr
.AddNewSourceBuffer(std::move(*FileOrErr
), SMLoc());
93 // Record the location of the include directory so that the lexer can find
95 SrcMgr
.setIncludeDirs(IncludeDirs
);
97 TGParser
Parser(SrcMgr
, MacroNames
, Records
);
99 if (Parser
.ParseFile())
102 // Write output to memory.
103 std::string OutString
;
104 raw_string_ostream
Out(OutString
);
105 if (MainFn(Out
, Records
))
108 // Always write the depfile, even if the main output hasn't changed.
109 // If it's missing, Ninja considers the output dirty. If this was below
110 // the early exit below and someone deleted the .inc.d file but not the .inc
111 // file, tablegen would never write the depfile.
112 if (!DependFilename
.empty()) {
113 if (int Ret
= createDependencyFile(Parser
, argv0
))
117 // Only updates the real output file if there are any differences.
118 // This prevents recompilation of all the files depending on it if there
120 if (auto ExistingOrErr
= MemoryBuffer::getFile(OutputFilename
))
121 if (std::move(ExistingOrErr
.get())->getBuffer() == Out
.str())
125 ToolOutputFile
OutFile(OutputFilename
, EC
, sys::fs::OF_None
);
127 return reportError(argv0
, "error opening " + OutputFilename
+ ":" +
128 EC
.message() + "\n");
129 OutFile
.os() << Out
.str();
131 if (ErrorsPrinted
> 0)
132 return reportError(argv0
, Twine(ErrorsPrinted
) + " errors.\n");