cmCTestBuildCommand.cxx 6.2 KB

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