cmCTestLaunchReporter.cxx 9.1 KB

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