cmCTestBuildAndTestHandler.cxx 15 KB

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