Main.cpp 5.4 KB

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