cmCTestBuildAndTestHandler.cxx 15 KB

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