cmExecProgramCommand.cxx 3.7 KB

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