91 lines
2.7 KiB
Plaintext
91 lines
2.7 KiB
Plaintext
// Declares clang::SyntaxOnlyAction.
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
#include "clang/Tooling/CommonOptionsParser.h"
|
|
#include "clang/Tooling/Tooling.h"
|
|
#include <iostream>
|
|
#include <sstream>
|
|
|
|
// Declares llvm::cl::extrahelp.
|
|
#include "llvm/Support/CommandLine.h"
|
|
#include "llvm/Support/FileSystem.h"
|
|
#include "llvm/Support/Program.h"
|
|
#include "llvm/Support/Path.h"
|
|
|
|
using namespace clang::tooling;
|
|
using namespace llvm;
|
|
|
|
#define COMPILER_NAME "cdt-cc"
|
|
#include <compiler_options.hpp>
|
|
|
|
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();
|
|
|
|
if (opts.abigen) {
|
|
llvm::outs() << "Warning, ABI generation is only available with cdt-abigen or cdt-cpp\n";
|
|
}
|
|
|
|
std::vector<std::string> outputs;
|
|
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())+"/"+input+"-tmp.c";
|
|
std::string output;
|
|
|
|
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);
|
|
|
|
output = tmp_file+".o";
|
|
|
|
new_opts.insert(new_opts.begin(), input);
|
|
|
|
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);
|
|
if (!eosio::cdt::environment::exec_subprogram("clang-9", new_opts)) {
|
|
llvm::sys::fs::remove(tmp_file);
|
|
return -1;
|
|
}
|
|
llvm::sys::fs::remove(tmp_file);
|
|
}
|
|
// then link
|
|
//
|
|
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("cdt-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;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|