CMake Nightly Date Stamp
[kiteware-cmake.git] / Source / cmProcessTools.h
blob2bdabea90668693f32e35075e1a89b57b1308112
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #pragma once
5 #include "cmConfigure.h" // IWYU pragma: keep
7 #include <cstring>
8 #include <iosfwd>
9 #include <string>
10 #include <vector>
12 #include "cmProcessOutput.h"
13 #include "cmUVProcessChain.h"
15 /** \class cmProcessTools
16 * \brief Helper classes for process output parsing
19 class cmProcessTools
21 public:
22 using Encoding = cmProcessOutput::Encoding;
23 /** Abstract interface for process output parsers. */
24 class OutputParser
26 public:
27 /** Process the given output data from a tool. Processing may be
28 done incrementally. Returns true if the parser is interested
29 in any more data and false if it is done. */
30 bool Process(const char* data, int length)
32 return this->ProcessChunk(data, length);
34 bool Process(const char* data)
36 return this->Process(data, static_cast<int>(strlen(data)));
39 virtual ~OutputParser() = default;
41 protected:
42 /** Implement in a subclass to process a chunk of data. It should
43 return true only if it is interested in more data. */
44 virtual bool ProcessChunk(const char* data, int length) = 0;
47 /** Process output parser that extracts one line at a time. */
48 class LineParser : public OutputParser
50 public:
51 /** Construct with line separation character and choose whether to
52 ignore carriage returns. */
53 LineParser(char sep = '\n', bool ignoreCR = true);
55 /** Configure logging of lines as they are extracted. */
56 void SetLog(std::ostream* log, const char* prefix);
58 protected:
59 std::ostream* Log = nullptr;
60 const char* Prefix = nullptr;
61 std::string Line;
62 char Separator;
63 char LineEnd = '\0';
64 bool IgnoreCR;
65 bool ProcessChunk(const char* data, int length) override;
67 /** Implement in a subclass to process one line of input. It
68 should return true only if it is interested in more data. */
69 virtual bool ProcessLine() = 0;
72 /** Trivial line handler for simple logging. */
73 class OutputLogger : public LineParser
75 public:
76 OutputLogger(std::ostream& log, const char* prefix = nullptr)
78 this->SetLog(&log, prefix);
81 private:
82 bool ProcessLine() override { return true; }
85 /** Run a process and send output to given parsers. */
86 static std::vector<cmUVProcessChain::Status> RunProcess(
87 cmUVProcessChainBuilder& builder, OutputParser* out,
88 OutputParser* err = nullptr, Encoding encoding = cmProcessOutput::Auto);