cmExecProgramCommand.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. 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. count ++;
  48. }
  49. else if(args[i] == "RETURN_VALUE")
  50. {
  51. count++;
  52. doingargs = false;
  53. haveoutput_variable = false;
  54. havereturn_variable = true;
  55. }
  56. else if ( havereturn_variable )
  57. {
  58. if ( return_variable.size() > 0 )
  59. {
  60. this->SetError("called with incorrect number of arguments");
  61. return false;
  62. }
  63. return_variable = args[i];
  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::ConvertToOutputPath(args[0].c_str());
  84. command += " ";
  85. command += arguments;
  86. }
  87. else
  88. {
  89. command = args[0];
  90. }
  91. int retVal = 0;
  92. std::string output;
  93. if(args.size() - count == 2)
  94. {
  95. cmSystemTools::MakeDirectory(args[1].c_str());
  96. cmSystemTools::RunCommand(command.c_str(), output, retVal,
  97. cmSystemTools::ConvertToOutputPath(args[1].c_str()).c_str());
  98. }
  99. else
  100. {
  101. cmSystemTools::RunCommand(command.c_str(), output, retVal);
  102. }
  103. if ( output_variable.size() > 0 )
  104. {
  105. std::string::size_type first = output.find_first_not_of(" \n\t\r");
  106. std::string::size_type last = output.find_last_not_of(" \n\t\r");
  107. std::string coutput = std::string(output, first, last);
  108. m_Makefile->AddDefinition(output_variable.c_str(), coutput.c_str());
  109. }
  110. if ( return_variable.size() > 0 )
  111. {
  112. char buffer[100];
  113. sprintf(buffer, "%d", retVal);
  114. m_Makefile->AddDefinition(return_variable.c_str(), buffer);
  115. }
  116. return true;
  117. }