1
0

cmCTest.h 11 KB

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