cmCTestBuildCommand.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <cmext/string_view>
  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() = default;
  27. cmCTestGenericHandler* cmCTestBuildCommand::InitializeHandler()
  28. {
  29. cmCTestBuildHandler* handler = this->CTest->GetBuildHandler();
  30. handler->Initialize();
  31. this->Handler = handler;
  32. const char* ctestBuildCommand =
  33. this->Makefile->GetDefinition("CTEST_BUILD_COMMAND");
  34. if (ctestBuildCommand && *ctestBuildCommand) {
  35. this->CTest->SetCTestConfiguration("MakeCommand", ctestBuildCommand,
  36. this->Quiet);
  37. } else {
  38. const char* 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. const char* ctestBuildConfiguration =
  46. this->Makefile->GetDefinition("CTEST_BUILD_CONFIGURATION");
  47. const char* cmakeBuildConfiguration = !this->Configuration.empty()
  48. ? this->Configuration.c_str()
  49. : ((ctestBuildConfiguration && *ctestBuildConfiguration)
  50. ? ctestBuildConfiguration
  51. : this->CTest->GetConfigType().c_str());
  52. const char* cmakeBuildAdditionalFlags = !this->Flags.empty()
  53. ? this->Flags.c_str()
  54. : this->Makefile->GetDefinition("CTEST_BUILD_FLAGS");
  55. const char* cmakeBuildTarget = !this->Target.empty()
  56. ? this->Target.c_str()
  57. : this->Makefile->GetDefinition("CTEST_BUILD_TARGET");
  58. if (cmakeGeneratorName && *cmakeGeneratorName) {
  59. if (!cmakeBuildConfiguration) {
  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 (strlen(cmakeBuildConfiguration) == 0) {
  80. const char* config = nullptr;
  81. #ifdef CMAKE_INTDIR
  82. config = CMAKE_INTDIR;
  83. #endif
  84. if (!config) {
  85. config = "Debug";
  86. }
  87. cmakeBuildConfiguration = config;
  88. }
  89. std::string dir = this->CTest->GetCTestConfiguration("BuildDirectory");
  90. std::string buildCommand =
  91. this->GlobalGenerator->GenerateCMakeBuildCommand(
  92. cmakeBuildTarget ? cmakeBuildTarget : "", cmakeBuildConfiguration,
  93. cmakeBuildAdditionalFlags ? cmakeBuildAdditionalFlags : "",
  94. this->Makefile->IgnoreErrorsCMP0061());
  95. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  96. "SetMakeCommand:" << buildCommand << "\n",
  97. this->Quiet);
  98. this->CTest->SetCTestConfiguration("MakeCommand", buildCommand.c_str(),
  99. this->Quiet);
  100. } else {
  101. std::ostringstream ostr;
  102. /* clang-format off */
  103. ostr << "has no project to build. If this is a "
  104. "\"built with CMake\" project, verify that CTEST_CMAKE_GENERATOR "
  105. "is set. Otherwise, set CTEST_BUILD_COMMAND to build the project "
  106. "with a custom command line.";
  107. /* clang-format on */
  108. this->SetError(ostr.str());
  109. return nullptr;
  110. }
  111. }
  112. if (const char* useLaunchers =
  113. this->Makefile->GetDefinition("CTEST_USE_LAUNCHERS")) {
  114. this->CTest->SetCTestConfiguration("UseLaunchers", useLaunchers,
  115. this->Quiet);
  116. }
  117. if (const char* labelsForSubprojects =
  118. this->Makefile->GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {
  119. this->CTest->SetCTestConfiguration("LabelsForSubprojects",
  120. labelsForSubprojects, this->Quiet);
  121. }
  122. handler->SetQuiet(this->Quiet);
  123. return handler;
  124. }
  125. bool cmCTestBuildCommand::InitialPass(std::vector<std::string> const& args,
  126. cmExecutionStatus& status)
  127. {
  128. bool ret = cmCTestHandlerCommand::InitialPass(args, status);
  129. if (!this->NumberErrors.empty()) {
  130. this->Makefile->AddDefinition(
  131. this->NumberErrors, std::to_string(this->Handler->GetTotalErrors()));
  132. }
  133. if (!this->NumberWarnings.empty()) {
  134. this->Makefile->AddDefinition(
  135. this->NumberWarnings, std::to_string(this->Handler->GetTotalWarnings()));
  136. }
  137. return ret;
  138. }