cmCTestCoverageHandler.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 "cmCTestCoverageHandler.h"
  14. #include "cmCTest.h"
  15. #include "cmake.h"
  16. #include "cmSystemTools.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmGlob.h"
  19. #include <cmsys/Process.h>
  20. #include <cmsys/RegularExpression.hxx>
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <float.h>
  24. #define SAFEDIV(x,y) (((y)!=0)?((x)/(y)):(0))
  25. //----------------------------------------------------------------------
  26. cmCTestCoverageHandler::cmCTestCoverageHandler()
  27. {
  28. m_CTest = 0;
  29. }
  30. //----------------------------------------------------------------------
  31. bool cmCTestCoverageHandler::StartLogFile(cmGeneratedFileStream& covLogFile, int logFileCount)
  32. {
  33. char covLogFilename[1024];
  34. sprintf(covLogFilename, "CoverageLog-%d.xml", logFileCount);
  35. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Open file: " << covLogFilename << std::endl);
  36. if (!m_CTest->OpenOutputFile(m_CTest->GetCurrentTag(),
  37. covLogFilename, covLogFile, true))
  38. {
  39. cmCTestLog(m_CTest, ERROR_MESSAGE, "Cannot open log file: " << covLogFilename << std::endl);
  40. return false;
  41. }
  42. std::string local_start_time = m_CTest->CurrentTime();
  43. m_CTest->StartXML(covLogFile);
  44. covLogFile << "<CoverageLog>" << std::endl
  45. << "\t<StartDateTime>" << local_start_time << "</StartDateTime>" << std::endl;
  46. return true;
  47. }
  48. //----------------------------------------------------------------------
  49. void cmCTestCoverageHandler::EndLogFile(cmGeneratedFileStream& ostr, int logFileCount)
  50. {
  51. std::string local_end_time = m_CTest->CurrentTime();
  52. ostr << "\t<EndDateTime>" << local_end_time << "</EndDateTime>" << std::endl
  53. << "</CoverageLog>" << std::endl;
  54. m_CTest->EndXML(ostr);
  55. char covLogFilename[1024];
  56. sprintf(covLogFilename, "CoverageLog-%d.xml", logFileCount);
  57. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Close file: " << covLogFilename << std::endl);
  58. ostr.close();
  59. }
  60. //----------------------------------------------------------------------
  61. bool cmCTestCoverageHandler::ShouldIDoCoverage(const char* file, const char* srcDir,
  62. const char* binDir)
  63. {
  64. std::string fSrcDir = cmSystemTools::CollapseFullPath(srcDir);
  65. std::string fBinDir = cmSystemTools::CollapseFullPath(binDir);
  66. std::string fFile = cmSystemTools::CollapseFullPath(file);
  67. bool sourceSubDir = cmSystemTools::IsSubDirectory(fFile.c_str(), fSrcDir.c_str());
  68. bool buildSubDir = cmSystemTools::IsSubDirectory(fFile.c_str(), fBinDir.c_str());
  69. // Always check parent directory of the file.
  70. std::string fileDir = cmSystemTools::GetFilenamePath(fFile.c_str());
  71. std::string checkDir;
  72. // We also need to check the binary/source directory pair.
  73. if ( sourceSubDir && buildSubDir )
  74. {
  75. if ( fSrcDir.size() > fBinDir.size() )
  76. {
  77. checkDir = fSrcDir;
  78. }
  79. else
  80. {
  81. checkDir = fBinDir;
  82. }
  83. }
  84. else if ( sourceSubDir )
  85. {
  86. checkDir = fSrcDir;
  87. }
  88. else if ( buildSubDir )
  89. {
  90. checkDir = fBinDir;
  91. }
  92. std::string ndc
  93. = cmSystemTools::FileExistsInParentDirectories(".NoDartCoverage",
  94. fFile.c_str(), checkDir.c_str());
  95. if ( ndc.size() )
  96. {
  97. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Found: " << ndc.c_str() << " so skip coverage of " << file << std::endl);
  98. return false;
  99. }
  100. // By now checkDir should be set to parent directory of the file.
  101. // Get the relative path to the file an apply it to the opposite directory.
  102. // If it is the same as fileDir, then ignore, otherwise check.
  103. std::string relPath = cmSystemTools::RelativePath(checkDir.c_str(),
  104. fFile.c_str());
  105. if ( checkDir == fSrcDir )
  106. {
  107. checkDir = fBinDir;
  108. }
  109. else
  110. {
  111. checkDir = fSrcDir;
  112. }
  113. fFile = checkDir + "/" + relPath;
  114. fFile = cmSystemTools::GetFilenamePath(fFile.c_str());
  115. if ( fileDir == fFile )
  116. {
  117. // This is in-source build, so we trust the previous check.
  118. return true;
  119. }
  120. ndc = cmSystemTools::FileExistsInParentDirectories(".NoDartCoverage",
  121. fFile.c_str(), checkDir.c_str());
  122. if ( ndc.size() )
  123. {
  124. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Found: " << ndc.c_str() << " so skip coverage of: " << file << std::endl);
  125. return false;
  126. }
  127. // Ok, nothing in source tree, nothing in binary tree
  128. return true;
  129. }
  130. //----------------------------------------------------------------------
  131. //clearly it would be nice if this were broken up into a few smaller
  132. //functions and commented...
  133. int cmCTestCoverageHandler::ProcessHandler()
  134. {
  135. int error = 0;
  136. std::string sourceDir = m_CTest->GetCTestConfiguration("SourceDirectory");
  137. std::string binaryDir = m_CTest->GetCTestConfiguration("BuildDirectory");
  138. std::string gcovCommand = m_CTest->GetCTestConfiguration("CoverageCommand");
  139. cmSystemTools::ConvertToUnixSlashes(sourceDir);
  140. cmSystemTools::ConvertToUnixSlashes(binaryDir);
  141. //std::string asfGlob = sourceDir + "/*";
  142. //std::string abfGlob = binaryDir + "/*";
  143. std::string daGlob = binaryDir + "/*.da";
  144. std::string gcovOutputRex = "[0-9]+\\.[0-9]+% of [0-9]+ (source |)lines executed in file (.*)$";
  145. std::string gcovOutputRex2 = "^Creating (.*\\.gcov)\\.";
  146. cmCTestLog(m_CTest, HANDLER_OUTPUT, "Performing coverage" << std::endl);
  147. double elapsed_time_start = cmSystemTools::GetTime();
  148. std::string coverage_start_time = m_CTest->CurrentTime();
  149. std::string testingDir = m_CTest->GetBinaryDir() + "/Testing";
  150. std::string tempDir = testingDir + "/CoverageInfo";
  151. std::string currentDirectory = cmSystemTools::GetCurrentWorkingDirectory();
  152. cmSystemTools::MakeDirectory(tempDir.c_str());
  153. cmSystemTools::ChangeDirectory(tempDir.c_str());
  154. cmGlob gl;
  155. gl.RecurseOn();
  156. gl.FindFiles(daGlob);
  157. std::vector<std::string> files = gl.GetFiles();
  158. std::vector<std::string>::iterator it;
  159. if ( files.size() == 0 )
  160. {
  161. cmCTestLog(m_CTest, ERROR_MESSAGE, " Cannot find any coverage files." << std::endl);
  162. // No coverage files is a valid thing, so the exit code is 0
  163. return 0;
  164. }
  165. // Regular expressions for output of gcov
  166. cmsys::RegularExpression re(gcovOutputRex.c_str());
  167. cmsys::RegularExpression re2(gcovOutputRex2.c_str());
  168. typedef std::vector<int> singleFileCoverageVector;
  169. typedef std::map<std::string, singleFileCoverageVector> totalCoverageMap;
  170. totalCoverageMap totalCoverage;
  171. std::string cfile = "";
  172. for ( it = files.begin(); it != files.end(); ++ it )
  173. {
  174. std::string fileDir = cmSystemTools::GetFilenamePath(it->c_str());
  175. std::string command = "\"" + gcovCommand + "\" -l -o \"" + fileDir + "\" \"" + *it + "\"";
  176. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, command.c_str() << std::endl);
  177. std::string output = "";
  178. int retVal = 0;
  179. int res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
  180. &retVal, tempDir.c_str(),
  181. false, 0 /*m_TimeOut*/);
  182. if ( ! res )
  183. {
  184. cmCTestLog(m_CTest, ERROR_MESSAGE, "Problem running coverage on file: " << it->c_str() << std::endl);
  185. error ++;
  186. continue;
  187. }
  188. if ( retVal != 0 )
  189. {
  190. cmCTestLog(m_CTest, ERROR_MESSAGE, "Coverage command returned: " << retVal << " while processing: " << it->c_str() << std::endl);
  191. }
  192. std::vector<cmStdString> lines;
  193. std::vector<cmStdString>::iterator line;
  194. cmSystemTools::Split(output.c_str(), lines);
  195. for ( line = lines.begin(); line != lines.end(); ++line)
  196. {
  197. if ( re.find(line->c_str()) )
  198. {
  199. cfile = "";
  200. std::string file = re.match(2);
  201. // Is it in the source dir?
  202. if ( file.size() > sourceDir.size() &&
  203. file.substr(0, sourceDir.size()) == sourceDir &&
  204. file[sourceDir.size()] == '/' )
  205. {
  206. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, " produced s: " << file << std::endl);
  207. cfile = file;
  208. }
  209. // Binary dir?
  210. if ( file.size() > binaryDir.size() &&
  211. file.substr(0, binaryDir.size()) == binaryDir &&
  212. file[binaryDir.size()] == '/' )
  213. {
  214. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, " produce b: " << file << std::endl);
  215. cfile = file;
  216. }
  217. if ( cfile.empty() )
  218. {
  219. cmCTestLog(m_CTest, ERROR_MESSAGE, "Something went wrong" << std::endl);
  220. cmCTestLog(m_CTest, ERROR_MESSAGE, "File: [" << file << "]" << std::endl);
  221. cmCTestLog(m_CTest, ERROR_MESSAGE, "s: [" << file.substr(0, sourceDir.size()) << "]" << std::endl);
  222. cmCTestLog(m_CTest, ERROR_MESSAGE, "b: [" << file.substr(0, binaryDir.size()) << "]" << std::endl);
  223. }
  224. }
  225. else if ( re2.find(line->c_str() ) )
  226. {
  227. std::string fname = re2.match(1);
  228. if ( cfile.size() )
  229. {
  230. singleFileCoverageVector* vec = &totalCoverage[cfile];
  231. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, " in file: " << fname << std::endl);
  232. std::ifstream ifile(fname.c_str());
  233. if ( ! ifile )
  234. {
  235. cmCTestLog(m_CTest, ERROR_MESSAGE, "Cannot open file: " << fname << std::endl);
  236. }
  237. else
  238. {
  239. long cnt = -1;
  240. std::string nl;
  241. while ( cmSystemTools::GetLineFromStream(ifile, nl) )
  242. {
  243. cnt ++;
  244. if ( vec->size() <= static_cast<singleFileCoverageVector::size_type>(cnt) )
  245. {
  246. vec->push_back(-1);
  247. }
  248. //TODO: Handle gcov 3.0 non-coverage lines
  249. // Skip empty lines
  250. if ( !nl.size() )
  251. {
  252. continue;
  253. }
  254. // Skip unused lines
  255. if ( nl[0] == '\t' || nl.size() < 12 )
  256. {
  257. continue;
  258. }
  259. std::string prefix = nl.substr(0, 12);
  260. int cov = atoi(prefix.c_str());
  261. (*vec)[cnt] += cov;
  262. }
  263. }
  264. }
  265. }
  266. else
  267. {
  268. cmCTestLog(m_CTest, ERROR_MESSAGE, "Unknown line: " << line->c_str() << std::endl);
  269. error ++;
  270. }
  271. }
  272. }
  273. cmGeneratedFileStream covSumFile;
  274. cmGeneratedFileStream covLogFile;
  275. if (!m_CTest->OpenOutputFile(m_CTest->GetCurrentTag(),
  276. "Coverage.xml", covSumFile, true))
  277. {
  278. cmCTestLog(m_CTest, ERROR_MESSAGE, "Cannot open coverage summary file: Coverage.xml" << std::endl);
  279. return -1;
  280. }
  281. m_CTest->StartXML(covSumFile);
  282. // Produce output xml files
  283. covSumFile << "<Coverage>" << std::endl
  284. << "\t<StartDateTime>" << coverage_start_time << "</StartDateTime>" << std::endl;
  285. int logFileCount = 0;
  286. if ( !this->StartLogFile(covLogFile, logFileCount) )
  287. {
  288. return -1;
  289. }
  290. totalCoverageMap::iterator fileIterator;
  291. int cnt = 0;
  292. long total_tested = 0;
  293. long total_untested = 0;
  294. //std::string fullSourceDir = sourceDir + "/";
  295. //std::string fullBinaryDir = binaryDir + "/";
  296. for ( fileIterator = totalCoverage.begin();
  297. fileIterator != totalCoverage.end();
  298. ++fileIterator )
  299. {
  300. if ( cnt == 100 )
  301. {
  302. cnt = 0;
  303. this->EndLogFile(covLogFile, logFileCount);
  304. logFileCount ++;
  305. if ( !this->StartLogFile(covLogFile, logFileCount) )
  306. {
  307. return -1;
  308. }
  309. }
  310. const std::string fullFileName = fileIterator->first;
  311. const std::string fileName = cmSystemTools::GetFilenameName(fullFileName.c_str());
  312. std::string fullFilePath = cmSystemTools::GetFilenamePath(fullFileName.c_str());
  313. cmCTestLog(m_CTest, ERROR_MESSAGE, "Process file: " << fullFileName << std::endl);
  314. cmSystemTools::ConvertToUnixSlashes(fullFilePath);
  315. if ( !cmSystemTools::FileExists(fullFileName.c_str()) )
  316. {
  317. cmCTestLog(m_CTest, ERROR_MESSAGE, "Cannot find file: " << fullFileName.c_str() << std::endl);
  318. continue;
  319. }
  320. bool shouldIDoCoverage
  321. = this->ShouldIDoCoverage(fullFileName.c_str(),
  322. sourceDir.c_str(), binaryDir.c_str());
  323. if ( !shouldIDoCoverage )
  324. {
  325. cmCTestLog(m_CTest, ERROR_MESSAGE, ".NoDartCoverage found, so skip coverage check for: "
  326. << fullFileName.c_str()
  327. << std::endl);
  328. continue;
  329. }
  330. const singleFileCoverageVector& fcov = fileIterator->second;
  331. covLogFile << "\t<File Name=\""
  332. << m_CTest->MakeXMLSafe(fileName.c_str())
  333. << "\" FullPath=\"" << m_CTest->MakeXMLSafe(m_CTest->GetShortPathToFile(
  334. fileIterator->first.c_str())) << "\">" << std::endl
  335. << "\t\t<Report>" << std::endl;
  336. std::ifstream ifs(fullFileName.c_str());
  337. if ( !ifs)
  338. {
  339. cmCTestLog(m_CTest, ERROR_MESSAGE, "Cannot open source file: " << fullFileName.c_str() << std::endl);
  340. error ++;
  341. continue;
  342. }
  343. int tested = 0;
  344. int untested = 0;
  345. singleFileCoverageVector::size_type cc;
  346. std::string line;
  347. for ( cc= 0; cc < fcov.size(); cc ++ )
  348. {
  349. if ( !cmSystemTools::GetLineFromStream(ifs, line) )
  350. {
  351. cmCTestLog(m_CTest, ERROR_MESSAGE, "Problem reading source file: " << fullFileName.c_str() << " line:" << cc << std::endl);
  352. error ++;
  353. break;
  354. }
  355. covLogFile << "\t\t<Line Number=\"" << cc << "\" Count=\"" << fcov[cc] << "\">"
  356. << m_CTest->MakeXMLSafe(line.c_str()) << "</Line>" << std::endl;
  357. if ( fcov[cc] == 0 )
  358. {
  359. untested ++;
  360. }
  361. else if ( fcov[cc] > 0 )
  362. {
  363. tested ++;
  364. }
  365. }
  366. if ( cmSystemTools::GetLineFromStream(ifs, line) )
  367. {
  368. cmCTestLog(m_CTest, ERROR_MESSAGE, "Looks like there are more lines in the file: " << line << std::endl);
  369. }
  370. float cper = 0;
  371. float cmet = 0;
  372. if ( tested + untested > 0 )
  373. {
  374. cper = (100 * SAFEDIV(static_cast<float>(tested),
  375. static_cast<float>(tested + untested)));
  376. cmet = ( SAFEDIV(static_cast<float>(tested + 10),
  377. static_cast<float>(tested + untested + 10)));
  378. }
  379. total_tested += tested;
  380. total_untested += untested;
  381. covLogFile << "\t\t</Report>" << std::endl
  382. << "\t</File>" << std::endl;
  383. covSumFile << "\t<File Name=\"" << m_CTest->MakeXMLSafe(fileName)
  384. << "\" FullPath=\"" << m_CTest->MakeXMLSafe(
  385. m_CTest->GetShortPathToFile(fullFileName.c_str()))
  386. << "\" Covered=\"" << (cmet>0?"true":"false") << "\">\n"
  387. << "\t\t<LOCTested>" << tested << "</LOCTested>\n"
  388. << "\t\t<LOCUnTested>" << untested << "</LOCUnTested>\n"
  389. << "\t\t<PercentCoverage>";
  390. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  391. covSumFile.precision(2);
  392. covSumFile << (cper) << "</PercentCoverage>\n"
  393. << "\t\t<CoverageMetric>";
  394. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  395. covSumFile.precision(2);
  396. covSumFile << (cmet) << "</CoverageMetric>\n"
  397. << "\t</File>" << std::endl;
  398. cnt ++;
  399. }
  400. this->EndLogFile(covLogFile, logFileCount);
  401. int total_lines = total_tested + total_untested;
  402. float percent_coverage = 100 * SAFEDIV(static_cast<float>(total_tested),
  403. static_cast<float>(total_lines));
  404. if ( total_lines == 0 )
  405. {
  406. percent_coverage = 0;
  407. }
  408. std::string end_time = m_CTest->CurrentTime();
  409. covSumFile << "\t<LOCTested>" << total_tested << "</LOCTested>\n"
  410. << "\t<LOCUntested>" << total_untested << "</LOCUntested>\n"
  411. << "\t<LOC>" << total_lines << "</LOC>\n"
  412. << "\t<PercentCoverage>";
  413. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  414. covSumFile.precision(2);
  415. covSumFile << (percent_coverage)<< "</PercentCoverage>\n"
  416. << "\t<EndDateTime>" << end_time << "</EndDateTime>\n";
  417. covSumFile << "<ElapsedMinutes>" <<
  418. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start)/6)/10.0
  419. << "</ElapsedMinutes>"
  420. << "</Coverage>" << std::endl;
  421. m_CTest->EndXML(covSumFile);
  422. cmCTestLog(m_CTest, HANDLER_OUTPUT, "\tCovered LOC: " << total_tested << std::endl
  423. << "\tNot covered LOC: " << total_untested << std::endl
  424. << "\tTotal LOC: " << total_lines << std::endl
  425. << "\tPercentage Coverage: "
  426. << std::setiosflags(std::ios::fixed)
  427. << std::setprecision(2)
  428. << (percent_coverage) << "%" << std::endl);
  429. cmSystemTools::ChangeDirectory(currentDirectory.c_str());
  430. if ( error )
  431. {
  432. return -1;
  433. }
  434. return 0;
  435. }