cmExecProgramCommand.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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. std::string e_command;
  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. 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. count ++;
  66. }
  67. else if(args[i] == "ARGS")
  68. {
  69. count++;
  70. havereturn_variable = false;
  71. haveoutput_variable = false;
  72. doingargs = true;
  73. }
  74. else if(doingargs)
  75. {
  76. arguments += args[i];
  77. arguments += " ";
  78. count++;
  79. }
  80. }
  81. std::string command;
  82. if(arguments.size())
  83. {
  84. command = cmSystemTools::ConvertToOutputPath(args[0].c_str());
  85. command += " ";
  86. command += arguments;
  87. }
  88. else
  89. {
  90. command = args[0];
  91. }
  92. int retVal = 0;
  93. std::string output;
  94. if(args.size() - count == 2)
  95. {
  96. cmSystemTools::MakeDirectory(args[1].c_str());
  97. cmSystemTools::RunCommand(command.c_str(), output, retVal,
  98. cmSystemTools::ConvertToOutputPath(args[1].c_str()).c_str());
  99. }
  100. else
  101. {
  102. cmSystemTools::RunCommand(command.c_str(), output, retVal);
  103. }
  104. if ( output_variable.size() > 0 )
  105. {
  106. std::string::size_type first = output.find_first_not_of(" \n\t\r");
  107. std::string::size_type last = output.find_last_not_of(" \n\t\r");
  108. std::string coutput = std::string(output, first, last);
  109. m_Makefile->AddDefinition(output_variable.c_str(), coutput.c_str());
  110. }
  111. if ( return_variable.size() > 0 )
  112. {
  113. char buffer[100];
  114. sprintf(buffer, "%d", retVal);
  115. m_Makefile->AddDefinition(return_variable.c_str(), buffer);
  116. }
  117. return true;
  118. }