cmCTestBuildCommand.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. std::string cmakeBuildConfiguration = cmNonempty(this->Configuration)
  48. ? this->Configuration
  49. : cmNonempty(ctestBuildConfiguration) ? *ctestBuildConfiguration
  50. : this->CTest->GetConfigType();
  51. const std::string& cmakeBuildAdditionalFlags = cmNonempty(this->Flags)
  52. ? this->Flags
  53. : this->Makefile->GetSafeDefinition("CTEST_BUILD_FLAGS");
  54. const std::string& cmakeBuildTarget = cmNonempty(this->Target)
  55. ? this->Target
  56. : this->Makefile->GetSafeDefinition("CTEST_BUILD_TARGET");
  57. if (cmNonempty(cmakeGeneratorName)) {
  58. if (cmakeBuildConfiguration.empty()) {
  59. cmakeBuildConfiguration = "Release";
  60. }
  61. if (this->GlobalGenerator) {
  62. if (this->GlobalGenerator->GetName() != *cmakeGeneratorName) {
  63. this->GlobalGenerator.reset();
  64. }
  65. }
  66. if (!this->GlobalGenerator) {
  67. this->GlobalGenerator =
  68. this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
  69. *cmakeGeneratorName);
  70. if (!this->GlobalGenerator) {
  71. std::string e = cmStrCat("could not create generator named \"",
  72. *cmakeGeneratorName, '"');
  73. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  74. cmSystemTools::SetFatalErrorOccured();
  75. return nullptr;
  76. }
  77. }
  78. if (cmakeBuildConfiguration.empty()) {
  79. #ifdef CMAKE_INTDIR
  80. cmakeBuildConfiguration = CMAKE_INTDIR;
  81. #else
  82. cmakeBuildConfiguration = "Debug";
  83. #endif
  84. }
  85. std::string dir = this->CTest->GetCTestConfiguration("BuildDirectory");
  86. std::string buildCommand =
  87. this->GlobalGenerator->GenerateCMakeBuildCommand(
  88. cmakeBuildTarget, cmakeBuildConfiguration, cmakeBuildAdditionalFlags,
  89. this->Makefile->IgnoreErrorsCMP0061());
  90. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  91. "SetMakeCommand:" << buildCommand << "\n",
  92. this->Quiet);
  93. this->CTest->SetCTestConfiguration("MakeCommand", buildCommand,
  94. this->Quiet);
  95. } else {
  96. std::ostringstream ostr;
  97. /* clang-format off */
  98. ostr << "has no project to build. If this is a "
  99. "\"built with CMake\" project, verify that CTEST_CMAKE_GENERATOR "
  100. "is set. Otherwise, set CTEST_BUILD_COMMAND to build the project "
  101. "with a custom command line.";
  102. /* clang-format on */
  103. this->SetError(ostr.str());
  104. return nullptr;
  105. }
  106. }
  107. if (cmProp useLaunchers =
  108. this->Makefile->GetDefinition("CTEST_USE_LAUNCHERS")) {
  109. this->CTest->SetCTestConfiguration("UseLaunchers", *useLaunchers,
  110. this->Quiet);
  111. }
  112. if (cmProp labelsForSubprojects =
  113. this->Makefile->GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {
  114. this->CTest->SetCTestConfiguration("LabelsForSubprojects",
  115. *labelsForSubprojects, this->Quiet);
  116. }
  117. handler->SetQuiet(this->Quiet);
  118. return handler;
  119. }
  120. bool cmCTestBuildCommand::InitialPass(std::vector<std::string> const& args,
  121. cmExecutionStatus& status)
  122. {
  123. bool ret = this->cmCTestHandlerCommand::InitialPass(args, status);
  124. if (!this->NumberErrors.empty()) {
  125. this->Makefile->AddDefinition(
  126. this->NumberErrors, std::to_string(this->Handler->GetTotalErrors()));
  127. }
  128. if (!this->NumberWarnings.empty()) {
  129. this->Makefile->AddDefinition(
  130. this->NumberWarnings, std::to_string(this->Handler->GetTotalWarnings()));
  131. }
  132. return ret;
  133. }