cmCTestConfigureCommand.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. 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. const char* cmakeGeneratorPlatform =
  91. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR_PLATFORM");
  92. if (cmakeGeneratorPlatform && *cmakeGeneratorPlatform) {
  93. cmakeConfigureCommand += " \"-A";
  94. cmakeConfigureCommand += cmakeGeneratorPlatform;
  95. cmakeConfigureCommand += "\"";
  96. }
  97. const char* cmakeGeneratorToolset =
  98. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR_TOOLSET");
  99. if (cmakeGeneratorToolset && *cmakeGeneratorToolset) {
  100. cmakeConfigureCommand += " \"-T";
  101. cmakeConfigureCommand += cmakeGeneratorToolset;
  102. cmakeConfigureCommand += "\"";
  103. }
  104. cmakeConfigureCommand += " \"";
  105. cmakeConfigureCommand += source_dir;
  106. cmakeConfigureCommand += "\"";
  107. this->CTest->SetCTestConfiguration(
  108. "ConfigureCommand", cmakeConfigureCommand.c_str(), 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 (const char* 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. }