cmCTestHandlerCommand.cxx 9.9 KB

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