Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmExecProgramCommand.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cmExecProgramCommand.h"
13 #include "cmSystemTools.h"
14
15 // cmExecProgramCommand
16 bool cmExecProgramCommand
17 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
18 {
19   if(args.size() < 1 )
20     {
21     this->SetError("called with incorrect number of arguments");
22     return false;
23     }
24   std::string arguments;
25   bool doingargs = false;
26   int count = 0;
27   std::string output_variable;
28   bool haveoutput_variable = false;
29   std::string return_variable;
30   bool havereturn_variable = false;
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       haveoutput_variable = false;
49       count ++;
50       }
51     else if(args[i] == "RETURN_VALUE")
52       {
53       count++;
54       doingargs = false;
55       haveoutput_variable = false;
56       havereturn_variable = true;
57       }
58     else if ( havereturn_variable )
59       {
60       if ( return_variable.size() > 0 )
61         {
62         this->SetError("called with incorrect number of arguments");
63         return false;
64         }
65       return_variable = args[i];
66       havereturn_variable = false;
67       count ++;
68       }
69     else if(args[i] == "ARGS")
70       {
71       count++;
72       havereturn_variable = false;
73       haveoutput_variable = false;
74       doingargs = true;
75       }
76     else if(doingargs)
77       {
78       arguments += args[i];
79       arguments += " ";
80       count++;
81       }
82     }
83
84   std::string command;
85   if(arguments.size())
86     {
87     command = cmSystemTools::ConvertToRunCommandPath(args[0].c_str());
88     command += " ";
89     command += arguments;
90     }
91   else
92     {
93     command = args[0];
94     }
95   bool verbose = true;
96   if(output_variable.size() > 0)
97     {
98     verbose = false;
99     }
100   int retVal = 0;
101   std::string output;
102   bool result = true;
103   if(args.size() - count == 2)
104     {
105     cmSystemTools::MakeDirectory(args[1].c_str());
106     result = cmSystemTools::RunCommand(command.c_str(), output, retVal,
107                                        args[1].c_str(), verbose);
108     }
109   else
110     {
111     result = cmSystemTools::RunCommand(command.c_str(), output,
112                                        retVal, 0, verbose);
113     }
114   if(!result)
115     {
116     retVal = -1;
117     }
118
119   if ( output_variable.size() > 0 )
120     {
121     std::string::size_type first = output.find_first_not_of(" \n\t\r");
122     std::string::size_type last = output.find_last_not_of(" \n\t\r");
123     if(first == std::string::npos)
124       {
125       first = 0;
126       }
127     if(last == std::string::npos)
128       {
129       last = output.size()-1;
130       }
131
132     std::string coutput = std::string(output, first, last-first+1);
133     this->Makefile->AddDefinition(output_variable.c_str(), coutput.c_str());
134     }
135
136   if ( return_variable.size() > 0 )
137     {
138     char buffer[100];
139     sprintf(buffer, "%d", retVal);
140     this->Makefile->AddDefinition(return_variable.c_str(), buffer);
141     }
142
143   return true;
144 }
145