cmExecProgramCommand.cxx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 variable;
  27. bool havevariable = false;
  28. std::string e_command;
  29. for(size_t i=0; i < args.size(); ++i)
  30. {
  31. if(args[i] == "OUTPUT_VARIABLE")
  32. {
  33. count++;
  34. doingargs = false;
  35. havevariable = true;
  36. }
  37. else if ( havevariable )
  38. {
  39. if ( variable.size() > 0 )
  40. {
  41. this->SetError("called with incorrect number of arguments");
  42. return false;
  43. }
  44. variable = args[i];
  45. count ++;
  46. }
  47. else if(doingargs)
  48. {
  49. arguments += args[i];
  50. arguments += " ";
  51. count++;
  52. }
  53. else if(args[i] == "ARGS")
  54. {
  55. count++;
  56. doingargs = true;
  57. }
  58. }
  59. std::string command;
  60. if(arguments.size())
  61. {
  62. command = cmSystemTools::ConvertToOutputPath(args[0].c_str());
  63. command += " ";
  64. command += arguments;
  65. }
  66. else
  67. {
  68. command = args[0];
  69. }
  70. std::string output;
  71. if(args.size() - count == 2)
  72. {
  73. cmSystemTools::MakeDirectory(args[1].c_str());
  74. cmSystemTools::RunCommand(command.c_str(), output,
  75. cmSystemTools::ConvertToOutputPath(args[1].c_str()).c_str());
  76. }
  77. else
  78. {
  79. cmSystemTools::RunCommand(command.c_str(), output);
  80. }
  81. if ( variable.size() > 0 )
  82. {
  83. std::string::size_type first = output.find_first_not_of(" \n\t\r");
  84. std::string::size_type last = output.find_last_not_of(" \n\t\r");
  85. std::string coutput = std::string(output, first, last);
  86. m_Makefile->AddDefinition(variable.c_str(), coutput.c_str());
  87. }
  88. return true;
  89. }