-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathself_builder.cpp
More file actions
197 lines (158 loc) · 5.72 KB
/
self_builder.cpp
File metadata and controls
197 lines (158 loc) · 5.72 KB
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
// Copyright (C) 2017-2018 Egor Pugin <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <sw/manager/database.h>
#include <sw/manager/settings.h>
#include <sw/manager/storage.h>
#include <sw/manager/sw_context.h>
#include <primitives/emitter.h>
#include <primitives/executor.h>
#include <primitives/sw/main.h>
#include <primitives/sw/cl.h>
#include <primitives/sw/settings_program_name.h>
#include <primitives/log.h>
DECLARE_STATIC_LOGGER(logger, "self_builder");
#define SW_TARGET "org.sw.sw.client.driver.cpp-0.3.1"
using namespace sw;
static cl::opt<path> p(cl::Positional, cl::Required);
static cl::opt<path> packages(cl::Positional, cl::Required);
void setup_log(const std::string &log_level)
{
LoggerSettings log_settings;
log_settings.log_level = log_level;
log_settings.simple_logger = true;
log_settings.print_trace = true;
initLogger(log_settings);
// first trace message
LOG_TRACE(logger, "----------------------------------------");
LOG_TRACE(logger, "Starting sw...");
}
void write_required_packages(const std::unordered_map<UnresolvedPackage, LocalPackage> &m)
{
StringSet pkgs_sorted;
for (auto &[p, d] : m)
pkgs_sorted.insert(d.toString());
primitives::CppEmitter ctx_packages;
for (auto &s : pkgs_sorted)
ctx_packages.addLine("\"" + s + "\"s,");
write_file_if_different(packages, ctx_packages.getText());
}
void write_build_script(const std::unordered_map<UnresolvedPackage, LocalPackage> &m)
{
std::set<PackageVersionGroupNumber> used_gns;
std::vector<LocalPackage> lpkgs;
// some packages must be before others
std::vector<UnresolvedPackage> prepkgs;
// goes before primitives
prepkgs.push_back("org.sw.demo.ragel"s);
//#ifdef _WIN32
// goes before primitives
prepkgs.push_back("org.sw.demo.lexxmark.winflexbison.bison"s);
//#endif
// goes before grpc
prepkgs.push_back("org.sw.demo.google.protobuf.protobuf"s);
// goes before sw cpp driver (client)
prepkgs.push_back("org.sw.demo.google.grpc.cpp.plugin"s);
// goes before sw cpp driver (client)
prepkgs.push_back("pub.egorpugin.primitives.filesystem-master"s);
// cpp driver
prepkgs.push_back({SW_TARGET});
for (auto &u : prepkgs)
{
const LocalPackage *lp = nullptr;
for (auto &[u2, lp2] : m)
{
if (u2.ppath == u.ppath)
lp = &lp2;
}
if (!lp)
throw SW_RUNTIME_ERROR("Cannot find dependency: " + u.toString());
auto &r = *lp;
auto &d = r.getData();
if (used_gns.find(d.group_number) != used_gns.end())
continue;
used_gns.insert(d.group_number);
lpkgs.emplace_back(r);
}
for (auto &[u, r] : m)
{
auto &d = r.getData();
if (used_gns.find(d.group_number) != used_gns.end())
continue;
used_gns.insert(d.group_number);
lpkgs.emplace_back(r);
}
primitives::CppEmitter build;
build.beginFunction("TargetEntryPointMap build_self_generated()");
build.addLine("TargetEntryPointMap epm;");
build.addLine();
primitives::CppEmitter ctx;
for (auto &r : lpkgs)
{
auto f = read_file(r.getDirSrc2() / "sw.cpp");
bool has_checks = f.find("Checker") != f.npos; // more presize than setChecks
auto &d = r.getData();
ctx.addLine("#define configure configure_" + r.getVariableName());
ctx.addLine("#define build build_" + r.getVariableName());
if (has_checks)
ctx.addLine("#define check check_" + r.getVariableName());
ctx.addLine("#include \"" + normalize_path(r.getDirSrc2() / "sw.cpp") + "\"");
ctx.addLine("#undef configure");
ctx.addLine("#undef build");
if (has_checks)
ctx.addLine("#undef check");
ctx.addLine();
auto gn = std::to_string(d.group_number) + "LL";
build.beginBlock();
build.addLine("auto ep = std::make_shared<sw::NativeBuiltinTargetEntryPoint>(build_" + r.getVariableName() + ");");
if (has_checks)
build.addLine("ep->cf = check_" + r.getVariableName() + ";");
//build.addLine("ep->module_data.NamePrefix = \"" + r.getPath().slice(0, d.prefix).toString() + "\";");
//build.addLine("epm[\"" + r.toString() + "\"s] = ep;");
build.addLine("epm[" + std::to_string(r.getData().group_number) + "] = ep;");
build.endBlock();
build.addLine();
}
build.addLine("return epm;");
build.endFunction();
ctx += build;
ctx.addLine("#undef build");
ctx.addLine("#undef check");
ctx.addLine("#undef configure");
write_file(p, ctx.getText());
}
int main(int argc, char **argv)
{
setup_log("INFO");
cl::ParseCommandLineOptions(argc, argv);
// init
Executor e(select_number_of_threads());
getExecutor(&e);
SwManagerContext swctx(Settings::get_user_settings().storage_dir);
auto m = swctx.install(
{
// our main cpp driver target
{SW_TARGET},
// other needed stuff (libcxx)
{"org.sw.demo.llvm_project.libcxx"},
});
// calc GNs
for (auto &[u2, r] : m)
{
auto &d = r.getData();
if (d.group_number == 0)
{
((PackageData&)d).group_number = get_specification_hash(read_file(r.getDirSrc2() / "sw.cpp"));
}
}
write_required_packages(m);
write_build_script(m);
return 0;
}
EXPORT_FROM_EXECUTABLE
std::string getProgramName()
{
return PACKAGE_NAME_CLEAN;
}