summaryrefslogtreecommitdiffhomepage
path: root/xmake.cpp
blob: a1831e803d8b943b2b4e22826af0347d54c8afef (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "xmake.h"

#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <iterator>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/process.hpp>

#include <fmt/format.h>

#include <libreichwein/file.h>
#include <libreichwein/stringhelper.h>

namespace fs = std::filesystem;
namespace bp = boost::process;
namespace pt = boost::property_tree;
using namespace std::string_literals;

namespace {
 const fs::path XMakefile{"XMakefile"};

 void usage()
 {
  std::cout << "Usage: xmake <target>" << std::endl;
 }

 fs::path get_target(const pt::ptree& ptree) {
  return ptree.get<std::string>("xmake.build.name");
 }
 
 std::vector<fs::path> get_sources(const pt::ptree& ptree) {
  std::vector<fs::path> sources;
  for (const pt::ptree::value_type &v: ptree.get_child("xmake.build")) {
   if (v.first == "source")
    sources.push_back(v.second.data());
  }
  return sources;
 }

 std::vector<fs::path> get_objects(const pt::ptree& ptree) {
  std::vector<fs::path> objects{get_sources(ptree)};
  for (auto &i: objects) {
   i.replace_extension("o");
  }
  return objects;
 }

 // both need to exist
 bool is_older(const fs::path& p, const fs::path& other) {
  auto t_p{fs::last_write_time(p)};
  auto t_other{fs::last_write_time(other)};
  return t_p < t_other;
 }

 // outdated according to dependency file list, non-recursively
 bool is_outdated(const fs::path& p, const std::vector<fs::path> &dependencies) {
  if (!fs::exists(p))
   return true;

  for (const auto& dep: dependencies) {
   if (!exists(dep) || is_older(p, dep)) {
    return true;
   }
  }

  return false;
 }

 std::vector<fs::path> dependencies_of(const fs::path& p, const std::unordered_map<fs::path, std::vector<fs::path>> &dependencies) {
  try {
   return dependencies.at(p);
  } catch (const std::out_of_range& ex) {
   return {}; // empty by default
  }
 }

 // outdated according to dependency tree, recursively
 bool is_outdated(const fs::path& p, const std::unordered_map<fs::path, std::vector<fs::path>> &dependencies) {
  if (!fs::exists(p))
   return true;

  std::vector<fs::path> deps{dependencies_of(p, dependencies)};
  for (const auto& dep: deps) {
   if (!exists(dep) || is_older(p, dep)) {
    return true;
   }

   if (is_outdated(dep, dependencies)) {
    return true;
   }
  }

  return false;
 }

 std::vector<fs::path> deps_from_depfile(const fs::path& path) {
  std::string depfile_content{Reichwein::File::getFile(path)};
  std::vector<std::string> parts {Reichwein::Stringhelper::split(depfile_content, ":\r\n")};
  if (parts.size() >= 2) {
   std::vector<std::string> deps {Reichwein::Stringhelper::split(parts[1], " ")};
   std::vector<fs::path> result;
   std::transform(deps.cbegin(), deps.cend(), std::back_inserter(result), [](const std::string& s){ return s; });
   return result;
  } else {
   throw std::runtime_error("Bad depfile contents: "s + path.string());
  }
 }

 // return contained dependencies
 // input: cpp
 std::vector<fs::path> make_depfile_from(const fs::path& p) {
  fs::path depfile{p};
  depfile.replace_extension("d");

  // check if depfile exists and if it contains up to date info
  if (!fs::exists(depfile) || is_outdated(depfile, deps_from_depfile(depfile))) {
   // actually create depfile
   int result{system(fmt::format("g++ -MM -MF {} -c {}", depfile.string(), p.string()).c_str())};
   if (result != 0) {
    throw std::runtime_error(fmt::format("Depfile {} can't be created", depfile.string()));
   }
  }

  return deps_from_depfile(depfile);
 }

 // type of file can be built from dependencies
 bool is_buildable_by_extension(const fs::path& p) {
  fs::path ext{p.extension()};
  return ext.empty() || ext == ".o";
 }

 // build 1 file
 void build(const fs::path& p, const std::unordered_map<fs::path, std::vector<fs::path>> &dependencies) {
  std::string command;

  if (p.extension() == ".o") {
    // compile
    fs::path cppfile{p};
    cppfile.replace_extension("cpp");
    command = fmt::format("g++ -std=c++17 -c {} -o {}", cppfile.string(), p.string());
  } else {
   // link
   command = "g++";
   std::vector<fs::path> objects{dependencies_of(p, dependencies)};
   for (auto &i: objects) {
    command += fmt::format(" {}", i.string());
   }
   command += " -lfmt -lreichwein";
   command += fmt::format(" -o {}", p.string());
  }

  std::cout << command << std::endl;
  int result{system(command.c_str())};
  if (result != 0) {
   throw std::runtime_error(fmt::format("Error {}", result));
  }
 }

 // build list of files
 // eats up input
 void build(std::unordered_set<fs::path>& buildlist, const std::unordered_map<fs::path, std::vector<fs::path>> &dependencies) {
  std::unordered_set<fs::path> activelist; // currently building
  std::unordered_set<fs::path> donelist; // build done

  if (buildlist.empty()) {
   std::cout << "Everything up to date." << std::endl;
  } else {
   std::cout << "Running commands: " << std::endl;
  }

  while (!buildlist.empty()) {
   // find file which can be built directly since its build dependencies are up to date
   // this holds for either any member of the set (e.g. at begin()), or any of its direct or indirect dependencies
   fs::path current {*buildlist.begin()};

   bool found_outdated{}; // outdated dependencies

   do {
    found_outdated = false;
    std::vector<fs::path> deps{dependencies_of(current, dependencies)};
    for (auto& i: deps) {
     if (activelist.find(i) == activelist.end() && is_outdated(i, dependencies)) {
      found_outdated = true;
      current = i;
      break;
     }
    }
   } while (found_outdated);

   buildlist.erase(current);

   activelist.insert(current);
   // TODO: build in background
   build(current, dependencies);
   activelist.erase(current);

   donelist.insert(current);
  }
 }

 // build everything according to specified configuration
 void build(const pt::ptree& ptree) {
  fs::path target{get_target(ptree)};
  std::vector<fs::path> objects{get_objects(ptree)};
  std::vector<fs::path> sources{get_sources(ptree)};

  std::cout << "Target: " << target << std::endl;

  std::cout << "Sources: " << std::endl;
  for (auto &i: sources) {
   std::cout << "  " << i << std::endl;
  }

  std::cout << "Calculating dependencies..." << std::endl;
  std::unordered_map<fs::path, std::vector<fs::path>> dependencies;
  dependencies.emplace(target, objects);
  for (const auto& p: sources) {
   fs::path p_obj{p};
   p_obj.replace_extension("o");
   std::vector<fs::path> deps {make_depfile_from(p)};
   dependencies.emplace(p_obj, deps);
  }

  // create build list by depth-first search
  std::cout << "Calculating build list..." << std::endl;
  std::unordered_set<fs::path> buildlist;
  std::stack<fs::path> container; // temporary container for search algorithm
  container.push(target);
  while (!container.empty()) {
   fs::path current{container.top()};
   container.pop();

   std::vector<fs::path> deps{dependencies_of(current, dependencies)};
   for (auto &i: deps) {
    container.push(i);
   }

   if (is_outdated(current, dependencies) && is_buildable_by_extension(current)) {
    buildlist.insert(current);
   }
  }

  std::cout << "Build list:" << std::endl;
  for (auto &i: buildlist) {
   std::cout << "  " << i << std::endl;
  }

  build(buildlist, dependencies);
 }

 void clean(const pt::ptree& ptree) {
  std::vector<fs::path> cleanlist{get_objects(ptree)};
  cleanlist.push_back(get_target(ptree));

  std::vector<std::string> commands;
  for (auto &i: cleanlist) {
   if (fs::exists(i)) {
    commands.push_back(fmt::format("rm -f {}", i.string()));
   }
   fs::path dep{i};
   dep.replace_extension("d");
   if (fs::exists(dep)) {
    commands.push_back(fmt::format("rm -f {}", dep.string()));
   }
  }

  std::cout << "Running commands: " << std::endl;
  for (auto &i: commands) {
   std::cout << i << std::endl;
   int result{system(i.c_str())};
   if (result != 0) {
    throw std::runtime_error(fmt::format("Error {}", result));
   }
  }
 }
}

int xmake(int argc, char* argv[])
{
 try {
  pt::ptree ptree;
  pt::read_xml(XMakefile, ptree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace);

  std::string action{"default"};

  if (argc == 1) { // default action
  } else if (argc == 2) {
   action = argv[1];
  } else {
   std::cerr << "Invalid arguments." << std::endl;
   usage;
   exit(1);
  }

  if (action == "default"s || action == "build") { // build
   build(ptree);
  } else if (action == "clean") {
   clean(ptree);
  } else if (action == "install") {
   throw std::runtime_error("unimplemented");
  } else if (action == "rebuild") {
   throw std::runtime_error("unimplemented");
  } else if (action == "run") {
   throw std::runtime_error("unimplemented");
  } else {
   std::cerr << "Invalid action: " << action << std::endl;
   usage;
   exit(1);
  }

 } catch (const std::exception& ex) {
  std::cerr << "xmake: " << ex.what() << std::endl;
  return 1;
 }

 return 0;
}