cmCTestConfigureCommand.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "cmGlobalGenerator.h"
  12. #include "cmCTest.h"
  13. #include "cmCTestGenericHandler.h"
  14. cmCTestConfigureCommand::cmCTestConfigureCommand()
  15. {
  16. this->Arguments[ctc_OPTIONS] = "OPTIONS";
  17. this->Arguments[ctc_LAST] = 0;
  18. this->Last = ctc_LAST;
  19. }
  20. cmCTestGenericHandler* cmCTestConfigureCommand::InitializeHandler()
  21. {
  22. std::vector<std::string> options;
  23. if (this->Values[ctc_OPTIONS])
  24. {
  25. cmSystemTools::ExpandListArgument(this->Values[ctc_OPTIONS], options);
  26. }
  27. if ( this->Values[ct_BUILD] )
  28. {
  29. this->CTest->SetCTestConfiguration("BuildDirectory",
  30. cmSystemTools::CollapseFullPath(
  31. this->Values[ct_BUILD]).c_str());
  32. }
  33. else
  34. {
  35. this->CTest->SetCTestConfiguration("BuildDirectory",
  36. cmSystemTools::CollapseFullPath(
  37. this->Makefile->GetDefinition("CTEST_BINARY_DIRECTORY")).c_str());
  38. }
  39. if ( this->Values[ct_SOURCE] )
  40. {
  41. this->CTest->SetCTestConfiguration("SourceDirectory",
  42. cmSystemTools::CollapseFullPath(
  43. this->Values[ct_SOURCE]).c_str());
  44. }
  45. else
  46. {
  47. this->CTest->SetCTestConfiguration("SourceDirectory",
  48. cmSystemTools::CollapseFullPath(
  49. this->Makefile->GetDefinition("CTEST_SOURCE_DIRECTORY")).c_str());
  50. }
  51. if ( this->CTest->GetCTestConfiguration("BuildDirectory").empty() )
  52. {
  53. this->SetError("Build directory not specified. Either use BUILD "
  54. "argument to CTEST_CONFIGURE command or set CTEST_BINARY_DIRECTORY "
  55. "variable");
  56. return 0;
  57. }
  58. const char* ctestConfigureCommand
  59. = this->Makefile->GetDefinition("CTEST_CONFIGURE_COMMAND");
  60. if ( ctestConfigureCommand && *ctestConfigureCommand )
  61. {
  62. this->CTest->SetCTestConfiguration("ConfigureCommand",
  63. ctestConfigureCommand);
  64. }
  65. else
  66. {
  67. const char* cmakeGeneratorName
  68. = this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR");
  69. if ( cmakeGeneratorName && *cmakeGeneratorName )
  70. {
  71. const std::string& source_dir
  72. = this->CTest->GetCTestConfiguration("SourceDirectory");
  73. if ( source_dir.empty() )
  74. {
  75. this->SetError("Source directory not specified. Either use SOURCE "
  76. "argument to CTEST_CONFIGURE command or set CTEST_SOURCE_DIRECTORY "
  77. "variable");
  78. return 0;
  79. }
  80. const std::string cmakelists_file = source_dir + "/CMakeLists.txt";
  81. if ( !cmSystemTools::FileExists(cmakelists_file.c_str()) )
  82. {
  83. cmOStringStream e;
  84. e << "CMakeLists.txt file does not exist ["
  85. << cmakelists_file << "]";
  86. this->SetError(e.str().c_str());
  87. return 0;
  88. }
  89. bool multiConfig = false;
  90. bool cmakeBuildTypeInOptions = false;
  91. cmGlobalGenerator *gg =
  92. this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
  93. cmakeGeneratorName);
  94. if(gg)
  95. {
  96. multiConfig = gg->IsMultiConfig();
  97. delete gg;
  98. }
  99. std::string cmakeConfigureCommand = "\"";
  100. cmakeConfigureCommand += this->CTest->GetCMakeExecutable();
  101. cmakeConfigureCommand += "\"";
  102. std::vector<std::string>::const_iterator it;
  103. std::string option;
  104. for (it= options.begin(); it!=options.end(); ++it)
  105. {
  106. option = *it;
  107. cmakeConfigureCommand += " \"";
  108. cmakeConfigureCommand += option;
  109. cmakeConfigureCommand += "\"";
  110. if ((0 != strstr(option.c_str(), "CMAKE_BUILD_TYPE=")) ||
  111. (0 != strstr(option.c_str(), "CMAKE_BUILD_TYPE:STRING=")))
  112. {
  113. cmakeBuildTypeInOptions = true;
  114. }
  115. }
  116. if (!multiConfig && !cmakeBuildTypeInOptions &&
  117. !this->CTest->GetConfigType().empty())
  118. {
  119. cmakeConfigureCommand += " \"-DCMAKE_BUILD_TYPE:STRING=";
  120. cmakeConfigureCommand += this->CTest->GetConfigType();
  121. cmakeConfigureCommand += "\"";
  122. }
  123. cmakeConfigureCommand += " \"-G";
  124. cmakeConfigureCommand += cmakeGeneratorName;
  125. cmakeConfigureCommand += "\"";
  126. cmakeConfigureCommand += " \"";
  127. cmakeConfigureCommand += source_dir;
  128. cmakeConfigureCommand += "\"";
  129. this->CTest->SetCTestConfiguration("ConfigureCommand",
  130. cmakeConfigureCommand.c_str());
  131. }
  132. else
  133. {
  134. this->SetError("Configure command is not specified. If this is a "
  135. "\"built with CMake\" project, set CTEST_CMAKE_GENERATOR. If not, "
  136. "set CTEST_CONFIGURE_COMMAND.");
  137. return 0;
  138. }
  139. }
  140. cmCTestGenericHandler* handler
  141. = this->CTest->GetInitializedHandler("configure");
  142. if ( !handler )
  143. {
  144. this->SetError(
  145. "internal CTest error. Cannot instantiate configure handler");
  146. return 0;
  147. }
  148. return handler;
  149. }