cmCTest.h 15 KB

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