cmCTest.h 13 KB

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