sapphire/src/Arguments.cpp
2022-06-16 17:13:01 +02:00

78 lines
2.5 KiB
C++

#include "Arguments.h"
#include "Error.h"
#include "tclap/CmdLine.h"
#include "utils.h"
#include "llvm/Support/Host.h"
std::string Arguments::input_fname;
std::string Arguments::output_fname;
bool Arguments::wimport;
llvm::Triple Arguments::TargetTriple;
std::string Arguments::cpu;
void Arguments::parse(int argc, char** argv)
{
__benchmark_impl::init();
try
{
TCLAP::CmdLine command_line("The Sapphire compiler.", ' ', "0.1");
TCLAP::UnlabeledValueArg<std::string> input_fname_arg("file", "Input file.", true, "test.sp", "string");
TCLAP::ValueArg<std::string> output_fname_arg("o", "output", "Output file.", false, "sp-output", "string");
TCLAP::ValueArg<std::string> march_arg("", "march", "Architecture to compile for.", false, "native", "string");
TCLAP::ValueArg<std::string> mcpu_arg("", "mcpu", "CPU to compile for.", false, "generic", "string");
TCLAP::ValueArg<std::string> msystem_arg("", "msystem", "Operating System to compile for.", false, "native",
"string");
TCLAP::SwitchArg mprofile_arg("", "mprofile", "Show execution times for functions.");
TCLAP::SwitchArg wimport_arg("", "wimport", "Show a warning when trying to import an already imported file.");
command_line.add(wimport_arg);
command_line.add(mprofile_arg);
command_line.add(input_fname_arg);
command_line.add(output_fname_arg);
command_line.add(march_arg);
command_line.add(mcpu_arg);
command_line.add(msystem_arg);
command_line.parse(argc, argv);
input_fname = input_fname_arg.getValue();
output_fname = output_fname_arg.getValue();
wimport = wimport_arg.getValue();
cpu = mcpu_arg.getValue();
setTriple(march_arg.getValue(), msystem_arg.getValue());
if (mprofile_arg.getValue()) __benchmark_impl::enable();
}
catch (TCLAP::ArgException& e)
{
Error::throw_error_without_location(e.error());
}
}
void Arguments::setTriple(const std::string& arch, const std::string& system)
{
std::string triple = llvm::sys::getDefaultTargetTriple();
llvm::Triple targetTriple(triple);
if (arch != "native")
{
targetTriple.setArchName(arch);
}
if (system != "native")
{
targetTriple.setOSAndEnvironmentName(system);
}
targetTriple.setVendor(llvm::Triple::VendorType::UnknownVendor); // let's leave it like that
TargetTriple = targetTriple;
}