cmCTestBuildAndTestHandler.cxx 12 KB

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