-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pipeline.cpp
98 lines (80 loc) · 2.8 KB
/
Pipeline.cpp
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
#include "Pipeline.h"
#include <array>
#include <filesystem>
#include <fstream>
#include <sstream>
namespace
{
std::pair<int, std::string> runCommand(std::string command) {
auto c_command = command.c_str();
std::array<char, 128> buffer{};
std::string processOutput;
FILE* pipe = popen(c_command, "r");
if (!pipe)
throw std::runtime_error("popen() failed!");
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
processOutput += buffer.data();
auto exitCode = pclose(pipe);
return {exitCode, processOutput};
}
}
std::string _Pipeline::getGlobalDeclarations() {
std::string globalDeclarations;
for (auto& _operator : _operators)
globalDeclarations += _operator->getGlobalDeclarations();
return globalDeclarations;
}
std::set<std::string> _Pipeline::getHeaders() {
std::set<std::string> headers;
for (auto& _operator : _operators) {
auto currentHeaders = _operator->getHeaders();
headers.insert(currentHeaders.begin(), currentHeaders.end());
}
return headers;
}
std::string _Pipeline::getQueryCode() {
std::string queryCode;
for (auto it = _operators.rbegin(); it != _operators.rend(); ++it)
queryCode = fmt::format((*it)->getCode(), fmt::arg("yield", queryCode));
return queryCode;
}
std::string _Pipeline::generateSourcefile() {
std::stringstream file;
for (auto& header : getHeaders())
file << "#include " + header << std::endl;
file << std::endl
<< "int main() {" << std::endl
<< getGlobalDeclarations() << std::endl
<< getQueryCode() << std::endl
<< "return 0;" << std::endl
<< "}" << std::endl;
return file.str();
}
void _Pipeline::compile() {
{
std::cout << "Pipeline generated!" << std::endl << "Writing to file...";
std::filesystem::create_directories("compiled_queries");
std::ofstream queryFile("compiled_queries/query.cpp");
queryFile << generateSourcefile();
queryFile.close();
std::cout << "done!" << std::endl;
}
{
std::cout << "Attempting Clang Format..." << std::flush;
auto [exitCode, processOutput] = runCommand("clang-format -i compiled_queries/query.cpp");
if (exitCode == 0) {
std::cout << "done!" << std::endl;
} else {
std::cout << "failed!" << std::endl;
}
}
{
std::cout << "Attempting compilation..." << std::flush;
auto [exitCode, processOutput] = runCommand("g++ -std=c++2a -pthread -O3 -o compiled_queries/query -I ../generated_code_libraries/ compiled_queries/query.cpp");
if (exitCode == 0) {
std::cout << "done!" << std::endl;
} else {
std::cout << "failed!" << std::endl;
}
}
}