cmCTest.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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__, cmCTestLog_msg.str().c_str());\
  28. } while ( 0 )
  29. #ifdef cerr
  30. # undef cerr
  31. #endif
  32. #define cerr no_cerr_use_cmCTestLog
  33. #ifdef cout
  34. # undef cout
  35. #endif
  36. #define cout no_cout_use_cmCTestLog
  37. class cmCTest
  38. {
  39. public:
  40. typedef std::vector<cmStdString> tm_VectorOfStrings;
  41. ///! Process Command line arguments
  42. int Run(std::vector<std::string>const&, std::string* output = 0);
  43. /**
  44. * Initialize and finalize testing
  45. */
  46. int Initialize(const char* binary_dir, bool new_tag = false, bool verbose_tag = true);
  47. bool InitializeFromCommand(cmCTestCommand* command, bool first = false);
  48. void Finalize();
  49. /**
  50. * Process the tests. This is the main routine. The execution of the
  51. * tests should look like this:
  52. *
  53. * ctest foo;
  54. * foo.Initialize();
  55. * // Set some things on foo
  56. * foo.ProcessTests();
  57. * foo.Finalize();
  58. */
  59. int ProcessTests();
  60. /*
  61. * A utility function that returns the nightly time
  62. */
  63. struct tm* GetNightlyTime(std::string str,
  64. bool tomorrowtag);
  65. /*
  66. * Is the tomorrow tag set?
  67. */
  68. bool GetTomorrowTag() { return m_TomorrowTag; };
  69. /**
  70. * Try to run tests of the project
  71. */
  72. int TestDirectory(bool memcheck);
  73. ///! what is the configuraiton type, e.g. Debug, Release etc.
  74. std::string GetConfigType();
  75. double GetTimeOut() { return m_TimeOut; }
  76. /**
  77. * Check if CTest file exists
  78. */
  79. bool CTestFileExists(const std::string& filename);
  80. bool AddIfExists(tm_VectorOfStrings& files, const char* file);
  81. /**
  82. * Set the cmake test
  83. */
  84. bool SetTest(const char*, bool report = true);
  85. /**
  86. * Set the cmake test mode (experimental, nightly, continuous).
  87. */
  88. void SetTestModel(int mode);
  89. int GetTestModel() { return m_TestModel; };
  90. std::string GetTestModelString();
  91. static int GetTestModelFromString(const char* str);
  92. static std::string CleanString(const std::string& str);
  93. std::string GetCTestConfiguration(const char *name);
  94. void SetCTestConfiguration(const char *name, const char* value);
  95. void EmptyCTestConfiguration();
  96. /**
  97. * constructor and destructor
  98. */
  99. cmCTest();
  100. ~cmCTest();
  101. //! Set the notes files to be created.
  102. void SetNotesFiles(const char* notes);
  103. static void PopulateCustomVector(cmMakefile* mf, const char* definition,
  104. tm_VectorOfStrings& vec);
  105. static void PopulateCustomInteger(cmMakefile* mf, const char* def, int& val);
  106. ///! Get the current time as string
  107. std::string CurrentTime();
  108. ///! Open file in the output directory and set the stream
  109. bool OpenOutputFile(const std::string& path,
  110. const std::string& name,
  111. cmGeneratedFileStream& stream,
  112. bool compress = false);
  113. ///! Convert string to something that is XML safe
  114. static std::string MakeXMLSafe(const std::string&);
  115. ///! Should we only show what we would do?
  116. bool GetShowOnly();
  117. /**
  118. * Run a single executable command and put the stdout and stderr
  119. * in output.
  120. *
  121. * If verbose is false, no user-viewable output from the program
  122. * being run will be generated.
  123. *
  124. * If timeout is specified, the command will be terminated after
  125. * timeout expires. Timeout is specified in seconds.
  126. *
  127. * Argument retVal should be a pointer to the location where the
  128. * exit code will be stored. If the retVal is not specified and
  129. * the program exits with a code other than 0, then the this
  130. * function will return false.
  131. *
  132. * If the command has spaces in the path the caller MUST call
  133. * cmSystemTools::ConvertToRunCommandPath on the command before passing
  134. * it into this function or it will not work. The command must be correctly
  135. * escaped for this to with spaces.
  136. */
  137. bool RunCommand(const char* command,
  138. std::string* stdOut, std::string* stdErr,
  139. int* retVal = 0, const char* dir = 0, double timeout = 0.0);
  140. //! Start CTest XML output file
  141. void StartXML(std::ostream& ostr);
  142. //! End CTest XML output file
  143. void EndXML(std::ostream& ostr);
  144. //! Run command specialized for make and configure. Returns process status
  145. // and retVal is return value or exception.
  146. int RunMakeCommand(const char* command, std::string* output,
  147. int* retVal, const char* dir, int timeout,
  148. std::ofstream& ofs);
  149. /*
  150. * return the current tag
  151. */
  152. std::string GetCurrentTag();
  153. //! Get the path to the build tree
  154. std::string GetBinaryDir();
  155. //! Get the short path to the file. This means if the file is in binary or
  156. //source directory, it will become /.../relative/path/to/file
  157. std::string GetShortPathToFile(const char* fname);
  158. //! Get the path to CTest
  159. const char* GetCTestExecutable() { return m_CTestSelf.c_str(); }
  160. const char* GetCMakeExecutable() { return m_CMakeSelf.c_str(); }
  161. enum {
  162. EXPERIMENTAL,
  163. NIGHTLY,
  164. CONTINUOUS
  165. };
  166. // provide some more detailed info on the return code for ctest
  167. enum {
  168. UPDATE_ERRORS = 0x01,
  169. CONFIGURE_ERRORS = 0x02,
  170. BUILD_ERRORS = 0x04,
  171. TEST_ERRORS = 0x08,
  172. MEMORY_ERRORS = 0x10,
  173. COVERAGE_ERRORS = 0x20,
  174. SUBMIT_ERRORS = 0x40
  175. };
  176. ///! Are we producing XML
  177. bool GetProduceXML();
  178. void SetProduceXML(bool v);
  179. //! Run command specialized for tests. Returns process status and retVal is
  180. // return value or exception.
  181. int RunTest(std::vector<const char*> args, std::string* output, int *retVal,
  182. std::ostream* logfile);
  183. /**
  184. * Execute handler and return its result. If the handler fails, it returns negative value.
  185. */
  186. int ExecuteHandler(const char* handler);
  187. /*
  188. * Get the handler object
  189. */
  190. cmCTestGenericHandler* GetHandler(const char* handler);
  191. /*
  192. * Set the CTest variable from CMake variable
  193. */
  194. bool SetCTestConfigurationFromCMakeVariable(cmMakefile* mf, const char* dconfig, const char* cmake_var);
  195. //! Make string safe to be send as an URL
  196. static std::string MakeURLSafe(const std::string&);
  197. //! Should ctect configuration be updated. When using new style ctest script,
  198. // this should be true.
  199. void SetSuppressUpdatingCTestConfiguration(bool val)
  200. {
  201. m_SuppressUpdatingCTestConfiguration = val;
  202. }
  203. //! Create XML file that contains all the notes specified
  204. int GenerateNotesFile(const std::vector<cmStdString> &files);
  205. //! Set the output log file name
  206. void SetOutputLogFileName(const char* name);
  207. //! Various log types
  208. enum {
  209. DEBUG = 0,
  210. OUTPUT,
  211. HANDLER_OUTPUT,
  212. HANDLER_VERBOSE_OUTPUT,
  213. WARNING,
  214. ERROR_MESSAGE,
  215. OTHER
  216. };
  217. //! Add log to the output
  218. void Log(int logType, const char* file, int line, const char* msg);
  219. private:
  220. std::string m_ConfigType;
  221. bool m_Verbose;
  222. bool m_ExtraVerbose;
  223. bool m_ProduceXML;
  224. bool m_ForceNewCTestProcess;
  225. bool m_RunConfigurationScript;
  226. int GenerateNotesFile(const char* files);
  227. // these are helper classes
  228. typedef std::map<cmStdString,cmCTestGenericHandler*> t_TestingHandlers;
  229. t_TestingHandlers m_TestingHandlers;
  230. bool m_ShowOnly;
  231. enum {
  232. FIRST_TEST = 0,
  233. UPDATE_TEST = 1,
  234. START_TEST = 2,
  235. CONFIGURE_TEST = 3,
  236. BUILD_TEST = 4,
  237. TEST_TEST = 5,
  238. COVERAGE_TEST = 6,
  239. MEMCHECK_TEST = 7,
  240. SUBMIT_TEST = 8,
  241. NOTES_TEST = 9,
  242. ALL_TEST = 10,
  243. LAST_TEST = 11
  244. };
  245. //! Map of configuration properties
  246. typedef std::map<cmStdString, cmStdString> tm_CTestConfigurationMap;
  247. std::string m_CTestConfigFile;
  248. tm_CTestConfigurationMap m_CTestConfiguration;
  249. int m_Tests[LAST_TEST];
  250. std::string m_CurrentTag;
  251. bool m_TomorrowTag;
  252. int m_TestModel;
  253. double m_TimeOut;
  254. int m_CompatibilityMode;
  255. // information for the --build-and-test options
  256. std::string m_CMakeSelf;
  257. std::string m_CTestSelf;
  258. std::string m_BinaryDir;
  259. std::string m_NotesFiles;
  260. int ReadCustomConfigurationFileTree(const char* dir);
  261. bool m_InteractiveDebugMode;
  262. bool m_ShortDateFormat;
  263. bool m_CompressXMLFiles;
  264. void BlockTestErrorDiagnostics();
  265. //! Reread the configuration file
  266. bool UpdateCTestConfiguration();
  267. //! Create not from files.
  268. int GenerateCTestNotesOutput(std::ostream& os, const tm_VectorOfStrings& files);
  269. ///! Find the running cmake
  270. void FindRunningCMake(const char* arg0);
  271. bool m_SuppressUpdatingCTestConfiguration;
  272. bool m_Debug;
  273. bool m_ShowLineNumbers;
  274. bool m_Quiet;
  275. cmGeneratedFileStream* m_OutputLogFile;
  276. int m_OutputLogFileLastTag;
  277. };
  278. class cmCTestLogWrite
  279. {
  280. public:
  281. cmCTestLogWrite(const char* data, size_t length) : Data(data), Length(length) {}
  282. const char* Data;
  283. size_t Length;
  284. };
  285. inline std::ostream& operator<< (std::ostream& os, const cmCTestLogWrite& c)
  286. {
  287. os.write(c.Data, c.Length);
  288. os.flush();
  289. return os;
  290. }
  291. #endif