[llvm-commits] CVS: llvm/tools/gccld/gccld.cpp

Reid Spencer reid at x10sys.com
Sun Nov 14 14:17:40 PST 2004



Changes in directory llvm/tools/gccld:

gccld.cpp updated: 1.78 -> 1.79
---
Log message:

Provide exception handling

---
Diffs of the changes:  (+123 -109)

Index: llvm/tools/gccld/gccld.cpp
diff -u llvm/tools/gccld/gccld.cpp:1.78 llvm/tools/gccld/gccld.cpp:1.79
--- llvm/tools/gccld/gccld.cpp:1.78	Wed Sep  1 17:55:37 2004
+++ llvm/tools/gccld/gccld.cpp	Sun Nov 14 16:17:03 2004
@@ -157,120 +157,134 @@
   cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
   sys::PrintStackTraceOnErrorSignal();
 
-  std::string ModuleID("gccld-output");
-  std::auto_ptr<Module> Composite(new Module(ModuleID));
+  int exitCode = 0;
 
-  // We always look first in the current directory when searching for libraries.
-  LibPaths.insert(LibPaths.begin(), ".");
-
-  // If the user specified an extra search path in their environment, respect
-  // it.
-  if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
-    LibPaths.push_back(SearchPath);
-
-  // Remove any consecutive duplicates of the same library...
-  Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
-                  Libraries.end());
-
-  // Link in all of the files
-  if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose))
-    return 1; // Error already printed
-
-  if (!LinkAsLibrary)
-    LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths,
-                  Verbose, Native);
-
-  // Link in all of the libraries next...
-
-  // Create the output file.
-  std::string RealBytecodeOutput = OutputFilename;
-  if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
-  std::ofstream Out(RealBytecodeOutput.c_str());
-  if (!Out.good())
-    return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
-                                   "' for writing!");
-
-  // Ensure that the bytecode file gets removed from the disk if we get a
-  // SIGINT signal.
-  sys::RemoveFileOnSignal(RealBytecodeOutput);
+  try {
+    std::string ModuleID("gccld-output");
+    std::auto_ptr<Module> Composite(new Module(ModuleID));
+
+    // We always look first in the current directory when searching for libraries.
+    LibPaths.insert(LibPaths.begin(), ".");
+
+    // If the user specified an extra search path in their environment, respect
+    // it.
+    if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
+      LibPaths.push_back(SearchPath);
+
+    // Remove any consecutive duplicates of the same library...
+    Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
+                    Libraries.end());
+
+    // Link in all of the files
+    if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose))
+      return 1; // Error already printed
+
+    if (!LinkAsLibrary)
+      LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths,
+                    Verbose, Native);
+
+    // Link in all of the libraries next...
+
+    // Create the output file.
+    std::string RealBytecodeOutput = OutputFilename;
+    if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
+    std::ofstream Out(RealBytecodeOutput.c_str());
+    if (!Out.good())
+      return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
+                                     "' for writing!");
+
+    // Ensure that the bytecode file gets removed from the disk if we get a
+    // SIGINT signal.
+    sys::RemoveFileOnSignal(sys::Path(RealBytecodeOutput));
+
+    // Generate the bytecode file.
+    if (GenerateBytecode(Composite.get(), Strip, !NoInternalize, &Out)) {
+      Out.close();
+      return PrintAndReturn(argv[0], "error generating bytecode");
+    }
 
