2011-10-02 00:41:13 +08:00
|
|
|
//===- Main.cpp - Top-Level TableGen implementation -----------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-10-02 00:41:13 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// TableGen is a tool which can be used to build up a description of something,
|
|
|
|
// then invoke one or more "tablegen backends" to emit information about the
|
|
|
|
// description in some predefined format. In practice, this is used by the LLVM
|
|
|
|
// code generators to automate generation of a code generator through a
|
|
|
|
// high-level description of the target.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-26 14:48:46 +08:00
|
|
|
#include "llvm/TableGen/Main.h"
|
2011-10-02 00:41:13 +08:00
|
|
|
#include "TGParser.h"
|
2016-12-06 06:58:01 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2011-10-02 00:41:13 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-04-30 07:26:49 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2011-10-02 00:41:13 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include "llvm/TableGen/Error.h"
|
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2011-10-02 00:41:13 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2015-05-26 14:48:38 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
|
|
|
|
cl::init("-"));
|
2011-10-02 00:41:13 +08:00
|
|
|
|
2015-05-26 14:48:38 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
DependFilename("d",
|
|
|
|
cl::desc("Dependency filename"),
|
|
|
|
cl::value_desc("filename"),
|
|
|
|
cl::init(""));
|
2011-10-02 00:41:13 +08:00
|
|
|
|
2015-05-26 14:48:38 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
|
2011-10-02 00:41:13 +08:00
|
|
|
|
2015-05-26 14:48:38 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
IncludeDirs("I", cl::desc("Directory of include files"),
|
|
|
|
cl::value_desc("directory"), cl::Prefix);
|
2011-10-02 00:41:13 +08:00
|
|
|
|
2018-11-28 02:57:43 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
MacroNames("D", cl::desc("Name of the macro to be defined"),
|
|
|
|
cl::value_desc("macro name"), cl::Prefix);
|
|
|
|
|
2019-10-04 05:22:28 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
WriteIfChanged("write-if-changed", cl::desc("Only write output if it changed"));
|
|
|
|
|
2016-12-06 06:58:01 +08:00
|
|
|
static int reportError(const char *ProgName, Twine Msg) {
|
|
|
|
errs() << ProgName << ": " << Msg;
|
|
|
|
errs().flush();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Create a dependency file for `-d` option.
|
2012-10-10 04:29:03 +08:00
|
|
|
///
|
|
|
|
/// This functionality is really only for the benefit of the build system.
|
|
|
|
/// It is similar to GCC's `-M*` family of options.
|
2012-10-10 04:39:28 +08:00
|
|
|
static int createDependencyFile(const TGParser &Parser, const char *argv0) {
|
2016-12-06 06:58:01 +08:00
|
|
|
if (OutputFilename == "-")
|
|
|
|
return reportError(argv0, "the option -d must be used together with -o\n");
|
|
|
|
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
2019-09-12 06:33:50 +08:00
|
|
|
ToolOutputFile DepOut(DependFilename, EC, sys::fs::OF_None);
|
2016-12-06 06:58:01 +08:00
|
|
|
if (EC)
|
|
|
|
return reportError(argv0, "error opening " + DependFilename + ":" +
|
|
|
|
EC.message() + "\n");
|
2012-10-10 04:29:03 +08:00
|
|
|
DepOut.os() << OutputFilename << ":";
|
2014-12-11 15:04:54 +08:00
|
|
|
for (const auto &Dep : Parser.getDependencies()) {
|
2019-11-21 10:21:50 +08:00
|
|
|
DepOut.os() << ' ' << Dep;
|
2012-10-10 04:29:03 +08:00
|
|
|
}
|
|
|
|
DepOut.os() << "\n";
|
|
|
|
DepOut.keep();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-26 04:42:32 +08:00
|
|
|
int llvm::TableGenMain(const char *argv0, TableGenMainFn *MainFn) {
|
2011-10-02 00:41:13 +08:00
|
|
|
RecordKeeper Records;
|
|
|
|
|
2012-10-26 04:33:17 +08:00
|
|
|
// Parse the input file.
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(InputFilename);
|
2016-12-06 06:58:01 +08:00
|
|
|
if (std::error_code EC = FileOrErr.getError())
|
|
|
|
return reportError(argv0, "Could not open input file '" + InputFilename +
|
|
|
|
"': " + EC.message() + "\n");
|
2012-10-26 04:33:17 +08:00
|
|
|
|
|
|
|
// Tell SrcMgr about this buffer, which is what TGParser will pick up.
|
2014-08-22 04:44:56 +08:00
|
|
|
SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
|
2012-10-26 04:33:17 +08:00
|
|
|
|
|
|
|
// Record the location of the include directory so that the lexer can find
|
|
|
|
// it later.
|
|
|
|
SrcMgr.setIncludeDirs(IncludeDirs);
|
|
|
|
|
2018-11-28 02:57:43 +08:00
|
|
|
TGParser Parser(SrcMgr, MacroNames, Records);
|
2012-10-26 04:33:17 +08:00
|
|
|
|
|
|
|
if (Parser.ParseFile())
|
|
|
|
return 1;
|
|
|
|
|
2019-10-04 05:22:28 +08:00
|
|
|
// Write output to memory.
|
|
|
|
std::string OutString;
|
|
|
|
raw_string_ostream Out(OutString);
|
|
|
|
if (MainFn(Out, Records))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Always write the depfile, even if the main output hasn't changed.
|
|
|
|
// If it's missing, Ninja considers the output dirty. If this was below
|
|
|
|
// the early exit below and someone deleted the .inc.d file but not the .inc
|
|
|
|
// file, tablegen would never write the depfile.
|
2012-10-26 04:33:17 +08:00
|
|
|
if (!DependFilename.empty()) {
|
|
|
|
if (int Ret = createDependencyFile(Parser, argv0))
|
|
|
|
return Ret;
|
|
|
|
}
|
2018-05-08 07:41:48 +08:00
|
|
|
|
2019-10-04 05:22:28 +08:00
|
|
|
if (WriteIfChanged) {
|
|
|
|
// Only updates the real output file if there are any differences.
|
|
|
|
// This prevents recompilation of all the files depending on it if there
|
|
|
|
// aren't any.
|
|
|
|
if (auto ExistingOrErr = MemoryBuffer::getFile(OutputFilename))
|
|
|
|
if (std::move(ExistingOrErr.get())->getBuffer() == Out.str())
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code EC;
|
|
|
|
ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None);
|
|
|
|
if (EC)
|
|
|
|
return reportError(argv0, "error opening " + OutputFilename + ":" +
|
|
|
|
EC.message() + "\n");
|
|
|
|
OutFile.os() << Out.str();
|
2012-10-26 04:33:17 +08:00
|
|
|
|
2016-12-06 06:58:01 +08:00
|
|
|
if (ErrorsPrinted > 0)
|
2017-12-29 00:58:54 +08:00
|
|
|
return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");
|
2013-03-21 04:43:11 +08:00
|
|
|
|
2012-10-26 04:33:17 +08:00
|
|
|
// Declare success.
|
2019-10-04 05:22:28 +08:00
|
|
|
OutFile.keep();
|
2012-10-26 04:33:17 +08:00
|
|
|
return 0;
|
2011-10-02 00:41:13 +08:00
|
|
|
}
|