cmCTestScriptHandler.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <chrono>
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "cmCTestGenericHandler.h"
  10. #include "cmDuration.h"
  11. class cmCTest;
  12. class cmCTestCommand;
  13. class cmGlobalGenerator;
  14. class cmMakefile;
  15. class cmake;
  16. /** \class cmCTestScriptHandler
  17. * \brief A class that handles ctest -S invocations
  18. *
  19. * CTest script is controlled using several variables that script has to
  20. * specify and some optional ones. Required ones are:
  21. * CTEST_SOURCE_DIRECTORY - Source directory of the project
  22. * CTEST_BINARY_DIRECTORY - Binary directory of the project
  23. * CTEST_COMMAND - Testing commands
  24. *
  25. * Optional variables are:
  26. * CTEST_BACKUP_AND_RESTORE
  27. * CTEST_CMAKE_COMMAND
  28. * CTEST_CMAKE_OUTPUT_FILE_NAME
  29. * CTEST_CONTINUOUS_DURATION
  30. * CTEST_CONTINUOUS_MINIMUM_INTERVAL
  31. * CTEST_CVS_CHECKOUT
  32. * CTEST_CVS_COMMAND
  33. * CTEST_UPDATE_COMMAND
  34. * CTEST_DASHBOARD_ROOT
  35. * CTEST_ENVIRONMENT
  36. * CTEST_INITIAL_CACHE
  37. * CTEST_START_WITH_EMPTY_BINARY_DIRECTORY
  38. * CTEST_START_WITH_EMPTY_BINARY_DIRECTORY_ONCE
  39. *
  40. * In addition the following variables can be used. The number can be 1-10.
  41. * CTEST_EXTRA_UPDATES_1
  42. * CTEST_EXTRA_UPDATES_2
  43. * ...
  44. * CTEST_EXTRA_UPDATES_10
  45. *
  46. * CTest script can use the following arguments CTest provides:
  47. * CTEST_SCRIPT_ARG
  48. * CTEST_SCRIPT_DIRECTORY
  49. * CTEST_SCRIPT_NAME
  50. *
  51. */
  52. class cmCTestScriptHandler : public cmCTestGenericHandler
  53. {
  54. public:
  55. using Superclass = cmCTestGenericHandler;
  56. /**
  57. * Add a script to run, and if is should run in the current process
  58. */
  59. void AddConfigurationScript(const std::string&, bool pscope);
  60. /**
  61. * Run a dashboard using a specified confiuration script
  62. */
  63. int ProcessHandler() override;
  64. /*
  65. * Run a script
  66. */
  67. static bool RunScript(cmCTest* ctest, cmMakefile* mf,
  68. const std::string& script, bool InProcess,
  69. int* returnValue);
  70. int RunCurrentScript();
  71. /*
  72. * Empty Binary Directory
  73. */
  74. static bool EmptyBinaryDirectory(const std::string& dir);
  75. /*
  76. * Write an initial CMakeCache.txt from the given contents.
  77. */
  78. static bool WriteInitialCache(const std::string& directory,
  79. const std::string& text);
  80. /*
  81. * Some elapsed time handling functions
  82. */
  83. static void SleepInSeconds(unsigned int secondsToWait);
  84. void UpdateElapsedTime();
  85. /**
  86. * Return the time remaianing that the script is allowed to run in
  87. * seconds if the user has set the variable CTEST_TIME_LIMIT. If that has
  88. * not been set it returns a very large value.
  89. */
  90. cmDuration GetRemainingTimeAllowed();
  91. cmCTestScriptHandler();
  92. cmCTestScriptHandler(const cmCTestScriptHandler&) = delete;
  93. const cmCTestScriptHandler& operator=(const cmCTestScriptHandler&) = delete;
  94. ~cmCTestScriptHandler() override;
  95. void Initialize() override;
  96. void CreateCMake();
  97. cmake* GetCMake() { return this->CMake.get(); }
  98. void SetRunCurrentScript(bool value);
  99. private:
  100. // reads in a script
  101. int ReadInScript(const std::string& total_script_arg);
  102. int ExecuteScript(const std::string& total_script_arg);
  103. // extract vars from the script to set ivars
  104. int ExtractVariables();
  105. // perform a CVS checkout of the source dir
  106. int CheckOutSourceDir();
  107. // perform any extra cvs updates that were requested
  108. int PerformExtraUpdates();
  109. // backup and restore dirs
  110. int BackupDirectories();
  111. void RestoreBackupDirectories();
  112. int RunConfigurationScript(const std::string& script, bool pscope);
  113. int RunConfigurationDashboard();
  114. // Add ctest command
  115. void AddCTestCommand(std::string const& name,
  116. std::unique_ptr<cmCTestCommand> command);
  117. // Try to remove the binary directory once
  118. static bool TryToRemoveBinaryDirectoryOnce(const std::string& directoryPath);
  119. std::vector<std::string> ConfigurationScripts;
  120. std::vector<bool> ScriptProcessScope;
  121. bool ShouldRunCurrentScript;
  122. bool Backup = false;
  123. bool EmptyBinDir = false;
  124. bool EmptyBinDirOnce = false;
  125. std::string SourceDir;
  126. std::string BinaryDir;
  127. std::string BackupSourceDir;
  128. std::string BackupBinaryDir;
  129. std::string CTestRoot;
  130. std::string CVSCheckOut;
  131. std::string CTestCmd;
  132. std::string UpdateCmd;
  133. std::string CTestEnv;
  134. std::string InitialCache;
  135. std::string CMakeCmd;
  136. std::string CMOutFile;
  137. std::vector<std::string> ExtraUpdates;
  138. // the *60 is because the settings are in minutes but GetTime is seconds
  139. double MinimumInterval = 30 * 60;
  140. double ContinuousDuration = -1;
  141. // what time in seconds did this script start running
  142. std::chrono::steady_clock::time_point ScriptStartTime =
  143. std::chrono::steady_clock::time_point();
  144. std::unique_ptr<cmMakefile> Makefile;
  145. cmMakefile* ParentMakefile = nullptr;
  146. std::unique_ptr<cmGlobalGenerator> GlobalGenerator;
  147. std::unique_ptr<cmake> CMake;
  148. };