Created
December 8, 2016 00:12
-
-
Save luizperes/4723d8108ac966e0dde5e64c28345a42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto TargetTriple = sys::getDefaultTargetTriple(); | |
Owner->setTargetTriple(TargetTriple); | |
std::string Error; | |
auto Target = TargetRegistry::lookupTarget(TargetTriple, Error); | |
if (!Target) { | |
errs() << Error; | |
return 1; | |
} | |
auto CPU = "generic"; | |
auto Features = ""; | |
TargetOptions opt; | |
auto RM = Optional<Reloc::Model>(); | |
auto TheTargetMachine = | |
Target->createTargetMachine(TargetTriple, CPU, Features, opt, RM); | |
Owner->setDataLayout(TheTargetMachine->createDataLayout()); | |
auto Filename = "output.o"; | |
std::error_code EC; | |
raw_fd_ostream dest(Filename, EC, sys::fs::F_None); | |
if (EC) { | |
errs() << "Could not open file: " << EC.message(); | |
return 1; | |
} | |
legacy::PassManager pass; | |
auto FileType = TargetMachine::CGFT_ObjectFile; | |
if (TheTargetMachine->addPassesToEmitFile(pass, dest, FileType)) { | |
errs() << "TheTargetMachine can't emit a file of this type"; | |
return 1; | |
} | |
pass.run(*Owner); | |
dest.flush(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment