cmCTestBuildCommand.cxx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "cmCTest.h"
  5. #include "cmCTestBuildHandler.h"
  6. #include "cmGlobalGenerator.h"
  7. #include "cmMakefile.h"
  8. #include "cmMessageType.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmSystemTools.h"
  11. #include "cmake.h"
  12. #include <sstream>
  13. #include <string.h>
  14. class cmExecutionStatus;
  15. cmCTestBuildCommand::cmCTestBuildCommand()
  16. {
  17. this->GlobalGenerator = nullptr;
  18. this->Arguments[ctb_NUMBER_ERRORS] = "NUMBER_ERRORS";
  19. this->Arguments[ctb_NUMBER_WARNINGS] = "NUMBER_WARNINGS";
  20. this->Arguments[ctb_TARGET] = "TARGET";
  21. this->Arguments[ctb_CONFIGURATION] = "CONFIGURATION";
  22. this->Arguments[ctb_FLAGS] = "FLAGS";
  23. this->Arguments[ctb_PROJECT_NAME] = "PROJECT_NAME";
  24. this->Arguments[ctb_LAST] = nullptr;
  25. this->Last = ctb_LAST;
  26. }
  27. cmCTestBuildCommand::~cmCTestBuildCommand()
  28. {
  29. if (this->GlobalGenerator) {
  30. delete this->GlobalGenerator;
  31. this->GlobalGenerator = nullptr;
  32. }
  33. }
  34. cmCTestGenericHandler* cmCTestBuildCommand::InitializeHandler()
  35. {
  36. cmCTestBuildHandler* handler = this->CTest->GetBuildHandler();
  37. handler->Initialize();
  38. this->Handler = handler;
  39. const char* ctestBuildCommand =
  40. this->Makefile->GetDefinition("CTEST_BUILD_COMMAND");
  41. if (ctestBuildCommand && *ctestBuildCommand) {
  42. this->CTest->SetCTestConfiguration("MakeCommand", ctestBuildCommand,
  43. this->Quiet);
  44. } else {
  45. const char* cmakeGeneratorName =
  46. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR");
  47. // Build configuration is determined by: CONFIGURATION argument,
  48. // or CTEST_BUILD_CONFIGURATION script variable, or
  49. // CTEST_CONFIGURATION_TYPE script variable, or ctest -C command
  50. // line argument... in that order.
  51. //
  52. const char* ctestBuildConfiguration =
  53. this->Makefile->GetDefinition("CTEST_BUILD_CONFIGURATION");
  54. const char* cmakeBuildConfiguration =
  55. (this->Values[ctb_CONFIGURATION] && *this->Values[ctb_CONFIGURATION])
  56. ? this->Values[ctb_CONFIGURATION]
  57. : ((ctestBuildConfiguration && *ctestBuildConfiguration)
  58. ? ctestBuildConfiguration
  59. : this->CTest->GetConfigType().c_str());
  60. const char* cmakeBuildAdditionalFlags =
  61. (this->Values[ctb_FLAGS] && *this->Values[ctb_FLAGS])
  62. ? this->Values[ctb_FLAGS]
  63. : this->Makefile->GetDefinition("CTEST_BUILD_FLAGS");
  64. const char* cmakeBuildTarget =
  65. (this->Values[ctb_TARGET] && *this->Values[ctb_TARGET])
  66. ? this->Values[ctb_TARGET]
  67. : this->Makefile->GetDefinition("CTEST_BUILD_TARGET");
  68. if (cmakeGeneratorName && *cmakeGeneratorName) {
  69. if (!cmakeBuildConfiguration) {
  70. cmakeBuildConfiguration = "Release";
  71. }
  72. if (this->GlobalGenerator) {
  73. if (this->GlobalGenerator->GetName() != cmakeGeneratorName) {
  74. delete this->GlobalGenerator;
  75. this->GlobalGenerator = nullptr;
  76. }
  77. }
  78. if (!this->GlobalGenerator) {
  79. this->GlobalGenerator =
  80. this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
  81. cmakeGeneratorName);
  82. if (!this->GlobalGenerator) {
  83. std::string e = cmStrCat("could not create generator named \"",
  84. cmakeGeneratorName, '"');
  85. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  86. cmSystemTools::SetFatalErrorOccured();
  87. return nullptr;
  88. }
  89. }
  90. if (strlen(cmakeBuildConfiguration) == 0) {
  91. const char* config = nullptr;
  92. #ifdef CMAKE_INTDIR
  93. config = CMAKE_INTDIR;
  94. #endif
  95. if (!config) {
  96. config = "Debug";
  97. }
  98. cmakeBuildConfiguration = config;
  99. }
  100. std::string dir = this->CTest->GetCTestConfiguration("BuildDirectory");
  101. std::string buildCommand =
  102. this->GlobalGenerator->GenerateCMakeBuildCommand(
  103. cmakeBuildTarget ? cmakeBuildTarget : "", cmakeBuildConfiguration,
  104. cmakeBuildAdditionalFlags ? cmakeBuildAdditionalFlags : "",
  105. this->Makefile->IgnoreErrorsCMP0061());
  106. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  107. "SetMakeCommand:" << buildCommand << "\n",
  108. this->Quiet);
  109. this->CTest->SetCTestConfiguration("MakeCommand", buildCommand.c_str(),
  110. this->Quiet);
  111. } else {
  112. std::ostringstream ostr;
  113. /* clang-format off */
  114. ostr << "has no project to build. If this is a "
  115. "\"built with CMake\" project, verify that CTEST_CMAKE_GENERATOR "
  116. "is set. Otherwise, set CTEST_BUILD_COMMAND to build the project "
  117. "with a custom command line.";
  118. /* clang-format on */
  119. this->SetError(ostr.str());
  120. return nullptr;
  121. }
  122. }
  123. if (const char* useLaunchers =
  124. this->Makefile->GetDefinition("CTEST_USE_LAUNCHERS")) {
  125. this->CTest->SetCTestConfiguration("UseLaunchers", useLaunchers,
  126. this->Quiet);
  127. }
  128. if (const char* labelsForSubprojects =
  129. this->Makefile->GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {
  130. this->CTest->SetCTestConfiguration("LabelsForSubprojects",
  131. labelsForSubprojects, this->Quiet);
  132. }
  133. handler->SetQuiet(this->Quiet);
  134. return handler;
  135. }
  136. bool cmCTestBuildCommand::InitialPass(std::vector<std::string> const& args,
  137. cmExecutionStatus& status)
  138. {
  139. bool ret = cmCTestHandlerCommand::InitialPass(args, status);
  140. if (this->Values[ctb_NUMBER_ERRORS] && *this->Values[ctb_NUMBER_ERRORS]) {
  141. this->Makefile->AddDefinition(
  142. this->Values[ctb_NUMBER_ERRORS],
  143. std::to_string(this->Handler->GetTotalErrors()));
  144. }
  145. if (this->Values[ctb_NUMBER_WARNINGS] &&
  146. *this->Values[ctb_NUMBER_WARNINGS]) {
  147. this->Makefile->AddDefinition(
  148. this->Values[ctb_NUMBER_WARNINGS],
  149. std::to_string(this->Handler->GetTotalWarnings()));
  150. }
  151. return ret;
  152. }