Main.cpp 5.5 KB

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