cmCTestBuildAndTestHandler.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. m_BuildTwoConfig = false;
  23. m_BuildNoClean = false;
  24. m_BuildNoCMake = false;
  25. }
  26. //----------------------------------------------------------------------
  27. void cmCTestBuildAndTestHandler::Initialize()
  28. {
  29. }
  30. //----------------------------------------------------------------------
  31. const char* cmCTestBuildAndTestHandler::GetOutput()
  32. {
  33. return m_Output.c_str();
  34. }
  35. //----------------------------------------------------------------------
  36. int cmCTestBuildAndTestHandler::ProcessHandler()
  37. {
  38. m_Output = "";
  39. std::string output;
  40. cmSystemTools::ResetErrorOccuredFlag();
  41. cmListFileCache::ClearCache();
  42. int retv = this->RunCMakeAndTest(&m_Output);
  43. cmSystemTools::ResetErrorOccuredFlag();
  44. cmListFileCache::ClearCache();
  45. return retv;
  46. }
  47. //----------------------------------------------------------------------
  48. int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring, cmOStringStream &out,
  49. std::string &cmakeOutString, std::string &cwd,
  50. cmake *cm)
  51. {
  52. unsigned int k;
  53. std::vector<std::string> args;
  54. args.push_back(m_CTest->GetCMakeExecutable());
  55. args.push_back(m_SourceDir);
  56. if(m_BuildGenerator.size())
  57. {
  58. std::string generator = "-G";
  59. generator += m_BuildGenerator;
  60. args.push_back(generator);
  61. }
  62. if ( m_CTest->GetConfigType().size() > 0 )
  63. {
  64. std::string btype = "-DBUILD_TYPE:STRING=" + m_CTest->GetConfigType();
  65. args.push_back(btype);
  66. }
  67. for(k=0; k < m_BuildOptions.size(); ++k)
  68. {
  69. args.push_back(m_BuildOptions[k]);
  70. }
  71. if (cm->Run(args) != 0)
  72. {
  73. out << "Error: cmake execution failed\n";
  74. out << cmakeOutString << "\n";
  75. // return to the original directory
  76. cmSystemTools::ChangeDirectory(cwd.c_str());
  77. if(outstring)
  78. {
  79. *outstring = out.str();
  80. }
  81. else
  82. {
  83. cmCTestLog(m_CTest, ERROR_MESSAGE, out.str() << std::endl);
  84. }
  85. return 1;
  86. }
  87. // do another config?
  88. if(m_BuildTwoConfig)
  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(m_CTest, ERROR_MESSAGE, out.str() << std::endl);
  103. }
  104. return 1;
  105. }
  106. }
  107. return 0;
  108. }
  109. //----------------------------------------------------------------------
  110. void CMakeMessageCallback(const char* m, const char*, bool&, void* s)
  111. {
  112. std::string* out = (std::string*)s;
  113. *out += m;
  114. *out += "\n";
  115. }
  116. //----------------------------------------------------------------------
  117. void CMakeStdoutCallback(const char* m, int len, void* s)
  118. {
  119. std::string* out = (std::string*)s;
  120. out->append(m, len);
  121. }
  122. //----------------------------------------------------------------------
  123. int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
  124. {
  125. unsigned int k;
  126. std::string cmakeOutString;
  127. cmSystemTools::SetErrorCallback(CMakeMessageCallback, &cmakeOutString);
  128. cmSystemTools::SetStdoutCallback(CMakeStdoutCallback, &cmakeOutString);
  129. cmOStringStream out;
  130. // What is this? double timeout = m_CTest->GetTimeOut();
  131. int retVal = 0;
  132. // if the generator and make program are not specified then it is an error
  133. if (!m_BuildGenerator.size() || !m_BuildMakeProgram.size())
  134. {
  135. if(outstring)
  136. {
  137. *outstring =
  138. "--build-and-test requires that both the generator and makeprogram "
  139. "be provided using the --build-generator and --build-makeprogram "
  140. "command line options. ";
  141. }
  142. return 1;
  143. }
  144. // make sure the binary dir is there
  145. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  146. out << "Internal cmake changing into directory: " << m_BinaryDir << "\n";
  147. if (!cmSystemTools::FileIsDirectory(m_BinaryDir.c_str()))
  148. {
  149. cmSystemTools::MakeDirectory(m_BinaryDir.c_str());
  150. }
  151. cmSystemTools::ChangeDirectory(m_BinaryDir.c_str());
  152. // should we cmake?
  153. cmake cm;
  154. cm.SetGlobalGenerator(cm.CreateGlobalGenerator(m_BuildGenerator.c_str()));
  155. if(!m_BuildNoCMake)
  156. {
  157. // do the cmake step
  158. if (this->RunCMake(outstring,out,cmakeOutString,cwd,&cm))
  159. {
  160. return 1;
  161. }
  162. }
  163. // do the build
  164. std::string output;
  165. retVal = cm.GetGlobalGenerator()->Build(
  166. m_SourceDir.c_str(), m_BinaryDir.c_str(),
  167. m_BuildProject.c_str(), m_BuildTarget.c_str(),
  168. &output, m_BuildMakeProgram.c_str(),
  169. m_CTest->GetConfigType().c_str(),!m_BuildNoClean);
  170. out << output;
  171. if(outstring)
  172. {
  173. *outstring = out.str();
  174. }
  175. // if the build failed then return
  176. if (retVal)
  177. {
  178. return 1;
  179. }
  180. // if not test was specified then we are done
  181. if (!m_TestCommand.size())
  182. {
  183. return 0;
  184. }
  185. // now run the compiled test if we can find it
  186. std::vector<std::string> attempted;
  187. std::vector<std::string> failed;
  188. std::string tempPath;
  189. std::string filepath =
  190. cmSystemTools::GetFilenamePath(m_TestCommand);
  191. std::string filename =
  192. cmSystemTools::GetFilenameName(m_TestCommand);
  193. // if full path specified then search that first
  194. if (filepath.size())
  195. {
  196. tempPath = filepath;
  197. tempPath += "/";
  198. tempPath += filename;
  199. attempted.push_back(tempPath);
  200. if(m_CTest->GetConfigType().size())
  201. {
  202. tempPath = filepath;
  203. tempPath += "/";
  204. tempPath += m_CTest->GetConfigType();
  205. tempPath += "/";
  206. tempPath += filename;
  207. attempted.push_back(tempPath);
  208. }
  209. }
  210. // otherwise search local dirs
  211. else
  212. {
  213. attempted.push_back(filename);
  214. if(m_CTest->GetConfigType().size())
  215. {
  216. tempPath = m_CTest->GetConfigType();
  217. tempPath += "/";
  218. tempPath += filename;
  219. attempted.push_back(tempPath);
  220. }
  221. }
  222. // if m_ExecutableDirectory is set try that as well
  223. if (m_ExecutableDirectory.size())
  224. {
  225. tempPath = m_ExecutableDirectory;
  226. tempPath += "/";
  227. tempPath += m_TestCommand;
  228. attempted.push_back(tempPath);
  229. if(m_CTest->GetConfigType().size())
  230. {
  231. tempPath = m_ExecutableDirectory;
  232. tempPath += "/";
  233. tempPath += m_CTest->GetConfigType();
  234. tempPath += "/";
  235. tempPath += filename;
  236. attempted.push_back(tempPath);
  237. }
  238. }
  239. // store the final location in fullPath
  240. std::string fullPath;
  241. // now look in the paths we specified above
  242. for(unsigned int ai=0;
  243. ai < attempted.size() && fullPath.size() == 0; ++ai)
  244. {
  245. // first check without exe extension
  246. if(cmSystemTools::FileExists(attempted[ai].c_str())
  247. && !cmSystemTools::FileIsDirectory(attempted[ai].c_str()))
  248. {
  249. fullPath = cmSystemTools::CollapseFullPath(attempted[ai].c_str());
  250. }
  251. // then try with the exe extension
  252. else
  253. {
  254. failed.push_back(attempted[ai].c_str());
  255. tempPath = attempted[ai];
  256. tempPath += cmSystemTools::GetExecutableExtension();
  257. if(cmSystemTools::FileExists(tempPath.c_str())
  258. && !cmSystemTools::FileIsDirectory(tempPath.c_str()))
  259. {
  260. fullPath = cmSystemTools::CollapseFullPath(tempPath.c_str());
  261. }
  262. else
  263. {
  264. failed.push_back(tempPath.c_str());
  265. }
  266. }
  267. }
  268. if(!cmSystemTools::FileExists(fullPath.c_str()))
  269. {
  270. out << "Could not find path to executable, perhaps it was not built: " <<
  271. m_TestCommand << "\n";
  272. out << "tried to find it in these places:\n";
  273. out << fullPath.c_str() << "\n";
  274. for(unsigned int i=0; i < failed.size(); ++i)
  275. {
  276. out << failed[i] << "\n";
  277. }
  278. if(outstring)
  279. {
  280. *outstring = out.str();
  281. }
  282. else
  283. {
  284. cmCTestLog(m_CTest, ERROR_MESSAGE, out.str());
  285. }
  286. // return to the original directory
  287. cmSystemTools::ChangeDirectory(cwd.c_str());
  288. return 1;
  289. }
  290. std::vector<const char*> testCommand;
  291. testCommand.push_back(fullPath.c_str());
  292. for(k=0; k < m_TestCommandArgs.size(); ++k)
  293. {
  294. testCommand.push_back(m_TestCommandArgs[k].c_str());
  295. }
  296. testCommand.push_back(0);
  297. std::string outs;
  298. int retval = 0;
  299. // run the test from the m_BuildRunDir if set
  300. if(m_BuildRunDir.size())
  301. {
  302. out << "Run test in directory: " << m_BuildRunDir << "\n";
  303. cmSystemTools::ChangeDirectory(m_BuildRunDir.c_str());
  304. }
  305. out << "Running test executable: " << fullPath << " ";
  306. for(k=0; k < m_TestCommandArgs.size(); ++k)
  307. {
  308. out << m_TestCommandArgs[k] << " ";
  309. }
  310. out << "\n";
  311. // What is this? m_TimeOut = timeout;
  312. int runTestRes = m_CTest->RunTest(testCommand, &outs, &retval, 0);
  313. if(runTestRes != cmsysProcess_State_Exited || retval != 0)
  314. {
  315. out << "Failed to run test command: " << testCommand[0] << "\n";
  316. retval = 1;
  317. }
  318. out << outs << "\n";
  319. if(outstring)
  320. {
  321. *outstring = out.str();
  322. }
  323. else
  324. {
  325. cmCTestLog(m_CTest, OUTPUT, out.str() << std::endl);
  326. }
  327. return retval;
  328. }
  329. //----------------------------------------------------------------------
  330. int cmCTestBuildAndTestHandler::ProcessCommandLineArguments(
  331. const std::string& currentArg, size_t& idx,
  332. const std::vector<std::string>& allArgs)
  333. {
  334. // --build-and-test options
  335. if(currentArg.find("--build-and-test",0) == 0 && idx < allArgs.size() - 1)
  336. {
  337. if(idx+2 < allArgs.size())
  338. {
  339. idx++;
  340. m_SourceDir = allArgs[idx];
  341. idx++;
  342. m_BinaryDir = allArgs[idx];
  343. // dir must exist before CollapseFullPath is called
  344. cmSystemTools::MakeDirectory(m_BinaryDir.c_str());
  345. m_BinaryDir = cmSystemTools::CollapseFullPath(m_BinaryDir.c_str());
  346. m_SourceDir = cmSystemTools::CollapseFullPath(m_SourceDir.c_str());
  347. }
  348. else
  349. {
  350. cmCTestLog(m_CTest, ERROR_MESSAGE, "--build-and-test must have source and binary dir" << std::endl);
  351. return 0;
  352. }
  353. }
  354. if(currentArg.find("--build-target",0) == 0 && idx < allArgs.size() - 1)
  355. {
  356. idx++;
  357. m_BuildTarget = allArgs[idx];
  358. }
  359. if(currentArg.find("--build-nocmake",0) == 0)
  360. {
  361. m_BuildNoCMake = true;
  362. }
  363. if(currentArg.find("--build-run-dir",0) == 0 && idx < allArgs.size() - 1)
  364. {
  365. idx++;
  366. m_BuildRunDir = allArgs[idx];
  367. }
  368. if(currentArg.find("--build-two-config",0) == 0)
  369. {
  370. m_BuildTwoConfig = true;
  371. }
  372. if(currentArg.find("--build-exe-dir",0) == 0 && idx < allArgs.size() - 1)
  373. {
  374. idx++;
  375. m_ExecutableDirectory = allArgs[idx];
  376. }
  377. if(currentArg.find("--build-generator",0) == 0 && idx < allArgs.size() - 1)
  378. {
  379. idx++;
  380. m_BuildGenerator = allArgs[idx];
  381. }
  382. if(currentArg.find("--build-project",0) == 0 && idx < allArgs.size() - 1)
  383. {
  384. idx++;
  385. m_BuildProject = allArgs[idx];
  386. }
  387. if(currentArg.find("--build-makeprogram",0) == 0 && idx < allArgs.size() - 1)
  388. {
  389. idx++;
  390. m_BuildMakeProgram = allArgs[idx];
  391. }
  392. if(currentArg.find("--build-noclean",0) == 0)
  393. {
  394. m_BuildNoClean = true;
  395. }
  396. if(currentArg.find("--build-options",0) == 0 && idx < allArgs.size() - 1)
  397. {
  398. ++idx;
  399. bool done = false;
  400. while(idx < allArgs.size() && !done)
  401. {
  402. m_BuildOptions.push_back(allArgs[idx]);
  403. if(idx+1 < allArgs.size()
  404. && (allArgs[idx+1] == "--build-target" || allArgs[idx+1] == "--test-command"))
  405. {
  406. done = true;
  407. }
  408. else
  409. {
  410. ++idx;
  411. }
  412. }
  413. }
  414. if(currentArg.find("--test-command",0) == 0 && idx < allArgs.size() - 1)
  415. {
  416. ++idx;
  417. m_TestCommand = allArgs[idx];
  418. while(idx+1 < allArgs.size())
  419. {
  420. ++idx;
  421. m_TestCommandArgs.push_back(allArgs[idx]);
  422. }
  423. }
  424. return 1;
  425. }