cmCTestBuildCommand.cxx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. }
  55. else
  56. {
  57. const char* cmakeGeneratorName
  58. = this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR");
  59. const char* cmakeProjectName
  60. = (this->Values[ctb_PROJECT_NAME] && *this->Values[ctb_PROJECT_NAME])
  61. ? this->Values[ctb_PROJECT_NAME]
  62. : this->Makefile->GetDefinition("CTEST_PROJECT_NAME");
  63. // Build configuration is determined by: CONFIGURATION argument,
  64. // or CTEST_BUILD_CONFIGURATION script variable, or
  65. // CTEST_CONFIGURATION_TYPE script variable, or ctest -C command
  66. // line argument... in that order.
  67. //
  68. const char* ctestBuildConfiguration
  69. = this->Makefile->GetDefinition("CTEST_BUILD_CONFIGURATION");
  70. const char* cmakeBuildConfiguration
  71. = (this->Values[ctb_CONFIGURATION] && *this->Values[ctb_CONFIGURATION])
  72. ? this->Values[ctb_CONFIGURATION]
  73. : ((ctestBuildConfiguration && *ctestBuildConfiguration)
  74. ? ctestBuildConfiguration
  75. : this->CTest->GetConfigType().c_str());
  76. const char* cmakeBuildAdditionalFlags
  77. = (this->Values[ctb_FLAGS] && *this->Values[ctb_FLAGS])
  78. ? this->Values[ctb_FLAGS]
  79. : this->Makefile->GetDefinition("CTEST_BUILD_FLAGS");
  80. const char* cmakeBuildTarget
  81. = (this->Values[ctb_TARGET] && *this->Values[ctb_TARGET])
  82. ? this->Values[ctb_TARGET]
  83. : this->Makefile->GetDefinition("CTEST_BUILD_TARGET");
  84. if ( cmakeGeneratorName && *cmakeGeneratorName &&
  85. cmakeProjectName && *cmakeProjectName )
  86. {
  87. if ( !cmakeBuildConfiguration )
  88. {
  89. cmakeBuildConfiguration = "Release";
  90. }
  91. if ( this->GlobalGenerator )
  92. {
  93. if ( strcmp(this->GlobalGenerator->GetName(),
  94. cmakeGeneratorName) != 0 )
  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. }
  106. this->GlobalGenerator->FindMakeProgram(this->Makefile);
  107. const char* cmakeMakeProgram
  108. = this->Makefile->GetDefinition("CMAKE_MAKE_PROGRAM");
  109. if(strlen(cmakeBuildConfiguration) == 0)
  110. {
  111. const char* config = 0;
  112. #ifdef CMAKE_INTDIR
  113. config = CMAKE_INTDIR;
  114. #endif
  115. if(!config)
  116. {
  117. config = "Debug";
  118. }
  119. cmakeBuildConfiguration = config;
  120. }
  121. std::string buildCommand
  122. = this->GlobalGenerator->
  123. GenerateBuildCommand(cmakeMakeProgram,
  124. cmakeProjectName, 0,
  125. cmakeBuildAdditionalFlags, cmakeBuildTarget,
  126. cmakeBuildConfiguration, true, false);
  127. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  128. "SetMakeCommand:"
  129. << buildCommand.c_str() << "\n");
  130. this->CTest->SetCTestConfiguration("MakeCommand", buildCommand.c_str());
  131. }
  132. else
  133. {
  134. cmOStringStream ostr;
  135. ostr << "has no project to build. If this is a "
  136. "\"built with CMake\" project, verify that CTEST_CMAKE_GENERATOR "
  137. "and CTEST_PROJECT_NAME are set."
  138. "\n"
  139. "CTEST_PROJECT_NAME is usually set in CTestConfig.cmake. Verify "
  140. "that CTestConfig.cmake exists, or CTEST_PROJECT_NAME "
  141. "is set in the script, or PROJECT_NAME is passed as an argument "
  142. "to ctest_build."
  143. "\n"
  144. "Alternatively, set CTEST_BUILD_COMMAND to build the project "
  145. "with a custom command line.";
  146. this->SetError(ostr.str().c_str());
  147. return 0;
  148. }
  149. }
  150. if(const char* useLaunchers =
  151. this->Makefile->GetDefinition("CTEST_USE_LAUNCHERS"))
  152. {
  153. this->CTest->SetCTestConfiguration("UseLaunchers", useLaunchers);
  154. }
  155. return handler;
  156. }
  157. bool cmCTestBuildCommand::InitialPass(std::vector<std::string> const& args,
  158. cmExecutionStatus &status)
  159. {
  160. bool ret = cmCTestHandlerCommand::InitialPass(args, status);
  161. if ( this->Values[ctb_NUMBER_ERRORS] && *this->Values[ctb_NUMBER_ERRORS])
  162. {
  163. cmOStringStream str;
  164. str << this->Handler->GetTotalErrors();
  165. this->Makefile->AddDefinition(
  166. this->Values[ctb_NUMBER_ERRORS], str.str().c_str());
  167. }
  168. if ( this->Values[ctb_NUMBER_WARNINGS]
  169. && *this->Values[ctb_NUMBER_WARNINGS])
  170. {
  171. cmOStringStream str;
  172. str << this->Handler->GetTotalWarnings();
  173. this->Makefile->AddDefinition(
  174. this->Values[ctb_NUMBER_WARNINGS], str.str().c_str());
  175. }
  176. return ret;
  177. }