cmake/Source/cmProcessTools.cxx

107 lines
3.0 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2009-10-04 10:30:41 +03:00
#include "cmProcessTools.h"
2024-04-14 22:45:38 +02:00
#include <algorithm>
#include <iterator>
2016-10-30 18:24:19 +01:00
#include <ostream>
2009-10-04 10:30:41 +03:00
2024-04-14 22:45:38 +02:00
#include <cm3p/uv.h>
2020-02-01 23:06:01 +01:00
#include "cmProcessOutput.h"
2024-04-14 22:45:38 +02:00
#include "cmUVHandlePtr.h"
#include "cmUVStream.h"
2020-02-01 23:06:01 +01:00
2024-04-14 22:45:38 +02:00
std::vector<cmUVProcessChain::Status> cmProcessTools::RunProcess(
cmUVProcessChainBuilder& builder, OutputParser* out, OutputParser* err,
Encoding encoding)
2009-10-04 10:30:41 +03:00
{
2017-04-14 19:02:05 +02:00
cmProcessOutput processOutput(encoding);
2024-04-14 22:45:38 +02:00
builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
.SetBuiltinStream(cmUVProcessChainBuilder::Stream_ERROR);
auto chain = builder.Start();
2017-04-14 19:02:05 +02:00
std::string strdata;
2024-04-14 22:45:38 +02:00
cm::uv_pipe_ptr outputPipe;
outputPipe.init(chain.GetLoop(), 0);
uv_pipe_open(outputPipe, chain.OutputStream());
auto outputHandle = cmUVStreamRead(
outputPipe,
[&out, &processOutput, &strdata](std::vector<char> data) {
if (out) {
processOutput.DecodeText(data.data(), data.size(), strdata, 1);
if (!out->Process(strdata.c_str(), static_cast<int>(strdata.size()))) {
out = nullptr;
}
2009-10-04 10:30:41 +03:00
}
2024-04-14 22:45:38 +02:00
},
[&out]() { out = nullptr; });
cm::uv_pipe_ptr errorPipe;
errorPipe.init(chain.GetLoop(), 0);
uv_pipe_open(errorPipe, chain.ErrorStream());
auto errorHandle = cmUVStreamRead(
errorPipe,
[&err, &processOutput, &strdata](std::vector<char> data) {
if (err) {
processOutput.DecodeText(data.data(), data.size(), strdata, 2);
if (!err->Process(strdata.c_str(), static_cast<int>(strdata.size()))) {
err = nullptr;
}
2009-10-04 10:30:41 +03:00
}
2024-04-14 22:45:38 +02:00
},
[&err]() { err = nullptr; });
while (out || err || !chain.Finished()) {
uv_run(&chain.GetLoop(), UV_RUN_ONCE);
2016-07-09 11:21:54 +02:00
}
2024-04-14 22:45:38 +02:00
std::vector<cmUVProcessChain::Status> result;
auto status = chain.GetStatus();
std::transform(
status.begin(), status.end(), std::back_inserter(result),
[](const cmUVProcessChain::Status* s) -> cmUVProcessChain::Status {
return *s;
});
return result;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
cmProcessTools::LineParser::LineParser(char sep, bool ignoreCR)
2019-11-11 23:01:05 +01:00
: Separator(sep)
2016-07-09 11:21:54 +02:00
, IgnoreCR(ignoreCR)
2009-10-04 10:30:41 +03:00
{
}
void cmProcessTools::LineParser::SetLog(std::ostream* log, const char* prefix)
{
this->Log = log;
2016-07-09 11:21:54 +02:00
this->Prefix = prefix ? prefix : "";
2009-10-04 10:30:41 +03:00
}
bool cmProcessTools::LineParser::ProcessChunk(const char* first, int length)
{
const char* last = first + length;
2016-07-09 11:21:54 +02:00
for (const char* c = first; c != last; ++c) {
if (*c == this->Separator || *c == '\0') {
2010-06-28 22:39:51 +03:00
this->LineEnd = *c;
2009-10-04 10:30:41 +03:00
// Log this line.
2016-07-09 11:21:54 +02:00
if (this->Log && this->Prefix) {
2009-10-04 10:30:41 +03:00
*this->Log << this->Prefix << this->Line << "\n";
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// Hand this line to the subclass implementation.
2016-07-09 11:21:54 +02:00
if (!this->ProcessLine()) {
2018-01-26 17:06:56 +01:00
this->Line.clear();
2009-10-04 10:30:41 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2018-01-26 17:06:56 +01:00
this->Line.clear();
2016-07-09 11:21:54 +02:00
} else if (*c != '\r' || !this->IgnoreCR) {
2009-10-04 10:30:41 +03:00
// Append this character to the line under construction.
this->Line.append(1, *c);
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return true;
}