cmCTestBuildCommand.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "cmStringAlgorithms.h"
  12. #include "cmSystemTools.h"
  13. #include "cmValue.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. this->Bind("PARALLEL_LEVEL"_s, this->ParallelLevel);
  26. }
  27. cmCTestBuildCommand::~cmCTestBuildCommand() = default;
  28. cmCTestGenericHandler* cmCTestBuildCommand::InitializeHandler()
  29. {
  30. cmCTestBuildHandler* handler = this->CTest->GetBuildHandler();
  31. handler->Initialize();
  32. this->Handler = handler;
  33. cmValue ctestBuildCommand =
  34. this->Makefile->GetDefinition("CTEST_BUILD_COMMAND");
  35. if (cmNonempty(ctestBuildCommand)) {
  36. this->CTest->SetCTestConfiguration("MakeCommand", *ctestBuildCommand,
  37. this->Quiet);
  38. } else {
  39. cmValue cmakeGeneratorName =
  40. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR");
  41. // Build configuration is determined by: CONFIGURATION argument,
  42. // or CTEST_BUILD_CONFIGURATION script variable, or
  43. // CTEST_CONFIGURATION_TYPE script variable, or ctest -C command
  44. // line argument... in that order.
  45. //
  46. cmValue ctestBuildConfiguration =
  47. this->Makefile->GetDefinition("CTEST_BUILD_CONFIGURATION");
  48. std::string cmakeBuildConfiguration = cmNonempty(this->Configuration)
  49. ? this->Configuration
  50. : cmNonempty(ctestBuildConfiguration) ? *ctestBuildConfiguration
  51. : this->CTest->GetConfigType();
  52. const std::string& cmakeBuildAdditionalFlags = cmNonempty(this->Flags)
  53. ? this->Flags
  54. : this->Makefile->GetSafeDefinition("CTEST_BUILD_FLAGS");
  55. const std::string& cmakeBuildTarget = cmNonempty(this->Target)
  56. ? this->Target
  57. : this->Makefile->GetSafeDefinition("CTEST_BUILD_TARGET");
  58. if (cmNonempty(cmakeGeneratorName)) {
  59. if (cmakeBuildConfiguration.empty()) {
  60. cmakeBuildConfiguration = "Release";
  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. #ifdef CMAKE_INTDIR
  81. cmakeBuildConfiguration = CMAKE_INTDIR;
  82. #else
  83. cmakeBuildConfiguration = "Debug";
  84. #endif
  85. }
  86. std::string dir = this->CTest->GetCTestConfiguration("BuildDirectory");
  87. std::string buildCommand =
  88. this->GlobalGenerator->GenerateCMakeBuildCommand(
  89. cmakeBuildTarget, cmakeBuildConfiguration, this->ParallelLevel,
  90. cmakeBuildAdditionalFlags, this->Makefile->IgnoreErrorsCMP0061());
  91. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  92. "SetMakeCommand:" << buildCommand << "\n",
  93. this->Quiet);
  94. this->CTest->SetCTestConfiguration("MakeCommand", buildCommand,
  95. this->Quiet);
  96. } else {
  97. std::ostringstream ostr;
  98. /* clang-format off */
  99. ostr << "has no project to build. If this is a "
  100. "\"built with CMake\" project, verify that CTEST_CMAKE_GENERATOR "
  101. "is set. Otherwise, set CTEST_BUILD_COMMAND to build the project "
  102. "with a custom command line.";
  103. /* clang-format on */
  104. this->SetError(ostr.str());
  105. return nullptr;
  106. }
  107. }
  108. if (cmValue useLaunchers =
  109. this->Makefile->GetDefinition("CTEST_USE_LAUNCHERS")) {
  110. this->CTest->SetCTestConfiguration("UseLaunchers", *useLaunchers,
  111. this->Quiet);
  112. }
  113. if (cmValue labelsForSubprojects =
  114. this->Makefile->GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {
  115. this->CTest->SetCTestConfiguration("LabelsForSubprojects",
  116. *labelsForSubprojects, this->Quiet);
  117. }
  118. handler->SetQuiet(this->Quiet);
  119. return handler;
  120. }
  121. bool cmCTestBuildCommand::InitialPass(std::vector<std::string> const& args,
  122. cmExecutionStatus& status)
  123. {
  124. bool ret = this->cmCTestHandlerCommand::InitialPass(args, status);
  125. if (!this->NumberErrors.empty()) {
  126. this->Makefile->AddDefinition(
  127. this->NumberErrors, std::to_string(this->Handler->GetTotalErrors()));
  128. }
  129. if (!this->NumberWarnings.empty()) {
  130. this->Makefile->AddDefinition(
  131. this->NumberWarnings, std::to_string(this->Handler->GetTotalWarnings()));
  132. }
  133. return ret;
  134. }