sapphire/src/Arguments.cpp

71 lines
2.2 KiB
C++
Raw Normal View History

#include "Arguments.h"
#include "Error.h"
#include "tclap/CmdLine.h"
2022-06-07 16:12:43 +00:00
#include "llvm/Support/Host.h"
std::string Arguments::input_fname;
std::string Arguments::output_fname;
bool Arguments::wimport;
2022-06-07 16:12:43 +00:00
llvm::Triple Arguments::TargetTriple;
std::string Arguments::cpu;
2022-06-14 14:29:51 +00:00
void Arguments::parse(int argc, char** argv)
{
2022-06-14 14:29:51 +00:00
try
{
TCLAP::CmdLine command_line("The Sapphire compiler.", ' ', "0.1");
2022-06-14 14:29:51 +00:00
TCLAP::UnlabeledValueArg<std::string> input_fname_arg("file", "Input file.", true, "test.sp", "string");
2022-06-14 14:29:51 +00:00
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");
2022-06-14 14:29:51 +00:00
TCLAP::SwitchArg wimport_arg("", "wimport", "Show a warning when trying to import an already imported file.");
command_line.add(wimport_arg);
command_line.add(input_fname_arg);
command_line.add(output_fname_arg);
2022-06-07 16:12:43 +00:00
command_line.add(march_arg);
command_line.add(mcpu_arg);
command_line.add(msystem_arg);
2022-06-14 14:29:51 +00:00
command_line.parse(argc, argv);
input_fname = input_fname_arg.getValue();
output_fname = output_fname_arg.getValue();
wimport = wimport_arg.getValue();
2022-06-07 16:12:43 +00:00
cpu = mcpu_arg.getValue();
2022-06-14 14:29:51 +00:00
setTriple(march_arg.getValue(), msystem_arg.getValue());
}
catch (TCLAP::ArgException& e)
{
Error::throw_error_without_location(e.error());
}
}
2022-06-07 16:12:43 +00:00
void Arguments::setTriple(const std::string& arch, const std::string& system)
{
std::string triple = llvm::sys::getDefaultTargetTriple();
2022-06-14 14:29:51 +00:00
llvm::Triple targetTriple(triple);
2022-06-07 16:12:43 +00:00
2022-06-14 14:29:51 +00:00
if (arch != "native")
2022-06-07 16:12:43 +00:00
{
2022-06-14 14:29:51 +00:00
targetTriple.setArchName(arch);
2022-06-07 16:12:43 +00:00
}
2022-06-14 14:29:51 +00:00
if (system != "native")
2022-06-07 16:12:43 +00:00
{
2022-06-14 14:29:51 +00:00
targetTriple.setOSAndEnvironmentName(system);
2022-06-07 16:12:43 +00:00
}
2022-06-14 14:29:51 +00:00
targetTriple.setVendor(llvm::Triple::VendorType::UnknownVendor); // let's leave it like that
2022-06-07 16:12:43 +00:00
2022-06-14 14:29:51 +00:00
TargetTriple = targetTriple;
2022-06-07 16:12:43 +00:00
}