ADD: CLI calls clang++.

main
bog 2023-09-28 22:18:03 +02:00
parent bd819bfc54
commit 506961fff5
3 changed files with 81 additions and 14 deletions

View File

@ -21,7 +21,7 @@ namespace wg
{
}
void Compiler::gen()
void Compiler::gen(std::filesystem::path obj)
{
auto target_triple = llvm::sys::getDefaultTargetTriple();
llvm::InitializeAllTargetInfos();
@ -53,7 +53,7 @@ namespace wg
m_module->setDataLayout(target_machine->createDataLayout());
m_module->setTargetTriple(target_triple);
auto filename = "output.o";
auto filename = obj.string();
std::error_code ec;
llvm::raw_fd_ostream dest(filename, ec, llvm::sys::fs::OF_None);
@ -71,9 +71,6 @@ namespace wg
pass.run(*m_module);
dest.flush();
m_module->print(llvm::errs(), nullptr);
}
llvm::Value* Compiler::compile(std::shared_ptr<Node> node)

View File

@ -17,7 +17,7 @@ namespace wg
explicit Compiler();
virtual ~Compiler();
void gen();
void gen(std::filesystem::path obj);
llvm::Value* compile(std::shared_ptr<Node> node);
private:

View File

@ -1,22 +1,29 @@
#include <iostream>
#include <fstream>
#include <getopt.h>
#include <Lexer.hpp>
#include <Parser.hpp>
#include <Compiler.hpp>
int main(int argc, char** argv)
std::filesystem::path name_to_obj(std::string const& path)
{
if (argc < 2)
{
return -1;
return std::filesystem::path(path).stem().string() + ".o";
}
void load(std::string const& path)
{
std::string source;
// Get Sources
{
std::ifstream file {argv[1]};
std::ifstream file {path};
if (!file)
{
std::cerr << "unknown file '"<< path <<"'" << std::endl;
exit(-1);
}
std::string line;
while (std::getline(file, line))
@ -35,7 +42,70 @@ int main(int argc, char** argv)
wg::Compiler compiler;
compiler.compile(ast);
compiler.gen();
compiler.gen(name_to_obj(path));
}
int main(int argc, char** argv)
{
std::filesystem::path output_path = "a.out";
while (true)
{
static struct option options[] = {
{"help", no_argument, 0, 'h'},
{"output", required_argument, 0, 'o'},
{0, 0, 0, 0}
};
int option_index = 0;
int c = getopt_long(argc, argv, "ho:",
options, &option_index);
if (c == -1)
{
break;
}
switch (c)
{
case 'h': {
std::cout << "Usage: wongoc [OPTIONS] <sources>" << std::endl;
std::cout << "OPTIONS" << std::endl;
std::cout << "\t" << "-h, --help," <<
"\t" << "show this message" << std::endl;
std::cout << "\t" << "-o, --output," <<
"\t" << "set the output binary name" << std::endl;
exit(0);
} break;
case 'o': {
output_path = std::filesystem::path(optarg);
} break;
default: break;
}
}
if (optind < argc)
{
while (optind < argc)
{
load(argv[optind]);
auto objname = name_to_obj(argv[optind]).string();
std::stringstream ss;
ss << "clang++" << " -o " << output_path << " ";
ss << objname;
system(ss.str().c_str());
std::filesystem::remove(objname);
optind++;
}
}
return 0;
}