cmCTestBuildCommand.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "cmCTestBuildCommand.h"
  11. #include "cmCTest.h"
  12. #include "cmCTestGenericHandler.h"
  13. #include "cmCTestBuildHandler.h"
  14. #include "cmake.h"
  15. #include "cmGlobalGenerator.h"
  16. //----------------------------------------------------------------------------
  17. cmCTestBuildCommand::cmCTestBuildCommand()
  18. {
  19. this->GlobalGenerator = 0;
  20. this->Arguments[ctb_NUMBER_ERRORS] = "NUMBER_ERRORS";
  21. this->Arguments[ctb_NUMBER_WARNINGS] = "NUMBER_WARNINGS";
  22. this->Arguments[ctb_TARGET] = "TARGET";
  23. this->Arguments[ctb_CONFIGURATION] = "CONFIGURATION";
  24. this->Arguments[ctb_FLAGS] = "FLAGS";
  25. this->Arguments[ctb_PROJECT_NAME] = "PROJECT_NAME";
  26. this->Arguments[ctb_LAST] = 0;
  27. this->Last = ctb_LAST;
  28. }
  29. //----------------------------------------------------------------------------
  30. cmCTestBuildCommand::~cmCTestBuildCommand()
  31. {
  32. if ( this->GlobalGenerator )
  33. {
  34. delete this->GlobalGenerator;
  35. this->GlobalGenerator = 0;
  36. }
  37. }
  38. //----------------------------------------------------------------------------
  39. cmCTestGenericHandler* cmCTestBuildCommand::InitializeHandler()
  40. {
  41. cmCTestGenericHandler* handler
  42. = this->CTest->GetInitializedHandler("build");
  43. if ( !handler )
  44. {
  45. this->SetError("internal CTest error. Cannot instantiate build handler");
  46. return 0;
  47. }
  48. this->Handler = (cmCTestBuildHandler*)handler;
  49. const char* ctestBuildCommand
  50. = this->Makefile->GetDefinition("CTEST_BUILD_COMMAND");
  51. if ( ctestBuildCommand && *ctestBuildCommand )
  52. {
  53. this->CTest->SetCTestConfiguration("MakeCommand", ctestBuildCommand,
  54. this->Quiet);
  55. }
  56. else
  57. {
  58. const char* cmakeGeneratorName
  59. = this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR");
  60. const char* cmakeProjectName
  61. = (this->Values[ctb_PROJECT_NAME] && *this->Values[ctb_PROJECT_NAME])
  62. ? this->Values[ctb_PROJECT_NAME]
  63. : this->Makefile->GetDefinition("CTEST_PROJECT_NAME");
  64. // Build configuration is determined by: CONFIGURATION argument,
  65. // or CTEST_BUILD_CONFIGURATION script variable, or
  66. // CTEST_CONFIGURATION_TYPE script variable, or ctest -C command
  67. // line argument... in that order.
  68. //
  69. const char* ctestBuildConfiguration
  70. = this->Makefile->GetDefinition("CTEST_BUILD_CONFIGURATION");
  71. const char* cmakeBuildConfiguration
  72. = (this->Values[ctb_CONFIGURATION] && *this->Values[ctb_CONFIGURATION])
  73. ? this->Values[ctb_CONFIGURATION]
  74. : ((ctestBuildConfiguration && *ctestBuildConfiguration)
  75. ? ctestBuildConfiguration
  76. : this->CTest->GetConfigType().c_str());
  77. const char* cmakeBuildAdditionalFlags
  78. = (this->Values[ctb_FLAGS] && *this->Values[ctb_FLAGS])
  79. ? this->Values[ctb_FLAGS]
  80. : this->Makefile->GetDefinition("CTEST_BUILD_FLAGS");
  81. const char* cmakeBuildTarget
  82. = (this->Values[ctb_TARGET] && *this->Values[ctb_TARGET])
  83. ? this->Values[ctb_TARGET]
  84. : this->Makefile->GetDefinition("CTEST_BUILD_TARGET");
  85. if ( cmakeGeneratorName && *cmakeGeneratorName &&
  86. cmakeProjectName && *cmakeProjectName )
  87. {
  88. if ( !cmakeBuildConfiguration )
  89. {
  90. cmakeBuildConfiguration = "Release";
  91. }
  92. if ( this->GlobalGenerator )
  93. {
  94. if ( this->GlobalGenerator->GetName() != cmakeGeneratorName )
  95. {
  96. delete this->GlobalGenerator;
  97. this->GlobalGenerator = 0;
  98. }
  99. }
  100. if ( !this->GlobalGenerator )
  101. {
  102. this->GlobalGenerator =
  103. this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
  104. cmakeGeneratorName);
  105. if(!this->GlobalGenerator)
  106. {
  107. std::string e = "could not create generator named \"";
  108. e += cmakeGeneratorName;
  109. e += "\"";
  110. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e);
  111. cmSystemTools::SetFatalErrorOccured();
  112. return 0;
  113. }
  114. }
  115. if(strlen(cmakeBuildConfiguration) == 0)
  116. {
  117. const char* config = 0;
  118. #ifdef CMAKE_INTDIR
  119. config = CMAKE_INTDIR;
  120. #endif
  121. if(!config)
  122. {
  123. config = "Debug";
  124. }
  125. cmakeBuildConfiguration = config;
  126. }
  127. std::string dir = this->CTest->GetCTestConfiguration("BuildDirectory");
  128. std::string buildCommand
  129. = this->GlobalGenerator->
  130. GenerateCMakeBuildCommand(cmakeBuildTarget ? cmakeBuildTarget : "",
  131. cmakeBuildConfiguration,
  132. cmakeBuildAdditionalFlags ? cmakeBuildAdditionalFlags : "", true);
  133. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  134. "SetMakeCommand:" << buildCommand << "\n", this->Quiet);
  135. this->CTest->SetCTestConfiguration("MakeCommand", buildCommand.c_str(),
  136. this->Quiet);
  137. }
  138. else
  139. {
  140. std::ostringstream ostr;
  141. ostr << "has no project to build. If this is a "
  142. "\"built with CMake\" project, verify that CTEST_CMAKE_GENERATOR "
  143. "and CTEST_PROJECT_NAME are set."
  144. "\n"
  145. "CTEST_PROJECT_NAME is usually set in CTestConfig.cmake. Verify "
  146. "that CTestConfig.cmake exists, or CTEST_PROJECT_NAME "
  147. "is set in the script, or PROJECT_NAME is passed as an argument "
  148. "to ctest_build."
  149. "\n"
  150. "Alternatively, set CTEST_BUILD_COMMAND to build the project "
  151. "with a custom command line.";
  152. this->SetError(ostr.str());
  153. return 0;
  154. }
  155. }
  156. if(const char* useLaunchers =
  157. this->Makefile->GetDefinition("CTEST_USE_LAUNCHERS"))
  158. {
  159. this->CTest->SetCTestConfiguration("UseLaunchers", useLaunchers,
  160. this->Quiet);
  161. }
  162. handler->SetQuiet(this->Quiet);
  163. return handler;
  164. }
  165. bool cmCTestBuildCommand::InitialPass(std::vector<std::string> const& args,
  166. cmExecutionStatus &status)
  167. {
  168. bool ret = cmCTestHandlerCommand::InitialPass(args, status);
  169. if ( this->Values[ctb_NUMBER_ERRORS] && *this->Values[ctb_NUMBER_ERRORS])
  170. {
  171. std::ostringstream str;
  172. str << this->Handler->GetTotalErrors();
  173. this->Makefile->AddDefinition(
  174. this->Values[ctb_NUMBER_ERRORS], str.str().c_str());
  175. }
  176. if ( this->Values[ctb_NUMBER_WARNINGS]
  177. && *this->Values[ctb_NUMBER_WARNINGS])
  178. {
  179. std::ostringstream str;
  180. str << this->Handler->GetTotalWarnings();
  181. this->Makefile->AddDefinition(
  182. this->Values[ctb_NUMBER_WARNINGS], str.str().c_str());
  183. }
  184. return ret;
  185. }