1
0

cmCTestBuildCommand.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "cmSystemTools.h"
  10. #include "cmake.h"
  11. #include <sstream>
  12. #include <string.h>
  13. class cmExecutionStatus;
  14. cmCTestBuildCommand::cmCTestBuildCommand()
  15. {
  16. this->GlobalGenerator = nullptr;
  17. this->Arguments[ctb_NUMBER_ERRORS] = "NUMBER_ERRORS";
  18. this->Arguments[ctb_NUMBER_WARNINGS] = "NUMBER_WARNINGS";
  19. this->Arguments[ctb_TARGET] = "TARGET";
  20. this->Arguments[ctb_CONFIGURATION] = "CONFIGURATION";
  21. this->Arguments[ctb_FLAGS] = "FLAGS";
  22. this->Arguments[ctb_PROJECT_NAME] = "PROJECT_NAME";
  23. this->Arguments[ctb_LAST] = nullptr;
  24. this->Last = ctb_LAST;
  25. }
  26. cmCTestBuildCommand::~cmCTestBuildCommand()
  27. {
  28. if (this->GlobalGenerator) {
  29. delete this->GlobalGenerator;
  30. this->GlobalGenerator = nullptr;
  31. }
  32. }
  33. cmCTestGenericHandler* cmCTestBuildCommand::InitializeHandler()
  34. {
  35. cmCTestBuildHandler* handler = this->CTest->GetBuildHandler();
  36. handler->Initialize();
  37. this->Handler = handler;
  38. const char* ctestBuildCommand =
  39. this->Makefile->GetDefinition("CTEST_BUILD_COMMAND");
  40. if (ctestBuildCommand && *ctestBuildCommand) {
  41. this->CTest->SetCTestConfiguration("MakeCommand", ctestBuildCommand,
  42. this->Quiet);
  43. } else {
  44. const char* cmakeGeneratorName =
  45. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR");
  46. // Build configuration is determined by: CONFIGURATION argument,
  47. // or CTEST_BUILD_CONFIGURATION script variable, or
  48. // CTEST_CONFIGURATION_TYPE script variable, or ctest -C command
  49. // line argument... in that order.
  50. //
  51. const char* ctestBuildConfiguration =
  52. this->Makefile->GetDefinition("CTEST_BUILD_CONFIGURATION");
  53. const char* cmakeBuildConfiguration =
  54. (this->Values[ctb_CONFIGURATION] && *this->Values[ctb_CONFIGURATION])
  55. ? this->Values[ctb_CONFIGURATION]
  56. : ((ctestBuildConfiguration && *ctestBuildConfiguration)
  57. ? ctestBuildConfiguration
  58. : this->CTest->GetConfigType().c_str());
  59. const char* cmakeBuildAdditionalFlags =
  60. (this->Values[ctb_FLAGS] && *this->Values[ctb_FLAGS])
  61. ? this->Values[ctb_FLAGS]
  62. : this->Makefile->GetDefinition("CTEST_BUILD_FLAGS");
  63. const char* cmakeBuildTarget =
  64. (this->Values[ctb_TARGET] && *this->Values[ctb_TARGET])
  65. ? this->Values[ctb_TARGET]
  66. : this->Makefile->GetDefinition("CTEST_BUILD_TARGET");
  67. if (cmakeGeneratorName && *cmakeGeneratorName) {
  68. if (!cmakeBuildConfiguration) {
  69. cmakeBuildConfiguration = "Release";
  70. }
  71. if (this->GlobalGenerator) {
  72. if (this->GlobalGenerator->GetName() != cmakeGeneratorName) {
  73. delete this->GlobalGenerator;
  74. this->GlobalGenerator = nullptr;
  75. }
  76. }
  77. if (!this->GlobalGenerator) {
  78. this->GlobalGenerator =
  79. this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
  80. cmakeGeneratorName);
  81. if (!this->GlobalGenerator) {
  82. std::string e = "could not create generator named \"";
  83. e += cmakeGeneratorName;
  84. e += "\"";
  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. std::ostringstream str;
  142. str << this->Handler->GetTotalErrors();
  143. this->Makefile->AddDefinition(this->Values[ctb_NUMBER_ERRORS], str.str());
  144. }
  145. if (this->Values[ctb_NUMBER_WARNINGS] &&
  146. *this->Values[ctb_NUMBER_WARNINGS]) {
  147. std::ostringstream str;
  148. str << this->Handler->GetTotalWarnings();
  149. this->Makefile->AddDefinition(this->Values[ctb_NUMBER_WARNINGS],
  150. str.str());
  151. }
  152. return ret;
  153. }