cmCTestBuildAndTestHandler.cxx 14 KB

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