cmCTestLaunchReporter.cxx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCTestLaunchReporter.h"
  4. #include <utility>
  5. #include "cmsys/FStream.hxx"
  6. #include "cmsys/RegularExpression.hxx"
  7. #include "cmCryptoHash.h"
  8. #include "cmGeneratedFileStream.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmSystemTools.h"
  11. #include "cmXMLWriter.h"
  12. #ifdef _WIN32
  13. # include <cstdio> // for std{out,err} and fileno
  14. # include <fcntl.h> // for _O_BINARY
  15. # include <io.h> // for _setmode
  16. #endif
  17. cmCTestLaunchReporter::cmCTestLaunchReporter()
  18. {
  19. this->Passthru = true;
  20. this->Status.Finished = true;
  21. this->ExitCode = 1;
  22. this->CWD = cmSystemTools::GetLogicalWorkingDirectory();
  23. this->ComputeFileNames();
  24. // Common compiler warning formats. These are much simpler than the
  25. // full log-scraping expressions because we do not need to extract
  26. // file and line information.
  27. this->RegexWarning.emplace_back("(^|[ :])[Ww][Aa][Rr][Nn][Ii][Nn][Gg]");
  28. this->RegexWarning.emplace_back("(^|[ :])[Rr][Ee][Mm][Aa][Rr][Kk]");
  29. this->RegexWarning.emplace_back("(^|[ :])[Nn][Oo][Tt][Ee]");
  30. }
  31. cmCTestLaunchReporter::~cmCTestLaunchReporter()
  32. {
  33. if (!this->Passthru) {
  34. cmSystemTools::RemoveFile(this->LogOut);
  35. cmSystemTools::RemoveFile(this->LogErr);
  36. }
  37. }
  38. void cmCTestLaunchReporter::ComputeFileNames()
  39. {
  40. // We just passthru the behavior of the real command unless the
  41. // CTEST_LAUNCH_LOGS environment variable is set.
  42. std::string d;
  43. if (!cmSystemTools::GetEnv("CTEST_LAUNCH_LOGS", d) || d.empty()) {
  44. return;
  45. }
  46. this->Passthru = false;
  47. // The environment variable specifies the directory into which we
  48. // generate build logs.
  49. this->LogDir = d;
  50. cmSystemTools::ConvertToUnixSlashes(this->LogDir);
  51. this->LogDir += "/";
  52. // We hash the input command working dir and command line to obtain
  53. // a repeatable and (probably) unique name for log files.
  54. cmCryptoHash md5(cmCryptoHash::AlgoMD5);
  55. md5.Initialize();
  56. md5.Append(this->CWD);
  57. for (std::string const& realArg : this->RealArgs) {
  58. md5.Append(realArg);
  59. }
  60. this->LogHash = md5.FinalizeHex();
  61. // We store stdout and stderr in temporary log files.
  62. this->LogOut = cmStrCat(this->LogDir, "launch-", this->LogHash, "-out.txt");
  63. this->LogErr = cmStrCat(this->LogDir, "launch-", this->LogHash, "-err.txt");
  64. }
  65. void cmCTestLaunchReporter::LoadLabels()
  66. {
  67. if (this->OptionBuildDir.empty() || this->OptionTargetName.empty()) {
  68. return;
  69. }
  70. // Labels are listed in per-target files.
  71. std::string fname = cmStrCat(this->OptionBuildDir, "/CMakeFiles/",
  72. this->OptionTargetName, ".dir/Labels.txt");
  73. // We are interested in per-target labels for this source file.
  74. std::string source = this->OptionSource;
  75. cmSystemTools::ConvertToUnixSlashes(source);
  76. // Load the labels file.
  77. cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary);
  78. if (!fin) {
  79. return;
  80. }
  81. bool inTarget = true;
  82. bool inSource = false;
  83. std::string line;
  84. while (cmSystemTools::GetLineFromStream(fin, line)) {
  85. if (line.empty() || line[0] == '#') {
  86. // Ignore blank and comment lines.
  87. continue;
  88. }
  89. if (line[0] == ' ') {
  90. // Label lines appear indented by one space.
  91. if (inTarget || inSource) {
  92. this->Labels.insert(line.substr(1));
  93. }
  94. } else if (!this->OptionSource.empty() && !inSource) {
  95. // Non-indented lines specify a source file name. The first one
  96. // is the end of the target-wide labels. Use labels following a
  97. // matching source.
  98. inTarget = false;
  99. inSource = this->SourceMatches(line, source);
  100. } else {
  101. return;
  102. }
  103. }
  104. }
  105. bool cmCTestLaunchReporter::SourceMatches(std::string const& lhs,
  106. std::string const& rhs)
  107. {
  108. // TODO: Case sensitivity, UseRelativePaths, etc. Note that both
  109. // paths in the comparison get generated by CMake. This is done for
  110. // every source in the target, so it should be efficient (cannot use
  111. // cmSystemTools::IsSameFile).
  112. return lhs == rhs;
  113. }
  114. bool cmCTestLaunchReporter::IsError() const
  115. {
  116. return this->ExitCode != 0;
  117. }
  118. void cmCTestLaunchReporter::WriteXML()
  119. {
  120. // Name the xml file.
  121. std::string logXML =
  122. cmStrCat(this->LogDir, this->IsError() ? "error-" : "warning-",
  123. this->LogHash, ".xml");
  124. // Use cmGeneratedFileStream to atomically create the report file.
  125. cmGeneratedFileStream fxml(logXML);
  126. cmXMLWriter xml(fxml, 2);
  127. cmXMLElement e2(xml, "Failure");
  128. e2.Attribute("type", this->IsError() ? "Error" : "Warning");
  129. this->WriteXMLAction(e2);
  130. this->WriteXMLCommand(e2);
  131. this->WriteXMLResult(e2);
  132. this->WriteXMLLabels(e2);
  133. }
  134. void cmCTestLaunchReporter::WriteXMLAction(cmXMLElement& e2) const
  135. {
  136. e2.Comment("Meta-information about the build action");
  137. cmXMLElement e3(e2, "Action");
  138. // TargetName
  139. if (!this->OptionTargetName.empty()) {
  140. e3.Element("TargetName", this->OptionTargetName);
  141. }
  142. // Language
  143. if (!this->OptionLanguage.empty()) {
  144. e3.Element("Language", this->OptionLanguage);
  145. }
  146. // SourceFile
  147. if (!this->OptionSource.empty()) {
  148. std::string source = this->OptionSource;
  149. cmSystemTools::ConvertToUnixSlashes(source);
  150. // If file is in source tree use its relative location.
  151. if (cmSystemTools::FileIsFullPath(this->SourceDir) &&
  152. cmSystemTools::FileIsFullPath(source) &&
  153. cmSystemTools::IsSubDirectory(source, this->SourceDir)) {
  154. source = cmSystemTools::RelativePath(this->SourceDir, source);
  155. }
  156. e3.Element("SourceFile", source);
  157. }
  158. // OutputFile
  159. if (!this->OptionOutput.empty()) {
  160. e3.Element("OutputFile", this->OptionOutput);
  161. }
  162. // OutputType
  163. const char* outputType = nullptr;
  164. if (!this->OptionTargetType.empty()) {
  165. if (this->OptionTargetType == "EXECUTABLE") {
  166. outputType = "executable";
  167. } else if (this->OptionTargetType == "SHARED_LIBRARY") {
  168. outputType = "shared library";
  169. } else if (this->OptionTargetType == "MODULE_LIBRARY") {
  170. outputType = "module library";
  171. } else if (this->OptionTargetType == "STATIC_LIBRARY") {
  172. outputType = "static library";
  173. }
  174. } else if (!this->OptionSource.empty()) {
  175. outputType = "object file";
  176. }
  177. if (outputType) {
  178. e3.Element("OutputType", outputType);
  179. }
  180. }
  181. void cmCTestLaunchReporter::WriteXMLCommand(cmXMLElement& e2)
  182. {
  183. e2.Comment("Details of command");
  184. cmXMLElement e3(e2, "Command");
  185. if (!this->CWD.empty()) {
  186. e3.Element("WorkingDirectory", this->CWD);
  187. }
  188. for (std::string const& realArg : this->RealArgs) {
  189. e3.Element("Argument", realArg);
  190. }
  191. }
  192. void cmCTestLaunchReporter::WriteXMLResult(cmXMLElement& e2)
  193. {
  194. e2.Comment("Result of command");
  195. cmXMLElement e3(e2, "Result");
  196. // StdOut
  197. this->DumpFileToXML(e3, "StdOut", this->LogOut);
  198. // StdErr
  199. this->DumpFileToXML(e3, "StdErr", this->LogErr);
  200. // ExitCondition
  201. cmXMLElement e4(e3, "ExitCondition");
  202. if (this->Status.Finished) {
  203. auto exception = this->Status.GetException();
  204. switch (exception.first) {
  205. case cmUVProcessChain::ExceptionCode::None:
  206. e4.Content(this->ExitCode);
  207. break;
  208. case cmUVProcessChain::ExceptionCode::Spawn:
  209. e4.Content("Error administrating child process: ");
  210. e4.Content(exception.second);
  211. break;
  212. default:
  213. e4.Content("Terminated abnormally: ");
  214. e4.Content(exception.second);
  215. break;
  216. }
  217. } else {
  218. e4.Content("Killed when timeout expired");
  219. }
  220. }
  221. void cmCTestLaunchReporter::WriteXMLLabels(cmXMLElement& e2)
  222. {
  223. this->LoadLabels();
  224. if (!this->Labels.empty()) {
  225. e2.Comment("Interested parties");
  226. cmXMLElement e3(e2, "Labels");
  227. for (std::string const& label : this->Labels) {
  228. e3.Element("Label", label);
  229. }
  230. }
  231. }
  232. void cmCTestLaunchReporter::DumpFileToXML(cmXMLElement& e3, const char* tag,
  233. std::string const& fname)
  234. {
  235. cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary);
  236. std::string line;
  237. const char* sep = "";
  238. cmXMLElement e4(e3, tag);
  239. while (cmSystemTools::GetLineFromStream(fin, line)) {
  240. if (this->MatchesFilterPrefix(line)) {
  241. continue;
  242. }
  243. if (this->Match(line, this->RegexWarningSuppress)) {
  244. line = cmStrCat("[CTest: warning suppressed] ", line);
  245. } else if (this->Match(line, this->RegexWarning)) {
  246. line = cmStrCat("[CTest: warning matched] ", line);
  247. }
  248. e4.Content(sep);
  249. e4.Content(line);
  250. sep = "\n";
  251. }
  252. }
  253. bool cmCTestLaunchReporter::Match(
  254. std::string const& line, std::vector<cmsys::RegularExpression>& regexps)
  255. {
  256. for (cmsys::RegularExpression& r : regexps) {
  257. if (r.find(line)) {
  258. return true;
  259. }
  260. }
  261. return false;
  262. }
  263. bool cmCTestLaunchReporter::MatchesFilterPrefix(std::string const& line) const
  264. {
  265. return !this->OptionFilterPrefix.empty() &&
  266. cmHasPrefix(line, this->OptionFilterPrefix);
  267. }