1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmExecProgramCommand.h"
7 #include "cmsys/Process.h"
9 #include "cmExecutionStatus.h"
10 #include "cmMakefile.h"
11 #include "cmProcessOutput.h"
12 #include "cmStringAlgorithms.h"
13 #include "cmSystemTools.h"
15 using Encoding
= cmProcessOutput::Encoding
;
18 bool RunCommand(std::string command
, std::string
& output
, int& retVal
,
19 const char* directory
= nullptr, bool verbose
= true,
20 Encoding encoding
= cmProcessOutput::Auto
);
23 // cmExecProgramCommand
24 bool cmExecProgramCommand(std::vector
<std::string
> const& args
,
25 cmExecutionStatus
& status
)
28 status
.SetError("called with incorrect number of arguments");
31 std::string arguments
;
32 bool doingargs
= false;
34 std::string output_variable
;
35 bool haveoutput_variable
= false;
36 std::string return_variable
;
37 bool havereturn_variable
= false;
38 for (std::string
const& arg
: args
) {
39 if (arg
== "OUTPUT_VARIABLE") {
42 havereturn_variable
= false;
43 haveoutput_variable
= true;
44 } else if (haveoutput_variable
) {
45 if (!output_variable
.empty()) {
46 status
.SetError("called with incorrect number of arguments");
49 output_variable
= arg
;
50 haveoutput_variable
= false;
52 } else if (arg
== "RETURN_VALUE") {
55 haveoutput_variable
= false;
56 havereturn_variable
= true;
57 } else if (havereturn_variable
) {
58 if (!return_variable
.empty()) {
59 status
.SetError("called with incorrect number of arguments");
62 return_variable
= arg
;
63 havereturn_variable
= false;
65 } else if (arg
== "ARGS") {
67 havereturn_variable
= false;
68 haveoutput_variable
= false;
70 } else if (doingargs
) {
78 if (!arguments
.empty()) {
79 command
= cmStrCat(cmSystemTools::ConvertToRunCommandPath(args
[0]), ' ',
85 if (!output_variable
.empty()) {
91 if (args
.size() - count
== 2) {
92 cmSystemTools::MakeDirectory(args
[1]);
93 result
= RunCommand(command
, output
, retVal
, args
[1].c_str(), verbose
);
95 result
= RunCommand(command
, output
, retVal
, nullptr, verbose
);
101 if (!output_variable
.empty()) {
102 std::string::size_type first
= output
.find_first_not_of(" \n\t\r");
103 std::string::size_type last
= output
.find_last_not_of(" \n\t\r");
104 if (first
== std::string::npos
) {
107 if (last
== std::string::npos
) {
108 last
= output
.size() - 1;
111 std::string coutput
= std::string(output
, first
, last
- first
+ 1);
112 status
.GetMakefile().AddDefinition(output_variable
, coutput
);
115 if (!return_variable
.empty()) {
117 snprintf(buffer
, sizeof(buffer
), "%d", retVal
);
118 status
.GetMakefile().AddDefinition(return_variable
, buffer
);
125 bool RunCommand(std::string command
, std::string
& output
, int& retVal
,
126 const char* dir
, bool verbose
, Encoding encoding
)
128 if (cmSystemTools::GetRunCommandOutput()) {
132 #if defined(_WIN32) && !defined(__CYGWIN__)
133 // if the command does not start with a quote, then
134 // try to find the program, and if the program can not be
135 // found use system to run the command as it must be a built in
136 // shell command like echo or dir
137 if (!command
.empty() && command
[0] == '\"') {
138 // count the number of quotes
140 for (char c
: command
) {
148 // if there are more than two double quotes use
149 // GetShortPathName, the cmd.exe program in windows which
150 // is used by system fails to execute if there are more than
151 // one set of quotes in the arguments
153 cmsys::RegularExpression
quoted("^\"([^\"]*)\"[ \t](.*)");
154 if (quoted
.find(command
)) {
155 std::string shortCmd
;
156 std::string cmd
= quoted
.match(1);
157 std::string args
= quoted
.match(2);
158 if (!cmSystemTools::FileExists(cmd
)) {
160 } else if (!cmSystemTools::GetShortPath(cmd
, shortCmd
)) {
161 cmSystemTools::Error("GetShortPath failed for " + cmd
);
169 cmSystemTools::Error("Could not parse command line with quotes " +
176 // Allocate a process instance.
177 cmsysProcess
* cp
= cmsysProcess_New();
179 cmSystemTools::Error("Error allocating process instance.");
183 #if defined(_WIN32) && !defined(__CYGWIN__)
185 cmsysProcess_SetWorkingDirectory(cp
, dir
);
187 if (cmSystemTools::GetRunCommandHideConsole()) {
188 cmsysProcess_SetOption(cp
, cmsysProcess_Option_HideWindow
, 1);
190 cmsysProcess_SetOption(cp
, cmsysProcess_Option_Verbatim
, 1);
191 const char* cmd
[] = { command
.c_str(), nullptr };
192 cmsysProcess_SetCommand(cp
, cmd
);
194 std::string commandInDir
;
196 commandInDir
= cmStrCat("cd \"", dir
, "\" && ", command
);
198 commandInDir
= command
;
201 commandInDir
+= " 2>&1";
203 command
= commandInDir
;
205 cmSystemTools::Stdout("running ");
206 cmSystemTools::Stdout(command
);
207 cmSystemTools::Stdout("\n");
211 const char* cmd
[] = { "/bin/sh", "-c", command
.c_str(), nullptr };
212 cmsysProcess_SetCommand(cp
, cmd
);
215 cmsysProcess_Execute(cp
);
217 // Read the process output.
221 cmProcessOutput
processOutput(encoding
);
223 while ((p
= cmsysProcess_WaitForData(cp
, &data
, &length
, nullptr))) {
224 if (p
== cmsysProcess_Pipe_STDOUT
|| p
== cmsysProcess_Pipe_STDERR
) {
226 processOutput
.DecodeText(data
, length
, strdata
);
227 cmSystemTools::Stdout(strdata
);
229 output
.append(data
, length
);
234 processOutput
.DecodeText(std::string(), strdata
);
235 if (!strdata
.empty()) {
236 cmSystemTools::Stdout(strdata
);
240 // All output has been read. Wait for the process to exit.
241 cmsysProcess_WaitForExit(cp
, nullptr);
242 processOutput
.DecodeText(output
, output
);
244 // Check the result of running the process.
246 switch (cmsysProcess_GetState(cp
)) {
247 case cmsysProcess_State_Exited
:
248 retVal
= cmsysProcess_GetExitValue(cp
);
250 case cmsysProcess_State_Exception
:
252 msg
+= "\nProcess terminated due to: ";
253 msg
+= cmsysProcess_GetExceptionString(cp
);
255 case cmsysProcess_State_Error
:
257 msg
+= "\nProcess failed because: ";
258 msg
+= cmsysProcess_GetErrorString(cp
);
260 case cmsysProcess_State_Expired
:
262 msg
+= "\nProcess terminated due to timeout.";
266 #if defined(_WIN32) && !defined(__CYGWIN__)
267 // Old Windows process execution printed this info.
268 msg
+= "\n\nfor command: ";
276 cmSystemTools::Stdout(msg
);
280 // Old UNIX process execution only put message in output.
285 // Delete the process instance.
286 cmsysProcess_Delete(cp
);