cmCTestBuildAndTestHandler.cxx 14 KB

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