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 "llvm/TableGen/Main.h" 19 #include "TGParser.h" 20 #include "llvm/ADT/StringExtras.h" 21 #include "llvm/Support/CommandLine.h" 22 #include "llvm/Support/FileSystem.h" 23 #include "llvm/Support/MemoryBuffer.h" 24 #include "llvm/Support/ToolOutputFile.h" 25 #include "llvm/TableGen/Error.h" 26 #include "llvm/TableGen/Record.h" 27 #include <algorithm> 28 #include <cstdio> 29 #include <system_error> 30 using namespace llvm; 31 32 static cl::opt<std::string> 33 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), 34 cl::init("-")); 35 36 static cl::opt<std::string> 37 DependFilename("d", 38 cl::desc("Dependency filename"), 39 cl::value_desc("filename"), 40 cl::init("")); 41 42 static cl::opt<std::string> 43 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); 44 45 static cl::list<std::string> 46 IncludeDirs("I", cl::desc("Directory of include files"), 47 cl::value_desc("directory"), cl::Prefix); 48 49 static cl::list<std::string> 50 MacroNames("D", cl::desc("Name of the macro to be defined"), 51 cl::value_desc("macro name"), cl::Prefix); 52 53 static int reportError(const char *ProgName, Twine Msg) { 54 errs() << ProgName << ": " << Msg; 55 errs().flush(); 56 return 1; 57 } 58 59 /// Create a dependency file for `-d` option. 60 /// 61 /// This functionality is really only for the benefit of the build system. 62 /// It is similar to GCC's `-M*` family of options. 63 static int createDependencyFile(const TGParser &Parser, const char *argv0) { 64 if (OutputFilename == "-") 65 return reportError(argv0, "the option -d must be used together with -o\n"); 66 67 std::error_code EC; 68 ToolOutputFile DepOut(DependFilename, EC, sys::fs::F_Text); 69 if (EC) 70 return reportError(argv0, "error opening " + DependFilename + ":" + 71 EC.message() + "\n"); 72 DepOut.os() << OutputFilename << ":"; 73 for (const auto &Dep : Parser.getDependencies()) { 74 DepOut.os() << ' ' << Dep.first; 75 } 76 DepOut.os() << "\n"; 77 DepOut.keep(); 78 return 0; 79 } 80 81 int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) { 82 RecordKeeper Records; 83 84 // Parse the input file. 85 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 86 MemoryBuffer::getFileOrSTDIN(InputFilename); 87 if (std::error_code EC = FileOrErr.getError()) 88 return reportError(argv0, "Could not open input file '" + InputFilename + 89 "': " + EC.message() + "\n"); 90 91 // Tell SrcMgr about this buffer, which is what TGParser will pick up. 92 SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc()); 93 94 // Record the location of the include directory so that the lexer can find 95 // it later. 96 SrcMgr.setIncludeDirs(IncludeDirs); 97 98 TGParser Parser(SrcMgr, MacroNames, Records); 99 100 if (Parser.ParseFile()) 101 return 1; 102 103 // Write output to memory. 104 std::string OutString; 105 raw_string_ostream Out(OutString); 106 if (MainFn(Out, Records)) 107 return 1; 108 109 // Always write the depfile, even if the main output hasn't changed. 110 // If it's missing, Ninja considers the output dirty. If this was below 111 // the early exit below and someone deleted the .inc.d file but not the .inc 112 // file, tablegen would never write the depfile. 113 if (!DependFilename.empty()) { 114 if (int Ret = createDependencyFile(Parser, argv0)) 115 return Ret; 116 } 117 118 // Only updates the real output file if there are any differences. 119 // This prevents recompilation of all the files depending on it if there 120 // aren't any. 121 if (auto ExistingOrErr = MemoryBuffer::getFile(OutputFilename)) 122 if (std::move(ExistingOrErr.get())->getBuffer() == Out.str()) 123 return 0; 124 125 std::error_code EC; 126 ToolOutputFile OutFile(OutputFilename, EC, sys::fs::F_Text); 127 if (EC) 128 return reportError(argv0, "error opening " + OutputFilename + ":" + 129 EC.message() + "\n"); 130 OutFile.os() << Out.str(); 131 132 if (ErrorsPrinted > 0) 133 return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n"); 134 135 // Declare success. 136 OutFile.keep(); 137 return 0; 138 } 139