123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
- file Copyright.txt or https://cmake.org/licensing for details. */
- #include "cmExecuteProcessCommand.h"
- #include "cm_static_string_view.hxx"
- #include "cmsys/Process.h"
- #include <algorithm>
- #include <ctype.h> /* isspace */
- #include <iostream>
- #include <memory>
- #include <stdio.h>
- #include <vector>
- #include "cmAlgorithms.h"
- #include "cmArgumentParser.h"
- #include "cmMakefile.h"
- #include "cmMessageType.h"
- #include "cmProcessOutput.h"
- #include "cmSystemTools.h"
- class cmExecutionStatus;
- static bool cmExecuteProcessCommandIsWhitespace(char c)
- {
- return (isspace(static_cast<int>(c)) || c == '\n' || c == '\r');
- }
- void cmExecuteProcessCommandFixText(std::vector<char>& output,
- bool strip_trailing_whitespace);
- void cmExecuteProcessCommandAppend(std::vector<char>& output, const char* data,
- int length);
- // cmExecuteProcessCommand
- bool cmExecuteProcessCommand::InitialPass(std::vector<std::string> const& args,
- cmExecutionStatus&)
- {
- if (args.empty()) {
- this->SetError("called with incorrect number of arguments");
- return false;
- }
- struct Arguments
- {
- std::vector<std::vector<std::string>> Commands;
- std::string OutputVariable;
- std::string ErrorVariable;
- std::string ResultVariable;
- std::string ResultsVariable;
- std::string WorkingDirectory;
- std::string InputFile;
- std::string OutputFile;
- std::string ErrorFile;
- std::string Timeout;
- std::string CommandEcho;
- bool OutputQuiet = false;
- bool ErrorQuiet = false;
- bool OutputStripTrailingWhitespace = false;
- bool ErrorStripTrailingWhitespace = false;
- std::string Encoding;
- };
- static auto const parser =
- cmArgumentParser<Arguments>{}
- .Bind("COMMAND"_s, &Arguments::Commands)
- .Bind("COMMAND_ECHO"_s, &Arguments::CommandEcho)
- .Bind("OUTPUT_VARIABLE"_s, &Arguments::OutputVariable)
- .Bind("ERROR_VARIABLE"_s, &Arguments::ErrorVariable)
- .Bind("RESULT_VARIABLE"_s, &Arguments::ResultVariable)
- .Bind("RESULTS_VARIABLE"_s, &Arguments::ResultsVariable)
- .Bind("WORKING_DIRECTORY"_s, &Arguments::WorkingDirectory)
- .Bind("INPUT_FILE"_s, &Arguments::InputFile)
- .Bind("OUTPUT_FILE"_s, &Arguments::OutputFile)
- .Bind("ERROR_FILE"_s, &Arguments::ErrorFile)
- .Bind("TIMEOUT"_s, &Arguments::Timeout)
- .Bind("OUTPUT_QUIET"_s, &Arguments::OutputQuiet)
- .Bind("ERROR_QUIET"_s, &Arguments::ErrorQuiet)
- .Bind("OUTPUT_STRIP_TRAILING_WHITESPACE"_s,
- &Arguments::OutputStripTrailingWhitespace)
- .Bind("ERROR_STRIP_TRAILING_WHITESPACE"_s,
- &Arguments::ErrorStripTrailingWhitespace)
- .Bind("ENCODING"_s, &Arguments::Encoding);
- std::vector<std::string> unparsedArguments;
- std::vector<std::string> keywordsMissingValue;
- Arguments const arguments =
- parser.Parse(args, &unparsedArguments, &keywordsMissingValue);
- if (!keywordsMissingValue.empty()) {
- this->SetError(" called with no value for " +
- keywordsMissingValue.front() + ".");
- return false;
- }
- if (!unparsedArguments.empty()) {
- this->SetError(" given unknown argument \"" + unparsedArguments.front() +
- "\".");
- return false;
- }
- if (!this->Makefile->CanIWriteThisFile(arguments.OutputFile)) {
- this->SetError("attempted to output into a file: " + arguments.OutputFile +
- " into a source directory.");
- cmSystemTools::SetFatalErrorOccured();
- return false;
- }
- // Check for commands given.
- if (arguments.Commands.empty()) {
- this->SetError(" called with no COMMAND argument.");
- return false;
- }
- for (std::vector<std::string> const& cmd : arguments.Commands) {
- if (cmd.empty()) {
- this->SetError(" given COMMAND argument with no value.");
- return false;
- }
- }
- // Parse the timeout string.
- double timeout = -1;
- if (!arguments.Timeout.empty()) {
- if (sscanf(arguments.Timeout.c_str(), "%lg", &timeout) != 1) {
- this->SetError(" called with TIMEOUT value that could not be parsed.");
- return false;
- }
- }
- // Create a process instance.
- std::unique_ptr<cmsysProcess, void (*)(cmsysProcess*)> cp_ptr(
- cmsysProcess_New(), cmsysProcess_Delete);
- cmsysProcess* cp = cp_ptr.get();
- // Set the command sequence.
- for (std::vector<std::string> const& cmd : arguments.Commands) {
- std::vector<const char*> argv(cmd.size() + 1);
- std::transform(cmd.begin(), cmd.end(), argv.begin(),
- [](std::string const& s) { return s.c_str(); });
- argv.back() = nullptr;
- cmsysProcess_AddCommand(cp, argv.data());
- }
- // Set the process working directory.
- if (!arguments.WorkingDirectory.empty()) {
- cmsysProcess_SetWorkingDirectory(cp, arguments.WorkingDirectory.c_str());
- }
- // Always hide the process window.
- cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
- // Check the output variables.
- bool merge_output = false;
- if (!arguments.InputFile.empty()) {
- cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDIN,
- arguments.InputFile.c_str());
- }
- if (!arguments.OutputFile.empty()) {
- cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDOUT,
- arguments.OutputFile.c_str());
- }
- if (!arguments.ErrorFile.empty()) {
- if (arguments.ErrorFile == arguments.OutputFile) {
- merge_output = true;
- } else {
- cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDERR,
- arguments.ErrorFile.c_str());
- }
- }
- if (!arguments.OutputVariable.empty() &&
- arguments.OutputVariable == arguments.ErrorVariable) {
- merge_output = true;
- }
- if (merge_output) {
- cmsysProcess_SetOption(cp, cmsysProcess_Option_MergeOutput, 1);
- }
- // Set the timeout if any.
- if (timeout >= 0) {
- cmsysProcess_SetTimeout(cp, timeout);
- }
- bool echo_stdout = false;
- bool echo_stderr = false;
- bool echo_output_from_variable = true;
- std::string echo_output =
- this->Makefile->GetSafeDefinition("CMAKE_EXECUTE_PROCESS_COMMAND_ECHO");
- if (!arguments.CommandEcho.empty()) {
- echo_output_from_variable = false;
- echo_output = arguments.CommandEcho;
- }
- if (!echo_output.empty()) {
- if (echo_output == "STDERR") {
- echo_stderr = true;
- } else if (echo_output == "STDOUT") {
- echo_stdout = true;
- } else if (echo_output != "NONE") {
- std::string error;
- if (echo_output_from_variable) {
- error = "CMAKE_EXECUTE_PROCESS_COMMAND_ECHO set to '";
- } else {
- error = " called with '";
- }
- error += echo_output;
- error += "' expected STDERR|STDOUT|NONE";
- if (!echo_output_from_variable) {
- error += " for COMMAND_ECHO.";
- }
- this->Makefile->IssueMessage(MessageType::FATAL_ERROR, error);
- return true;
- }
- }
- if (echo_stdout || echo_stderr) {
- std::string command;
- for (auto& cmd : arguments.Commands) {
- command += "'";
- command += cmJoin(cmd, "' '");
- command += "'";
- command += "\n";
- }
- if (echo_stdout) {
- std::cout << command;
- } else if (echo_stderr) {
- std::cerr << command;
- }
- }
- // Start the process.
- cmsysProcess_Execute(cp);
- // Read the process output.
- std::vector<char> tempOutput;
- std::vector<char> tempError;
- int length;
- char* data;
- int p;
- cmProcessOutput processOutput(
- cmProcessOutput::FindEncoding(arguments.Encoding));
- std::string strdata;
- while ((p = cmsysProcess_WaitForData(cp, &data, &length, nullptr))) {
- // Put the output in the right place.
- if (p == cmsysProcess_Pipe_STDOUT && !arguments.OutputQuiet) {
- if (arguments.OutputVariable.empty()) {
- processOutput.DecodeText(data, length, strdata, 1);
- cmSystemTools::Stdout(strdata);
- } else {
- cmExecuteProcessCommandAppend(tempOutput, data, length);
- }
- } else if (p == cmsysProcess_Pipe_STDERR && !arguments.ErrorQuiet) {
- if (arguments.ErrorVariable.empty()) {
- processOutput.DecodeText(data, length, strdata, 2);
- cmSystemTools::Stderr(strdata);
- } else {
- cmExecuteProcessCommandAppend(tempError, data, length);
- }
- }
- }
- if (!arguments.OutputQuiet && arguments.OutputVariable.empty()) {
- processOutput.DecodeText(std::string(), strdata, 1);
- if (!strdata.empty()) {
- cmSystemTools::Stdout(strdata);
- }
- }
- if (!arguments.ErrorQuiet && arguments.ErrorVariable.empty()) {
- processOutput.DecodeText(std::string(), strdata, 2);
- if (!strdata.empty()) {
- cmSystemTools::Stderr(strdata);
- }
- }
- // All output has been read. Wait for the process to exit.
- cmsysProcess_WaitForExit(cp, nullptr);
- processOutput.DecodeText(tempOutput, tempOutput);
- processOutput.DecodeText(tempError, tempError);
- // Fix the text in the output strings.
- cmExecuteProcessCommandFixText(tempOutput,
- arguments.OutputStripTrailingWhitespace);
- cmExecuteProcessCommandFixText(tempError,
- arguments.ErrorStripTrailingWhitespace);
- // Store the output obtained.
- if (!arguments.OutputVariable.empty() && !tempOutput.empty()) {
- this->Makefile->AddDefinition(arguments.OutputVariable, tempOutput.data());
- }
- if (!merge_output && !arguments.ErrorVariable.empty() &&
- !tempError.empty()) {
- this->Makefile->AddDefinition(arguments.ErrorVariable, tempError.data());
- }
- // Store the result of running the process.
- if (!arguments.ResultVariable.empty()) {
- switch (cmsysProcess_GetState(cp)) {
- case cmsysProcess_State_Exited: {
- int v = cmsysProcess_GetExitValue(cp);
- char buf[16];
- sprintf(buf, "%d", v);
- this->Makefile->AddDefinition(arguments.ResultVariable, buf);
- } break;
- case cmsysProcess_State_Exception:
- this->Makefile->AddDefinition(arguments.ResultVariable,
- cmsysProcess_GetExceptionString(cp));
- break;
- case cmsysProcess_State_Error:
- this->Makefile->AddDefinition(arguments.ResultVariable,
- cmsysProcess_GetErrorString(cp));
- break;
- case cmsysProcess_State_Expired:
- this->Makefile->AddDefinition(arguments.ResultVariable,
- "Process terminated due to timeout");
- break;
- }
- }
- // Store the result of running the processes.
- if (!arguments.ResultsVariable.empty()) {
- switch (cmsysProcess_GetState(cp)) {
- case cmsysProcess_State_Exited: {
- std::vector<std::string> res;
- for (size_t i = 0; i < arguments.Commands.size(); ++i) {
- switch (cmsysProcess_GetStateByIndex(cp, static_cast<int>(i))) {
- case kwsysProcess_StateByIndex_Exited: {
- int exitCode =
- cmsysProcess_GetExitValueByIndex(cp, static_cast<int>(i));
- char buf[16];
- sprintf(buf, "%d", exitCode);
- res.emplace_back(buf);
- } break;
- case kwsysProcess_StateByIndex_Exception:
- res.emplace_back(cmsysProcess_GetExceptionStringByIndex(
- cp, static_cast<int>(i)));
- break;
- case kwsysProcess_StateByIndex_Error:
- default:
- res.emplace_back("Error getting the child return code");
- break;
- }
- }
- this->Makefile->AddDefinition(arguments.ResultsVariable,
- cmJoin(res, ";").c_str());
- } break;
- case cmsysProcess_State_Exception:
- this->Makefile->AddDefinition(arguments.ResultsVariable,
- cmsysProcess_GetExceptionString(cp));
- break;
- case cmsysProcess_State_Error:
- this->Makefile->AddDefinition(arguments.ResultsVariable,
- cmsysProcess_GetErrorString(cp));
- break;
- case cmsysProcess_State_Expired:
- this->Makefile->AddDefinition(arguments.ResultsVariable,
- "Process terminated due to timeout");
- break;
- }
- }
- return true;
- }
- void cmExecuteProcessCommandFixText(std::vector<char>& output,
- bool strip_trailing_whitespace)
- {
- // Remove \0 characters and the \r part of \r\n pairs.
- unsigned int in_index = 0;
- unsigned int out_index = 0;
- while (in_index < output.size()) {
- char c = output[in_index++];
- if ((c != '\r' ||
- !(in_index < output.size() && output[in_index] == '\n')) &&
- c != '\0') {
- output[out_index++] = c;
- }
- }
- // Remove trailing whitespace if requested.
- if (strip_trailing_whitespace) {
- while (out_index > 0 &&
- cmExecuteProcessCommandIsWhitespace(output[out_index - 1])) {
- --out_index;
- }
- }
- // Shrink the vector to the size needed.
- output.resize(out_index);
- // Put a terminator on the text string.
- output.push_back('\0');
- }
- void cmExecuteProcessCommandAppend(std::vector<char>& output, const char* data,
- int length)
- {
- #if defined(__APPLE__)
- // HACK on Apple to work around bug with inserting at the
- // end of an empty vector. This resulted in random failures
- // that were hard to reproduce.
- if (output.empty() && length > 0) {
- output.push_back(data[0]);
- ++data;
- --length;
- }
- #endif
- cmAppend(output, data, data + length);
- }
|