cmCTest.h 16 KB

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