Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 1 | //===- Main.cpp - Top-Level TableGen implementation -----------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 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. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Craig Topper | 1407b17 | 2015-05-26 06:48:46 +0000 | [diff] [blame] | 17 | #include "llvm/TableGen/Main.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 18 | #include "TGParser.h" |
Davide Italiano | ebd5350 | 2016-12-05 22:58:01 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MemoryBuffer.h" |
| 23 | #include "llvm/Support/ToolOutputFile.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 24 | #include "llvm/TableGen/Error.h" |
| 25 | #include "llvm/TableGen/Record.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | #include <cstdio> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 28 | #include <system_error> |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Craig Topper | b7644fd | 2015-05-26 06:48:38 +0000 | [diff] [blame] | 31 | static cl::opt<std::string> |
| 32 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), |
| 33 | cl::init("-")); |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 34 | |
Craig Topper | b7644fd | 2015-05-26 06:48:38 +0000 | [diff] [blame] | 35 | static cl::opt<std::string> |
| 36 | DependFilename("d", |
| 37 | cl::desc("Dependency filename"), |
| 38 | cl::value_desc("filename"), |
| 39 | cl::init("")); |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 40 | |
Craig Topper | b7644fd | 2015-05-26 06:48:38 +0000 | [diff] [blame] | 41 | static cl::opt<std::string> |
| 42 | InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 43 | |
Craig Topper | b7644fd | 2015-05-26 06:48:38 +0000 | [diff] [blame] | 44 | static cl::list<std::string> |
| 45 | IncludeDirs("I", cl::desc("Directory of include files"), |
| 46 | cl::value_desc("directory"), cl::Prefix); |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 47 | |
Vyacheslav Zakharin | f7d079e | 2018-11-27 18:57:43 +0000 | [diff] [blame] | 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); |
| 51 | |
Davide Italiano | ebd5350 | 2016-12-05 22:58:01 +0000 | [diff] [blame] | 52 | static int reportError(const char *ProgName, Twine Msg) { |
| 53 | errs() << ProgName << ": " << Msg; |
| 54 | errs().flush(); |
| 55 | return 1; |
| 56 | } |
| 57 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 58 | /// Create a dependency file for `-d` option. |
Sean Silva | 0cd3536 | 2012-10-09 20:29:03 +0000 | [diff] [blame] | 59 | /// |
| 60 | /// This functionality is really only for the benefit of the build system. |
| 61 | /// It is similar to GCC's `-M*` family of options. |
Sean Silva | 2f7bf41 | 2012-10-09 20:39:28 +0000 | [diff] [blame] | 62 | static int createDependencyFile(const TGParser &Parser, const char *argv0) { |
Davide Italiano | ebd5350 | 2016-12-05 22:58:01 +0000 | [diff] [blame] | 63 | if (OutputFilename == "-") |
| 64 | return reportError(argv0, "the option -d must be used together with -o\n"); |
| 65 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 66 | std::error_code EC; |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 67 | ToolOutputFile DepOut(DependFilename, EC, sys::fs::F_Text); |
Davide Italiano | ebd5350 | 2016-12-05 22:58:01 +0000 | [diff] [blame] | 68 | if (EC) |
| 69 | return reportError(argv0, "error opening " + DependFilename + ":" + |
| 70 | EC.message() + "\n"); |
Sean Silva | 0cd3536 | 2012-10-09 20:29:03 +0000 | [diff] [blame] | 71 | DepOut.os() << OutputFilename << ":"; |
Craig Topper | 5fcc5ab | 2014-12-11 07:04:54 +0000 | [diff] [blame] | 72 | for (const auto &Dep : Parser.getDependencies()) { |
| 73 | DepOut.os() << ' ' << Dep.first; |
Sean Silva | 0cd3536 | 2012-10-09 20:29:03 +0000 | [diff] [blame] | 74 | } |
| 75 | DepOut.os() << "\n"; |
| 76 | DepOut.keep(); |
| 77 | return 0; |
| 78 | } |
| 79 | |
Craig Topper | cd37121 | 2015-05-26 06:48:41 +0000 | [diff] [blame] | 80 | int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) { |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 81 | RecordKeeper Records; |
| 82 | |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 83 | // Parse the input file. |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 84 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 85 | MemoryBuffer::getFileOrSTDIN(InputFilename); |
Davide Italiano | ebd5350 | 2016-12-05 22:58:01 +0000 | [diff] [blame] | 86 | if (std::error_code EC = FileOrErr.getError()) |
| 87 | return reportError(argv0, "Could not open input file '" + InputFilename + |
| 88 | "': " + EC.message() + "\n"); |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 89 | |
| 90 | // Tell SrcMgr about this buffer, which is what TGParser will pick up. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 91 | SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc()); |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 92 | |
| 93 | // Record the location of the include directory so that the lexer can find |
| 94 | // it later. |
| 95 | SrcMgr.setIncludeDirs(IncludeDirs); |
| 96 | |
Vyacheslav Zakharin | f7d079e | 2018-11-27 18:57:43 +0000 | [diff] [blame] | 97 | TGParser Parser(SrcMgr, MacroNames, Records); |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 98 | |
| 99 | if (Parser.ParseFile()) |
| 100 | return 1; |
| 101 | |
Nico Weber | f7cf1a1 | 2018-12-19 13:35:53 +0000 | [diff] [blame] | 102 | // Write output to memory. |
| 103 | std::string OutString; |
| 104 | raw_string_ostream Out(OutString); |
| 105 | if (MainFn(Out, Records)) |
| 106 | return 1; |
| 107 | |
| 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. |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 112 | if (!DependFilename.empty()) { |
| 113 | if (int Ret = createDependencyFile(Parser, argv0)) |
| 114 | return Ret; |
| 115 | } |
Chandler Carruth | 029cea9 | 2018-05-07 23:41:48 +0000 | [diff] [blame] | 116 | |
Nico Weber | f7cf1a1 | 2018-12-19 13:35:53 +0000 | [diff] [blame] | 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 |
| 119 | // aren't any. |
| 120 | if (auto ExistingOrErr = MemoryBuffer::getFile(OutputFilename)) |
| 121 | if (std::move(ExistingOrErr.get())->getBuffer() == Out.str()) |
| 122 | return 0; |
| 123 | |
| 124 | std::error_code EC; |
| 125 | ToolOutputFile OutFile(OutputFilename, EC, sys::fs::F_Text); |
| 126 | if (EC) |
| 127 | return reportError(argv0, "error opening " + OutputFilename + ":" + |
| 128 | EC.message() + "\n"); |
| 129 | OutFile.os() << Out.str(); |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 130 | |
Davide Italiano | ebd5350 | 2016-12-05 22:58:01 +0000 | [diff] [blame] | 131 | if (ErrorsPrinted > 0) |
Benjamin Kramer | 3a13ed6 | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 132 | return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n"); |
Jakob Stoklund Olesen | a1214e7 | 2013-03-20 20:43:11 +0000 | [diff] [blame] | 133 | |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 134 | // Declare success. |
Nico Weber | f7cf1a1 | 2018-12-19 13:35:53 +0000 | [diff] [blame] | 135 | OutFile.keep(); |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 136 | return 0; |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 137 | } |