cmCTestGenericHandler.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "cmCTestGenericHandler.h"
  4. #include <sstream>
  5. #include <utility>
  6. #include "cmCTest.h"
  7. #include "cmStringAlgorithms.h"
  8. #include "cmSystemTools.h"
  9. cmCTestGenericHandler::cmCTestGenericHandler()
  10. {
  11. this->HandlerVerbose = cmSystemTools::OUTPUT_NONE;
  12. this->CTest = nullptr;
  13. this->SubmitIndex = 0;
  14. this->AppendXML = false;
  15. this->Quiet = false;
  16. this->TestLoad = 0;
  17. }
  18. cmCTestGenericHandler::~cmCTestGenericHandler() = default;
  19. /* Modify the given `map`, setting key `op` to `value` if `value`
  20. * is non-null, otherwise removing key `op` (if it exists).
  21. */
  22. static void SetMapValue(cmCTestGenericHandler::t_StringToString& map,
  23. const std::string& op, const char* value)
  24. {
  25. if (!value) {
  26. map.erase(op);
  27. return;
  28. }
  29. map[op] = value;
  30. }
  31. void cmCTestGenericHandler::SetOption(const std::string& op, const char* value)
  32. {
  33. SetMapValue(this->Options, op, value);
  34. }
  35. void cmCTestGenericHandler::SetPersistentOption(const std::string& op,
  36. const char* value)
  37. {
  38. this->SetOption(op, value);
  39. SetMapValue(this->PersistentOptions, op, value);
  40. }
  41. void cmCTestGenericHandler::AddMultiOption(const std::string& op,
  42. const std::string& value)
  43. {
  44. if (!value.empty()) {
  45. this->MultiOptions[op].emplace_back(value);
  46. }
  47. }
  48. void cmCTestGenericHandler::AddPersistentMultiOption(const std::string& op,
  49. const std::string& value)
  50. {
  51. if (!value.empty()) {
  52. this->MultiOptions[op].emplace_back(value);
  53. this->PersistentMultiOptions[op].emplace_back(value);
  54. }
  55. }
  56. void cmCTestGenericHandler::Initialize()
  57. {
  58. this->AppendXML = false;
  59. this->TestLoad = 0;
  60. this->Options.clear();
  61. for (auto const& po : this->PersistentOptions) {
  62. this->Options[po.first] = po.second;
  63. }
  64. }
  65. const char* cmCTestGenericHandler::GetOption(const std::string& op)
  66. {
  67. auto remit = this->Options.find(op);
  68. if (remit == this->Options.end()) {
  69. return nullptr;
  70. }
  71. return remit->second.c_str();
  72. }
  73. std::vector<std::string> cmCTestGenericHandler::GetMultiOption(
  74. const std::string& optionName) const
  75. {
  76. // Avoid inserting a key, which MultiOptions[op] would do.
  77. auto remit = this->MultiOptions.find(optionName);
  78. if (remit == this->MultiOptions.end()) {
  79. return {};
  80. }
  81. return remit->second;
  82. }
  83. bool cmCTestGenericHandler::StartResultingXML(cmCTest::Part part,
  84. const char* name,
  85. cmGeneratedFileStream& xofs)
  86. {
  87. if (!name) {
  88. cmCTestLog(this->CTest, ERROR_MESSAGE,
  89. "Cannot create resulting XML file without providing the name"
  90. << std::endl;);
  91. return false;
  92. }
  93. std::ostringstream ostr;
  94. ostr << name;
  95. if (this->SubmitIndex > 0) {
  96. ostr << "_" << this->SubmitIndex;
  97. }
  98. ostr << ".xml";
  99. if (this->CTest->GetCurrentTag().empty()) {
  100. cmCTestLog(this->CTest, ERROR_MESSAGE,
  101. "Current Tag empty, this may mean NightlyStartTime / "
  102. "CTEST_NIGHTLY_START_TIME was not set correctly. Or "
  103. "maybe you forgot to call ctest_start() before calling "
  104. "ctest_configure()."
  105. << std::endl);
  106. cmSystemTools::SetFatalErrorOccured();
  107. return false;
  108. }
  109. if (!this->CTest->OpenOutputFile(this->CTest->GetCurrentTag(), ostr.str(),
  110. xofs, true)) {
  111. cmCTestLog(this->CTest, ERROR_MESSAGE,
  112. "Cannot create resulting XML file: " << ostr.str()
  113. << std::endl);
  114. return false;
  115. }
  116. this->CTest->AddSubmitFile(part, ostr.str());
  117. return true;
  118. }
  119. bool cmCTestGenericHandler::StartLogFile(const char* name,
  120. cmGeneratedFileStream& xofs)
  121. {
  122. if (!name) {
  123. cmCTestLog(this->CTest, ERROR_MESSAGE,
  124. "Cannot create log file without providing the name"
  125. << std::endl;);
  126. return false;
  127. }
  128. std::ostringstream ostr;
  129. ostr << "Last" << name;
  130. if (this->SubmitIndex > 0) {
  131. ostr << "_" << this->SubmitIndex;
  132. }
  133. if (!this->CTest->GetCurrentTag().empty()) {
  134. ostr << "_" << this->CTest->GetCurrentTag();
  135. }
  136. ostr << ".log";
  137. this->LogFileNames[name] =
  138. cmStrCat(this->CTest->GetBinaryDir(), "/Testing/Temporary/", ostr.str());
  139. if (!this->CTest->OpenOutputFile("Temporary", ostr.str(), xofs)) {
  140. cmCTestLog(this->CTest, ERROR_MESSAGE,
  141. "Cannot create log file: " << ostr.str() << std::endl);
  142. return false;
  143. }
  144. return true;
  145. }