cmCTestHandlerCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 "cmCTest.h"
  5. #include "cmCTestGenericHandler.h"
  6. #include "cmMakefile.h"
  7. #include "cmSystemTools.h"
  8. #include "cmWorkingDirectory.h"
  9. #include "cmake.h"
  10. #include <cstring>
  11. #include <sstream>
  12. #include <stdlib.h>
  13. class cmExecutionStatus;
  14. cmCTestHandlerCommand::cmCTestHandlerCommand()
  15. {
  16. const size_t INIT_SIZE = 100;
  17. size_t cc;
  18. this->Arguments.reserve(INIT_SIZE);
  19. for (cc = 0; cc < INIT_SIZE; ++cc) {
  20. this->Arguments.push_back(nullptr);
  21. }
  22. this->Arguments[ct_RETURN_VALUE] = "RETURN_VALUE";
  23. this->Arguments[ct_CAPTURE_CMAKE_ERROR] = "CAPTURE_CMAKE_ERROR";
  24. this->Arguments[ct_SOURCE] = "SOURCE";
  25. this->Arguments[ct_BUILD] = "BUILD";
  26. this->Arguments[ct_SUBMIT_INDEX] = "SUBMIT_INDEX";
  27. this->Last = ct_LAST;
  28. this->AppendXML = false;
  29. this->Quiet = false;
  30. }
  31. namespace {
  32. // class to save and restore the error state for ctest_* commands
  33. // if a ctest_* command has a CAPTURE_CMAKE_ERROR then put the error
  34. // state into there and restore the system wide error to what
  35. // it was before the command ran
  36. class SaveRestoreErrorState
  37. {
  38. public:
  39. SaveRestoreErrorState()
  40. {
  41. this->InitialErrorState = cmSystemTools::GetErrorOccuredFlag();
  42. cmSystemTools::ResetErrorOccuredFlag(); // rest the error state
  43. this->CaptureCMakeErrorValue = false;
  44. }
  45. // if the function has a CAPTURE_CMAKE_ERROR then we should restore
  46. // the error state to what it was before the function was run
  47. // if not then let the error state be what it is
  48. void CaptureCMakeError() { this->CaptureCMakeErrorValue = true; }
  49. ~SaveRestoreErrorState()
  50. {
  51. // if we are not saving the return value then make sure
  52. // if it was in error it goes back to being in error
  53. // otherwise leave it be what it is
  54. if (!this->CaptureCMakeErrorValue) {
  55. if (this->InitialErrorState) {
  56. cmSystemTools::SetErrorOccured();
  57. }
  58. return;
  59. }
  60. // if we have saved the error in a return variable
  61. // then put things back exactly like they were
  62. bool currentState = cmSystemTools::GetErrorOccuredFlag();
  63. // if the state changed during this command we need
  64. // to handle it, if not then nothing needs to be done
  65. if (currentState != this->InitialErrorState) {
  66. // restore the initial error state
  67. if (this->InitialErrorState) {
  68. cmSystemTools::SetErrorOccured();
  69. } else {
  70. cmSystemTools::ResetErrorOccuredFlag();
  71. }
  72. }
  73. }
  74. private:
  75. bool InitialErrorState;
  76. bool CaptureCMakeErrorValue;
  77. };
  78. }
  79. bool cmCTestHandlerCommand::InitialPass(std::vector<std::string> const& args,
  80. cmExecutionStatus& /*unused*/)
  81. {
  82. // save error state and restore it if needed
  83. SaveRestoreErrorState errorState;
  84. // Allocate space for argument values.
  85. this->Values.clear();
  86. this->Values.resize(this->Last, nullptr);
  87. // Process input arguments.
  88. this->ArgumentDoing = ArgumentDoingNone;
  89. // look at all arguments and do not short circuit on the first
  90. // bad one so that CAPTURE_CMAKE_ERROR can override setting the
  91. // global error state
  92. bool foundBadArgument = false;
  93. for (std::string const& arg : args) {
  94. // Check this argument.
  95. if (!this->CheckArgumentKeyword(arg) && !this->CheckArgumentValue(arg)) {
  96. std::ostringstream e;
  97. e << "called with unknown argument \"" << arg << "\".";
  98. this->SetError(e.str());
  99. foundBadArgument = true;
  100. }
  101. // note bad argument
  102. if (this->ArgumentDoing == ArgumentDoingError) {
  103. foundBadArgument = true;
  104. }
  105. }
  106. bool capureCMakeError = (this->Values[ct_CAPTURE_CMAKE_ERROR] &&
  107. *this->Values[ct_CAPTURE_CMAKE_ERROR]);
  108. // now that arguments are parsed check to see if there is a
  109. // CAPTURE_CMAKE_ERROR specified let the errorState object know.
  110. if (capureCMakeError) {
  111. errorState.CaptureCMakeError();
  112. }
  113. // if we found a bad argument then exit before running command
  114. if (foundBadArgument) {
  115. // store the cmake error
  116. if (capureCMakeError) {
  117. this->Makefile->AddDefinition(this->Values[ct_CAPTURE_CMAKE_ERROR],
  118. "-1");
  119. std::string const err = this->GetName() + " " + this->GetError();
  120. if (!cmSystemTools::FindLastString(err.c_str(), "unknown error.")) {
  121. cmCTestLog(this->CTest, ERROR_MESSAGE, err << " error from command\n");
  122. }
  123. // return success because failure is recorded in CAPTURE_CMAKE_ERROR
  124. return true;
  125. }
  126. // return failure because of bad argument
  127. return false;
  128. }
  129. // Set the config type of this ctest to the current value of the
  130. // CTEST_CONFIGURATION_TYPE script variable if it is defined.
  131. // The current script value trumps the -C argument on the command
  132. // line.
  133. const char* ctestConfigType =
  134. this->Makefile->GetDefinition("CTEST_CONFIGURATION_TYPE");
  135. if (ctestConfigType) {
  136. this->CTest->SetConfigType(ctestConfigType);
  137. }
  138. if (this->Values[ct_BUILD]) {
  139. this->CTest->SetCTestConfiguration(
  140. "BuildDirectory",
  141. cmSystemTools::CollapseFullPath(this->Values[ct_BUILD]).c_str(),
  142. this->Quiet);
  143. } else {
  144. std::string const& bdir =
  145. this->Makefile->GetSafeDefinition("CTEST_BINARY_DIRECTORY");
  146. if (!bdir.empty()) {
  147. this->CTest->SetCTestConfiguration(
  148. "BuildDirectory", cmSystemTools::CollapseFullPath(bdir).c_str(),
  149. this->Quiet);
  150. } else {
  151. cmCTestLog(this->CTest, ERROR_MESSAGE,
  152. "CTEST_BINARY_DIRECTORY not set" << std::endl;);
  153. }
  154. }
  155. if (this->Values[ct_SOURCE]) {
  156. cmCTestLog(this->CTest, DEBUG,
  157. "Set source directory to: " << this->Values[ct_SOURCE]
  158. << std::endl);
  159. this->CTest->SetCTestConfiguration(
  160. "SourceDirectory",
  161. cmSystemTools::CollapseFullPath(this->Values[ct_SOURCE]).c_str(),
  162. this->Quiet);
  163. } else {
  164. this->CTest->SetCTestConfiguration(
  165. "SourceDirectory",
  166. cmSystemTools::CollapseFullPath(
  167. this->Makefile->GetSafeDefinition("CTEST_SOURCE_DIRECTORY"))
  168. .c_str(),
  169. this->Quiet);
  170. }
  171. if (const char* changeId =
  172. this->Makefile->GetDefinition("CTEST_CHANGE_ID")) {
  173. this->CTest->SetCTestConfiguration("ChangeId", changeId, this->Quiet);
  174. }
  175. cmCTestLog(this->CTest, DEBUG, "Initialize handler" << std::endl;);
  176. cmCTestGenericHandler* handler = this->InitializeHandler();
  177. if (!handler) {
  178. cmCTestLog(this->CTest, ERROR_MESSAGE,
  179. "Cannot instantiate test handler " << this->GetName()
  180. << std::endl);
  181. if (capureCMakeError) {
  182. this->Makefile->AddDefinition(this->Values[ct_CAPTURE_CMAKE_ERROR],
  183. "-1");
  184. const char* err = this->GetError();
  185. if (err && !cmSystemTools::FindLastString(err, "unknown error.")) {
  186. cmCTestLog(this->CTest, ERROR_MESSAGE, err << " error from command\n");
  187. }
  188. return true;
  189. }
  190. return false;
  191. }
  192. handler->SetAppendXML(this->AppendXML);
  193. handler->PopulateCustomVectors(this->Makefile);
  194. if (this->Values[ct_SUBMIT_INDEX]) {
  195. if (!this->CTest->GetDropSiteCDash() &&
  196. this->CTest->GetDartVersion() <= 1) {
  197. cmCTestLog(
  198. this->CTest, ERROR_MESSAGE,
  199. "Dart before version 2.0 does not support collecting submissions."
  200. << std::endl
  201. << "Please upgrade the server to Dart 2 or higher, or do not use "
  202. "SUBMIT_INDEX."
  203. << std::endl);
  204. } else {
  205. handler->SetSubmitIndex(atoi(this->Values[ct_SUBMIT_INDEX]));
  206. }
  207. }
  208. cmWorkingDirectory workdir(
  209. this->CTest->GetCTestConfiguration("BuildDirectory"));
  210. if (workdir.Failed()) {
  211. this->SetError("failed to change directory to " +
  212. this->CTest->GetCTestConfiguration("BuildDirectory") +
  213. " : " + std::strerror(workdir.GetLastResult()));
  214. if (capureCMakeError) {
  215. this->Makefile->AddDefinition(this->Values[ct_CAPTURE_CMAKE_ERROR],
  216. "-1");
  217. cmCTestLog(this->CTest, ERROR_MESSAGE,
  218. this->GetName() << " " << this->GetError() << "\n");
  219. // return success because failure is recorded in CAPTURE_CMAKE_ERROR
  220. return true;
  221. }
  222. return false;
  223. }
  224. int res = handler->ProcessHandler();
  225. if (this->Values[ct_RETURN_VALUE] && *this->Values[ct_RETURN_VALUE]) {
  226. std::ostringstream str;
  227. str << res;
  228. this->Makefile->AddDefinition(this->Values[ct_RETURN_VALUE],
  229. str.str().c_str());
  230. }
  231. this->ProcessAdditionalValues(handler);
  232. // log the error message if there was an error
  233. if (capureCMakeError) {
  234. const char* returnString = "0";
  235. if (cmSystemTools::GetErrorOccuredFlag()) {
  236. returnString = "-1";
  237. const char* err = this->GetError();
  238. // print out the error if it is not "unknown error" which means
  239. // there was no message
  240. if (err && !cmSystemTools::FindLastString(err, "unknown error.")) {
  241. cmCTestLog(this->CTest, ERROR_MESSAGE, err);
  242. }
  243. }
  244. // store the captured cmake error state 0 or -1
  245. this->Makefile->AddDefinition(this->Values[ct_CAPTURE_CMAKE_ERROR],
  246. returnString);
  247. }
  248. return true;
  249. }
  250. void cmCTestHandlerCommand::ProcessAdditionalValues(cmCTestGenericHandler*)
  251. {
  252. }
  253. bool cmCTestHandlerCommand::CheckArgumentKeyword(std::string const& arg)
  254. {
  255. // Look for non-value arguments common to all commands.
  256. if (arg == "APPEND") {
  257. this->ArgumentDoing = ArgumentDoingNone;
  258. this->AppendXML = true;
  259. return true;
  260. }
  261. if (arg == "QUIET") {
  262. this->ArgumentDoing = ArgumentDoingNone;
  263. this->Quiet = true;
  264. return true;
  265. }
  266. // Check for a keyword in our argument/value table.
  267. for (unsigned int k = 0; k < this->Arguments.size(); ++k) {
  268. if (this->Arguments[k] && arg == this->Arguments[k]) {
  269. this->ArgumentDoing = ArgumentDoingKeyword;
  270. this->ArgumentIndex = k;
  271. return true;
  272. }
  273. }
  274. return false;
  275. }
  276. bool cmCTestHandlerCommand::CheckArgumentValue(std::string const& arg)
  277. {
  278. if (this->ArgumentDoing == ArgumentDoingKeyword) {
  279. this->ArgumentDoing = ArgumentDoingNone;
  280. unsigned int k = this->ArgumentIndex;
  281. if (this->Values[k]) {
  282. std::ostringstream e;
  283. e << "Called with more than one value for " << this->Arguments[k];
  284. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  285. this->ArgumentDoing = ArgumentDoingError;
  286. return true;
  287. }
  288. this->Values[k] = arg.c_str();
  289. cmCTestLog(this->CTest, DEBUG,
  290. "Set " << this->Arguments[k] << " to " << arg << "\n");
  291. return true;
  292. }
  293. return false;
  294. }