cmCTestBuildAndTestHandler.cxx 14 KB

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