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