llvm.org GIT mirror llvm / 132a994
Fix llvm-ld to clean up its output files in case of an error. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@99915 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
2 changed file(s) with 59 addition(s) and 38 deletion(s). Raw diff Collapse all Expand all
5151 }
5252 }
5353
54 /// setFile - Give ownership of the file to the FileRemover so it will
55 /// be removed when the object is destroyed. If the FileRemover already
56 /// had ownership of a file, remove it first.
57 void setFile(const sys::Path &filename, bool deleteIt = true) {
58 if (DeleteIt)
59 Filename.eraseFromDisk();
60
61 Filename = filename;
62 DeleteIt = deleteIt;
63 }
64
5465 /// releaseFile - Take ownership of the file away from the FileRemover so it
5566 /// will not be removed when the object is destroyed.
5667 void releaseFile() { DeleteIt = false; }
2929 #include "llvm/Target/TargetData.h"
3030 #include "llvm/Target/TargetMachine.h"
3131 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/FileUtilities.h"
3233 #include "llvm/Support/ManagedStatic.h"
3334 #include "llvm/Support/MemoryBuffer.h"
3435 #include "llvm/Support/PrettyStackTrace.h"
123124 /// everywhere.
124125 static std::string progname;
125126
127 /// FileRemover objects to clean up output files in the event of an error.
128 static FileRemover OutputRemover;
129 static FileRemover BitcodeOutputRemover;
130
126131 /// PrintAndExit - Prints a message to standard error and exits with error code
127132 ///
128133 /// Inputs:
234239 raw_fd_ostream::F_Binary);
235240 if (!ErrorInfo.empty())
236241 PrintAndExit(ErrorInfo, M);
237
238 // Ensure that the bitcode file gets removed from the disk if we get a
239 // terminating signal.
240 sys::RemoveFileOnSignal(sys::Path(FileName));
241242
242243 // Write it out
243244 WriteBitcodeToFile(M, Out);
516517 // Parse the command line options
517518 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
518519
520 #if defined(_WIN32) || defined(__CYGWIN__)
521 if (!LinkAsLibrary) {
522 // Default to "a.exe" instead of "a.out".
523 if (OutputFilename.getNumOccurrences() == 0)
524 OutputFilename = "a.exe";
525
526 // If there is no suffix add an "exe" one.
527 sys::Path ExeFile( OutputFilename );
528 if (ExeFile.getSuffix() == "") {
529 ExeFile.appendSuffix("exe");
530 OutputFilename = ExeFile.str();
531 }
532 }
533 #endif
534
535 // Generate the bitcode for the optimized module.
536 // If -b wasn't specified, use the name specified
537 // with -o to construct BitcodeOutputFilename.
538 if (BitcodeOutputFilename.empty()) {
539 BitcodeOutputFilename = OutputFilename;
540 if (!LinkAsLibrary) BitcodeOutputFilename += ".bc";
541 }
542
543 // Arrange for the bitcode output file to be deleted on any errors.
544 BitcodeOutputRemover.setFile(sys::Path(BitcodeOutputFilename));
545 sys::RemoveFileOnSignal(sys::Path(BitcodeOutputFilename));
546
547 // Arrange for the output file to be deleted on any errors.
548 if (!LinkAsLibrary) {
549 OutputRemover.setFile(sys::Path(OutputFilename));
550 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
551 }
552
519553 // Construct a Linker (now that Verbose is set)
520554 Linker TheLinker(progname, OutputFilename, Context, Verbose);
521555
558592 // Optimize the module
559593 Optimize(Composite.get());
560594
561 #if defined(_WIN32) || defined(__CYGWIN__)
562 if (!LinkAsLibrary) {
563 // Default to "a.exe" instead of "a.out".
564 if (OutputFilename.getNumOccurrences() == 0)
565 OutputFilename = "a.exe";
566
567 // If there is no suffix add an "exe" one.
568 sys::Path ExeFile( OutputFilename );
569 if (ExeFile.getSuffix() == "") {
570 ExeFile.appendSuffix("exe");
571 OutputFilename = ExeFile.str();
572 }
573 }
574 #endif
575
576 // Generate the bitcode for the optimized module.
577 // If -b wasn't specified, use the name specified
578 // with -o to construct BitcodeOutputFilename.
579 if (BitcodeOutputFilename.empty()) {
580 BitcodeOutputFilename = OutputFilename;
581 if (!LinkAsLibrary) BitcodeOutputFilename += ".bc";
582 }
583
595 // Generate the bitcode output.
584596 GenerateBitcode(Composite.get(), BitcodeOutputFilename);
585597
586598 // If we are not linking a library, generate either a native executable
633645 sys::Path AssemblyFile ( OutputFilename);
634646 AssemblyFile.appendSuffix("s");
635647
636 // Mark the output files for removal if we get an interrupt.
648 // Mark the output files for removal.
649 FileRemover AssemblyFileRemover(AssemblyFile);
637650 sys::RemoveFileOnSignal(AssemblyFile);
638 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
639651
640652 // Determine the locations of the llc and gcc programs.
641653 sys::Path llc = FindExecutable("llc", argv[0],
656668 if (0 != GenerateNative(OutputFilename, AssemblyFile.str(),
657669 NativeLinkItems, gcc, envp, ErrMsg))
658670 PrintAndExit(ErrMsg, Composite.get());
659
660 // Remove the assembly language file.
661 AssemblyFile.eraseFromDisk();
662671 } else if (NativeCBE) {
663672 sys::Path CFile (OutputFilename);
664673 CFile.appendSuffix("cbe.c");
665674
666 // Mark the output files for removal if we get an interrupt.
675 // Mark the output files for removal.
676 FileRemover CFileRemover(CFile);
667677 sys::RemoveFileOnSignal(CFile);
668 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
669678
670679 // Determine the locations of the llc and gcc programs.
671680 sys::Path llc = FindExecutable("llc", argv[0],
685694 if (GenerateNative(OutputFilename, CFile.str(),
686695 NativeLinkItems, gcc, envp, ErrMsg))
687696 PrintAndExit(ErrMsg, Composite.get());
688
689 // Remove the assembly language file.
690 CFile.eraseFromDisk();
691
692697 } else {
693698 EmitShellScript(argv, Composite.get());
694699 }
706711 PrintAndExit(ErrMsg, Composite.get());
707712 }
708713
714 // Operations which may fail are now complete.
715 BitcodeOutputRemover.releaseFile();
716 if (!LinkAsLibrary)
717 OutputRemover.releaseFile();
718
709719 // Graceful exit
710720 return 0;
711721 }