summaryrefslogtreecommitdiffstats
path: root/Tests/CMakeLib/testGccDepfileReader.cxx
diff options
context:
space:
mode:
authorJoerg Bornemann <joerg.bornemann@qt.io>2020-01-21 09:26:42 (GMT)
committerBrad King <brad.king@kitware.com>2020-01-28 16:16:11 (GMT)
commitf8c505d4b30aa636b013486913591060b2040716 (patch)
tree2853577d4c09ff8f11dac45bfd4ba8a2a014dc90 /Tests/CMakeLib/testGccDepfileReader.cxx
parent306328ace87376dddb2a42bf33c6338aac5d09ad (diff)
downloadCMake-f8c505d4b30aa636b013486913591060b2040716.zip
CMake-f8c505d4b30aa636b013486913591060b2040716.tar.gz
CMake-f8c505d4b30aa636b013486913591060b2040716.tar.bz2
Add a parser for GCC-style depfiles
Introduce the function cmReadGccDepfile that parses a GCC-style depfile and returns its content. The implementation uses a lexer that is modeled after the re2c implementation in Ninja. The sample files of the autotest have been created with gcc 8.3.0. This depfile reader is to be used by the Autogen facility to make use of the depfiles that are generated by Qt's meta object compiler.
Diffstat (limited to 'Tests/CMakeLib/testGccDepfileReader.cxx')
-rw-r--r--Tests/CMakeLib/testGccDepfileReader.cxx131
1 files changed, 131 insertions, 0 deletions
diff --git a/Tests/CMakeLib/testGccDepfileReader.cxx b/Tests/CMakeLib/testGccDepfileReader.cxx
new file mode 100644
index 0000000..924d87b
--- /dev/null
+++ b/Tests/CMakeLib/testGccDepfileReader.cxx
@@ -0,0 +1,131 @@
+#include <cstddef>
+#include <iostream>
+#include <memory>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "cmsys/FStream.hxx"
+
+#include "cmGccDepfileReader.h"
+#include "cmGccDepfileReaderTypes.h" // for cmGccDepfileContent, cmGccStyle...
+#include "cmSystemTools.h"
+
+namespace {
+
+cmGccDepfileContent readPlainDepfile(const char* filePath)
+{
+ cmGccDepfileContent result;
+ cmsys::ifstream is(filePath);
+ if (!is.is_open())
+ return result;
+ std::string line;
+
+ cmGccStyleDependency dep;
+ bool readingRules = true;
+ while (cmSystemTools::GetLineFromStream(is, line)) {
+ if (line == "--RULES--") {
+ if (!dep.rules.empty()) {
+ result.push_back(std::move(dep));
+ dep = cmGccStyleDependency();
+ }
+ readingRules = true;
+ } else if (line == "--DEPENDENCIES--") {
+ readingRules = false;
+ } else {
+ std::vector<std::string>& dst = readingRules ? dep.rules : dep.paths;
+ dst.push_back(std::move(line));
+ line = std::string();
+ }
+ }
+
+ if (!dep.rules.empty()) {
+ result.push_back(std::move(dep));
+ }
+
+ return result;
+}
+
+bool compare(const std::vector<std::string>& actual,
+ const std::vector<std::string>& expected, const char* msg)
+{
+ if (actual.size() != expected.size()) {
+ std::cerr << msg << "expected " << expected.size() << " entries."
+ << std::endl
+ << "Actual number of entries: " << actual.size() << std::endl;
+ return false;
+ }
+ for (std::size_t i = 0; i < actual.size(); ++i) {
+ if (actual[i] != expected[i]) {
+ std::cerr << msg << std::endl
+ << "expected: " << expected[i] << std::endl
+ << "actual: " << actual[i] << std::endl;
+ return false;
+ }
+ }
+ return true;
+}
+
+bool compare(const cmGccDepfileContent& actual,
+ const cmGccDepfileContent& expected)
+{
+ if (actual.size() != expected.size()) {
+ std::cerr << "Expected " << expected.size() << " entries." << std::endl
+ << "Actual number of entries: " << actual.size() << std::endl;
+ return false;
+ }
+ for (std::size_t i = 0; i < actual.size(); ++i) {
+ if (!compare(actual[i].rules, expected[i].rules, "Rules differ: ") ||
+ !compare(actual[i].paths, expected[i].paths, "Paths differ: ")) {
+ return false;
+ }
+ }
+ return true;
+}
+
+void dump(const char* label, const cmGccDepfileContent& dfc)
+{
+ std::cerr << label << std::endl;
+ for (const auto& entry : dfc) {
+ auto rit = entry.rules.cbegin();
+ if (rit != entry.rules.cend()) {
+ std::cerr << *rit;
+ for (++rit; rit != entry.rules.cend(); ++rit) {
+ std::cerr << " " << *rit;
+ }
+ std::cerr << ": " << std::endl;
+ }
+ for (const auto& path : entry.paths) {
+ std::cerr << " " << path << std::endl;
+ }
+ }
+}
+
+} // anonymous namespace
+
+int testGccDepfileReader(int argc, char* argv[])
+{
+ if (argc < 2) {
+ std::cout << "Invalid arguments.\n";
+ return -1;
+ }
+
+ std::string dataDirPath = argv[1];
+ dataDirPath += "/testGccDepfileReader_data";
+ const int numberOfTestFiles = 3;
+ for (int i = 1; i <= numberOfTestFiles; ++i) {
+ const std::string base = dataDirPath + "/deps" + std::to_string(i);
+ const std::string depfile = base + ".d";
+ const std::string plainDepfile = base + ".txt";
+ std::cout << "Comparing " << base << " with " << plainDepfile << std::endl;
+ const auto actual = cmReadGccDepfile(depfile.c_str());
+ const auto expected = readPlainDepfile(plainDepfile.c_str());
+ if (!compare(actual, expected)) {
+ dump("actual", actual);
+ dump("expected", expected);
+ return 1;
+ }
+ }
+
+ return 0;
+}