1
0

cmCTestBuildAndTestHandler.cxx 13 KB

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