cmCTestBuildCommand.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // Build configuration is determined by: CONFIGURATION argument,
  50. // or CTEST_BUILD_CONFIGURATION script variable, or
  51. // CTEST_CONFIGURATION_TYPE script variable, or ctest -C command
  52. // line argument... in that order.
  53. //
  54. const char* ctestBuildConfiguration =
  55. this->Makefile->GetDefinition("CTEST_BUILD_CONFIGURATION");
  56. const char* cmakeBuildConfiguration =
  57. (this->Values[ctb_CONFIGURATION] && *this->Values[ctb_CONFIGURATION])
  58. ? this->Values[ctb_CONFIGURATION]
  59. : ((ctestBuildConfiguration && *ctestBuildConfiguration)
  60. ? ctestBuildConfiguration
  61. : this->CTest->GetConfigType().c_str());
  62. const char* cmakeBuildAdditionalFlags =
  63. (this->Values[ctb_FLAGS] && *this->Values[ctb_FLAGS])
  64. ? this->Values[ctb_FLAGS]
  65. : this->Makefile->GetDefinition("CTEST_BUILD_FLAGS");
  66. const char* cmakeBuildTarget =
  67. (this->Values[ctb_TARGET] && *this->Values[ctb_TARGET])
  68. ? this->Values[ctb_TARGET]
  69. : this->Makefile->GetDefinition("CTEST_BUILD_TARGET");
  70. if (cmakeGeneratorName && *cmakeGeneratorName) {
  71. if (!cmakeBuildConfiguration) {
  72. cmakeBuildConfiguration = "Release";
  73. }
  74. if (this->GlobalGenerator) {
  75. if (this->GlobalGenerator->GetName() != cmakeGeneratorName) {
  76. delete this->GlobalGenerator;
  77. this->GlobalGenerator = nullptr;
  78. }
  79. }
  80. if (!this->GlobalGenerator) {
  81. this->GlobalGenerator =
  82. this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
  83. cmakeGeneratorName);
  84. if (!this->GlobalGenerator) {
  85. std::string e = "could not create generator named \"";
  86. e += cmakeGeneratorName;
  87. e += "\"";
  88. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e);
  89. cmSystemTools::SetFatalErrorOccured();
  90. return nullptr;
  91. }
  92. }
  93. if (strlen(cmakeBuildConfiguration) == 0) {
  94. const char* config = nullptr;
  95. #ifdef CMAKE_INTDIR
  96. config = CMAKE_INTDIR;
  97. #endif
  98. if (!config) {
  99. config = "Debug";
  100. }
  101. cmakeBuildConfiguration = config;
  102. }
  103. std::string dir = this->CTest->GetCTestConfiguration("BuildDirectory");
  104. std::string buildCommand =
  105. this->GlobalGenerator->GenerateCMakeBuildCommand(
  106. cmakeBuildTarget ? cmakeBuildTarget : "", cmakeBuildConfiguration,
  107. cmakeBuildAdditionalFlags ? cmakeBuildAdditionalFlags : "",
  108. this->Makefile->IgnoreErrorsCMP0061());
  109. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  110. "SetMakeCommand:" << buildCommand << "\n",
  111. this->Quiet);
  112. this->CTest->SetCTestConfiguration("MakeCommand", buildCommand.c_str(),
  113. this->Quiet);
  114. } else {
  115. std::ostringstream ostr;
  116. /* clang-format off */
  117. ostr << "has no project to build. If this is a "
  118. "\"built with CMake\" project, verify that CTEST_CMAKE_GENERATOR "
  119. "is set. Otherwise, set CTEST_BUILD_COMMAND to build the project "
  120. "with a custom command line.";
  121. /* clang-format on */
  122. this->SetError(ostr.str());
  123. return nullptr;
  124. }
  125. }
  126. if (const char* useLaunchers =
  127. this->Makefile->GetDefinition("CTEST_USE_LAUNCHERS")) {
  128. this->CTest->SetCTestConfiguration("UseLaunchers", useLaunchers,
  129. this->Quiet);
  130. }
  131. if (const char* labelsForSubprojects =
  132. this->Makefile->GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {
  133. this->CTest->SetCTestConfiguration("LabelsForSubprojects",
  134. labelsForSubprojects, this->Quiet);
  135. }
  136. handler->SetQuiet(this->Quiet);
  137. return handler;
  138. }
  139. bool cmCTestBuildCommand::InitialPass(std::vector<std::string> const& args,
  140. cmExecutionStatus& status)
  141. {
  142. bool ret = cmCTestHandlerCommand::InitialPass(args, status);
  143. if (this->Values[ctb_NUMBER_ERRORS] && *this->Values[ctb_NUMBER_ERRORS]) {
  144. std::ostringstream str;
  145. str << this->Handler->GetTotalErrors();
  146. this->Makefile->AddDefinition(this->Values[ctb_NUMBER_ERRORS],
  147. str.str().c_str());
  148. }
  149. if (this->Values[ctb_NUMBER_WARNINGS] &&
  150. *this->Values[ctb_NUMBER_WARNINGS]) {
  151. std::ostringstream str;
  152. str << this->Handler->GetTotalWarnings();
  153. this->Makefile->AddDefinition(this->Values[ctb_NUMBER_WARNINGS],
  154. str.str().c_str());
  155. }
  156. return ret;
  157. }