cmCTestBuildAndTestHandler.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCTestBuildAndTestHandler.h"
  14. #include "cmSystemTools.h"
  15. #include "cmCTest.h"
  16. #include "cmake.h"
  17. #include "cmGlobalGenerator.h"
  18. #include <cmsys/Process.h>
  19. #include "cmCTestTestHandler.h"
  20. //----------------------------------------------------------------------
  21. cmCTestBuildAndTestHandler::cmCTestBuildAndTestHandler()
  22. {
  23. this->BuildTwoConfig = false;
  24. this->BuildNoClean = false;
  25. this->BuildNoCMake = false;
  26. this->Timeout = 0;
  27. }
  28. //----------------------------------------------------------------------
  29. void cmCTestBuildAndTestHandler::Initialize()
  30. {
  31. this->BuildTargets.erase(
  32. this->BuildTargets.begin(), this->BuildTargets.end());
  33. this->Superclass::Initialize();
  34. }
  35. //----------------------------------------------------------------------
  36. const char* cmCTestBuildAndTestHandler::GetOutput()
  37. {
  38. return this->Output.c_str();
  39. }
  40. //----------------------------------------------------------------------
  41. int cmCTestBuildAndTestHandler::ProcessHandler()
  42. {
  43. this->Output = "";
  44. std::string output;
  45. cmSystemTools::ResetErrorOccuredFlag();
  46. int retv = this->RunCMakeAndTest(&this->Output);
  47. cmSystemTools::ResetErrorOccuredFlag();
  48. return retv;
  49. }
  50. //----------------------------------------------------------------------
  51. int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring,
  52. cmOStringStream &out, std::string &cmakeOutString, std::string &cwd,
  53. cmake *cm)
  54. {
  55. unsigned int k;
  56. std::vector<std::string> args;
  57. args.push_back(this->CTest->GetCMakeExecutable());
  58. args.push_back(this->SourceDir);
  59. if(this->BuildGenerator.size())
  60. {
  61. std::string generator = "-G";
  62. generator += this->BuildGenerator;
  63. args.push_back(generator);
  64. }
  65. if ( this->CTest->GetConfigType().size() > 0 )
  66. {
  67. std::string btype
  68. = "-DCMAKE_BUILD_TYPE:STRING=" + this->CTest->GetConfigType();
  69. args.push_back(btype);
  70. }
  71. for(k=0; k < this->BuildOptions.size(); ++k)
  72. {
  73. args.push_back(this->BuildOptions[k]);
  74. }
  75. if (cm->Run(args) != 0)
  76. {
  77. out << "Error: cmake execution failed\n";
  78. out << cmakeOutString << "\n";
  79. // return to the original directory
  80. cmSystemTools::ChangeDirectory(cwd.c_str());
  81. if(outstring)
  82. {
  83. *outstring = out.str();
  84. }
  85. else
  86. {
  87. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
  88. }
  89. return 1;
  90. }
  91. // do another config?
  92. if(this->BuildTwoConfig)
  93. {
  94. if (cm->Run(args) != 0)
  95. {
  96. out << "Error: cmake execution failed\n";
  97. out << cmakeOutString << "\n";
  98. // return to the original directory
  99. cmSystemTools::ChangeDirectory(cwd.c_str());
  100. if(outstring)
  101. {
  102. *outstring = out.str();
  103. }
  104. else
  105. {
  106. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
  107. }
  108. return 1;
  109. }
  110. }
  111. out << "======== CMake output ======\n";
  112. out << cmakeOutString;
  113. out << "======== End CMake output ======\n";
  114. return 0;
  115. }
  116. //----------------------------------------------------------------------
  117. void CMakeMessageCallback(const char* m, const char*, bool&, void* s)
  118. {
  119. std::string* out = (std::string*)s;
  120. *out += m;
  121. *out += "\n";
  122. }
  123. //----------------------------------------------------------------------
  124. void CMakeStdoutCallback(const char* m, int len, void* s)
  125. {
  126. std::string* out = (std::string*)s;
  127. out->append(m, len);
  128. }
  129. //----------------------------------------------------------------------
  130. int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
  131. {
  132. unsigned int k;
  133. std::string cmakeOutString;
  134. cmSystemTools::SetErrorCallback(CMakeMessageCallback, &cmakeOutString);
  135. cmSystemTools::SetStdoutCallback(CMakeStdoutCallback, &cmakeOutString);
  136. cmOStringStream out;
  137. // if the generator and make program are not specified then it is an error
  138. if (!this->BuildGenerator.size() || !this->BuildMakeProgram.size())
  139. {
  140. if(outstring)
  141. {
  142. *outstring =
  143. "--build-and-test requires that both the generator and makeprogram "
  144. "be provided using the --build-generator and --build-makeprogram "
  145. "command line options. ";
  146. }
  147. return 1;
  148. }
  149. if ( this->CTest->GetConfigType().size() == 0 &&
  150. this->ConfigSample.size())
  151. {
  152. // use the config sample to set the ConfigType
  153. std::string fullPath;
  154. std::string resultingConfig;
  155. std::vector<std::string> extraPaths;
  156. std::vector<std::string> failed;
  157. fullPath =
  158. cmCTestTestHandler::FindExecutable(this->CTest,
  159. this->ConfigSample.c_str(),
  160. resultingConfig,
  161. extraPaths,
  162. failed);
  163. if (fullPath.size() && resultingConfig.size())
  164. {
  165. this->CTest->SetConfigType(resultingConfig.c_str());
  166. }
  167. out << "Using config sample with results: "
  168. << fullPath << " and " << resultingConfig << std::endl;
  169. }
  170. // we need to honor the timeout specified, the timeout include cmake, build
  171. // and test time
  172. double clock_start = cmSystemTools::GetTime();
  173. // make sure the binary dir is there
  174. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  175. out << "Internal cmake changing into directory: "
  176. << this->BinaryDir << std::endl;
  177. if (!cmSystemTools::FileIsDirectory(this->BinaryDir.c_str()))
  178. {
  179. cmSystemTools::MakeDirectory(this->BinaryDir.c_str());
  180. }
  181. cmSystemTools::ChangeDirectory(this->BinaryDir.c_str());
  182. // should we cmake?
  183. cmake cm;
  184. cm.SetGlobalGenerator(cm.CreateGlobalGenerator(
  185. this->BuildGenerator.c_str()));
  186. if(!this->BuildNoCMake)
  187. {
  188. // do the cmake step, no timeout here since it is not a sub process
  189. if (this->RunCMake(outstring,out,cmakeOutString,cwd,&cm))
  190. {
  191. return 1;
  192. }
  193. }
  194. // do the build
  195. std::vector<std::string>::iterator tarIt;
  196. if ( this->BuildTargets.size() == 0 )
  197. {
  198. this->BuildTargets.push_back("");
  199. }
  200. for ( tarIt = this->BuildTargets.begin(); tarIt != this->BuildTargets.end();
  201. ++ tarIt )
  202. {
  203. double remainingTime = 0;
  204. if (this->Timeout)
  205. {
  206. remainingTime = this->Timeout - cmSystemTools::GetTime() + clock_start;
  207. if (remainingTime <= 0)
  208. {
  209. if(outstring)
  210. {
  211. *outstring = "--build-and-test timeout exceeded. ";
  212. }
  213. return 1;
  214. }
  215. }
  216. std::string output;
  217. int retVal = cm.GetGlobalGenerator()->Build(
  218. this->SourceDir.c_str(), this->BinaryDir.c_str(),
  219. this->BuildProject.c_str(), tarIt->c_str(),
  220. &output, this->BuildMakeProgram.c_str(),
  221. this->CTest->GetConfigType().c_str(),
  222. !this->BuildNoClean,
  223. false, remainingTime);
  224. out << output;
  225. // if the build failed then return
  226. if (retVal)
  227. {
  228. if(outstring)
  229. {
  230. *outstring = out.str();
  231. }
  232. return 1;
  233. }
  234. }
  235. if(outstring)
  236. {
  237. *outstring = out.str();
  238. }
  239. // if no test was specified then we are done
  240. if (!this->TestCommand.size())
  241. {
  242. return 0;
  243. }
  244. // now run the compiled test if we can find it
  245. // store the final location in fullPath
  246. std::string fullPath;
  247. std::string resultingConfig;
  248. std::vector<std::string> extraPaths;
  249. // if this->ExecutableDirectory is set try that as well
  250. if (this->ExecutableDirectory.size())
  251. {
  252. std::string tempPath = this->ExecutableDirectory;
  253. tempPath += "/";
  254. tempPath += this->TestCommand;
  255. extraPaths.push_back(tempPath);
  256. }
  257. std::vector<std::string> failed;
  258. fullPath =
  259. cmCTestTestHandler::FindExecutable(this->CTest,
  260. this->TestCommand.c_str(),
  261. resultingConfig,
  262. extraPaths,
  263. failed);
  264. if(!cmSystemTools::FileExists(fullPath.c_str()))
  265. {
  266. out << "Could not find path to executable, perhaps it was not built: "
  267. << this->TestCommand << "\n";
  268. out << "tried to find it in these places:\n";
  269. out << fullPath.c_str() << "\n";
  270. for(unsigned int i=0; i < failed.size(); ++i)
  271. {
  272. out << failed[i] << "\n";
  273. }
  274. if(outstring)
  275. {
  276. *outstring = out.str();
  277. }
  278. else
  279. {
  280. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str());
  281. }
  282. // return to the original directory
  283. cmSystemTools::ChangeDirectory(cwd.c_str());
  284. return 1;
  285. }
  286. std::vector<const char*> testCommand;
  287. testCommand.push_back(fullPath.c_str());
  288. for(k=0; k < this->TestCommandArgs.size(); ++k)
  289. {
  290. testCommand.push_back(this->TestCommandArgs[k].c_str());
  291. }
  292. testCommand.push_back(0);
  293. std::string outs;
  294. int retval = 0;
  295. // run the test from the this->BuildRunDir if set
  296. if(this->BuildRunDir.size())
  297. {
  298. out << "Run test in directory: " << this->BuildRunDir << "\n";
  299. cmSystemTools::ChangeDirectory(this->BuildRunDir.c_str());
  300. }
  301. out << "Running test executable: " << fullPath << " ";
  302. for(k=0; k < this->TestCommandArgs.size(); ++k)
  303. {
  304. out << this->TestCommandArgs[k] << " ";
  305. }
  306. out << "\n";
  307. // how much time is remaining
  308. double remainingTime = 0;
  309. if (this->Timeout)
  310. {
  311. remainingTime = this->Timeout - cmSystemTools::GetTime() + clock_start;
  312. if (remainingTime <= 0)
  313. {
  314. if(outstring)
  315. {
  316. *outstring = "--build-and-test timeout exceeded. ";
  317. }
  318. return 1;
  319. }
  320. }
  321. int runTestRes = this->CTest->RunTest(testCommand, &outs, &retval, 0,
  322. remainingTime);
  323. if(runTestRes != cmsysProcess_State_Exited || retval != 0)
  324. {
  325. out << "Failed to run test command: " << testCommand[0] << "\n";
  326. retval = 1;
  327. }
  328. out << outs << "\n";
  329. if(outstring)
  330. {
  331. *outstring = out.str();
  332. }
  333. else
  334. {
  335. cmCTestLog(this->CTest, OUTPUT, out.str() << std::endl);
  336. }
  337. return retval;
  338. }
  339. //----------------------------------------------------------------------
  340. int cmCTestBuildAndTestHandler::ProcessCommandLineArguments(
  341. const std::string& currentArg, size_t& idx,
  342. const std::vector<std::string>& allArgs)
  343. {
  344. // --build-and-test options
  345. if(currentArg.find("--build-and-test",0) == 0 && idx < allArgs.size() - 1)
  346. {
  347. if(idx+2 < allArgs.size())
  348. {
  349. idx++;
  350. this->SourceDir = allArgs[idx];
  351. idx++;
  352. this->BinaryDir = allArgs[idx];
  353. // dir must exist before CollapseFullPath is called
  354. cmSystemTools::MakeDirectory(this->BinaryDir.c_str());
  355. this->BinaryDir
  356. = cmSystemTools::CollapseFullPath(this->BinaryDir.c_str());
  357. this->SourceDir
  358. = cmSystemTools::CollapseFullPath(this->SourceDir.c_str());
  359. }
  360. else
  361. {
  362. cmCTestLog(this->CTest, ERROR_MESSAGE,
  363. "--build-and-test must have source and binary dir" << std::endl);
  364. return 0;
  365. }
  366. }
  367. if(currentArg.find("--build-target",0) == 0 && idx < allArgs.size() - 1)
  368. {
  369. idx++;
  370. this->BuildTargets.push_back(allArgs[idx]);
  371. }
  372. if(currentArg.find("--build-nocmake",0) == 0)
  373. {
  374. this->BuildNoCMake = true;
  375. }
  376. if(currentArg.find("--build-run-dir",0) == 0 && idx < allArgs.size() - 1)
  377. {
  378. idx++;
  379. this->BuildRunDir = allArgs[idx];
  380. }
  381. if(currentArg.find("--build-two-config",0) == 0)
  382. {
  383. this->BuildTwoConfig = true;
  384. }
  385. if(currentArg.find("--build-exe-dir",0) == 0 && idx < allArgs.size() - 1)
  386. {
  387. idx++;
  388. this->ExecutableDirectory = allArgs[idx];
  389. }
  390. if(currentArg.find("--test-timeout",0) == 0 && idx < allArgs.size() - 1)
  391. {
  392. idx++;
  393. this->Timeout = atof(allArgs[idx].c_str());
  394. }
  395. if(currentArg.find("--build-generator",0) == 0 && idx < allArgs.size() - 1)
  396. {
  397. idx++;
  398. this->BuildGenerator = allArgs[idx];
  399. }
  400. if(currentArg.find("--build-project",0) == 0 && idx < allArgs.size() - 1)
  401. {
  402. idx++;
  403. this->BuildProject = allArgs[idx];
  404. }
  405. if(currentArg.find("--build-makeprogram",0) == 0 &&
  406. idx < allArgs.size() - 1)
  407. {
  408. idx++;
  409. this->BuildMakeProgram = allArgs[idx];
  410. }
  411. if(currentArg.find("--build-config-sample",0) == 0 &&
  412. idx < allArgs.size() - 1)
  413. {
  414. idx++;
  415. this->ConfigSample = allArgs[idx];
  416. }
  417. if(currentArg.find("--build-noclean",0) == 0)
  418. {
  419. this->BuildNoClean = true;
  420. }
  421. if(currentArg.find("--build-options",0) == 0 && idx < allArgs.size() - 1)
  422. {
  423. ++idx;
  424. bool done = false;
  425. while(idx < allArgs.size() && !done)
  426. {
  427. this->BuildOptions.push_back(allArgs[idx]);
  428. if(idx+1 < allArgs.size()
  429. && (allArgs[idx+1] == "--build-target" ||
  430. allArgs[idx+1] == "--test-command"))
  431. {
  432. done = true;
  433. }
  434. else
  435. {
  436. ++idx;
  437. }
  438. }
  439. }
  440. if(currentArg.find("--test-command",0) == 0 && idx < allArgs.size() - 1)
  441. {
  442. ++idx;
  443. this->TestCommand = allArgs[idx];
  444. while(idx+1 < allArgs.size())
  445. {
  446. ++idx;
  447. this->TestCommandArgs.push_back(allArgs[idx]);
  448. }
  449. }
  450. return 1;
  451. }