cmCTestLaunch.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 "cmCTestLaunch.h"
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <iostream>
  7. #include <map>
  8. #include <memory>
  9. #include <utility>
  10. #include <cm3p/uv.h>
  11. #include "cmsys/FStream.hxx"
  12. #include "cmsys/RegularExpression.hxx"
  13. #include "cmCTestLaunchReporter.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmInstrumentation.h"
  16. #include "cmMakefile.h"
  17. #include "cmProcessOutput.h"
  18. #include "cmState.h"
  19. #include "cmStateSnapshot.h"
  20. #include "cmStringAlgorithms.h"
  21. #include "cmSystemTools.h"
  22. #include "cmUVHandlePtr.h"
  23. #include "cmUVProcessChain.h"
  24. #include "cmUVStream.h"
  25. #include "cmake.h"
  26. #ifdef _WIN32
  27. # include <cstdio> // for std{out,err} and fileno
  28. # include <fcntl.h> // for _O_BINARY
  29. # include <io.h> // for _setmode
  30. #endif
  31. cmCTestLaunch::cmCTestLaunch(int argc, char const* const* argv, Op operation)
  32. {
  33. if (!this->ParseArguments(argc, argv)) {
  34. return;
  35. }
  36. this->Reporter.RealArgs = this->RealArgs;
  37. this->Reporter.ComputeFileNames();
  38. this->ScrapeRulesLoaded = false;
  39. this->HaveOut = false;
  40. this->HaveErr = false;
  41. this->Operation = operation;
  42. }
  43. cmCTestLaunch::~cmCTestLaunch() = default;
  44. bool cmCTestLaunch::ParseArguments(int argc, char const* const* argv)
  45. {
  46. // Launcher options occur first and are separated from the real
  47. // command line by a '--' option.
  48. enum Doing
  49. {
  50. DoingNone,
  51. DoingOutput,
  52. DoingSource,
  53. DoingLanguage,
  54. DoingTargetLabels,
  55. DoingTargetName,
  56. DoingTargetType,
  57. DoingCommandType,
  58. DoingRole,
  59. DoingBuildDir,
  60. DoingCurrentBuildDir,
  61. DoingCount,
  62. DoingFilterPrefix
  63. };
  64. Doing doing = DoingNone;
  65. int arg0 = 0;
  66. for (int i = 1; !arg0 && i < argc; ++i) {
  67. char const* arg = argv[i];
  68. if (strcmp(arg, "--") == 0) {
  69. arg0 = i + 1;
  70. } else if (strcmp(arg, "--command-type") == 0) {
  71. doing = DoingCommandType;
  72. } else if (strcmp(arg, "--output") == 0) {
  73. doing = DoingOutput;
  74. } else if (strcmp(arg, "--source") == 0) {
  75. doing = DoingSource;
  76. } else if (strcmp(arg, "--language") == 0) {
  77. doing = DoingLanguage;
  78. } else if (strcmp(arg, "--target-labels") == 0) {
  79. doing = DoingTargetLabels;
  80. } else if (strcmp(arg, "--target-name") == 0) {
  81. doing = DoingTargetName;
  82. } else if (strcmp(arg, "--target-type") == 0) {
  83. doing = DoingTargetType;
  84. } else if (strcmp(arg, "--role") == 0) {
  85. doing = DoingRole;
  86. } else if (strcmp(arg, "--build-dir") == 0) {
  87. doing = DoingBuildDir;
  88. } else if (strcmp(arg, "--current-build-dir") == 0) {
  89. doing = DoingCurrentBuildDir;
  90. } else if (strcmp(arg, "--filter-prefix") == 0) {
  91. doing = DoingFilterPrefix;
  92. } else if (doing == DoingOutput) {
  93. this->Reporter.OptionOutput = arg;
  94. doing = DoingNone;
  95. } else if (doing == DoingSource) {
  96. this->Reporter.OptionSource = arg;
  97. doing = DoingNone;
  98. } else if (doing == DoingLanguage) {
  99. this->Reporter.OptionLanguage = arg;
  100. if (this->Reporter.OptionLanguage == "CXX") {
  101. this->Reporter.OptionLanguage = "C++";
  102. }
  103. doing = DoingNone;
  104. } else if (doing == DoingTargetLabels) {
  105. this->Reporter.OptionTargetLabels = arg;
  106. doing = DoingNone;
  107. } else if (doing == DoingTargetName) {
  108. this->Reporter.OptionTargetName = arg;
  109. doing = DoingNone;
  110. } else if (doing == DoingTargetType) {
  111. this->Reporter.OptionTargetType = arg;
  112. doing = DoingNone;
  113. } else if (doing == DoingBuildDir) {
  114. this->Reporter.OptionBuildDir = arg;
  115. doing = DoingNone;
  116. } else if (doing == DoingCurrentBuildDir) {
  117. this->Reporter.OptionCurrentBuildDir = arg;
  118. doing = DoingNone;
  119. } else if (doing == DoingFilterPrefix) {
  120. this->Reporter.OptionFilterPrefix = arg;
  121. doing = DoingNone;
  122. } else if (doing == DoingCommandType) {
  123. this->Reporter.OptionCommandType = arg;
  124. doing = DoingNone;
  125. } else if (doing == DoingRole) {
  126. this->Reporter.OptionRole = arg;
  127. doing = DoingNone;
  128. }
  129. }
  130. // Extract the real command line.
  131. if (arg0) {
  132. for (int i = 0; i < argc - arg0; ++i) {
  133. this->RealArgV.emplace_back((argv + arg0)[i]);
  134. this->HandleRealArg((argv + arg0)[i]);
  135. }
  136. return true;
  137. }
  138. std::cerr << "No launch/command separator ('--') found!\n";
  139. return false;
  140. }
  141. void cmCTestLaunch::HandleRealArg(char const* arg)
  142. {
  143. #ifdef _WIN32
  144. // Expand response file arguments.
  145. if (arg[0] == '@' && cmSystemTools::FileExists(arg + 1)) {
  146. cmsys::ifstream fin(arg + 1);
  147. std::string line;
  148. while (cmSystemTools::GetLineFromStream(fin, line)) {
  149. cmSystemTools::ParseWindowsCommandLine(line.c_str(), this->RealArgs);
  150. }
  151. return;
  152. }
  153. #endif
  154. this->RealArgs.emplace_back(arg);
  155. }
  156. void cmCTestLaunch::RunChild()
  157. {
  158. // Ignore noopt make rules
  159. if (this->RealArgs.empty() || this->RealArgs[0] == ":") {
  160. this->Reporter.ExitCode = 0;
  161. return;
  162. }
  163. // Prepare to run the real command.
  164. cmUVProcessChainBuilder builder;
  165. builder.AddCommand(this->RealArgV);
  166. cmsys::ofstream fout;
  167. cmsys::ofstream ferr;
  168. if (this->Reporter.Passthru) {
  169. // In passthru mode we just share the output pipes.
  170. builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, stdout)
  171. .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, stderr);
  172. } else {
  173. // In full mode we record the child output pipes to log files.
  174. builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
  175. .SetBuiltinStream(cmUVProcessChainBuilder::Stream_ERROR);
  176. fout.open(this->Reporter.LogOut.c_str(), std::ios::out | std::ios::binary);
  177. ferr.open(this->Reporter.LogErr.c_str(), std::ios::out | std::ios::binary);
  178. }
  179. #ifdef _WIN32
  180. // Do this so that newline transformation is not done when writing to cout
  181. // and cerr below.
  182. _setmode(fileno(stdout), _O_BINARY);
  183. _setmode(fileno(stderr), _O_BINARY);
  184. #endif
  185. // Run the real command.
  186. auto chain = builder.Start();
  187. // Record child stdout and stderr if necessary.
  188. cm::uv_pipe_ptr outPipe;
  189. cm::uv_pipe_ptr errPipe;
  190. bool outFinished = true;
  191. bool errFinished = true;
  192. cmProcessOutput processOutput;
  193. std::unique_ptr<cmUVStreamReadHandle> outputHandle;
  194. std::unique_ptr<cmUVStreamReadHandle> errorHandle;
  195. if (!this->Reporter.Passthru) {
  196. auto beginRead = [&chain, &processOutput](
  197. cm::uv_pipe_ptr& pipe, int stream, std::ostream& out,
  198. cmsys::ofstream& file, bool& haveData, bool& finished,
  199. int id) -> std::unique_ptr<cmUVStreamReadHandle> {
  200. pipe.init(chain.GetLoop(), 0);
  201. uv_pipe_open(pipe, stream);
  202. finished = false;
  203. return cmUVStreamRead(
  204. pipe,
  205. [&processOutput, &out, &file, id, &haveData](std::vector<char> data) {
  206. std::string strdata;
  207. processOutput.DecodeText(data.data(), data.size(), strdata, id);
  208. file.write(strdata.c_str(), strdata.size());
  209. out.write(strdata.c_str(), strdata.size());
  210. haveData = true;
  211. },
  212. [&processOutput, &out, &file, &finished, id]() {
  213. std::string strdata;
  214. processOutput.DecodeText(std::string(), strdata, id);
  215. if (!strdata.empty()) {
  216. file.write(strdata.c_str(), strdata.size());
  217. out.write(strdata.c_str(), strdata.size());
  218. }
  219. finished = true;
  220. });
  221. };
  222. outputHandle = beginRead(outPipe, chain.OutputStream(), std::cout, fout,
  223. this->HaveOut, outFinished, 1);
  224. errorHandle = beginRead(errPipe, chain.ErrorStream(), std::cerr, ferr,
  225. this->HaveErr, errFinished, 2);
  226. }
  227. // Wait for the real command to finish.
  228. while (!(chain.Finished() && outFinished && errFinished)) {
  229. uv_run(&chain.GetLoop(), UV_RUN_ONCE);
  230. }
  231. this->Reporter.Status = chain.GetStatus(0);
  232. if (this->Reporter.Status.GetException().first ==
  233. cmUVProcessChain::ExceptionCode::Spawn) {
  234. this->Reporter.ExitCode = 1;
  235. } else {
  236. this->Reporter.ExitCode =
  237. static_cast<int>(this->Reporter.Status.ExitStatus);
  238. }
  239. }
  240. int cmCTestLaunch::Run()
  241. {
  242. auto instrumenter = cmInstrumentation(this->Reporter.OptionBuildDir);
  243. std::map<std::string, std::string> options;
  244. options["target"] = this->Reporter.OptionTargetName;
  245. options["source"] = this->Reporter.OptionSource;
  246. options["language"] = this->Reporter.OptionLanguage;
  247. options["targetType"] = this->Reporter.OptionTargetType;
  248. options["role"] = this->Reporter.OptionRole;
  249. std::map<std::string, std::string> arrayOptions;
  250. arrayOptions["outputs"] = this->Reporter.OptionOutput;
  251. arrayOptions["targetLabels"] = this->Reporter.OptionTargetLabels;
  252. instrumenter.InstrumentCommand(
  253. this->Reporter.OptionCommandType, this->RealArgV,
  254. [this]() -> int {
  255. this->RunChild();
  256. return 0;
  257. },
  258. options, arrayOptions);
  259. if (this->Operation == Op::Normal) {
  260. if (this->CheckResults()) {
  261. return this->Reporter.ExitCode;
  262. }
  263. this->LoadConfig();
  264. this->Reporter.WriteXML();
  265. }
  266. return this->Reporter.ExitCode;
  267. }
  268. bool cmCTestLaunch::CheckResults()
  269. {
  270. // Skip XML in passthru mode.
  271. if (this->Reporter.Passthru) {
  272. return true;
  273. }
  274. // We always report failure for error conditions.
  275. if (this->Reporter.IsError()) {
  276. return false;
  277. }
  278. // Scrape the output logs to look for warnings.
  279. if ((this->HaveErr && this->ScrapeLog(this->Reporter.LogErr)) ||
  280. (this->HaveOut && this->ScrapeLog(this->Reporter.LogOut))) {
  281. return false;
  282. }
  283. return true;
  284. }
  285. void cmCTestLaunch::LoadScrapeRules()
  286. {
  287. if (this->ScrapeRulesLoaded) {
  288. return;
  289. }
  290. this->ScrapeRulesLoaded = true;
  291. // Load custom match rules given to us by CTest.
  292. this->LoadScrapeRules("Warning", this->Reporter.RegexWarning);
  293. this->LoadScrapeRules("WarningSuppress",
  294. this->Reporter.RegexWarningSuppress);
  295. }
  296. void cmCTestLaunch::LoadScrapeRules(
  297. char const* purpose, std::vector<cmsys::RegularExpression>& regexps) const
  298. {
  299. std::string fname =
  300. cmStrCat(this->Reporter.LogDir, "Custom", purpose, ".txt");
  301. cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary);
  302. std::string line;
  303. cmsys::RegularExpression rex;
  304. while (cmSystemTools::GetLineFromStream(fin, line)) {
  305. if (rex.compile(line)) {
  306. regexps.push_back(rex);
  307. }
  308. }
  309. }
  310. bool cmCTestLaunch::ScrapeLog(std::string const& fname)
  311. {
  312. this->LoadScrapeRules();
  313. // Look for log file lines matching warning expressions but not
  314. // suppression expressions.
  315. cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary);
  316. std::string line;
  317. while (cmSystemTools::GetLineFromStream(fin, line)) {
  318. if (this->Reporter.MatchesFilterPrefix(line)) {
  319. continue;
  320. }
  321. if (this->Reporter.Match(line, this->Reporter.RegexWarning) &&
  322. !this->Reporter.Match(line, this->Reporter.RegexWarningSuppress)) {
  323. return true;
  324. }
  325. }
  326. return false;
  327. }
  328. int cmCTestLaunch::Main(int argc, char const* const argv[], Op operation)
  329. {
  330. if (argc == 2) {
  331. std::cerr << "ctest --launch: this mode is for internal CTest use only"
  332. << std::endl;
  333. return 1;
  334. }
  335. cmCTestLaunch self(argc, argv, operation);
  336. return self.Run();
  337. }
  338. void cmCTestLaunch::LoadConfig()
  339. {
  340. cmake cm(cmake::RoleScript, cmState::CTest);
  341. cm.SetHomeDirectory("");
  342. cm.SetHomeOutputDirectory("");
  343. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  344. cmGlobalGenerator gg(&cm);
  345. cmMakefile mf(&gg, cm.GetCurrentSnapshot());
  346. std::string fname =
  347. cmStrCat(this->Reporter.LogDir, "CTestLaunchConfig.cmake");
  348. if (cmSystemTools::FileExists(fname) && mf.ReadListFile(fname)) {
  349. this->Reporter.SourceDir = mf.GetSafeDefinition("CTEST_SOURCE_DIRECTORY");
  350. cmSystemTools::ConvertToUnixSlashes(this->Reporter.SourceDir);
  351. }
  352. }