cmCTestBuildCommand.cxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <cstring>
  5. #include <sstream>
  6. #include "cm_static_string_view.hxx"
  7. #include "cmCTest.h"
  8. #include "cmCTestBuildHandler.h"
  9. #include "cmGlobalGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmMessageType.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. #include "cmake.h"
  15. class cmExecutionStatus;
  16. void cmCTestBuildCommand::BindArguments()
  17. {
  18. this->cmCTestHandlerCommand::BindArguments();
  19. this->Bind("NUMBER_ERRORS"_s, this->NumberErrors);
  20. this->Bind("NUMBER_WARNINGS"_s, this->NumberWarnings);
  21. this->Bind("TARGET"_s, this->Target);
  22. this->Bind("CONFIGURATION"_s, this->Configuration);
  23. this->Bind("FLAGS"_s, this->Flags);
  24. this->Bind("PROJECT_NAME"_s, this->ProjectName);
  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. cmCTestBuildHandler* handler = this->CTest->GetBuildHandler();
  36. handler->Initialize();
  37. this->Handler = handler;
  38. const char* ctestBuildCommand =
  39. this->Makefile->GetDefinition("CTEST_BUILD_COMMAND");
  40. if (ctestBuildCommand && *ctestBuildCommand) {
  41. this->CTest->SetCTestConfiguration("MakeCommand", ctestBuildCommand,
  42. this->Quiet);
  43. } else {
  44. const char* cmakeGeneratorName =
  45. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR");
  46. // Build configuration is determined by: CONFIGURATION argument,
  47. // or CTEST_BUILD_CONFIGURATION script variable, or
  48. // CTEST_CONFIGURATION_TYPE script variable, or ctest -C command
  49. // line argument... in that order.
  50. //
  51. const char* ctestBuildConfiguration =
  52. this->Makefile->GetDefinition("CTEST_BUILD_CONFIGURATION");
  53. const char* cmakeBuildConfiguration = !this->Configuration.empty()
  54. ? this->Configuration.c_str()
  55. : ((ctestBuildConfiguration && *ctestBuildConfiguration)
  56. ? ctestBuildConfiguration
  57. : this->CTest->GetConfigType().c_str());
  58. const char* cmakeBuildAdditionalFlags = !this->Flags.empty()
  59. ? this->Flags.c_str()
  60. : this->Makefile->GetDefinition("CTEST_BUILD_FLAGS");
  61. const char* cmakeBuildTarget = !this->Target.empty()
  62. ? this->Target.c_str()
  63. : this->Makefile->GetDefinition("CTEST_BUILD_TARGET");
  64. if (cmakeGeneratorName && *cmakeGeneratorName) {
  65. if (!cmakeBuildConfiguration) {
  66. cmakeBuildConfiguration = "Release";
  67. }
  68. if (this->GlobalGenerator) {
  69. if (this->GlobalGenerator->GetName() != cmakeGeneratorName) {
  70. delete this->GlobalGenerator;
  71. this->GlobalGenerator = nullptr;
  72. }
  73. }
  74. if (!this->GlobalGenerator) {
  75. this->GlobalGenerator =
  76. this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
  77. cmakeGeneratorName);
  78. if (!this->GlobalGenerator) {
  79. std::string e = cmStrCat("could not create generator named \"",
  80. cmakeGeneratorName, '"');
  81. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  82. cmSystemTools::SetFatalErrorOccured();
  83. return nullptr;
  84. }
  85. }
  86. if (strlen(cmakeBuildConfiguration) == 0) {
  87. const char* config = nullptr;
  88. #ifdef CMAKE_INTDIR
  89. config = CMAKE_INTDIR;
  90. #endif
  91. if (!config) {
  92. config = "Debug";
  93. }
  94. cmakeBuildConfiguration = config;
  95. }
  96. std::string dir = this->CTest->GetCTestConfiguration("BuildDirectory");
  97. std::string buildCommand =
  98. this->GlobalGenerator->GenerateCMakeBuildCommand(
  99. cmakeBuildTarget ? cmakeBuildTarget : "", cmakeBuildConfiguration,
  100. cmakeBuildAdditionalFlags ? cmakeBuildAdditionalFlags : "",
  101. this->Makefile->IgnoreErrorsCMP0061());
  102. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  103. "SetMakeCommand:" << buildCommand << "\n",
  104. this->Quiet);
  105. this->CTest->SetCTestConfiguration("MakeCommand", buildCommand.c_str(),
  106. this->Quiet);
  107. } else {
  108. std::ostringstream ostr;
  109. /* clang-format off */
  110. ostr << "has no project to build. If this is a "
  111. "\"built with CMake\" project, verify that CTEST_CMAKE_GENERATOR "
  112. "is set. Otherwise, set CTEST_BUILD_COMMAND to build the project "
  113. "with a custom command line.";
  114. /* clang-format on */
  115. this->SetError(ostr.str());
  116. return nullptr;
  117. }
  118. }
  119. if (const char* useLaunchers =
  120. this->Makefile->GetDefinition("CTEST_USE_LAUNCHERS")) {
  121. this->CTest->SetCTestConfiguration("UseLaunchers", useLaunchers,
  122. this->Quiet);
  123. }
  124. if (const char* labelsForSubprojects =
  125. this->Makefile->GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {
  126. this->CTest->SetCTestConfiguration("LabelsForSubprojects",
  127. labelsForSubprojects, this->Quiet);
  128. }
  129. handler->SetQuiet(this->Quiet);
  130. return handler;
  131. }
  132. bool cmCTestBuildCommand::InitialPass(std::vector<std::string> const& args,
  133. cmExecutionStatus& status)
  134. {
  135. bool ret = cmCTestHandlerCommand::InitialPass(args, status);
  136. if (!this->NumberErrors.empty()) {
  137. this->Makefile->AddDefinition(
  138. this->NumberErrors, std::to_string(this->Handler->GetTotalErrors()));
  139. }
  140. if (!this->NumberWarnings.empty()) {
  141. this->Makefile->AddDefinition(
  142. this->NumberWarnings, std::to_string(this->Handler->GetTotalWarnings()));
  143. }
  144. return ret;
  145. }