blob: e0a972d65d1a7978c0bef1e600034ccebb380271 (
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
|
#include "xmake.h"
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/process.hpp>
#include <fmt/format.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;
}
std::string get_target(pt::ptree& ptree) {
return ptree.get<std::string>("xmake.build.name");
}
std::vector<std::string> get_sources(pt::ptree& ptree) {
std::vector<std::string> sources;
for (pt::ptree::value_type &v: ptree.get_child("xmake.build")) {
if (v.first == "source")
sources.push_back(v.second.data());
}
return sources;
}
std::vector<std::string> get_objects(pt::ptree& ptree) {
std::vector<std::string> objects{get_sources(ptree)};
for (auto &i: objects) {
fs::path p{i};
p.replace_extension("o");
i = p.string();
}
return objects;
}
void build(pt::ptree& ptree) {
std::string target{get_target(ptree)};
std::vector<std::string> 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::vector<std::string> commands;
// compile
for (auto &i: sources) {
fs::path p{i};
fs::path p_obj{i};
p_obj.replace_extension("o");
commands.push_back(fmt::format("g++ -std=c++17 -c {} -o {}", p.string(), p_obj.string()));
}
// link
std::string link_command{"g++"};
std::vector<std::string> objects{get_objects(ptree)};
for (auto &i: objects) {
link_command += fmt::format(" {}", i);
}
link_command += " -lfmt";
link_command += fmt::format(" -o {}", target);
commands.push_back(link_command);
std::cout << "Commands: " << std::endl;
for (auto &i: commands) {
std::cout << " " << i << std::endl;
}
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));
}
}
}
void clean(pt::ptree& ptree) {
std::vector<std::string> cleanlist{get_objects(ptree)};
cleanlist.push_back(get_target(ptree));
std::vector<std::string> commands;
for (auto &i: cleanlist) {
commands.push_back(fmt::format("rm -f {}", i));
}
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;
}
|