cmExecProgramCommand.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "cmExecProgramCommand.h"
  14. #include "cmSystemTools.h"
  15. // cmExecProgramCommand
  16. bool cmExecProgramCommand
  17. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  18. {
  19. if(args.size() < 1 )
  20. {
  21. this->SetError("called with incorrect number of arguments");
  22. return false;
  23. }
  24. std::string arguments;
  25. bool doingargs = false;
  26. int count = 0;
  27. std::string output_variable;
  28. bool haveoutput_variable = false;
  29. std::string return_variable;
  30. bool havereturn_variable = false;
  31. for(size_t i=0; i < args.size(); ++i)
  32. {
  33. if(args[i] == "OUTPUT_VARIABLE")
  34. {
  35. count++;
  36. doingargs = false;
  37. havereturn_variable = false;
  38. haveoutput_variable = true;
  39. }
  40. else if ( haveoutput_variable )
  41. {
  42. if ( output_variable.size() > 0 )
  43. {
  44. this->SetError("called with incorrect number of arguments");
  45. return false;
  46. }
  47. output_variable = args[i];
  48. haveoutput_variable = false;
  49. count ++;
  50. }
  51. else if(args[i] == "RETURN_VALUE")
  52. {
  53. count++;
  54. doingargs = false;
  55. haveoutput_variable = false;
  56. havereturn_variable = true;
  57. }
  58. else if ( havereturn_variable )
  59. {
  60. if ( return_variable.size() > 0 )
  61. {
  62. this->SetError("called with incorrect number of arguments");
  63. return false;
  64. }
  65. return_variable = args[i];
  66. havereturn_variable = false;
  67. count ++;
  68. }
  69. else if(args[i] == "ARGS")
  70. {
  71. count++;
  72. havereturn_variable = false;
  73. haveoutput_variable = false;
  74. doingargs = true;
  75. }
  76. else if(doingargs)
  77. {
  78. arguments += args[i];
  79. arguments += " ";
  80. count++;
  81. }
  82. }
  83. std::string command;
  84. if(arguments.size())
  85. {
  86. command = cmSystemTools::ConvertToRunCommandPath(args[0].c_str());
  87. command += " ";
  88. command += arguments;
  89. }
  90. else
  91. {
  92. command = args[0];
  93. }
  94. bool verbose = true;
  95. if(output_variable.size() > 0)
  96. {
  97. verbose = false;
  98. }
  99. int retVal = 0;
  100. std::string output;
  101. bool result = true;
  102. if(args.size() - count == 2)
  103. {
  104. cmSystemTools::MakeDirectory(args[1].c_str());
  105. result = cmSystemTools::RunCommand(command.c_str(), output, retVal,
  106. args[1].c_str(), verbose);
  107. }
  108. else
  109. {
  110. result = cmSystemTools::RunCommand(command.c_str(), output,
  111. retVal, 0, verbose);
  112. }
  113. if(!result)
  114. {
  115. retVal = -1;
  116. }
  117. if ( output_variable.size() > 0 )
  118. {
  119. std::string::size_type first = output.find_first_not_of(" \n\t\r");
  120. std::string::size_type last = output.find_last_not_of(" \n\t\r");
  121. if(first == std::string::npos)
  122. {
  123. first = 0;
  124. }
  125. if(last == std::string::npos)
  126. {
  127. last = output.size()-1;
  128. }
  129. std::string coutput = std::string(output, first, last-first+1);
  130. this->Makefile->AddDefinition(output_variable.c_str(), coutput.c_str());
  131. }
  132. if ( return_variable.size() > 0 )
  133. {
  134. char buffer[100];
  135. sprintf(buffer, "%d", retVal);
  136. this->Makefile->AddDefinition(return_variable.c_str(), buffer);
  137. }
  138. return true;
  139. }