cmCTestConfigureCommand.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "cmCTestConfigureCommand.h"
  4. #include <cstring>
  5. #include <sstream>
  6. #include <vector>
  7. #include <cmext/string_view>
  8. #include "cmCTest.h"
  9. #include "cmCTestConfigureHandler.h"
  10. #include "cmGlobalGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmProperty.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmSystemTools.h"
  15. #include "cmake.h"
  16. void cmCTestConfigureCommand::BindArguments()
  17. {
  18. this->cmCTestHandlerCommand::BindArguments();
  19. this->Bind("OPTIONS"_s, this->Options);
  20. }
  21. cmCTestGenericHandler* cmCTestConfigureCommand::InitializeHandler()
  22. {
  23. std::vector<std::string> options;
  24. if (!this->Options.empty()) {
  25. cmExpandList(this->Options, options);
  26. }
  27. if (this->CTest->GetCTestConfiguration("BuildDirectory").empty()) {
  28. this->SetError(
  29. "Build directory not specified. Either use BUILD "
  30. "argument to CTEST_CONFIGURE command or set CTEST_BINARY_DIRECTORY "
  31. "variable");
  32. return nullptr;
  33. }
  34. cmProp ctestConfigureCommand =
  35. this->Makefile->GetDefinition("CTEST_CONFIGURE_COMMAND");
  36. if (cmNonempty(ctestConfigureCommand)) {
  37. this->CTest->SetCTestConfiguration("ConfigureCommand",
  38. *ctestConfigureCommand, this->Quiet);
  39. } else {
  40. cmProp cmakeGeneratorName =
  41. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR");
  42. if (cmNonempty(cmakeGeneratorName)) {
  43. const std::string& source_dir =
  44. this->CTest->GetCTestConfiguration("SourceDirectory");
  45. if (source_dir.empty()) {
  46. this->SetError(
  47. "Source directory not specified. Either use SOURCE "
  48. "argument to CTEST_CONFIGURE command or set CTEST_SOURCE_DIRECTORY "
  49. "variable");
  50. return nullptr;
  51. }
  52. const std::string cmakelists_file = source_dir + "/CMakeLists.txt";
  53. if (!cmSystemTools::FileExists(cmakelists_file)) {
  54. std::ostringstream e;
  55. e << "CMakeLists.txt file does not exist [" << cmakelists_file << "]";
  56. this->SetError(e.str());
  57. return nullptr;
  58. }
  59. bool multiConfig = false;
  60. bool cmakeBuildTypeInOptions = false;
  61. auto gg = this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
  62. *cmakeGeneratorName);
  63. if (gg) {
  64. multiConfig = gg->IsMultiConfig();
  65. gg.reset();
  66. }
  67. std::string cmakeConfigureCommand =
  68. cmStrCat('"', cmSystemTools::GetCMakeCommand(), '"');
  69. for (std::string const& option : options) {
  70. cmakeConfigureCommand += " \"";
  71. cmakeConfigureCommand += option;
  72. cmakeConfigureCommand += "\"";
  73. if ((nullptr != strstr(option.c_str(), "CMAKE_BUILD_TYPE=")) ||
  74. (nullptr != strstr(option.c_str(), "CMAKE_BUILD_TYPE:STRING="))) {
  75. cmakeBuildTypeInOptions = true;
  76. }
  77. }
  78. if (!multiConfig && !cmakeBuildTypeInOptions &&
  79. !this->CTest->GetConfigType().empty()) {
  80. cmakeConfigureCommand += " \"-DCMAKE_BUILD_TYPE:STRING=";
  81. cmakeConfigureCommand += this->CTest->GetConfigType();
  82. cmakeConfigureCommand += "\"";
  83. }
  84. if (this->Makefile->IsOn("CTEST_USE_LAUNCHERS")) {
  85. cmakeConfigureCommand += " \"-DCTEST_USE_LAUNCHERS:BOOL=TRUE\"";
  86. }
  87. cmakeConfigureCommand += " \"-G";
  88. cmakeConfigureCommand += *cmakeGeneratorName;
  89. cmakeConfigureCommand += "\"";
  90. cmProp cmakeGeneratorPlatform =
  91. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR_PLATFORM");
  92. if (cmNonempty(cmakeGeneratorPlatform)) {
  93. cmakeConfigureCommand += " \"-A";
  94. cmakeConfigureCommand += *cmakeGeneratorPlatform;
  95. cmakeConfigureCommand += "\"";
  96. }
  97. cmProp cmakeGeneratorToolset =
  98. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR_TOOLSET");
  99. if (cmNonempty(cmakeGeneratorToolset)) {
  100. cmakeConfigureCommand += " \"-T";
  101. cmakeConfigureCommand += *cmakeGeneratorToolset;
  102. cmakeConfigureCommand += "\"";
  103. }
  104. cmakeConfigureCommand += " \"";
  105. cmakeConfigureCommand += source_dir;
  106. cmakeConfigureCommand += "\"";
  107. this->CTest->SetCTestConfiguration("ConfigureCommand",
  108. cmakeConfigureCommand, this->Quiet);
  109. } else {
  110. this->SetError(
  111. "Configure command is not specified. If this is a "
  112. "\"built with CMake\" project, set CTEST_CMAKE_GENERATOR. If not, "
  113. "set CTEST_CONFIGURE_COMMAND.");
  114. return nullptr;
  115. }
  116. }
  117. if (cmProp labelsForSubprojects =
  118. this->Makefile->GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {
  119. this->CTest->SetCTestConfiguration("LabelsForSubprojects",
  120. *labelsForSubprojects, this->Quiet);
  121. }
  122. cmCTestConfigureHandler* handler = this->CTest->GetConfigureHandler();
  123. handler->Initialize();
  124. handler->SetQuiet(this->Quiet);
  125. return handler;
  126. }