cmCTestCoverageHandler.cxx 18 KB

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