cmCTest.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmCTest_h
  14. #define cmCTest_h
  15. #include "cmStandardIncludes.h"
  16. #include "cmListFileCache.h"
  17. #include <time.h>
  18. class cmake;
  19. class cmMakefile;
  20. class cmCTestGenericHandler;
  21. class cmGeneratedFileStream;
  22. class cmCTestCommand;
  23. #define cmCTestLog(ctSelf, logType, msg) \
  24. do { \
  25. cmOStringStream cmCTestLog_msg; \
  26. cmCTestLog_msg << msg; \
  27. (ctSelf)->Log(cmCTest::logType, __FILE__, __LINE__,\
  28. cmCTestLog_msg.str().c_str());\
  29. } while ( 0 )
  30. #ifdef cerr
  31. # undef cerr
  32. #endif
  33. #define cerr no_cerr_use_cmCTestLog
  34. #ifdef cout
  35. # undef cout
  36. #endif
  37. #define cout no_cout_use_cmCTestLog
  38. class cmCTest
  39. {
  40. public:
  41. typedef std::vector<cmStdString> VectorOfStrings;
  42. typedef std::set<cmStdString> SetOfStrings;
  43. ///! Process Command line arguments
  44. int Run(std::vector<std::string>const&, std::string* output = 0);
  45. /**
  46. * Initialize and finalize testing
  47. */
  48. int Initialize(const char* binary_dir, bool new_tag = false,
  49. bool verbose_tag = true);
  50. bool InitializeFromCommand(cmCTestCommand* command, bool first = false);
  51. void Finalize();
  52. /**
  53. * Process the tests. This is the main routine. The execution of the
  54. * tests should look like this:
  55. *
  56. * ctest foo;
  57. * foo.Initialize();
  58. * // Set some things on foo
  59. * foo.ProcessTests();
  60. * foo.Finalize();
  61. */
  62. int ProcessTests();
  63. /*
  64. * A utility function that returns the nightly time
  65. */
  66. struct tm* GetNightlyTime(std::string str,
  67. bool tomorrowtag);
  68. /*
  69. * Is the tomorrow tag set?
  70. */
  71. bool GetTomorrowTag() { return this->TomorrowTag; };
  72. /**
  73. * Try to run tests of the project
  74. */
  75. int TestDirectory(bool memcheck);
  76. ///! what is the configuraiton type, e.g. Debug, Release etc.
  77. std::string GetConfigType();
  78. double GetTimeOut() { return this->TimeOut; }
  79. void SetTimeOut(double t) { this->TimeOut = t; }
  80. /**
  81. * Check if CTest file exists
  82. */
  83. bool CTestFileExists(const std::string& filename);
  84. bool AddIfExists(SetOfStrings& files, const char* file);
  85. /**
  86. * Set the cmake test
  87. */
  88. bool SetTest(const char*, bool report = true);
  89. /**
  90. * Set the cmake test mode (experimental, nightly, continuous).
  91. */
  92. void SetTestModel(int mode);
  93. int GetTestModel() { return this->TestModel; };
  94. std::string GetTestModelString();
  95. static int GetTestModelFromString(const char* str);
  96. static std::string CleanString(const std::string& str);
  97. std::string GetCTestConfiguration(const char *name);
  98. void SetCTestConfiguration(const char *name, const char* value);
  99. void EmptyCTestConfiguration();
  100. /**
  101. * constructor and destructor
  102. */
  103. cmCTest();
  104. ~cmCTest();
  105. //! Set the notes files to be created.
  106. void SetNotesFiles(const char* notes);
  107. void PopulateCustomVector(cmMakefile* mf, const char* definition,
  108. VectorOfStrings& vec);
  109. void PopulateCustomInteger(cmMakefile* mf, const char* def,
  110. int& val);
  111. ///! Get the current time as string
  112. std::string CurrentTime();
  113. ///! Open file in the output directory and set the stream
  114. bool OpenOutputFile(const std::string& path,
  115. const std::string& name,
  116. cmGeneratedFileStream& stream,
  117. bool compress = false);
  118. ///! Convert string to something that is XML safe
  119. static std::string MakeXMLSafe(const std::string&);
  120. ///! Should we only show what we would do?
  121. bool GetShowOnly();
  122. /**
  123. * Run a single executable command and put the stdout and stderr
  124. * in output.
  125. *
  126. * If verbose is false, no user-viewable output from the program
  127. * being run will be generated.
  128. *
  129. * If timeout is specified, the command will be terminated after
  130. * timeout expires. Timeout is specified in seconds.
  131. *
  132. * Argument retVal should be a pointer to the location where the
  133. * exit code will be stored. If the retVal is not specified and
  134. * the program exits with a code other than 0, then the this
  135. * function will return false.
  136. *
  137. * If the command has spaces in the path the caller MUST call
  138. * cmSystemTools::ConvertToRunCommandPath on the command before passing
  139. * it into this function or it will not work. The command must be correctly
  140. * escaped for this to with spaces.
  141. */
  142. bool RunCommand(const char* command,
  143. std::string* stdOut, std::string* stdErr,
  144. int* retVal = 0, const char* dir = 0, double timeout = 0.0);
  145. //! Start CTest XML output file
  146. void StartXML(std::ostream& ostr);
  147. //! End CTest XML output file
  148. void EndXML(std::ostream& ostr);
  149. //! Run command specialized for make and configure. Returns process status
  150. // and retVal is return value or exception.
  151. int RunMakeCommand(const char* command, std::string* output,
  152. int* retVal, const char* dir, int timeout,
  153. std::ofstream& ofs);
  154. /*
  155. * return the current tag
  156. */
  157. std::string GetCurrentTag();
  158. //! Get the path to the build tree
  159. std::string GetBinaryDir();
  160. //! Get the short path to the file. This means if the file is in binary or
  161. //source directory, it will become /.../relative/path/to/file
  162. std::string GetShortPathToFile(const char* fname);
  163. //! Get the path to CTest
  164. const char* GetCTestExecutable() { return this->CTestSelf.c_str(); }
  165. const char* GetCMakeExecutable() { return this->CMakeSelf.c_str(); }
  166. enum {
  167. EXPERIMENTAL,
  168. NIGHTLY,
  169. CONTINUOUS
  170. };
  171. // provide some more detailed info on the return code for ctest
  172. enum {
  173. UPDATE_ERRORS = 0x01,
  174. CONFIGURE_ERRORS = 0x02,
  175. BUILD_ERRORS = 0x04,
  176. TEST_ERRORS = 0x08,
  177. MEMORY_ERRORS = 0x10,
  178. COVERAGE_ERRORS = 0x20,
  179. SUBMIT_ERRORS = 0x40
  180. };
  181. ///! Are we producing XML
  182. bool GetProduceXML();
  183. void SetProduceXML(bool v);
  184. //! Run command specialized for tests. Returns process status and retVal is
  185. // return value or exception.
  186. int RunTest(std::vector<const char*> args, std::string* output, int *retVal,
  187. std::ostream* logfile);
  188. /**
  189. * Execute handler and return its result. If the handler fails, it returns
  190. * negative value.
  191. */
  192. int ExecuteHandler(const char* handler);
  193. /*
  194. * Get the handler object
  195. */
  196. cmCTestGenericHandler* GetHandler(const char* handler);
  197. cmCTestGenericHandler* GetInitializedHandler(const char* handler);
  198. /*
  199. * Set the CTest variable from CMake variable
  200. */
  201. bool SetCTestConfigurationFromCMakeVariable(cmMakefile* mf,
  202. const char* dconfig, const char* cmake_var);
  203. //! Make string safe to be send as an URL
  204. static std::string MakeURLSafe(const std::string&);
  205. //! Should ctect configuration be updated. When using new style ctest
  206. // script, this should be true.
  207. void SetSuppressUpdatingCTestConfiguration(bool val)
  208. {
  209. this->SuppressUpdatingCTestConfiguration = val;
  210. }
  211. //! Add overwrite to ctest configuration.
  212. // The format is key=value
  213. void AddCTestConfigurationOverwrite(const char* encstr);
  214. //! Create XML file that contains all the notes specified
  215. int GenerateNotesFile(const std::vector<cmStdString> &files);
  216. //! Submit extra files to the server
  217. bool SubmitExtraFiles(const char* files);
  218. bool SubmitExtraFiles(const std::vector<cmStdString> &files);
  219. //! Set the output log file name
  220. void SetOutputLogFileName(const char* name);
  221. //! Various log types
  222. enum {
  223. DEBUG = 0,
  224. OUTPUT,
  225. HANDLER_OUTPUT,
  226. HANDLER_VERBOSE_OUTPUT,
  227. WARNING,
  228. ERROR_MESSAGE,
  229. OTHER
  230. };
  231. //! Add log to the output
  232. void Log(int logType, const char* file, int line, const char* msg);
  233. //! Get the version of dart server
  234. int GetDartVersion() { return this->DartVersion; }
  235. //! Add file to be submitted
  236. void AddSubmitFile(const char* name);
  237. SetOfStrings* GetSubmitFiles() { return &this->SubmitFiles; }
  238. //! Read the custom configuration files and apply them to the current ctest
  239. int ReadCustomConfigurationFileTree(const char* dir, cmMakefile* mf,
  240. bool fast = false);
  241. std::vector<cmStdString> &GetInitialCommandLineArguments()
  242. { return this->InitialCommandLineArguments; };
  243. //! Set the track to submit to
  244. void SetSpecificTrack(const char* track);
  245. const char* GetSpecificTrack();
  246. private:
  247. std::string ConfigType;
  248. bool Verbose;
  249. bool ExtraVerbose;
  250. bool ProduceXML;
  251. bool ForceNewCTestProcess;
  252. bool RunConfigurationScript;
  253. int GenerateNotesFile(const char* files);
  254. // these are helper classes
  255. typedef std::map<cmStdString,cmCTestGenericHandler*> t_TestingHandlers;
  256. t_TestingHandlers TestingHandlers;
  257. bool ShowOnly;
  258. enum {
  259. FIRST_TEST = 0,
  260. UPDATE_TEST = 1,
  261. START_TEST = 2,
  262. CONFIGURE_TEST = 3,
  263. BUILD_TEST = 4,
  264. TEST_TEST = 5,
  265. COVERAGE_TEST = 6,
  266. MEMCHECK_TEST = 7,
  267. SUBMIT_TEST = 8,
  268. NOTES_TEST = 9,
  269. ALL_TEST = 10,
  270. LAST_TEST = 11
  271. };
  272. //! Map of configuration properties
  273. typedef std::map<cmStdString, cmStdString> CTestConfigurationMap;
  274. std::string CTestConfigFile;
  275. CTestConfigurationMap CTestConfiguration;
  276. CTestConfigurationMap CTestConfigurationOverwrites;
  277. int Tests[LAST_TEST];
  278. std::string CurrentTag;
  279. bool TomorrowTag;
  280. int TestModel;
  281. std::string SpecificTrack;
  282. double TimeOut;
  283. int CompatibilityMode;
  284. // information for the --build-and-test options
  285. std::string CMakeSelf;
  286. std::string CTestSelf;
  287. std::string BinaryDir;
  288. std::string NotesFiles;
  289. bool InteractiveDebugMode;
  290. bool ShortDateFormat;
  291. bool CompressXMLFiles;
  292. void BlockTestErrorDiagnostics();
  293. //! Reread the configuration file
  294. bool UpdateCTestConfiguration();
  295. //! Create not from files.
  296. int GenerateCTestNotesOutput(std::ostream& os,
  297. const VectorOfStrings& files);
  298. ///! Find the running cmake
  299. void FindRunningCMake(const char* arg0);
  300. //! Check if the argument is the one specified
  301. bool CheckArgument(const std::string& arg, const char* varg1,
  302. const char* varg2 = 0);
  303. bool SuppressUpdatingCTestConfiguration;
  304. bool Debug;
  305. bool ShowLineNumbers;
  306. bool Quiet;
  307. int DartVersion;
  308. std::set<cmStdString> SubmitFiles;
  309. std::vector<cmStdString> InitialCommandLineArguments;
  310. int SubmitIndex;
  311. cmGeneratedFileStream* OutputLogFile;
  312. int OutputLogFileLastTag;
  313. };
  314. class cmCTestLogWrite
  315. {
  316. public:
  317. cmCTestLogWrite(const char* data, size_t length)
  318. : Data(data), Length(length) {}
  319. const char* Data;
  320. size_t Length;
  321. };
  322. inline std::ostream& operator<< (std::ostream& os, const cmCTestLogWrite& c)
  323. {
  324. if (!c.Length)
  325. {
  326. return os;
  327. }
  328. os.write(c.Data, c.Length);
  329. os.flush();
  330. return os;
  331. }
  332. #endif