Main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 int reportError(const char *ProgName, Twine Msg) {
  46. errs() << ProgName << ": " << Msg;
  47. errs().flush();
  48. return 1;
  49. }
  50. /// Create a dependency file for `-d` option.
  51. ///
  52. /// This functionality is really only for the benefit of the build system.
  53. /// It is similar to GCC's `-M*` family of options.
  54. static int createDependencyFile(const TGParser &Parser, const char *argv0) {
  55. if (OutputFilename == "-")
  56. return reportError(argv0, "the option -d must be used together with -o\n");
  57. std::error_code EC;
  58. ToolOutputFile DepOut(DependFilename, EC, sys::fs::OF_Text);
  59. if (EC)
  60. return reportError(argv0, "error opening " + DependFilename + ":" +
  61. EC.message() + "\n");
  62. DepOut.os() << OutputFilename << ":";
  63. for (const auto &Dep : Parser.getDependencies()) {
  64. DepOut.os() << ' ' << Dep.first;
  65. }
  66. DepOut.os() << "\n";
  67. DepOut.keep();
  68. return 0;
  69. }
  70. int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
  71. RecordKeeper Records;
  72. // Parse the input file.
  73. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  74. MemoryBuffer::getFileOrSTDIN(InputFilename);
  75. if (std::error_code EC = FileOrErr.getError())
  76. return reportError(argv0, "Could not open input file '" + InputFilename +
  77. "': " + EC.message() + "\n");
  78. // Tell SrcMgr about this buffer, which is what TGParser will pick up.
  79. SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
  80. // Record the location of the include directory so that the lexer can find
  81. // it later.
  82. SrcMgr.setIncludeDirs(IncludeDirs);
  83. TGParser Parser(SrcMgr, MacroNames, Records);
  84. if (Parser.ParseFile())
  85. return 1;
  86. // Write output to memory.
  87. std::string OutString;
  88. raw_string_ostream Out(OutString);
  89. if (MainFn(Out, Records))
  90. return 1;
  91. // Always write the depfile, even if the main output hasn't changed.
  92. // If it's missing, Ninja considers the output dirty. If this was below
  93. // the early exit below and someone deleted the .inc.d file but not the .inc
  94. // file, tablegen would never write the depfile.
  95. if (!DependFilename.empty()) {
  96. if (int Ret = createDependencyFile(Parser, argv0))
  97. return Ret;
  98. }
  99. // Only updates the real output file if there are any differences.
  100. // This prevents recompilation of all the files depending on it if there
  101. // aren't any.
  102. if (auto ExistingOrErr = MemoryBuffer::getFile(OutputFilename))
  103. if (std::move(ExistingOrErr.get())->getBuffer() == Out.str())
  104. return 0;
  105. std::error_code EC;
  106. ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_Text);
  107. if (EC)
  108. return reportError(argv0, "error opening " + OutputFilename + ":" +
  109. EC.message() + "\n");
  110. OutFile.os() << Out.str();
  111. if (ErrorsPrinted > 0)
  112. return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");
  113. // Declare success.
  114. OutFile.keep();
  115. return 0;
  116. }