-  // Generate the bytecode file.
-  if (GenerateBytecode(Composite.get(), Strip, !NoInternalize, &Out)) {
+    // Close the bytecode file.
     Out.close();
-    return PrintAndReturn(argv[0], "error generating bytecode");
-  }
-
-  // Close the bytecode file.
-  Out.close();
-
-  // If we are not linking a library, generate either a native executable
-  // or a JIT shell script, depending upon what the user wants.
-  if (!LinkAsLibrary) {
-    // If the user wants to generate a native executable, compile it from the
-    // bytecode file.
-    //
-    // Otherwise, create a script that will run the bytecode through the JIT.
-    if (Native) {
-      // Name of the Assembly Language output file
-      std::string AssemblyFile = OutputFilename + ".s";
-
-      // Mark the output files for removal if we get an interrupt.
-      sys::RemoveFileOnSignal(AssemblyFile);
-      sys::RemoveFileOnSignal(OutputFilename);
-
-      // Determine the locations of the llc and gcc programs.
-      std::string llc = FindExecutable("llc", argv[0]);
-      std::string gcc = FindExecutable("gcc", argv[0]);
-      if (llc.empty())
-        return PrintAndReturn(argv[0], "Failed to find llc");
-
-      if (gcc.empty())
-        return PrintAndReturn(argv[0], "Failed to find gcc");
-
-      // Generate an assembly language file for the bytecode.
-      if (Verbose) std::cout << "Generating Assembly Code\n";
-      GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp);
-      if (Verbose) std::cout << "Generating Native Code\n";
-      GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths,
-                     gcc, envp);
-
-      // Remove the assembly language file.
-      removeFile (AssemblyFile);
-    } else if (NativeCBE) {
-      std::string CFile = OutputFilename + ".cbe.c";
-
-      // Mark the output files for removal if we get an interrupt.
-      sys::RemoveFileOnSignal(CFile);
-      sys::RemoveFileOnSignal(OutputFilename);
-
-      // Determine the locations of the llc and gcc programs.
-      std::string llc = FindExecutable("llc", argv[0]);
-      std::string gcc = FindExecutable("gcc", argv[0]);
-      if (llc.empty())
-        return PrintAndReturn(argv[0], "Failed to find llc");
-      if (gcc.empty())
-        return PrintAndReturn(argv[0], "Failed to find gcc");
-
-      // Generate an assembly language file for the bytecode.
-      if (Verbose) std::cout << "Generating Assembly Code\n";
-      GenerateCFile(CFile, RealBytecodeOutput, llc, envp);
-      if (Verbose) std::cout << "Generating Native Code\n";
-      GenerateNative(OutputFilename, CFile, Libraries, LibPaths, gcc, envp);
-
-      // Remove the assembly language file.
-      removeFile(CFile);
 
-    } else {
-      EmitShellScript(argv);
+    // If we are not linking a library, generate either a native executable
+    // or a JIT shell script, depending upon what the user wants.
+    if (!LinkAsLibrary) {
+      // If the user wants to generate a native executable, compile it from the
+      // bytecode file.
+      //
+      // Otherwise, create a script that will run the bytecode through the JIT.
+      if (Native) {
+        // Name of the Assembly Language output file
+        std::string AssemblyFile = OutputFilename + ".s";
+
+        // Mark the output files for removal if we get an interrupt.
+        sys::RemoveFileOnSignal(sys::Path(AssemblyFile));
+        sys::RemoveFileOnSignal(sys::Path(OutputFilename));
+
+        // Determine the locations of the llc and gcc programs.
+        std::string llc = FindExecutable("llc", argv[0]);
+        std::string gcc = FindExecutable("gcc", argv[0]);
+        if (llc.empty())
+          return PrintAndReturn(argv[0], "Failed to find llc");
+
+        if (gcc.empty())
+          return PrintAndReturn(argv[0], "Failed to find gcc");
+
+        // Generate an assembly language file for the bytecode.
+        if (Verbose) std::cout << "Generating Assembly Code\n";
+        GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp);
+        if (Verbose) std::cout << "Generating Native Code\n";
+        GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths,
+                       gcc, envp);
+
+        // Remove the assembly language file.
+        removeFile (AssemblyFile);
+      } else if (NativeCBE) {
+        std::string CFile = OutputFilename + ".cbe.c";
+
+        // Mark the output files for removal if we get an interrupt.
+        sys::RemoveFileOnSignal(sys::Path(CFile));
+        sys::RemoveFileOnSignal(sys::Path(OutputFilename));
+
+        // Determine the locations of the llc and gcc programs.
+        std::string llc = FindExecutable("llc", argv[0]);
+        std::string gcc = FindExecutable("gcc", argv[0]);
+        if (llc.empty())
+          return PrintAndReturn(argv[0], "Failed to find llc");
+        if (gcc.empty())
+          return PrintAndReturn(argv[0], "Failed to find gcc");
+
+        // Generate an assembly language file for the bytecode.
+        if (Verbose) std::cout << "Generating Assembly Code\n";
+        GenerateCFile(CFile, RealBytecodeOutput, llc, envp);
+        if (Verbose) std::cout << "Generating Native Code\n";
+        GenerateNative(OutputFilename, CFile, Libraries, LibPaths, gcc, envp);
+
+        // Remove the assembly language file.
+        removeFile(CFile);
+
+      } else {
+        EmitShellScript(argv);
+      }
+    
+      // Make the script executable...
+      MakeFileExecutable(OutputFilename);
+
+      // Make the bytecode file readable and directly executable in LLEE as well
+      MakeFileExecutable(RealBytecodeOutput);
+      MakeFileReadable(RealBytecodeOutput);
     }
-  
-    // Make the script executable...
-    MakeFileExecutable(OutputFilename);
-
-    // Make the bytecode file readable and directly executable in LLEE as well
-    MakeFileExecutable(RealBytecodeOutput);
-    MakeFileReadable(RealBytecodeOutput);
+  } catch (const char*msg) {
+    std::cerr << argv[0] << ": " << msg << "\n";
+    exitCode = 1;
+  } catch (const std::string& msg) {
+    std::cerr << argv[0] << ": " << msg << "\n";
+    exitCode = 2;
+  } catch (...) {
+    // This really shouldn't happen, but just in case ....
+    std::cerr << argv[0] << ": An nexpected unknown exception occurred.\n";
+    exitCode = 3;
   }
 
-  return 0;
+  return exitCode;
 }






More information about the llvm-commits mailing list