This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy patheosio-cpp.cpp.in
207 lines (176 loc) · 7.55 KB
/
eosio-cpp.cpp.in
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
// Declares clang::SyntaxOnlyAction.
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/Expr.h"
#include "clang/Basic/Builtins.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Rewrite/Frontend/Rewriters.h"
#include "llvm/Support/FileSystem.h"
#include <eosio/abigen.hpp>
#include <eosio/codegen.hpp>
#include <iostream>
#include <sstream>
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
using namespace clang::tooling;
using namespace llvm;
#define CPP_COMP 1
#define COMPILER_NAME "eosio-cpp"
#include <compiler_options.hpp>
#include <set>
#include <sstream>
using namespace clang::tooling;
using namespace clang::ast_matchers;
using namespace llvm;
using namespace eosio;
using namespace eosio::cdt;
void handle_empty_abigen(const std::string& contract_name, bool has_o_opt, bool has_contract_opt);
void generate(const std::vector<std::string>& base_options, std::string input, std::string contract_name, const std::vector<std::string>& resource_paths, const std::pair<int, int>& abi_version, bool abigen, bool suppress_ricardian_warning, bool has_o_opt, bool has_contract_opt, bool warn_action_read_only) {
std::vector<std::string> options;
options.push_back("eosio-cpp");
options.push_back(input); // don't remove oddity of CommonOptionsParser?
options.push_back(input);
options.push_back("--");
if (llvm::sys::path::extension(input).equals(".c"))
options.push_back("-xc++");
for (size_t i=0; i < base_options.size(); i++) {
options.push_back(base_options[i]);
}
options.push_back("-Wno-everything");
int size = options.size();
const char** new_argv = new const char*[size];
for (size_t i=0; i < size; i++)
new_argv[i] = options[i].c_str();
CommonOptionsParser opts( size, new_argv, EosioCompilerToolCategory, 0 );
ClangTool ctool(opts.getCompilations(), opts.getSourcePathList());
abigen::get().set_contract_name(contract_name);
abigen::get().set_resource_dirs(resource_paths);
abigen::get().set_abi_version(std::get<0>(abi_version), std::get<1>(abi_version));
abigen::get().set_suppress_ricardian_warning(suppress_ricardian_warning);
codegen::get().set_contract_name(contract_name);
codegen::get().set_warn_action_read_only(warn_action_read_only);
int tool_run = -1;
tool_run = ctool.run(newFrontendActionFactory<eosio_abigen_frontend_action>().get());
if (tool_run != 0) {
throw std::runtime_error("abigen error");
}
if (!abigen::get().is_empty()) {
std::string abi_s;
abigen::get().to_json().dump(abi_s);
codegen::get().set_abi(abi_s);
} else if (abigen) {
handle_empty_abigen(contract_name, has_o_opt, has_contract_opt);
}
tool_run = ctool.run(newFrontendActionFactory<eosio_codegen_frontend_action>().get());
if (tool_run != 0) {
throw std::runtime_error("codegen error");
}
}
void handle_empty_abigen(const std::string& contract_name, bool has_o_opt, bool has_contract_opt) {
const std::string& parsed_contract_name = abigen::get().get_parsed_contract_name();
if (parsed_contract_name.empty()) {
// if no contract name could be obtained by parsing methods/records, it means the contract is empty
std::cout << "Warning, contract is empty and ABI is not generated\n";
} else if (contract_name != parsed_contract_name) {
// if contract is empty but the contract file contains other contracts, the parsed contract name could be wrong
std::string err = "abigen error: contract is empty, or ";
// or, the contract is not empty but the specified or inferred contract name is wrong
err += has_contract_opt ? "'--contract' specified name" : (has_o_opt ? "'-o' specified name" : "contract filename");
err += " doesn't match the real contract name";
throw std::runtime_error(err);
} else {
throw std::runtime_error("abigen error"); // unknown error
}
}
int main(int argc, const char **argv) {
// fix to show version info without having to have any other arguments
for (int i=0; i < argc; i++) {
if (argv[i] == std::string("-v")) {
eosio::cdt::environment::exec_subprogram("clang-9", {"-v"});
return 0;
}
}
cl::SetVersionPrinter([](llvm::raw_ostream& os) {
os << COMPILER_NAME << " version " << "${VERSION_FULL}" << "\n";
});
cl::ParseCommandLineOptions(argc, argv, std::string(COMPILER_NAME)+" (Eosio C++ -> WebAssembly compiler)");
Options opts = CreateOptions();
std::vector<std::string> outputs;
try {
for (auto input : opts.inputs) {
std::vector<std::string> new_opts = opts.comp_options;
SmallString<64> res;
llvm::sys::path::system_temp_directory(true, res);
std::string tmp_file = std::string(res.c_str())+"/"+llvm::sys::path::filename(input).str();
std::string output;
if (!opts.pp_only) {
auto tool_opts = opts.comp_options;
std::set<std::string> non_tool_opts = { "-S", "-emit-llvm", "-emit-ast" };
tool_opts.erase(std::remove_if(tool_opts.begin(), tool_opts.end(),
[&](const auto& opt){ return non_tool_opts.count(opt); }),
tool_opts.end());
generate(tool_opts, input, opts.abigen_contract, opts.abigen_resources, opts.abi_version, opts.abigen, opts.suppress_ricardian_warning, opts.has_o_opt, opts.has_contract_opt, opts.warn_action_read_only);
auto src = SmallString<64>(input);
llvm::sys::path::remove_filename(src);
std::string source_path = src.str().empty() ? "." : src.str();
new_opts.insert(new_opts.begin(), "-I" + source_path);
if (llvm::sys::fs::exists(tmp_file)) {
input = tmp_file;
}
output = tmp_file+".o";
if (!opts.link) {
output = opts.output_fn.empty() ? "a.out" : opts.output_fn;
}
new_opts.insert(new_opts.begin(), {"-o", output});
outputs.push_back(output);
}
new_opts.insert(new_opts.begin(), input);
if (llvm::sys::path::extension(input).equals(".c"))
new_opts.insert(new_opts.begin(), "-xc++");
if (!eosio::cdt::environment::exec_subprogram("clang-9", new_opts)) {
llvm::sys::fs::remove(tmp_file);
return -1;
}
llvm::sys::fs::remove(tmp_file);
}
} catch (std::runtime_error& err) {
llvm::errs() << err.what() << '\n';
return -1;
}
if (opts.link) {
std::vector<std::string> new_opts = opts.ld_options;
for (auto input : outputs) {
new_opts.insert(new_opts.begin(), input);
}
if (!eosio::cdt::environment::exec_subprogram("eosio-ld", new_opts)) {
for (auto input : outputs) {
llvm::sys::fs::remove(input);
}
return -1;
}
for (auto input : outputs) {
llvm::sys::fs::remove(input);
}
if ( !llvm::sys::fs::exists( opts.output_fn ) ) {
return -1;
}
// need to generate dSYM for mac OSX
#ifdef __APPLE__
if (opts.native) {
if (opts.debug) {
if (!eosio::cdt::environment::exec_subprogram("dsymutil", std::vector<std::string>{opts.output_fn}, true)) {
return -1;
}
}
}
#endif
}
return 0;
}