Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmExecProgramCommand.cxx
blob7f919892219670b22dbf4bd986653265a1c1c14d
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmExecProgramCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008/01/23 15:27:59 $
7 Version: $Revision: 1.23 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmExecProgramCommand.h"
18 #include "cmSystemTools.h"
20 // cmExecProgramCommand
21 bool cmExecProgramCommand
22 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
24 if(args.size() < 1 )
26 this->SetError("called with incorrect number of arguments");
27 return false;
29 std::string arguments;
30 bool doingargs = false;
31 int count = 0;
32 std::string output_variable;
33 bool haveoutput_variable = false;
34 std::string return_variable;
35 bool havereturn_variable = false;
36 for(size_t i=0; i < args.size(); ++i)
38 if(args[i] == "OUTPUT_VARIABLE")
40 count++;
41 doingargs = false;
42 havereturn_variable = false;
43 haveoutput_variable = true;
45 else if ( haveoutput_variable )
47 if ( output_variable.size() > 0 )
49 this->SetError("called with incorrect number of arguments");
50 return false;
52 output_variable = args[i];
53 haveoutput_variable = false;
54 count ++;
56 else if(args[i] == "RETURN_VALUE")
58 count++;
59 doingargs = false;
60 haveoutput_variable = false;
61 havereturn_variable = true;
63 else if ( havereturn_variable )
65 if ( return_variable.size() > 0 )
67 this->SetError("called with incorrect number of arguments");
68 return false;
70 return_variable = args[i];
71 havereturn_variable = false;
72 count ++;
74 else if(args[i] == "ARGS")
76 count++;
77 havereturn_variable = false;
78 haveoutput_variable = false;
79 doingargs = true;
81 else if(doingargs)
83 arguments += args[i];
84 arguments += " ";
85 count++;
89 std::string command;
90 if(arguments.size())
92 command = cmSystemTools::ConvertToRunCommandPath(args[0].c_str());
93 command += " ";
94 command += arguments;
96 else
98 command = args[0];
100 bool verbose = true;
101 if(output_variable.size() > 0)
103 verbose = false;
105 int retVal = 0;
106 std::string output;
107 bool result = true;
108 if(args.size() - count == 2)
110 cmSystemTools::MakeDirectory(args[1].c_str());
111 result = cmSystemTools::RunCommand(command.c_str(), output, retVal,
112 args[1].c_str(), verbose);
114 else
116 result = cmSystemTools::RunCommand(command.c_str(), output,
117 retVal, 0, verbose);
119 if(!result)
121 retVal = -1;
124 if ( output_variable.size() > 0 )
126 std::string::size_type first = output.find_first_not_of(" \n\t\r");
127 std::string::size_type last = output.find_last_not_of(" \n\t\r");
128 if(first == std::string::npos)
130 first = 0;
132 if(last == std::string::npos)
134 last = output.size()-1;
137 std::string coutput = std::string(output, first, last-first+1);
138 this->Makefile->AddDefinition(output_variable.c_str(), coutput.c_str());
141 if ( return_variable.size() > 0 )
143 char buffer[100];
144 sprintf(buffer, "%d", retVal);
145 this->Makefile->AddDefinition(return_variable.c_str(), buffer);
148 return true;