cmExecProgramCommand.cxx 3.5 KB

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