cmCTestBuildAndTestHandler.cxx 15 KB

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