cmMessenger.cxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmMessenger.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmDocumentationFormatter.h"
  6. #include "cmState.h"
  7. #include "cmSystemTools.h"
  8. #if defined(CMAKE_BUILD_WITH_CMAKE)
  9. #include "cmsys/SystemInformation.hxx"
  10. #endif
  11. #include <sstream>
  12. cmake::MessageType cmMessenger::ConvertMessageType(cmake::MessageType t) const
  13. {
  14. bool warningsAsErrors;
  15. if (t == cmake::AUTHOR_WARNING || t == cmake::AUTHOR_ERROR) {
  16. warningsAsErrors = this->GetDevWarningsAsErrors();
  17. if (warningsAsErrors && t == cmake::AUTHOR_WARNING) {
  18. t = cmake::AUTHOR_ERROR;
  19. } else if (!warningsAsErrors && t == cmake::AUTHOR_ERROR) {
  20. t = cmake::AUTHOR_WARNING;
  21. }
  22. } else if (t == cmake::DEPRECATION_WARNING ||
  23. t == cmake::DEPRECATION_ERROR) {
  24. warningsAsErrors = this->GetDeprecatedWarningsAsErrors();
  25. if (warningsAsErrors && t == cmake::DEPRECATION_WARNING) {
  26. t = cmake::DEPRECATION_ERROR;
  27. } else if (!warningsAsErrors && t == cmake::DEPRECATION_ERROR) {
  28. t = cmake::DEPRECATION_WARNING;
  29. }
  30. }
  31. return t;
  32. }
  33. bool cmMessenger::IsMessageTypeVisible(cmake::MessageType t) const
  34. {
  35. bool isVisible = true;
  36. if (t == cmake::DEPRECATION_ERROR) {
  37. if (!this->GetDeprecatedWarningsAsErrors()) {
  38. isVisible = false;
  39. }
  40. } else if (t == cmake::DEPRECATION_WARNING) {
  41. if (this->GetSuppressDeprecatedWarnings()) {
  42. isVisible = false;
  43. }
  44. } else if (t == cmake::AUTHOR_ERROR) {
  45. if (!this->GetDevWarningsAsErrors()) {
  46. isVisible = false;
  47. }
  48. } else if (t == cmake::AUTHOR_WARNING) {
  49. if (this->GetSuppressDevWarnings()) {
  50. isVisible = false;
  51. }
  52. }
  53. return isVisible;
  54. }
  55. static bool printMessagePreamble(cmake::MessageType t, std::ostream& msg)
  56. {
  57. // Construct the message header.
  58. if (t == cmake::FATAL_ERROR) {
  59. msg << "CMake Error";
  60. } else if (t == cmake::INTERNAL_ERROR) {
  61. msg << "CMake Internal Error (please report a bug)";
  62. } else if (t == cmake::LOG) {
  63. msg << "CMake Debug Log";
  64. } else if (t == cmake::DEPRECATION_ERROR) {
  65. msg << "CMake Deprecation Error";
  66. } else if (t == cmake::DEPRECATION_WARNING) {
  67. msg << "CMake Deprecation Warning";
  68. } else if (t == cmake::AUTHOR_WARNING) {
  69. msg << "CMake Warning (dev)";
  70. } else if (t == cmake::AUTHOR_ERROR) {
  71. msg << "CMake Error (dev)";
  72. } else {
  73. msg << "CMake Warning";
  74. }
  75. return true;
  76. }
  77. void printMessageText(std::ostream& msg, std::string const& text)
  78. {
  79. msg << ":\n";
  80. cmDocumentationFormatter formatter;
  81. formatter.SetIndent(" ");
  82. formatter.PrintFormatted(msg, text.c_str());
  83. }
  84. void displayMessage(cmake::MessageType t, std::ostringstream& msg)
  85. {
  86. // Add a note about warning suppression.
  87. if (t == cmake::AUTHOR_WARNING) {
  88. msg << "This warning is for project developers. Use -Wno-dev to suppress "
  89. "it.";
  90. } else if (t == cmake::AUTHOR_ERROR) {
  91. msg << "This error is for project developers. Use -Wno-error=dev to "
  92. "suppress "
  93. "it.";
  94. }
  95. // Add a terminating blank line.
  96. msg << "\n";
  97. #if defined(CMAKE_BUILD_WITH_CMAKE)
  98. // Add a C++ stack trace to internal errors.
  99. if (t == cmake::INTERNAL_ERROR) {
  100. std::string stack = cmsys::SystemInformation::GetProgramStack(0, 0);
  101. if (!stack.empty()) {
  102. if (cmHasLiteralPrefix(stack, "WARNING:")) {
  103. stack = "Note:" + stack.substr(8);
  104. }
  105. msg << stack << "\n";
  106. }
  107. }
  108. #endif
  109. // Output the message.
  110. if (t == cmake::FATAL_ERROR || t == cmake::INTERNAL_ERROR ||
  111. t == cmake::DEPRECATION_ERROR || t == cmake::AUTHOR_ERROR) {
  112. cmSystemTools::SetErrorOccured();
  113. cmSystemTools::Message(msg.str().c_str(), "Error");
  114. } else {
  115. cmSystemTools::Message(msg.str().c_str(), "Warning");
  116. }
  117. }
  118. cmMessenger::cmMessenger(cmState* state)
  119. : State(state)
  120. {
  121. }
  122. void cmMessenger::IssueMessage(cmake::MessageType t, const std::string& text,
  123. const cmListFileBacktrace& backtrace) const
  124. {
  125. bool force = false;
  126. if (!force) {
  127. // override the message type, if needed, for warnings and errors
  128. cmake::MessageType override = this->ConvertMessageType(t);
  129. if (override != t) {
  130. t = override;
  131. force = true;
  132. }
  133. }
  134. if (!force && !this->IsMessageTypeVisible(t)) {
  135. return;
  136. }
  137. this->DisplayMessage(t, text, backtrace);
  138. }
  139. void cmMessenger::DisplayMessage(cmake::MessageType t, const std::string& text,
  140. const cmListFileBacktrace& backtrace) const
  141. {
  142. std::ostringstream msg;
  143. if (!printMessagePreamble(t, msg)) {
  144. return;
  145. }
  146. // Add the immediate context.
  147. backtrace.PrintTitle(msg);
  148. printMessageText(msg, text);
  149. // Add the rest of the context.
  150. backtrace.PrintCallStack(msg);
  151. displayMessage(t, msg);
  152. }
  153. bool cmMessenger::GetSuppressDevWarnings() const
  154. {
  155. const char* cacheEntryValue =
  156. this->State->GetCacheEntryValue("CMAKE_SUPPRESS_DEVELOPER_WARNINGS");
  157. return cmSystemTools::IsOn(cacheEntryValue);
  158. }
  159. bool cmMessenger::GetSuppressDeprecatedWarnings() const
  160. {
  161. const char* cacheEntryValue =
  162. this->State->GetCacheEntryValue("CMAKE_WARN_DEPRECATED");
  163. return cacheEntryValue && cmSystemTools::IsOff(cacheEntryValue);
  164. }
  165. bool cmMessenger::GetDevWarningsAsErrors() const
  166. {
  167. const char* cacheEntryValue =
  168. this->State->GetCacheEntryValue("CMAKE_SUPPRESS_DEVELOPER_ERRORS");
  169. return cacheEntryValue && cmSystemTools::IsOff(cacheEntryValue);
  170. }
  171. bool cmMessenger::GetDeprecatedWarningsAsErrors() const
  172. {
  173. const char* cacheEntryValue =
  174. this->State->GetCacheEntryValue("CMAKE_ERROR_DEPRECATED");
  175. return cmSystemTools::IsOn(cacheEntryValue);
  176. }