cmCTestLaunch.cxx 12 KB

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