cmCTestBuildCommand.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "cmProperty.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmSystemTools.h"
  15. #include "cmake.h"
  16. class cmExecutionStatus;
  17. void cmCTestBuildCommand::BindArguments()
  18. {
  19. this->cmCTestHandlerCommand::BindArguments();
  20. this->Bind("NUMBER_ERRORS"_s, this->NumberErrors);
  21. this->Bind("NUMBER_WARNINGS"_s, this->NumberWarnings);
  22. this->Bind("TARGET"_s, this->Target);
  23. this->Bind("CONFIGURATION"_s, this->Configuration);
  24. this->Bind("FLAGS"_s, this->Flags);
  25. this->Bind("PROJECT_NAME"_s, this->ProjectName);
  26. }
  27. cmCTestBuildCommand::~cmCTestBuildCommand() = default;
  28. cmCTestGenericHandler* cmCTestBuildCommand::InitializeHandler()
  29. {
  30. cmCTestBuildHandler* handler = this->CTest->GetBuildHandler();
  31. handler->Initialize();
  32. this->Handler = handler;
  33. cmProp ctestBuildCommand = this->Makefile->GetDef("CTEST_BUILD_COMMAND");
  34. if (cmNonempty(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,
  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 (cmProp useLaunchers = this->Makefile->GetDef("CTEST_USE_LAUNCHERS")) {
  113. this->CTest->SetCTestConfiguration("UseLaunchers", *useLaunchers,
  114. this->Quiet);
  115. }
  116. if (cmProp labelsForSubprojects =
  117. this->Makefile->GetDef("CTEST_LABELS_FOR_SUBPROJECTS")) {
  118. this->CTest->SetCTestConfiguration("LabelsForSubprojects",
  119. *labelsForSubprojects, this->Quiet);
  120. }
  121. handler->SetQuiet(this->Quiet);
  122. return handler;
  123. }
  124. bool cmCTestBuildCommand::InitialPass(std::vector<std::string> const& args,
  125. cmExecutionStatus& status)
  126. {
  127. bool ret = cmCTestHandlerCommand::InitialPass(args, status);
  128. if (!this->NumberErrors.empty()) {
  129. this->Makefile->AddDefinition(
  130. this->NumberErrors, std::to_string(this->Handler->GetTotalErrors()));
  131. }
  132. if (!this->NumberWarnings.empty()) {
  133. this->Makefile->AddDefinition(
  134. this->NumberWarnings, std::to_string(this->Handler->GetTotalWarnings()));
  135. }
  136. return ret;
  137. }