cmCTestBuildAndTestHandler.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. const char* config = 0;
  66. if ( this->CTest->GetConfigType().size() > 0 )
  67. {
  68. config = this->CTest->GetConfigType().c_str();
  69. }
  70. #ifdef CMAKE_INTDIR
  71. if(!config)
  72. {
  73. config = CMAKE_INTDIR;
  74. }
  75. #endif
  76. if ( config )
  77. {
  78. std::string btype
  79. = "-DCMAKE_BUILD_TYPE:STRING=" + std::string(config);
  80. args.push_back(btype);
  81. }
  82. for(k=0; k < this->BuildOptions.size(); ++k)
  83. {
  84. args.push_back(this->BuildOptions[k]);
  85. }
  86. if (cm->Run(args) != 0)
  87. {
  88. out << "Error: cmake execution failed\n";
  89. out << cmakeOutString << "\n";
  90. // return to the original directory
  91. cmSystemTools::ChangeDirectory(cwd.c_str());
  92. if(outstring)
  93. {
  94. *outstring = out.str();
  95. }
  96. else
  97. {
  98. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
  99. }
  100. return 1;
  101. }
  102. // do another config?
  103. if(this->BuildTwoConfig)
  104. {
  105. if (cm->Run(args) != 0)
  106. {
  107. out << "Error: cmake execution failed\n";
  108. out << cmakeOutString << "\n";
  109. // return to the original directory
  110. cmSystemTools::ChangeDirectory(cwd.c_str());
  111. if(outstring)
  112. {
  113. *outstring = out.str();
  114. }
  115. else
  116. {
  117. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
  118. }
  119. return 1;
  120. }
  121. }
  122. out << "======== CMake output ======\n";
  123. out << cmakeOutString;
  124. out << "======== End CMake output ======\n";
  125. return 0;
  126. }
  127. //----------------------------------------------------------------------
  128. void CMakeMessageCallback(const char* m, const char*, bool&, void* s)
  129. {
  130. std::string* out = (std::string*)s;
  131. *out += m;
  132. *out += "\n";
  133. }
  134. void CMakeProgressCallback(const char*msg, float , void * s)
  135. {
  136. std::string* out = (std::string*)s;
  137. *out += msg;
  138. *out += "\n";
  139. }
  140. //----------------------------------------------------------------------
  141. void CMakeStdoutCallback(const char* m, int len, void* s)
  142. {
  143. std::string* out = (std::string*)s;
  144. out->append(m, len);
  145. }
  146. struct cmSetupOutputCaptureCleanup
  147. {
  148. ~cmSetupOutputCaptureCleanup()
  149. {
  150. cmSystemTools::SetErrorCallback(0, 0);
  151. cmSystemTools::SetStdoutCallback(0, 0);
  152. }
  153. };
  154. //----------------------------------------------------------------------
  155. int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
  156. {
  157. unsigned int k;
  158. std::string cmakeOutString;
  159. cmSystemTools::SetErrorCallback(CMakeMessageCallback, &cmakeOutString);
  160. cmSystemTools::SetStdoutCallback(CMakeStdoutCallback, &cmakeOutString);
  161. // make sure SetStdoutCallback and SetErrorCallback are set to null
  162. // after this function exits so that they do not point at a destroyed
  163. // string cmakeOutString
  164. cmSetupOutputCaptureCleanup cleanup;
  165. cmOStringStream out;
  166. // if the generator and make program are not specified then it is an error
  167. if (!this->BuildGenerator.size() || !this->BuildMakeProgram.size())
  168. {
  169. if(outstring)
  170. {
  171. *outstring =
  172. "--build-and-test requires that both the generator and makeprogram "
  173. "be provided using the --build-generator and --build-makeprogram "
  174. "command line options. ";
  175. }
  176. return 1;
  177. }
  178. if ( this->CTest->GetConfigType().size() == 0 &&
  179. this->ConfigSample.size())
  180. {
  181. // use the config sample to set the ConfigType
  182. std::string fullPath;
  183. std::string resultingConfig;
  184. std::vector<std::string> extraPaths;
  185. std::vector<std::string> failed;
  186. fullPath =
  187. cmCTestTestHandler::FindExecutable(this->CTest,
  188. this->ConfigSample.c_str(),
  189. resultingConfig,
  190. extraPaths,
  191. failed);
  192. if (fullPath.size() && resultingConfig.size())
  193. {
  194. this->CTest->SetConfigType(resultingConfig.c_str());
  195. }
  196. out << "Using config sample with results: "
  197. << fullPath << " and " << resultingConfig << std::endl;
  198. }
  199. // we need to honor the timeout specified, the timeout include cmake, build
  200. // and test time
  201. double clock_start = cmSystemTools::GetTime();
  202. // make sure the binary dir is there
  203. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  204. out << "Internal cmake changing into directory: "
  205. << this->BinaryDir << std::endl;
  206. if (!cmSystemTools::FileIsDirectory(this->BinaryDir.c_str()))
  207. {
  208. cmSystemTools::MakeDirectory(this->BinaryDir.c_str());
  209. }
  210. cmSystemTools::ChangeDirectory(this->BinaryDir.c_str());
  211. // should we cmake?
  212. cmake cm;
  213. cm.SetProgressCallback(CMakeProgressCallback, &cmakeOutString);
  214. cm.SetGlobalGenerator(cm.CreateGlobalGenerator(
  215. this->BuildGenerator.c_str()));
  216. if(!this->BuildNoCMake)
  217. {
  218. // do the cmake step, no timeout here since it is not a sub process
  219. if (this->RunCMake(outstring,out,cmakeOutString,cwd,&cm))
  220. {
  221. return 1;
  222. }
  223. }
  224. // do the build
  225. std::vector<std::string>::iterator tarIt;
  226. if ( this->BuildTargets.size() == 0 )
  227. {
  228. this->BuildTargets.push_back("");
  229. }
  230. for ( tarIt = this->BuildTargets.begin();
  231. tarIt != this->BuildTargets.end(); ++ tarIt )
  232. {
  233. double remainingTime = 0;
  234. if (this->Timeout)
  235. {
  236. remainingTime = this->Timeout - cmSystemTools::GetTime() + clock_start;
  237. if (remainingTime <= 0)
  238. {
  239. if(outstring)
  240. {
  241. *outstring = "--build-and-test timeout exceeded. ";
  242. }
  243. return 1;
  244. }
  245. }
  246. std::string output;
  247. const char* config = 0;
  248. if ( this->CTest->GetConfigType().size() > 0 )
  249. {
  250. config = this->CTest->GetConfigType().c_str();
  251. }
  252. #ifdef CMAKE_INTDIR
  253. if(!config)
  254. {
  255. config = CMAKE_INTDIR;
  256. }
  257. #endif
  258. if(!config)
  259. {
  260. config = "Debug";
  261. }
  262. int retVal = cm.GetGlobalGenerator()->Build(
  263. this->SourceDir.c_str(), this->BinaryDir.c_str(),
  264. this->BuildProject.c_str(), tarIt->c_str(),
  265. &output, this->BuildMakeProgram.c_str(),
  266. config,
  267. !this->BuildNoClean,
  268. false, remainingTime);
  269. out << output;
  270. // if the build failed then return
  271. if (retVal)
  272. {
  273. if(outstring)
  274. {
  275. *outstring = out.str();
  276. }
  277. return 1;
  278. }
  279. }
  280. if(outstring)
  281. {
  282. *outstring = out.str();
  283. }
  284. // if no test was specified then we are done
  285. if (!this->TestCommand.size())
  286. {
  287. return 0;
  288. }
  289. // now run the compiled test if we can find it
  290. // store the final location in fullPath
  291. std::string fullPath;
  292. std::string resultingConfig;
  293. std::vector<std::string> extraPaths;
  294. // if this->ExecutableDirectory is set try that as well
  295. if (this->ExecutableDirectory.size())
  296. {
  297. std::string tempPath = this->ExecutableDirectory;
  298. tempPath += "/";
  299. tempPath += this->TestCommand;
  300. extraPaths.push_back(tempPath);
  301. }
  302. std::vector<std::string> failed;
  303. fullPath =
  304. cmCTestTestHandler::FindExecutable(this->CTest,
  305. this->TestCommand.c_str(),
  306. resultingConfig,
  307. extraPaths,
  308. failed);
  309. if(!cmSystemTools::FileExists(fullPath.c_str()))
  310. {
  311. out << "Could not find path to executable, perhaps it was not built: "
  312. << this->TestCommand << "\n";
  313. out << "tried to find it in these places:\n";
  314. out << fullPath.c_str() << "\n";
  315. for(unsigned int i=0; i < failed.size(); ++i)
  316. {
  317. out << failed[i] << "\n";
  318. }
  319. if(outstring)
  320. {
  321. *outstring = out.str();
  322. }
  323. else
  324. {
  325. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str());
  326. }
  327. // return to the original directory
  328. cmSystemTools::ChangeDirectory(cwd.c_str());
  329. return 1;
  330. }
  331. std::vector<const char*> testCommand;
  332. testCommand.push_back(fullPath.c_str());
  333. for(k=0; k < this->TestCommandArgs.size(); ++k)
  334. {
  335. testCommand.push_back(this->TestCommandArgs[k].c_str());
  336. }
  337. testCommand.push_back(0);
  338. std::string outs;
  339. int retval = 0;
  340. // run the test from the this->BuildRunDir if set
  341. if(this->BuildRunDir.size())
  342. {
  343. out << "Run test in directory: " << this->BuildRunDir << "\n";
  344. cmSystemTools::ChangeDirectory(this->BuildRunDir.c_str());
  345. }
  346. out << "Running test command: \"" << fullPath << "\"";
  347. for(k=0; k < this->TestCommandArgs.size(); ++k)
  348. {
  349. out << " \"" << this->TestCommandArgs[k] << "\"";
  350. }
  351. out << "\n";
  352. // how much time is remaining
  353. double remainingTime = 0;
  354. if (this->Timeout)
  355. {
  356. remainingTime = this->Timeout - cmSystemTools::GetTime() + clock_start;
  357. if (remainingTime <= 0)
  358. {
  359. if(outstring)
  360. {
  361. *outstring = "--build-and-test timeout exceeded. ";
  362. }
  363. return 1;
  364. }
  365. }
  366. int runTestRes = this->CTest->RunTest(testCommand, &outs, &retval, 0,
  367. remainingTime, 0);
  368. if(runTestRes != cmsysProcess_State_Exited || retval != 0)
  369. {
  370. out << "Test command failed: " << testCommand[0] << "\n";
  371. retval = 1;
  372. }
  373. out << outs << "\n";
  374. if(outstring)
  375. {
  376. *outstring = out.str();
  377. }
  378. else
  379. {
  380. cmCTestLog(this->CTest, OUTPUT, out.str() << std::endl);
  381. }
  382. return retval;
  383. }
  384. //----------------------------------------------------------------------
  385. int cmCTestBuildAndTestHandler::ProcessCommandLineArguments(
  386. const std::string& currentArg, size_t& idx,
  387. const std::vector<std::string>& allArgs)
  388. {
  389. // --build-and-test options
  390. if(currentArg.find("--build-and-test",0) == 0 && idx < allArgs.size() - 1)
  391. {
  392. if(idx+2 < allArgs.size())
  393. {
  394. idx++;
  395. this->SourceDir = allArgs[idx];
  396. idx++;
  397. this->BinaryDir = allArgs[idx];
  398. // dir must exist before CollapseFullPath is called
  399. cmSystemTools::MakeDirectory(this->BinaryDir.c_str());
  400. this->BinaryDir
  401. = cmSystemTools::CollapseFullPath(this->BinaryDir.c_str());
  402. this->SourceDir
  403. = cmSystemTools::CollapseFullPath(this->SourceDir.c_str());
  404. }
  405. else
  406. {
  407. cmCTestLog(this->CTest, ERROR_MESSAGE,
  408. "--build-and-test must have source and binary dir" << std::endl);
  409. return 0;
  410. }
  411. }
  412. if(currentArg.find("--build-target",0) == 0 && idx < allArgs.size() - 1)
  413. {
  414. idx++;
  415. this->BuildTargets.push_back(allArgs[idx]);
  416. }
  417. if(currentArg.find("--build-nocmake",0) == 0)
  418. {
  419. this->BuildNoCMake = true;
  420. }
  421. if(currentArg.find("--build-run-dir",0) == 0 && idx < allArgs.size() - 1)
  422. {
  423. idx++;
  424. this->BuildRunDir = allArgs[idx];
  425. }
  426. if(currentArg.find("--build-two-config",0) == 0)
  427. {
  428. this->BuildTwoConfig = true;
  429. }
  430. if(currentArg.find("--build-exe-dir",0) == 0 && idx < allArgs.size() - 1)
  431. {
  432. idx++;
  433. this->ExecutableDirectory = allArgs[idx];
  434. }
  435. if(currentArg.find("--test-timeout",0) == 0 && idx < allArgs.size() - 1)
  436. {
  437. idx++;
  438. this->Timeout = atof(allArgs[idx].c_str());
  439. }
  440. if(currentArg.find("--build-generator",0) == 0 && idx < allArgs.size() - 1)
  441. {
  442. idx++;
  443. this->BuildGenerator = allArgs[idx];
  444. }
  445. if(currentArg.find("--build-project",0) == 0 && idx < allArgs.size() - 1)
  446. {
  447. idx++;
  448. this->BuildProject = allArgs[idx];
  449. }
  450. if(currentArg.find("--build-makeprogram",0) == 0 &&
  451. idx < allArgs.size() - 1)
  452. {
  453. idx++;
  454. this->BuildMakeProgram = allArgs[idx];
  455. }
  456. if(currentArg.find("--build-config-sample",0) == 0 &&
  457. idx < allArgs.size() - 1)
  458. {
  459. idx++;
  460. this->ConfigSample = allArgs[idx];
  461. }
  462. if(currentArg.find("--build-noclean",0) == 0)
  463. {
  464. this->BuildNoClean = true;
  465. }
  466. if(currentArg.find("--build-options",0) == 0 && idx < allArgs.size() - 1)
  467. {
  468. ++idx;
  469. bool done = false;
  470. while(idx < allArgs.size() && !done)
  471. {
  472. this->BuildOptions.push_back(allArgs[idx]);
  473. if(idx+1 < allArgs.size()
  474. && (allArgs[idx+1] == "--build-target" ||
  475. allArgs[idx+1] == "--test-command"))
  476. {
  477. done = true;
  478. }
  479. else
  480. {
  481. ++idx;
  482. }
  483. }
  484. }
  485. if(currentArg.find("--test-command",0) == 0 && idx < allArgs.size() - 1)
  486. {
  487. ++idx;
  488. this->TestCommand = allArgs[idx];
  489. while(idx+1 < allArgs.size())
  490. {
  491. ++idx;
  492. this->TestCommandArgs.push_back(allArgs[idx]);
  493. }
  494. }
  495. return 1;
  496. }