cmCTestGenericHandler.cxx 5.1 KB

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