diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp index 6d49034..579a8a1 100644 --- a/lib/Compiler.cpp +++ b/lib/Compiler.cpp @@ -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) diff --git a/lib/Compiler.hpp b/lib/Compiler.hpp index 398d9a2..001e789 100644 --- a/lib/Compiler.hpp +++ b/lib/Compiler.hpp @@ -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); private: diff --git a/src/main.cpp b/src/main.cpp index 03f58cc..86f9174 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,22 +1,29 @@ #include #include - +#include #include #include #include -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] " << 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; }