cmCTestBuildCommand.cxx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCTestBuildCommand.h"
  14. #include "cmCTest.h"
  15. #include "cmCTestGenericHandler.h"
  16. #include "cmake.h"
  17. #include "cmGlobalGenerator.h"
  18. //----------------------------------------------------------------------------
  19. cmCTestBuildCommand::cmCTestBuildCommand()
  20. {
  21. this->GlobalGenerator = 0;
  22. }
  23. //----------------------------------------------------------------------------
  24. cmCTestBuildCommand::~cmCTestBuildCommand()
  25. {
  26. if ( this->GlobalGenerator )
  27. {
  28. delete this->GlobalGenerator;
  29. this->GlobalGenerator = 0;
  30. }
  31. }
  32. //----------------------------------------------------------------------------
  33. bool cmCTestBuildCommand::InitialPass(
  34. std::vector<std::string> const& args)
  35. {
  36. const char* build_dir = 0;
  37. const char* res_var = 0;
  38. bool havereturn_variable = false;
  39. bool havesource = false;
  40. for(size_t i=0; i < args.size(); ++i)
  41. {
  42. if ( havereturn_variable )
  43. {
  44. res_var = args[i].c_str();
  45. havereturn_variable = false;
  46. }
  47. else if ( havesource )
  48. {
  49. build_dir = args[i].c_str();
  50. havesource = false;
  51. }
  52. else if(args[i] == "RETURN_VALUE")
  53. {
  54. if ( res_var )
  55. {
  56. this->SetError("called with incorrect number of arguments. "
  57. "RETURN_VALUE specified twice.");
  58. return false;
  59. }
  60. havereturn_variable = true;
  61. }
  62. else if(args[i] == "BUILD")
  63. {
  64. if ( build_dir )
  65. {
  66. this->SetError("called with incorrect number of arguments. "
  67. "BUILD specified twice.");
  68. return false;
  69. }
  70. havesource = true;
  71. }
  72. else
  73. {
  74. cmOStringStream str;
  75. str << "called with incorrect number of arguments. Extra argument is: "
  76. << args[i].c_str() << ".";
  77. this->SetError(str.str().c_str());
  78. return false;
  79. }
  80. }
  81. if ( build_dir )
  82. {
  83. this->CTest->SetCTestConfiguration("BuildDirectory", build_dir);
  84. }
  85. cmCTestGenericHandler* handler
  86. = this->CTest->GetInitializedHandler("build");
  87. if ( !handler )
  88. {
  89. this->SetError("internal CTest error. Cannot instantiate build handler");
  90. return false;
  91. }
  92. const char* ctestBuildCommand
  93. = this->Makefile->GetDefinition("CTEST_BUILD_COMMAND");
  94. if ( ctestBuildCommand && *ctestBuildCommand )
  95. {
  96. this->CTest->SetCTestConfiguration("MakeCommand", ctestBuildCommand);
  97. }
  98. else
  99. {
  100. const char* cmakeGeneratorName
  101. = this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR");
  102. const char* cmakeProjectName
  103. = this->Makefile->GetDefinition("CTEST_PROJECT_NAME");
  104. const char* cmakeBuildConfiguration
  105. = this->Makefile->GetDefinition("CTEST_BUILD_CONFIGURATION");
  106. const char* cmakeBuildAdditionalFlags
  107. = this->Makefile->GetDefinition("CTEST_BUILD_FLAGS");
  108. if ( cmakeGeneratorName && *cmakeGeneratorName &&
  109. cmakeProjectName && *cmakeProjectName )
  110. {
  111. if ( !cmakeBuildConfiguration )
  112. {
  113. cmakeBuildConfiguration = "Release";
  114. }
  115. if ( this->GlobalGenerator )
  116. {
  117. if ( strcmp(this->GlobalGenerator->GetName(),
  118. cmakeGeneratorName) != 0 )
  119. {
  120. delete this->GlobalGenerator;
  121. this->GlobalGenerator = 0;
  122. }
  123. }
  124. if ( !this->GlobalGenerator )
  125. {
  126. this->GlobalGenerator =
  127. this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
  128. cmakeGeneratorName);
  129. }
  130. this->GlobalGenerator->FindMakeProgram(this->Makefile);
  131. const char* cmakeMakeProgram
  132. = this->Makefile->GetDefinition("CMAKE_MAKE_PROGRAM");
  133. std::string buildCommand
  134. = this->GlobalGenerator->GenerateBuildCommand(cmakeMakeProgram,
  135. cmakeProjectName,
  136. cmakeBuildAdditionalFlags, 0, cmakeBuildConfiguration, true);
  137. this->CTest->SetCTestConfiguration("MakeCommand", buildCommand.c_str());
  138. }
  139. else
  140. {
  141. cmOStringStream ostr;
  142. ostr << "CTEST_BUILD_COMMAND or CTEST_CMAKE_GENERATOR not specified. "
  143. "Please specify the CTEST_CMAKE_GENERATOR and CTEST_PROJECT_NAME if "
  144. "this is a CMake project, or specify the CTEST_BUILD_COMMAND for "
  145. "cmake or any other project.";
  146. this->SetError(ostr.str().c_str());
  147. return false;
  148. }
  149. }
  150. int res = handler->ProcessHandler();
  151. if ( res_var )
  152. {
  153. cmOStringStream str;
  154. str << res;
  155. this->Makefile->AddDefinition(res_var, str.str().c_str());
  156. }
  157. return true;
  158. }