summaryrefslogtreecommitdiffhomepage
path: root/MakefileReader.cpp
blob: 2a5fa9819c5238b767359888906e4affb003097f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "MakefileReader.h"

#include <boost/property_tree/xml_parser.hpp>

#include "file.h"

namespace pt = boost::property_tree;
namespace fs = std::filesystem;

namespace {
 // the elements with this name are considered paths, where relative prefixes will be added if necessary
 std::vector<std::string> path_elements{"name", "source", "test"};
 const std::string topelement{"ymake"};

 // add path to respective elements in tree, recursively
 void adjust_path(pt::ptree::value_type& element, const fs::path& path) {
  // add path prefix if path element
   if (std::find(path_elements.begin(), path_elements.end(), element.first) != path_elements.end()) {
    std::string content{element.second.data()};
    element.second.data() = simplified_path(path / fs::path{content}).string();
   }

  for (auto &i: element.second) {
   // recursively run
   adjust_path(i, path);
  }
 }

 void join(pt::ptree& tree, const pt::ptree& makefile_tree, const fs::path& path) {
  if (tree.empty()) {
   tree = makefile_tree; // including top level single "ymake" element
  } else {
   // copy all elements inside single "ymake"
   pt::ptree& target{tree.get_child(topelement)};
   const pt::ptree& source{makefile_tree.get_child(topelement)};
   for (const auto& i: source) {
    auto it {target.push_back(i)};
    auto &element{*it};
    adjust_path(element, path);
   }
  }
 }
}

MakefileReader::MakefileReader()
{
}

pt::ptree MakefileReader::read(const fs::path& path) const
{
 pt::ptree ptree;

 // YMakefile needs to exist, but it can be in a subdir, so we need to
 // iterate first over directories
 bool makefile_found{};

 for (const fs::directory_entry& dir_entry: fs::recursive_directory_iterator(path)) {
  pt::ptree makefile_tree;
  fs::path path{dir_entry.path()};

  if (dir_entry.is_regular_file() && path.filename() == YMakefile) {
   makefile_found = true;
   pt::read_xml(path, makefile_tree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace);

   join(ptree, makefile_tree, simplified_path(path.parent_path()));
  }
 }

 if (!makefile_found) {
  throw std::runtime_error("YMakefile not found");
 }

 return ptree;
}