cmCTestScriptHandler.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 char*, 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, const char* script,
  68. bool InProcess, int* returnValue);
  69. int RunCurrentScript();
  70. /*
  71. * Empty Binary Directory
  72. */
  73. static bool EmptyBinaryDirectory(const char* dir);
  74. /*
  75. * Write an initial CMakeCache.txt from the given contents.
  76. */
  77. static bool WriteInitialCache(const char* directory, const char* text);
  78. /*
  79. * Some elapsed time handling functions
  80. */
  81. static void SleepInSeconds(unsigned int secondsToWait);
  82. void UpdateElapsedTime();
  83. /**
  84. * Return the time remaianing that the script is allowed to run in
  85. * seconds if the user has set the variable CTEST_TIME_LIMIT. If that has
  86. * not been set it returns a very large value.
  87. */
  88. cmDuration GetRemainingTimeAllowed();
  89. cmCTestScriptHandler();
  90. cmCTestScriptHandler(const cmCTestScriptHandler&) = delete;
  91. const cmCTestScriptHandler& operator=(const cmCTestScriptHandler&) = delete;
  92. ~cmCTestScriptHandler() override;
  93. void Initialize() override;
  94. void CreateCMake();
  95. cmake* GetCMake() { return this->CMake.get(); }
  96. void SetRunCurrentScript(bool value);
  97. private:
  98. // reads in a script
  99. int ReadInScript(const std::string& total_script_arg);
  100. int ExecuteScript(const std::string& total_script_arg);
  101. // extract vars from the script to set ivars
  102. int ExtractVariables();
  103. // perform a CVS checkout of the source dir
  104. int CheckOutSourceDir();
  105. // perform any extra cvs updates that were requested
  106. int PerformExtraUpdates();
  107. // backup and restore dirs
  108. int BackupDirectories();
  109. void RestoreBackupDirectories();
  110. int RunConfigurationScript(const std::string& script, bool pscope);
  111. int RunConfigurationDashboard();
  112. // Add ctest command
  113. void AddCTestCommand(std::string const& name,
  114. std::unique_ptr<cmCTestCommand> command);
  115. // Try to remove the binary directory once
  116. static bool TryToRemoveBinaryDirectoryOnce(const std::string& directoryPath);
  117. std::vector<std::string> ConfigurationScripts;
  118. std::vector<bool> ScriptProcessScope;
  119. bool ShouldRunCurrentScript;
  120. bool Backup = false;
  121. bool EmptyBinDir = false;
  122. bool EmptyBinDirOnce = false;
  123. std::string SourceDir;
  124. std::string BinaryDir;
  125. std::string BackupSourceDir;
  126. std::string BackupBinaryDir;
  127. std::string CTestRoot;
  128. std::string CVSCheckOut;
  129. std::string CTestCmd;
  130. std::string UpdateCmd;
  131. std::string CTestEnv;
  132. std::string InitialCache;
  133. std::string CMakeCmd;
  134. std::string CMOutFile;
  135. std::vector<std::string> ExtraUpdates;
  136. // the *60 is because the settings are in minutes but GetTime is seconds
  137. double MinimumInterval = 30 * 60;
  138. double ContinuousDuration = -1;
  139. // what time in seconds did this script start running
  140. std::chrono::steady_clock::time_point ScriptStartTime =
  141. std::chrono::steady_clock::time_point();
  142. std::unique_ptr<cmMakefile> Makefile;
  143. cmMakefile* ParentMakefile = nullptr;
  144. std::unique_ptr<cmGlobalGenerator> GlobalGenerator;
  145. std::unique_ptr<cmake> CMake;
  146. };