-///Link all modules together and optimize them using IPO. Generate
-/// native object file using OutputFilename
-/// Return appropriate LTOStatus.
-enum LTOStatus
-LTO::optimizeModules(const std::string &OutputFilename,
- std::vector<const char *> &exportList,
- std::string &targetTriple,
- bool saveTemps,
- const char *FinalOutputFilename)
-{
- if (modules.empty())
- return LTO_NO_WORK;
-
- std::ios::openmode io_mode =
- std::ios::out | std::ios::trunc | std::ios::binary;
- std::string *errMsg = NULL;
- Module *bigOne = modules[0];
- Linker theLinker("LinkTimeOptimizer", bigOne, false);
- for (unsigned i = 1, e = modules.size(); i != e; ++i)
- if (theLinker.LinkModules(bigOne, modules[i], errMsg))
- return LTO_MODULE_MERGE_FAILURE;
-
- sys::Path FinalOutputPath(FinalOutputFilename);
- FinalOutputPath.eraseSuffix();
-
- if (saveTemps) {
- std::string tempFileName(FinalOutputPath.c_str());
- tempFileName += "0.bc";
- std::ofstream Out(tempFileName.c_str(), io_mode);
- OStream L(Out);
- WriteBytecodeToFile(bigOne, L, true);
- }
-
- // Strip leading underscore because it was added to match names
- // seen by linker.
- for (unsigned i = 0, e = exportList.size(); i != e; ++i) {
- const char *name = exportList[i];
- NameToSymbolMap::iterator itr = allSymbols.find(name);
- if (itr != allSymbols.end())
- exportList[i] = allSymbols[name]->getName();
- }
-
-
- std::string ErrMsg;
- sys::Path TempDir = sys::Path::GetTemporaryDirectory(&ErrMsg);
- if (TempDir.isEmpty()) {
- cerr << "lto: " << ErrMsg << "\n";
- return LTO_WRITE_FAILURE;
- }
- sys::Path tmpAsmFilePath(TempDir);
- if (!tmpAsmFilePath.appendComponent("lto")) {
- cerr << "lto: " << ErrMsg << "\n";
- TempDir.eraseFromDisk(true);
- return LTO_WRITE_FAILURE;
- }
- if (tmpAsmFilePath.createTemporaryFileOnDisk(&ErrMsg)) {
- cerr << "lto: " << ErrMsg << "\n";
- TempDir.eraseFromDisk(true);
- return LTO_WRITE_FAILURE;
- }
- sys::RemoveFileOnSignal(tmpAsmFilePath);
-
- std::ofstream asmFile(tmpAsmFilePath.c_str(), io_mode);
- if (!asmFile.is_open() || asmFile.bad()) {
- if (tmpAsmFilePath.exists()) {
- tmpAsmFilePath.eraseFromDisk();
- TempDir.eraseFromDisk(true);
- }
- return LTO_WRITE_FAILURE;
- }
-
- enum LTOStatus status = optimize(bigOne, asmFile, exportList);
- asmFile.close();
- if (status != LTO_OPT_SUCCESS) {
- tmpAsmFilePath.eraseFromDisk();
- TempDir.eraseFromDisk(true);
- return status;
- }
-
- if (saveTemps) {
- std::string tempFileName(FinalOutputPath.c_str());
- tempFileName += "1.bc";
- std::ofstream Out(tempFileName.c_str(), io_mode);
- OStream L(Out);
- WriteBytecodeToFile(bigOne, L, true);
- }
-
- targetTriple = bigOne->getTargetTriple();
-
- // Run GCC to assemble and link the program into native code.
- //
- // Note:
- // We can't just assemble and link the file with the system assembler
- // and linker because we don't know where to put the _start symbol.
- // GCC mysteriously knows how to do it.
- const sys::Path gcc = sys::Program::FindProgramByName("gcc");
- if (gcc.isEmpty()) {
- tmpAsmFilePath.eraseFromDisk();
- TempDir.eraseFromDisk(true);
- return LTO_ASM_FAILURE;
- }
-
- std::vector<const char*> args;
- args.push_back(gcc.c_str());
- args.push_back("-c");
- args.push_back("-x");
- args.push_back("assembler");
- args.push_back("-o");
- args.push_back(OutputFilename.c_str());
- args.push_back(tmpAsmFilePath.c_str());
- args.push_back(0);
-
- if (sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 1, &ErrMsg)) {
- cerr << "lto: " << ErrMsg << "\n";
- return LTO_ASM_FAILURE;
- }
-
- tmpAsmFilePath.eraseFromDisk();
- TempDir.eraseFromDisk(true);
-
- return LTO_OPT_SUCCESS;