cmCTestConfigureCommand.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCTestConfigureCommand.h"
  11. #include "cmCTest.h"
  12. #include "cmCTestGenericHandler.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmSystemTools.h"
  16. #include "cmake.h"
  17. #include <sstream>
  18. #include <string.h>
  19. #include <vector>
  20. cmCTestConfigureCommand::cmCTestConfigureCommand()
  21. {
  22. this->Arguments[ctc_OPTIONS] = "OPTIONS";
  23. this->Arguments[ctc_LAST] = CM_NULLPTR;
  24. this->Last = ctc_LAST;
  25. }
  26. cmCTestGenericHandler* cmCTestConfigureCommand::InitializeHandler()
  27. {
  28. std::vector<std::string> options;
  29. if (this->Values[ctc_OPTIONS]) {
  30. cmSystemTools::ExpandListArgument(this->Values[ctc_OPTIONS], options);
  31. }
  32. if (this->CTest->GetCTestConfiguration("BuildDirectory").empty()) {
  33. this->SetError(
  34. "Build directory not specified. Either use BUILD "
  35. "argument to CTEST_CONFIGURE command or set CTEST_BINARY_DIRECTORY "
  36. "variable");
  37. return CM_NULLPTR;
  38. }
  39. const char* ctestConfigureCommand =
  40. this->Makefile->GetDefinition("CTEST_CONFIGURE_COMMAND");
  41. if (ctestConfigureCommand && *ctestConfigureCommand) {
  42. this->CTest->SetCTestConfiguration("ConfigureCommand",
  43. ctestConfigureCommand, this->Quiet);
  44. } else {
  45. const char* cmakeGeneratorName =
  46. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR");
  47. if (cmakeGeneratorName && *cmakeGeneratorName) {
  48. const std::string& source_dir =
  49. this->CTest->GetCTestConfiguration("SourceDirectory");
  50. if (source_dir.empty()) {
  51. this->SetError(
  52. "Source directory not specified. Either use SOURCE "
  53. "argument to CTEST_CONFIGURE command or set CTEST_SOURCE_DIRECTORY "
  54. "variable");
  55. return CM_NULLPTR;
  56. }
  57. const std::string cmakelists_file = source_dir + "/CMakeLists.txt";
  58. if (!cmSystemTools::FileExists(cmakelists_file.c_str())) {
  59. std::ostringstream e;
  60. e << "CMakeLists.txt file does not exist [" << cmakelists_file << "]";
  61. this->SetError(e.str());
  62. return CM_NULLPTR;
  63. }
  64. bool multiConfig = false;
  65. bool cmakeBuildTypeInOptions = false;
  66. cmGlobalGenerator* gg =
  67. this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
  68. cmakeGeneratorName);
  69. if (gg) {
  70. multiConfig = gg->IsMultiConfig();
  71. delete gg;
  72. }
  73. std::string cmakeConfigureCommand = "\"";
  74. cmakeConfigureCommand += cmSystemTools::GetCMakeCommand();
  75. cmakeConfigureCommand += "\"";
  76. std::vector<std::string>::const_iterator it;
  77. std::string option;
  78. for (it = options.begin(); it != options.end(); ++it) {
  79. option = *it;
  80. cmakeConfigureCommand += " \"";
  81. cmakeConfigureCommand += option;
  82. cmakeConfigureCommand += "\"";
  83. if ((CM_NULLPTR != strstr(option.c_str(), "CMAKE_BUILD_TYPE=")) ||
  84. (CM_NULLPTR !=
  85. strstr(option.c_str(), "CMAKE_BUILD_TYPE:STRING="))) {
  86. cmakeBuildTypeInOptions = true;
  87. }
  88. }
  89. if (!multiConfig && !cmakeBuildTypeInOptions &&
  90. !this->CTest->GetConfigType().empty()) {
  91. cmakeConfigureCommand += " \"-DCMAKE_BUILD_TYPE:STRING=";
  92. cmakeConfigureCommand += this->CTest->GetConfigType();
  93. cmakeConfigureCommand += "\"";
  94. }
  95. cmakeConfigureCommand += " \"-G";
  96. cmakeConfigureCommand += cmakeGeneratorName;
  97. cmakeConfigureCommand += "\"";
  98. const char* cmakeGeneratorPlatform =
  99. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR_PLATFORM");
  100. if (cmakeGeneratorPlatform && *cmakeGeneratorPlatform) {
  101. cmakeConfigureCommand += " \"-A";
  102. cmakeConfigureCommand += cmakeGeneratorPlatform;
  103. cmakeConfigureCommand += "\"";
  104. }
  105. const char* cmakeGeneratorToolset =
  106. this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR_TOOLSET");
  107. if (cmakeGeneratorToolset && *cmakeGeneratorToolset) {
  108. cmakeConfigureCommand += " \"-T";
  109. cmakeConfigureCommand += cmakeGeneratorToolset;
  110. cmakeConfigureCommand += "\"";
  111. }
  112. cmakeConfigureCommand += " \"";
  113. cmakeConfigureCommand += source_dir;
  114. cmakeConfigureCommand += "\"";
  115. this->CTest->SetCTestConfiguration(
  116. "ConfigureCommand", cmakeConfigureCommand.c_str(), this->Quiet);
  117. } else {
  118. this->SetError(
  119. "Configure command is not specified. If this is a "
  120. "\"built with CMake\" project, set CTEST_CMAKE_GENERATOR. If not, "
  121. "set CTEST_CONFIGURE_COMMAND.");
  122. return CM_NULLPTR;
  123. }
  124. }
  125. cmCTestGenericHandler* handler =
  126. this->CTest->GetInitializedHandler("configure");
  127. if (!handler) {
  128. this->SetError(
  129. "internal CTest error. Cannot instantiate configure handler");
  130. return CM_NULLPTR;
  131. }
  132. handler->SetQuiet(this->Quiet);
  133. return handler;
  134. }