cmCTestBuildAndTestHandler.cxx 13 KB

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