cmCTest.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <chrono>
  6. #include <ctime>
  7. #include <map>
  8. #include <memory>
  9. #include <sstream>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. #include <cm/optional>
  14. #include <cm/string_view>
  15. #include "cmDuration.h"
  16. #include "cmProcessOutput.h"
  17. class cmake;
  18. class cmGeneratedFileStream;
  19. class cmInstrumentation;
  20. class cmMakefile;
  21. class cmValue;
  22. class cmXMLWriter;
  23. struct cmCTestTestOptions;
  24. /** \class cmCTest
  25. * \brief Represents a ctest invocation.
  26. *
  27. * This class represents a ctest invocation. It is the top level class when
  28. * running ctest.
  29. *
  30. */
  31. class cmCTest
  32. {
  33. public:
  34. using Encoding = cmProcessOutput::Encoding;
  35. /** Enumerate parts of the testing and submission process. */
  36. enum Part
  37. {
  38. PartStart,
  39. PartUpdate,
  40. PartConfigure,
  41. PartBuild,
  42. PartTest,
  43. PartCoverage,
  44. PartMemCheck,
  45. PartSubmit,
  46. PartNotes,
  47. PartExtraFiles,
  48. PartUpload,
  49. PartDone,
  50. PartCount // Update names in constructor when adding a part
  51. };
  52. /** Get a testing part id from its string name. Returns PartCount
  53. if the string does not name a valid part. */
  54. Part GetPartFromName(std::string const& name);
  55. /** Process Command line arguments */
  56. int Run(std::vector<std::string> const& args);
  57. /** Initialize a dashboard run in the given build tree. */
  58. void Initialize(std::string const& binary_dir);
  59. bool CreateNewTag(bool quiet);
  60. bool ReadExistingTag(bool quiet);
  61. /**
  62. * Process the dashboard client steps.
  63. */
  64. int ProcessSteps();
  65. /**
  66. * A utility function that returns the nightly time
  67. */
  68. struct tm* GetNightlyTime(std::string const& str, bool tomorrowtag);
  69. /**
  70. * Is the tomorrow tag set?
  71. */
  72. bool GetTomorrowTag() const;
  73. /**
  74. * Try to run tests of the project
  75. */
  76. int TestDirectory(bool memcheck);
  77. /** what is the configuration type, e.g. Debug, Release etc. */
  78. std::string const& GetConfigType();
  79. cmDuration GetTimeOut() const;
  80. void SetTimeOut(cmDuration t);
  81. cmDuration GetGlobalTimeout() const;
  82. /** how many test to run at the same time */
  83. cm::optional<size_t> GetParallelLevel() const;
  84. void SetParallelLevel(cm::optional<size_t> level);
  85. unsigned long GetTestLoad() const;
  86. void SetTestLoad(unsigned long);
  87. /**
  88. * Check if CTest file exists
  89. */
  90. bool CTestFileExists(std::string const& filename);
  91. bool AddIfExists(Part part, std::string const& file);
  92. /**
  93. * Set the cmake test
  94. */
  95. bool SetTest(std::string const&, bool report = true);
  96. /**
  97. * Set the cmake test mode (experimental, nightly, continuous).
  98. */
  99. void SetTestModel(int mode);
  100. int GetTestModel() const;
  101. std::string GetTestModelString() const;
  102. std::string GetTestGroupString() const;
  103. static int GetTestModelFromString(std::string const& str);
  104. static std::string CleanString(std::string const& str,
  105. std::string::size_type spos = 0);
  106. std::string GetCTestConfiguration(std::string const& name);
  107. void SetCTestConfiguration(char const* name, std::string const& value,
  108. bool suppress = false);
  109. void EmptyCTestConfiguration();
  110. std::string GetSubmitURL();
  111. /**
  112. * constructor and destructor
  113. */
  114. cmCTest();
  115. ~cmCTest();
  116. cmCTest(cmCTest const&) = delete;
  117. cmCTest& operator=(cmCTest const&) = delete;
  118. /** Set the notes files to be created. */
  119. void SetNotesFiles(std::string const& notes);
  120. void PopulateCustomVector(cmMakefile* mf, std::string const& definition,
  121. std::vector<std::string>& vec);
  122. void PopulateCustomInteger(cmMakefile* mf, std::string const& def, int& val);
  123. /** Get the current time as string */
  124. std::string CurrentTime();
  125. /** tar/gzip and then base 64 encode a file */
  126. std::string Base64GzipEncodeFile(std::string const& file);
  127. /** base64 encode a file */
  128. std::string Base64EncodeFile(std::string const& file);
  129. void SetTimeLimit(cmValue val);
  130. cmDuration GetElapsedTime() const;
  131. /**
  132. * Return the time remaining that the script is allowed to run in
  133. * seconds if the user has set the variable CTEST_TIME_LIMIT. If that has
  134. * not been set it returns a very large duration.
  135. */
  136. cmDuration GetRemainingTimeAllowed() const;
  137. static cmDuration MaxDuration();
  138. /**
  139. * Open file in the output directory and set the stream
  140. */
  141. bool OpenOutputFile(std::string const& path, std::string const& name,
  142. cmGeneratedFileStream& stream, bool compress = false);
  143. /** Should we only show what we would do? */
  144. bool GetShowOnly();
  145. bool GetOutputAsJson();
  146. int GetOutputAsJsonVersion();
  147. bool ShouldUseHTTP10() const;
  148. bool ShouldPrintLabels() const;
  149. bool ShouldCompressTestOutput();
  150. bool CompressString(std::string& str);
  151. bool GetStopOnFailure() const;
  152. void SetStopOnFailure(bool stop);
  153. std::chrono::system_clock::time_point GetStopTime() const;
  154. void SetStopTime(std::string const& time);
  155. /** Used for parallel ctest job scheduling */
  156. std::string GetScheduleType() const;
  157. void SetScheduleType(std::string const& type);
  158. /** The max output width */
  159. int GetMaxTestNameWidth() const;
  160. void SetMaxTestNameWidth(int w);
  161. /**
  162. * Run a single executable command and put the stdout and stderr
  163. * in output.
  164. *
  165. * If verbose is false, no user-viewable output from the program
  166. * being run will be generated.
  167. *
  168. * If timeout is specified, the command will be terminated after
  169. * timeout expires. Timeout is specified in seconds.
  170. *
  171. * Argument retVal should be a pointer to the location where the
  172. * exit code will be stored. If the retVal is not specified and
  173. * the program exits with a code other than 0, then the this
  174. * function will return false.
  175. */
  176. bool RunCommand(std::vector<std::string> const& args, std::string* stdOut,
  177. std::string* stdErr, int* retVal = nullptr,
  178. char const* dir = nullptr,
  179. cmDuration timeout = cmDuration::zero(),
  180. Encoding encoding = cmProcessOutput::Auto);
  181. /**
  182. * Clean/make safe for xml the given value such that it may be used as
  183. * one of the key fields by CDash when computing the buildid.
  184. */
  185. static std::string SafeBuildIdField(std::string const& value);
  186. /** Start CTest XML output file */
  187. void StartXML(cmXMLWriter& xml, cmake* cm, bool append);
  188. /** End CTest XML output file */
  189. void EndXML(cmXMLWriter& xml);
  190. /**
  191. * Run command specialized for make and configure. Returns process status
  192. * and retVal is return value or exception.
  193. */
  194. bool RunMakeCommand(std::string const& command, std::string& output,
  195. int* retVal, char const* dir, cmDuration timeout,
  196. std::ostream& ofs,
  197. Encoding encoding = cmProcessOutput::Auto);
  198. /** Return the current tag */
  199. std::string GetCurrentTag();
  200. /** Get the path to the build tree */
  201. std::string GetBinaryDir();
  202. /**
  203. * Get the short path to the file.
  204. *
  205. * This means if the file is in binary or
  206. * source directory, it will become /.../relative/path/to/file
  207. */
  208. std::string GetShortPathToFile(std::string const& fname);
  209. enum
  210. {
  211. UNKNOWN = -1,
  212. EXPERIMENTAL = 0,
  213. NIGHTLY = 1,
  214. CONTINUOUS = 2,
  215. };
  216. /** provide some more detailed info on the return code for ctest */
  217. enum
  218. {
  219. UPDATE_ERRORS = 0x01,
  220. CONFIGURE_ERRORS = 0x02,
  221. BUILD_ERRORS = 0x04,
  222. TEST_ERRORS = 0x08,
  223. MEMORY_ERRORS = 0x10,
  224. COVERAGE_ERRORS = 0x20,
  225. SUBMIT_ERRORS = 0x40
  226. };
  227. /** Are we producing XML */
  228. bool GetProduceXML();
  229. void SetProduceXML(bool v);
  230. /**
  231. * Set the CTest variable from CMake variable
  232. */
  233. bool SetCTestConfigurationFromCMakeVariable(cmMakefile* mf,
  234. char const* dconfig,
  235. std::string const& cmake_var,
  236. bool suppress = false);
  237. /**
  238. * Set CMake variables from CTest Options
  239. */
  240. void SetCMakeVariables(cmMakefile& mf);
  241. /** Decode a URL to the original string. */
  242. static std::string DecodeURL(std::string const&);
  243. /**
  244. * Should ctect configuration be updated. When using new style ctest
  245. * script, this should be true.
  246. */
  247. void SetSuppressUpdatingCTestConfiguration(bool val);
  248. /**
  249. * Add overwrite to ctest configuration.
  250. *
  251. * The format is key=value
  252. */
  253. void AddCTestConfigurationOverwrite(std::string const& encstr);
  254. /** Create XML file that contains all the notes specified */
  255. int GenerateNotesFile(cmake* cm, std::vector<std::string> const& files);
  256. /** Create XML file to indicate that build is complete */
  257. int GenerateDoneFile();
  258. /** Submit extra files to the server */
  259. bool SubmitExtraFiles(std::string const& files);
  260. bool SubmitExtraFiles(std::vector<std::string> const& files);
  261. /** Set the output log file name */
  262. void SetOutputLogFileName(std::string const& name);
  263. /** Set the output JUnit file name */
  264. void SetOutputJUnitFileName(std::string const& name);
  265. /** Set the visual studio or Xcode config type */
  266. void SetConfigType(std::string const& ct);
  267. /** Various log types */
  268. enum LogType
  269. {
  270. DEBUG,
  271. OUTPUT,
  272. HANDLER_OUTPUT,
  273. HANDLER_PROGRESS_OUTPUT,
  274. HANDLER_TEST_PROGRESS_OUTPUT,
  275. HANDLER_VERBOSE_OUTPUT,
  276. WARNING,
  277. ERROR_MESSAGE,
  278. };
  279. /** Add log to the output */
  280. void Log(LogType logType, std::string msg, bool suppress = false);
  281. /** Color values */
  282. enum class Color
  283. {
  284. CLEAR_COLOR = 0,
  285. RED = 31,
  286. GREEN = 32,
  287. YELLOW = 33,
  288. BLUE = 34
  289. };
  290. /** Get color code characters for a specific color */
  291. std::string GetColorCode(Color color) const;
  292. /** The Build ID is assigned by CDash */
  293. void SetBuildID(std::string const& id);
  294. std::string GetBuildID() const;
  295. /** Add file to be submitted */
  296. void AddSubmitFile(Part part, std::string const& name);
  297. std::vector<std::string> const& GetSubmitFiles(Part part) const;
  298. void ClearSubmitFiles(Part part);
  299. /**
  300. * Read the custom configuration files and apply them to the current ctest
  301. */
  302. void ReadCustomConfigurationFileTree(std::string const& dir, cmMakefile* mf);
  303. std::vector<std::string>& GetInitialCommandLineArguments();
  304. /** Set the group to submit to */
  305. void SetSpecificGroup(char const* group);
  306. char const* GetSpecificGroup();
  307. void SetFailover(bool failover);
  308. bool GetFailover() const;
  309. bool GetTestProgressOutput() const;
  310. bool GetVerbose() const;
  311. bool GetExtraVerbose() const;
  312. bool StartResultingXML(Part part, char const* name, int submitIndex,
  313. cmGeneratedFileStream& xofs);
  314. bool StartLogFile(char const* name, int submitIndex,
  315. cmGeneratedFileStream& xofs);
  316. void ConvertInstrumentationSnippetsToXML(cmXMLWriter& xml,
  317. std::string const& subdir);
  318. bool ConvertInstrumentationJSONFileToXML(std::string const& fpath,
  319. cmXMLWriter& xml);
  320. void AddSiteProperties(cmXMLWriter& xml, cmake* cm);
  321. bool GetInteractiveDebugMode() const;
  322. bool GetLabelSummary() const;
  323. bool GetSubprojectSummary() const;
  324. std::string GetCostDataFile();
  325. bool GetOutputTestOutputOnTestFailure() const;
  326. std::map<std::string, std::string> const& GetDefinitions() const;
  327. /** Return the number of times a test should be run */
  328. int GetRepeatCount() const;
  329. enum class Repeat
  330. {
  331. Never,
  332. UntilFail,
  333. UntilPass,
  334. AfterTimeout,
  335. };
  336. Repeat GetRepeatMode() const;
  337. enum class NoTests
  338. {
  339. Legacy,
  340. Error,
  341. Ignore
  342. };
  343. NoTests GetNoTestsMode() const;
  344. void GenerateSubprojectsOutput(cmXMLWriter& xml);
  345. std::vector<std::string> GetLabelsForSubprojects();
  346. /** Reread the configuration file */
  347. bool UpdateCTestConfiguration();
  348. cmCTestTestOptions const& GetTestOptions() const;
  349. std::vector<std::string> GetCommandLineHttpHeaders() const;
  350. cmInstrumentation& GetInstrumentation();
  351. bool GetUseVerboseInstrumentation() const;
  352. private:
  353. int GenerateNotesFile(cmake* cm, std::string const& files);
  354. void BlockTestErrorDiagnostics();
  355. /** parse the option after -D and convert it into the appropriate steps */
  356. bool AddTestsForDashboardType(std::string const& targ);
  357. /** read as "emit an error message for an unknown -D value" */
  358. void ErrorMessageUnknownDashDValue(std::string const& val);
  359. /** add a variable definition from a command line -D value */
  360. bool AddVariableDefinition(std::string const& arg);
  361. /** set command line arguments read from a test preset */
  362. bool SetArgsFromPreset(std::string const& presetName, bool listPresets);
  363. #if !defined(_WIN32)
  364. /** returns true iff the console supports progress output */
  365. static bool ConsoleIsNotDumb();
  366. #endif
  367. /** returns true iff the console supports progress output */
  368. static bool ProgressOutputSupportedByConsole();
  369. /** returns true iff the console supports colored output */
  370. static bool ColoredOutputSupportedByConsole();
  371. /** Create note from files. */
  372. int GenerateCTestNotesOutput(cmXMLWriter& xml, cmake* cm,
  373. std::vector<std::string> const& files);
  374. /** Check if the argument is the one specified */
  375. static bool CheckArgument(std::string const& arg, cm::string_view varg1,
  376. char const* varg2 = nullptr);
  377. int RunCMakeAndTest();
  378. int RunScripts(std::vector<std::pair<std::string, bool>> const& scripts);
  379. int ExecuteTests();
  380. struct Private;
  381. std::unique_ptr<Private> Impl;
  382. };
  383. #define cmCTestLog(ctSelf, logType, msg) \
  384. do { \
  385. std::ostringstream cmCTestLog_msg; \
  386. cmCTestLog_msg << msg; \
  387. (ctSelf)->Log(cmCTest::logType, cmCTestLog_msg.str()); \
  388. } while (false)
  389. #define cmCTestOptionalLog(ctSelf, logType, msg, suppress) \
  390. do { \
  391. std::ostringstream cmCTestLog_msg; \
  392. cmCTestLog_msg << msg; \
  393. (ctSelf)->Log(cmCTest::logType, cmCTestLog_msg.str(), suppress); \
  394. } while (false)