cmCTestHandlerCommand.cxx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "cmCTestHandlerCommand.h"
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <sstream>
  8. #include <cmext/string_view>
  9. #include "cmCTest.h"
  10. #include "cmCTestGenericHandler.h"
  11. #include "cmExecutionStatus.h"
  12. #include "cmMakefile.h"
  13. #include "cmMessageType.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. #include "cmValue.h"
  17. #include "cmWorkingDirectory.h"
  18. namespace {
  19. // class to save and restore the error state for ctest_* commands
  20. // if a ctest_* command has a CAPTURE_CMAKE_ERROR then put the error
  21. // state into there and restore the system wide error to what
  22. // it was before the command ran
  23. class SaveRestoreErrorState
  24. {
  25. public:
  26. SaveRestoreErrorState()
  27. {
  28. this->InitialErrorState = cmSystemTools::GetErrorOccuredFlag();
  29. cmSystemTools::ResetErrorOccuredFlag(); // rest the error state
  30. this->CaptureCMakeErrorValue = false;
  31. }
  32. // if the function has a CAPTURE_CMAKE_ERROR then we should restore
  33. // the error state to what it was before the function was run
  34. // if not then let the error state be what it is
  35. void CaptureCMakeError() { this->CaptureCMakeErrorValue = true; }
  36. ~SaveRestoreErrorState()
  37. {
  38. // if we are not saving the return value then make sure
  39. // if it was in error it goes back to being in error
  40. // otherwise leave it be what it is
  41. if (!this->CaptureCMakeErrorValue) {
  42. if (this->InitialErrorState) {
  43. cmSystemTools::SetErrorOccured();
  44. }
  45. return;
  46. }
  47. // if we have saved the error in a return variable
  48. // then put things back exactly like they were
  49. bool currentState = cmSystemTools::GetErrorOccuredFlag();
  50. // if the state changed during this command we need
  51. // to handle it, if not then nothing needs to be done
  52. if (currentState != this->InitialErrorState) {
  53. // restore the initial error state
  54. if (this->InitialErrorState) {
  55. cmSystemTools::SetErrorOccured();
  56. } else {
  57. cmSystemTools::ResetErrorOccuredFlag();
  58. }
  59. }
  60. }
  61. SaveRestoreErrorState(const SaveRestoreErrorState&) = delete;
  62. SaveRestoreErrorState& operator=(const SaveRestoreErrorState&) = delete;
  63. private:
  64. bool InitialErrorState;
  65. bool CaptureCMakeErrorValue;
  66. };
  67. }
  68. bool cmCTestHandlerCommand::InitialPass(std::vector<std::string> const& args,
  69. cmExecutionStatus& status)
  70. {
  71. // save error state and restore it if needed
  72. SaveRestoreErrorState errorState;
  73. // Allocate space for argument values.
  74. this->BindArguments();
  75. // Process input arguments.
  76. std::vector<std::string> unparsedArguments;
  77. std::vector<std::string> keywordsMissingValue;
  78. std::vector<std::string> parsedKeywords;
  79. this->Parse(args, &unparsedArguments, &keywordsMissingValue,
  80. &parsedKeywords);
  81. this->CheckArguments(keywordsMissingValue);
  82. std::sort(parsedKeywords.begin(), parsedKeywords.end());
  83. auto it = std::adjacent_find(parsedKeywords.begin(), parsedKeywords.end());
  84. if (it != parsedKeywords.end()) {
  85. this->Makefile->IssueMessage(
  86. MessageType::FATAL_ERROR,
  87. cmStrCat("Called with more than one value for ", *it));
  88. }
  89. bool const foundBadArgument = !unparsedArguments.empty();
  90. if (foundBadArgument) {
  91. this->SetError(cmStrCat("called with unknown argument \"",
  92. unparsedArguments.front(), "\"."));
  93. }
  94. bool const captureCMakeError = !this->CaptureCMakeError.empty();
  95. // now that arguments are parsed check to see if there is a
  96. // CAPTURE_CMAKE_ERROR specified let the errorState object know.
  97. if (captureCMakeError) {
  98. errorState.CaptureCMakeError();
  99. }
  100. // if we found a bad argument then exit before running command
  101. if (foundBadArgument) {
  102. // store the cmake error
  103. if (captureCMakeError) {
  104. this->Makefile->AddDefinition(this->CaptureCMakeError, "-1");
  105. std::string const err = this->GetName() + " " + status.GetError();
  106. if (!cmSystemTools::FindLastString(err.c_str(), "unknown error.")) {
  107. cmCTestLog(this->CTest, ERROR_MESSAGE, err << " error from command\n");
  108. }
  109. // return success because failure is recorded in CAPTURE_CMAKE_ERROR
  110. return true;
  111. }
  112. // return failure because of bad argument
  113. return false;
  114. }
  115. // Set the config type of this ctest to the current value of the
  116. // CTEST_CONFIGURATION_TYPE script variable if it is defined.
  117. // The current script value trumps the -C argument on the command
  118. // line.
  119. cmValue ctestConfigType =
  120. this->Makefile->GetDefinition("CTEST_CONFIGURATION_TYPE");
  121. if (ctestConfigType) {
  122. this->CTest->SetConfigType(*ctestConfigType);
  123. }
  124. if (!this->Build.empty()) {
  125. this->CTest->SetCTestConfiguration(
  126. "BuildDirectory", cmSystemTools::CollapseFullPath(this->Build),
  127. this->Quiet);
  128. } else {
  129. std::string const& bdir =
  130. this->Makefile->GetSafeDefinition("CTEST_BINARY_DIRECTORY");
  131. if (!bdir.empty()) {
  132. this->CTest->SetCTestConfiguration(
  133. "BuildDirectory", cmSystemTools::CollapseFullPath(bdir), this->Quiet);
  134. } else {
  135. cmCTestLog(this->CTest, ERROR_MESSAGE,
  136. "CTEST_BINARY_DIRECTORY not set" << std::endl;);
  137. }
  138. }
  139. if (!this->Source.empty()) {
  140. cmCTestLog(this->CTest, DEBUG,
  141. "Set source directory to: " << this->Source << std::endl);
  142. this->CTest->SetCTestConfiguration(
  143. "SourceDirectory", cmSystemTools::CollapseFullPath(this->Source),
  144. this->Quiet);
  145. } else {
  146. this->CTest->SetCTestConfiguration(
  147. "SourceDirectory",
  148. cmSystemTools::CollapseFullPath(
  149. this->Makefile->GetSafeDefinition("CTEST_SOURCE_DIRECTORY")),
  150. this->Quiet);
  151. }
  152. if (cmValue changeId = this->Makefile->GetDefinition("CTEST_CHANGE_ID")) {
  153. this->CTest->SetCTestConfiguration("ChangeId", *changeId, this->Quiet);
  154. }
  155. cmCTestLog(this->CTest, DEBUG, "Initialize handler" << std::endl;);
  156. cmCTestGenericHandler* handler = this->InitializeHandler();
  157. if (!handler) {
  158. cmCTestLog(this->CTest, ERROR_MESSAGE,
  159. "Cannot instantiate test handler " << this->GetName()
  160. << std::endl);
  161. if (captureCMakeError) {
  162. this->Makefile->AddDefinition(this->CaptureCMakeError, "-1");
  163. std::string const& err = status.GetError();
  164. if (!cmSystemTools::FindLastString(err.c_str(), "unknown error.")) {
  165. cmCTestLog(this->CTest, ERROR_MESSAGE, err << " error from command\n");
  166. }
  167. return true;
  168. }
  169. return false;
  170. }
  171. handler->SetAppendXML(this->Append);
  172. handler->PopulateCustomVectors(this->Makefile);
  173. if (!this->SubmitIndex.empty()) {
  174. handler->SetSubmitIndex(atoi(this->SubmitIndex.c_str()));
  175. }
  176. cmWorkingDirectory workdir(
  177. this->CTest->GetCTestConfiguration("BuildDirectory"));
  178. if (workdir.Failed()) {
  179. this->SetError("failed to change directory to " +
  180. this->CTest->GetCTestConfiguration("BuildDirectory") +
  181. " : " + std::strerror(workdir.GetLastResult()));
  182. if (captureCMakeError) {
  183. this->Makefile->AddDefinition(this->CaptureCMakeError, "-1");
  184. cmCTestLog(this->CTest, ERROR_MESSAGE,
  185. this->GetName() << " " << status.GetError() << "\n");
  186. // return success because failure is recorded in CAPTURE_CMAKE_ERROR
  187. return true;
  188. }
  189. return false;
  190. }
  191. int res = handler->ProcessHandler();
  192. if (!this->ReturnValue.empty()) {
  193. this->Makefile->AddDefinition(this->ReturnValue, std::to_string(res));
  194. }
  195. this->ProcessAdditionalValues(handler);
  196. // log the error message if there was an error
  197. if (captureCMakeError) {
  198. const char* returnString = "0";
  199. if (cmSystemTools::GetErrorOccuredFlag()) {
  200. returnString = "-1";
  201. std::string const& err = status.GetError();
  202. // print out the error if it is not "unknown error" which means
  203. // there was no message
  204. if (!cmSystemTools::FindLastString(err.c_str(), "unknown error.")) {
  205. cmCTestLog(this->CTest, ERROR_MESSAGE, err);
  206. }
  207. }
  208. // store the captured cmake error state 0 or -1
  209. this->Makefile->AddDefinition(this->CaptureCMakeError, returnString);
  210. }
  211. return true;
  212. }
  213. void cmCTestHandlerCommand::ProcessAdditionalValues(cmCTestGenericHandler*)
  214. {
  215. }
  216. void cmCTestHandlerCommand::BindArguments()
  217. {
  218. this->Bind("APPEND"_s, this->Append);
  219. this->Bind("QUIET"_s, this->Quiet);
  220. this->Bind("RETURN_VALUE"_s, this->ReturnValue);
  221. this->Bind("CAPTURE_CMAKE_ERROR"_s, this->CaptureCMakeError);
  222. this->Bind("SOURCE"_s, this->Source);
  223. this->Bind("BUILD"_s, this->Build);
  224. this->Bind("SUBMIT_INDEX"_s, this->SubmitIndex);
  225. }
  226. void cmCTestHandlerCommand::CheckArguments(std::vector<std::string> const&)
  227. {
  228. }