cmCTest.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmCTest_h
  11. #define cmCTest_h
  12. #include "cmStandardIncludes.h"
  13. #include "cmListFileCache.h"
  14. #include <time.h>
  15. class cmake;
  16. class cmMakefile;
  17. class cmCTestGenericHandler;
  18. class cmGeneratedFileStream;
  19. class cmCTestCommand;
  20. class cmCTestScriptHandler;
  21. #define cmCTestLog(ctSelf, logType, msg) \
  22. do { \
  23. cmOStringStream cmCTestLog_msg; \
  24. cmCTestLog_msg << msg; \
  25. (ctSelf)->Log(cmCTest::logType, __FILE__, __LINE__,\
  26. cmCTestLog_msg.str().c_str());\
  27. } while ( 0 )
  28. #ifdef cerr
  29. # undef cerr
  30. #endif
  31. #define cerr no_cerr_use_cmCTestLog
  32. #ifdef cout
  33. # undef cout
  34. #endif
  35. #define cout no_cout_use_cmCTestLog
  36. class cmCTest
  37. {
  38. friend class cmCTestRunTest;
  39. friend class cmCTestMultiProcessHandler;
  40. public:
  41. /** Enumerate parts of the testing and submission process. */
  42. enum Part
  43. {
  44. PartStart,
  45. PartUpdate,
  46. PartConfigure,
  47. PartBuild,
  48. PartTest,
  49. PartCoverage,
  50. PartMemCheck,
  51. PartSubmit,
  52. PartNotes,
  53. PartExtraFiles,
  54. PartCount // Update names in constructor when adding a part
  55. };
  56. /** Representation of one part. */
  57. struct PartInfo
  58. {
  59. PartInfo(): Enabled(false) {}
  60. void SetName(const char* name) { this->Name = name; }
  61. const char* GetName() const { return this->Name.c_str(); }
  62. void Enable() { this->Enabled = true; }
  63. operator bool() const { return this->Enabled; }
  64. std::vector<std::string> SubmitFiles;
  65. private:
  66. bool Enabled;
  67. std::string Name;
  68. };
  69. /** Get a testing part id from its string name. Returns PartCount
  70. if the string does not name a valid part. */
  71. Part GetPartFromName(const char* name);
  72. typedef std::vector<cmStdString> VectorOfStrings;
  73. typedef std::set<cmStdString> SetOfStrings;
  74. ///! Process Command line arguments
  75. int Run(std::vector<std::string> &, std::string* output = 0);
  76. /**
  77. * Initialize and finalize testing
  78. */
  79. int Initialize(const char* binary_dir, bool new_tag = false,
  80. bool verbose_tag = true);
  81. bool InitializeFromCommand(cmCTestCommand* command, bool first = false);
  82. void Finalize();
  83. /**
  84. * Process the tests. This is the main routine. The execution of the
  85. * tests should look like this:
  86. *
  87. * ctest foo;
  88. * foo.Initialize();
  89. * // Set some things on foo
  90. * foo.ProcessTests();
  91. * foo.Finalize();
  92. */
  93. int ProcessTests();
  94. /*
  95. * A utility function that returns the nightly time
  96. */
  97. struct tm* GetNightlyTime(std::string str,
  98. bool tomorrowtag);
  99. /*
  100. * Is the tomorrow tag set?
  101. */
  102. bool GetTomorrowTag() { return this->TomorrowTag; };
  103. /**
  104. * Try to run tests of the project
  105. */
  106. int TestDirectory(bool memcheck);
  107. ///! what is the configuraiton type, e.g. Debug, Release etc.
  108. std::string const& GetConfigType();
  109. double GetTimeOut() { return this->TimeOut; }
  110. void SetTimeOut(double t) { this->TimeOut = t; }
  111. // how many test to run at the same time
  112. int GetParallelLevel() { return this->ParallelLevel; }
  113. void SetParallelLevel(int);
  114. /**
  115. * Check if CTest file exists
  116. */
  117. bool CTestFileExists(const std::string& filename);
  118. bool AddIfExists(Part part, const char* file);
  119. /**
  120. * Set the cmake test
  121. */
  122. bool SetTest(const char*, bool report = true);
  123. /**
  124. * Set the cmake test mode (experimental, nightly, continuous).
  125. */
  126. void SetTestModel(int mode);
  127. int GetTestModel() { return this->TestModel; };
  128. std::string GetTestModelString();
  129. static int GetTestModelFromString(const char* str);
  130. static std::string CleanString(const std::string& str);
  131. std::string GetCTestConfiguration(const char *name);
  132. void SetCTestConfiguration(const char *name, const char* value);
  133. void EmptyCTestConfiguration();
  134. /**
  135. * constructor and destructor
  136. */
  137. cmCTest();
  138. ~cmCTest();
  139. //! Set the notes files to be created.
  140. void SetNotesFiles(const char* notes);
  141. void PopulateCustomVector(cmMakefile* mf, const char* definition,
  142. VectorOfStrings& vec);
  143. void PopulateCustomInteger(cmMakefile* mf, const char* def,
  144. int& val);
  145. ///! Get the current time as string
  146. std::string CurrentTime();
  147. /**
  148. * Return the time remaianing that the script is allowed to run in
  149. * seconds if the user has set the variable CTEST_TIME_LIMIT. If that has
  150. * not been set it returns 1e7 seconds
  151. */
  152. double GetRemainingTimeAllowed();
  153. ///! Open file in the output directory and set the stream
  154. bool OpenOutputFile(const std::string& path,
  155. const std::string& name,
  156. cmGeneratedFileStream& stream,
  157. bool compress = false);
  158. ///! Should we only show what we would do?
  159. bool GetShowOnly();
  160. ///! The max output width
  161. int GetMaxTestNameWidth() const;
  162. void SetMaxTestNameWidth(int w) { this->MaxTestNameWidth = w;}
  163. /**
  164. * Run a single executable command and put the stdout and stderr
  165. * in output.
  166. *
  167. * If verbose is false, no user-viewable output from the program
  168. * being run will be generated.
  169. *
  170. * If timeout is specified, the command will be terminated after
  171. * timeout expires. Timeout is specified in seconds.
  172. *
  173. * Argument retVal should be a pointer to the location where the
  174. * exit code will be stored. If the retVal is not specified and
  175. * the program exits with a code other than 0, then the this
  176. * function will return false.
  177. *
  178. * If the command has spaces in the path the caller MUST call
  179. * cmSystemTools::ConvertToRunCommandPath on the command before passing
  180. * it into this function or it will not work. The command must be correctly
  181. * escaped for this to with spaces.
  182. */
  183. bool RunCommand(const char* command,
  184. std::string* stdOut, std::string* stdErr,
  185. int* retVal = 0, const char* dir = 0, double timeout = 0.0);
  186. //! Start CTest XML output file
  187. void StartXML(std::ostream& ostr, bool append);
  188. //! End CTest XML output file
  189. void EndXML(std::ostream& ostr);
  190. //! Run command specialized for make and configure. Returns process status
  191. // and retVal is return value or exception.
  192. int RunMakeCommand(const char* command, std::string* output,
  193. int* retVal, const char* dir, int timeout,
  194. std::ofstream& ofs);
  195. /*
  196. * return the current tag
  197. */
  198. std::string GetCurrentTag();
  199. //! Get the path to the build tree
  200. std::string GetBinaryDir();
  201. //! Get the short path to the file. This means if the file is in binary or
  202. //source directory, it will become /.../relative/path/to/file
  203. std::string GetShortPathToFile(const char* fname);
  204. //! Get the path to CTest
  205. const char* GetCTestExecutable() { return this->CTestSelf.c_str(); }
  206. const char* GetCMakeExecutable() { return this->CMakeSelf.c_str(); }
  207. enum {
  208. EXPERIMENTAL,
  209. NIGHTLY,
  210. CONTINUOUS
  211. };
  212. // provide some more detailed info on the return code for ctest
  213. enum {
  214. UPDATE_ERRORS = 0x01,
  215. CONFIGURE_ERRORS = 0x02,
  216. BUILD_ERRORS = 0x04,
  217. TEST_ERRORS = 0x08,
  218. MEMORY_ERRORS = 0x10,
  219. COVERAGE_ERRORS = 0x20,
  220. SUBMIT_ERRORS = 0x40
  221. };
  222. ///! Are we producing XML
  223. bool GetProduceXML();
  224. void SetProduceXML(bool v);
  225. //! Run command specialized for tests. Returns process status and retVal is
  226. // return value or exception. If environment is non-null, it is used to set
  227. // environment variables prior to running the test. After running the test,
  228. // environment variables are restored to their previous values.
  229. int RunTest(std::vector<const char*> args, std::string* output, int *retVal,
  230. std::ostream* logfile, double testTimeOut,
  231. std::vector<std::string>* environment);
  232. /**
  233. * Execute handler and return its result. If the handler fails, it returns
  234. * negative value.
  235. */
  236. int ExecuteHandler(const char* handler);
  237. /*
  238. * Get the handler object
  239. */
  240. cmCTestGenericHandler* GetHandler(const char* handler);
  241. cmCTestGenericHandler* GetInitializedHandler(const char* handler);
  242. /*
  243. * Set the CTest variable from CMake variable
  244. */
  245. bool SetCTestConfigurationFromCMakeVariable(cmMakefile* mf,
  246. const char* dconfig, const char* cmake_var);
  247. //! Make string safe to be send as an URL
  248. static std::string MakeURLSafe(const std::string&);
  249. /** Decode a URL to the original string. */
  250. static std::string DecodeURL(const std::string&);
  251. //! Should ctect configuration be updated. When using new style ctest
  252. // script, this should be true.
  253. void SetSuppressUpdatingCTestConfiguration(bool val)
  254. {
  255. this->SuppressUpdatingCTestConfiguration = val;
  256. }
  257. //! Add overwrite to ctest configuration.
  258. // The format is key=value
  259. void AddCTestConfigurationOverwrite(const char* encstr);
  260. //! Create XML file that contains all the notes specified
  261. int GenerateNotesFile(const std::vector<cmStdString> &files);
  262. //! Submit extra files to the server
  263. bool SubmitExtraFiles(const char* files);
  264. bool SubmitExtraFiles(const std::vector<cmStdString> &files);
  265. //! Set the output log file name
  266. void SetOutputLogFileName(const char* name);
  267. //! Set the visual studio or Xcode config type
  268. void SetConfigType(const char* ct);
  269. //! Various log types
  270. enum {
  271. DEBUG = 0,
  272. OUTPUT,
  273. HANDLER_OUTPUT,
  274. HANDLER_VERBOSE_OUTPUT,
  275. WARNING,
  276. ERROR_MESSAGE,
  277. OTHER
  278. };
  279. //! Add log to the output
  280. void Log(int logType, const char* file, int line, const char* msg);
  281. //! Get the version of dart server
  282. int GetDartVersion() { return this->DartVersion; }
  283. //! Add file to be submitted
  284. void AddSubmitFile(Part part, const char* name);
  285. std::vector<std::string> const& GetSubmitFiles(Part part)
  286. { return this->Parts[part].SubmitFiles; }
  287. void ClearSubmitFiles(Part part) { this->Parts[part].SubmitFiles.clear(); }
  288. //! Read the custom configuration files and apply them to the current ctest
  289. int ReadCustomConfigurationFileTree(const char* dir, cmMakefile* mf);
  290. std::vector<cmStdString> &GetInitialCommandLineArguments()
  291. { return this->InitialCommandLineArguments; };
  292. //! Set the track to submit to
  293. void SetSpecificTrack(const char* track);
  294. const char* GetSpecificTrack();
  295. void SetFailover(bool failover) { this->Failover = failover; }
  296. bool GetFailover() { return this->Failover; }
  297. void SetBatchJobs(bool batch = true) { this->BatchJobs = batch; }
  298. bool GetBatchJobs() { return this->BatchJobs; }
  299. bool GetVerbose() { return this->Verbose;}
  300. bool GetExtraVerbose() { return this->ExtraVerbose;}
  301. /** Direct process output to given streams. */
  302. void SetStreams(std::ostream* out, std::ostream* err)
  303. { this->StreamOut = out; this->StreamErr = err; }
  304. void AddSiteProperties(std::ostream& );
  305. bool GetLabelSummary() { return this->LabelSummary;}
  306. private:
  307. std::string ConfigType;
  308. bool Verbose;
  309. bool ExtraVerbose;
  310. bool ProduceXML;
  311. bool LabelSummary;
  312. bool Failover;
  313. bool BatchJobs;
  314. bool ForceNewCTestProcess;
  315. bool RunConfigurationScript;
  316. int GenerateNotesFile(const char* files);
  317. // these are helper classes
  318. typedef std::map<cmStdString,cmCTestGenericHandler*> t_TestingHandlers;
  319. t_TestingHandlers TestingHandlers;
  320. bool ShowOnly;
  321. //! Map of configuration properties
  322. typedef std::map<cmStdString, cmStdString> CTestConfigurationMap;
  323. std::string CTestConfigFile;
  324. // TODO: The ctest configuration should be a hierarchy of
  325. // configuration option sources: command-line, script, ini file.
  326. // Then the ini file can get re-loaded whenever it changes without
  327. // affecting any higher-precedence settings.
  328. CTestConfigurationMap CTestConfiguration;
  329. CTestConfigurationMap CTestConfigurationOverwrites;
  330. PartInfo Parts[PartCount];
  331. typedef std::map<cmStdString, Part> PartMapType;
  332. PartMapType PartMap;
  333. std::string CurrentTag;
  334. bool TomorrowTag;
  335. int TestModel;
  336. std::string SpecificTrack;
  337. double TimeOut;
  338. int MaxTestNameWidth;
  339. int ParallelLevel;
  340. int CompatibilityMode;
  341. // information for the --build-and-test options
  342. std::string CMakeSelf;
  343. std::string CTestSelf;
  344. std::string BinaryDir;
  345. std::string NotesFiles;
  346. bool InteractiveDebugMode;
  347. bool ShortDateFormat;
  348. bool CompressXMLFiles;
  349. void InitStreams();
  350. std::ostream* StreamOut;
  351. std::ostream* StreamErr;
  352. void BlockTestErrorDiagnostics();
  353. //! parse the option after -D and convert it into the appropriate steps
  354. bool AddTestsForDashboardType(std::string &targ);
  355. //! parse and process most common command line arguments
  356. void HandleCommandLineArguments(size_t &i,
  357. std::vector<std::string> &args);
  358. //! hande the -S -SP and -SR arguments
  359. void HandleScriptArguments(size_t &i,
  360. std::vector<std::string> &args,
  361. bool &SRArgumentSpecified);
  362. //! Reread the configuration file
  363. bool UpdateCTestConfiguration();
  364. //! Create not from files.
  365. int GenerateCTestNotesOutput(std::ostream& os,
  366. const VectorOfStrings& files);
  367. ///! Find the running cmake
  368. void FindRunningCMake();
  369. //! Check if the argument is the one specified
  370. bool CheckArgument(const std::string& arg, const char* varg1,
  371. const char* varg2 = 0);
  372. //! Output errors from a test
  373. void OutputTestErrors(std::vector<char> const &process_output);
  374. bool SuppressUpdatingCTestConfiguration;
  375. bool Debug;
  376. bool ShowLineNumbers;
  377. bool Quiet;
  378. int DartVersion;
  379. std::vector<cmStdString> InitialCommandLineArguments;
  380. int SubmitIndex;
  381. cmGeneratedFileStream* OutputLogFile;
  382. int OutputLogFileLastTag;
  383. bool OutputTestOutputOnTestFailure;
  384. };
  385. class cmCTestLogWrite
  386. {
  387. public:
  388. cmCTestLogWrite(const char* data, size_t length)
  389. : Data(data), Length(length) {}
  390. const char* Data;
  391. size_t Length;
  392. };
  393. inline std::ostream& operator<< (std::ostream& os, const cmCTestLogWrite& c)
  394. {
  395. if (!c.Length)
  396. {
  397. return os;
  398. }
  399. os.write(c.Data, c.Length);
  400. os.flush();
  401. return os;
  402. }
  403. #endif