cmCTestLaunch.cxx 11 KB

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