cmCTestBuildCommand.cxx 5.6 KB

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