cmCTestBuildCommand.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCTestBuildCommand.h"
  4. #include "cmCTest.h"
  5. #include "cmCTestBuildHandler.h"
  6. #include "cmCTestGenericHandler.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmMakefile.h"
  9. #include "cmSystemTools.h"
  10. #include "cmake.h"
  11. #include <sstream>
  12. #include <string.h>
  13. class cmExecutionStatus;
  14. cmCTestBuildCommand::cmCTestBuildCommand()
  15. {
  16. this->GlobalGenerator = nullptr;
  17. this->Arguments[ctb_NUMBER_ERRORS] = "NUMBER_ERRORS";
  18. this->Arguments[ctb_NUMBER_WARNINGS] = "NUMBER_WARNINGS";
  19. this->Arguments[ctb_TARGET] = "TARGET";
  20. this->Arguments[ctb_CONFIGURATION] = "CONFIGURATION";
  21. this->Arguments[ctb_FLAGS] = "FLAGS";
  22. this->Arguments[ctb_PROJECT_NAME] = "PROJECT_NAME";
  23. this->Arguments[ctb_LAST] = nullptr;
  24. this->Last = ctb_LAST;
  25. }
  26. cmCTestBuildCommand::~cmCTestBuildCommand()
  27. {
  28. if (this->GlobalGenerator) {
  29. delete this->GlobalGenerator;
  30. this->GlobalGenerator = nullptr;
  31. }
  32. }
  33. cmCTestGenericHandler* cmCTestBuildCommand::InitializeHandler()
  34. {
  35. cmCTestGenericHandler* handler = this->CTest->GetInitializedHandler("build");
  36. if (!handler) {
  37. this->SetError("internal CTest error. Cannot instantiate build handler");
  38. return nullptr;
  39. }
  40. this->Handler = static_cast<cmCTestBuildHandler*>(handler);
  41. const char* ctestBuildCommand =
  42. this->Makefile->GetDefinition("CTEST_BUILD_COMMAND");
  43. if (ctestBuildCommand && *ctestBuildCommand) {
  44. this->CTest->SetCTestConfiguration("MakeCommand", ctestBuildCommand,
  45. this->Quiet);
  46. } else {
  47. const char* cmakeGeneratorName =
  48. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR");
  49. const char* cmakeProjectName =
  50. (this->Values[ctb_PROJECT_NAME] && *this->Values[ctb_PROJECT_NAME])
  51. ? this->Values[ctb_PROJECT_NAME]
  52. : this->Makefile->GetDefinition("CTEST_PROJECT_NAME");
  53. // Build configuration is determined by: CONFIGURATION argument,
  54. // or CTEST_BUILD_CONFIGURATION script variable, or
  55. // CTEST_CONFIGURATION_TYPE script variable, or ctest -C command
  56. // line argument... in that order.
  57. //
  58. const char* ctestBuildConfiguration =
  59. this->Makefile->GetDefinition("CTEST_BUILD_CONFIGURATION");
  60. const char* cmakeBuildConfiguration =
  61. (this->Values[ctb_CONFIGURATION] && *this->Values[ctb_CONFIGURATION])
  62. ? this->Values[ctb_CONFIGURATION]
  63. : ((ctestBuildConfiguration && *ctestBuildConfiguration)
  64. ? ctestBuildConfiguration
  65. : this->CTest->GetConfigType().c_str());
  66. const char* cmakeBuildAdditionalFlags =
  67. (this->Values[ctb_FLAGS] && *this->Values[ctb_FLAGS])
  68. ? this->Values[ctb_FLAGS]
  69. : this->Makefile->GetDefinition("CTEST_BUILD_FLAGS");
  70. const char* cmakeBuildTarget =
  71. (this->Values[ctb_TARGET] && *this->Values[ctb_TARGET])
  72. ? this->Values[ctb_TARGET]
  73. : this->Makefile->GetDefinition("CTEST_BUILD_TARGET");
  74. if (cmakeGeneratorName && *cmakeGeneratorName && cmakeProjectName &&
  75. *cmakeProjectName) {
  76. if (!cmakeBuildConfiguration) {
  77. cmakeBuildConfiguration = "Release";
  78. }
  79. if (this->GlobalGenerator) {
  80. if (this->GlobalGenerator->GetName() != cmakeGeneratorName) {
  81. delete this->GlobalGenerator;
  82. this->GlobalGenerator = nullptr;
  83. }
  84. }
  85. if (!this->GlobalGenerator) {
  86. this->GlobalGenerator =
  87. this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
  88. cmakeGeneratorName);
  89. if (!this->GlobalGenerator) {
  90. std::string e = "could not create generator named \"";
  91. e += cmakeGeneratorName;
  92. e += "\"";
  93. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e);
  94. cmSystemTools::SetFatalErrorOccured();
  95. return nullptr;
  96. }
  97. }
  98. if (strlen(cmakeBuildConfiguration) == 0) {
  99. const char* config = nullptr;
  100. #ifdef CMAKE_INTDIR
  101. config = CMAKE_INTDIR;
  102. #endif
  103. if (!config) {
  104. config = "Debug";
  105. }
  106. cmakeBuildConfiguration = config;
  107. }
  108. std::string dir = this->CTest->GetCTestConfiguration("BuildDirectory");
  109. std::string buildCommand =
  110. this->GlobalGenerator->GenerateCMakeBuildCommand(
  111. cmakeBuildTarget ? cmakeBuildTarget : "", cmakeBuildConfiguration,
  112. cmakeBuildAdditionalFlags ? cmakeBuildAdditionalFlags : "",
  113. this->Makefile->IgnoreErrorsCMP0061());
  114. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  115. "SetMakeCommand:" << buildCommand << "\n",
  116. this->Quiet);
  117. this->CTest->SetCTestConfiguration("MakeCommand", buildCommand.c_str(),
  118. this->Quiet);
  119. } else {
  120. std::ostringstream ostr;
  121. /* clang-format off */
  122. ostr << "has no project to build. If this is a "
  123. "\"built with CMake\" project, verify that CTEST_CMAKE_GENERATOR "
  124. "and CTEST_PROJECT_NAME are set."
  125. "\n"
  126. "CTEST_PROJECT_NAME is usually set in CTestConfig.cmake. Verify "
  127. "that CTestConfig.cmake exists, or CTEST_PROJECT_NAME "
  128. "is set in the script, or PROJECT_NAME is passed as an argument "
  129. "to ctest_build."
  130. "\n"
  131. "Alternatively, set CTEST_BUILD_COMMAND to build the project "
  132. "with a custom command line.";
  133. /* clang-format on */
  134. this->SetError(ostr.str());
  135. return nullptr;
  136. }
  137. }
  138. if (const char* useLaunchers =
  139. this->Makefile->GetDefinition("CTEST_USE_LAUNCHERS")) {
  140. this->CTest->SetCTestConfiguration("UseLaunchers", useLaunchers,
  141. this->Quiet);
  142. }
  143. if (const char* labelsForSubprojects =
  144. this->Makefile->GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {
  145. this->CTest->SetCTestConfiguration("LabelsForSubprojects",
  146. labelsForSubprojects, this->Quiet);
  147. }
  148. handler->SetQuiet(this->Quiet);
  149. return handler;
  150. }
  151. bool cmCTestBuildCommand::InitialPass(std::vector<std::string> const& args,
  152. cmExecutionStatus& status)
  153. {
  154. bool ret = cmCTestHandlerCommand::InitialPass(args, status);
  155. if (this->Values[ctb_NUMBER_ERRORS] && *this->Values[ctb_NUMBER_ERRORS]) {
  156. std::ostringstream str;
  157. str << this->Handler->GetTotalErrors();
  158. this->Makefile->AddDefinition(this->Values[ctb_NUMBER_ERRORS],
  159. str.str().c_str());
  160. }
  161. if (this->Values[ctb_NUMBER_WARNINGS] &&
  162. *this->Values[ctb_NUMBER_WARNINGS]) {
  163. std::ostringstream str;
  164. str << this->Handler->GetTotalWarnings();
  165. this->Makefile->AddDefinition(this->Values[ctb_NUMBER_WARNINGS],
  166. str.str().c_str());
  167. }
  168. return ret;
  169. }