Ensure that the arguments passed to sys::Program::ExecuteAndWait include
[oota-llvm.git] / tools / llvmc / llvmc.cpp
index 8c5102a0c4a23ca8ef737cc8826480a359f1d0e8..75ea338d5c2304e02f28861ba0a732230f5fabeb 100644 (file)
@@ -18,7 +18,7 @@
 #include "Configuration.h"
 #include "llvm/Pass.h"
 #include "llvm/System/Signals.h"
-#include "Support/CommandLine.h"
+#include "llvm/Support/CommandLine.h"
 #include <iostream>
 
 using namespace llvm;
@@ -27,7 +27,7 @@ namespace {
 //===------------------------------------------------------------------------===
 //===          PHASE OPTIONS
 //===------------------------------------------------------------------------===
-cl::opt<CompilerDriver::Phases> FinalPhase(
+cl::opt<CompilerDriver::Phases> FinalPhase(cl::Optional,
   cl::desc("Choose final phase of compilation:"), 
   cl::init(CompilerDriver::LINKING),
   cl::values(
@@ -46,7 +46,7 @@ cl::opt<CompilerDriver::Phases> FinalPhase(
 //===------------------------------------------------------------------------===
 //===          OPTIMIZATION OPTIONS
 //===------------------------------------------------------------------------===
-cl::opt<CompilerDriver::OptimizationLevels> OptLevel(
+cl::opt<CompilerDriver::OptimizationLevels> OptLevel(cl::ZeroOrMore,
   cl::desc("Choose level of optimization to apply:"),
   cl::init(CompilerDriver::OPT_FAST_COMPILE),
   cl::values(
@@ -74,6 +74,9 @@ cl::list<std::string> PreprocessorToolOpts("Tpre", cl::ZeroOrMore,
   cl::desc("Pass specific options to the pre-processor"), 
   cl::value_desc("option"));
 
+cl::alias PreprocessorToolOptsAlias("Wp,", cl::ZeroOrMore,
+  cl::desc("Alias for -Tpre"), cl::aliasopt(PreprocessorToolOpts));
+
 cl::list<std::string> TranslatorToolOpts("Ttrn", cl::ZeroOrMore,
   cl::desc("Pass specific options to the assembler"),
   cl::value_desc("option"));
@@ -82,6 +85,9 @@ cl::list<std::string> AssemblerToolOpts("Tasm", cl::ZeroOrMore,
   cl::desc("Pass specific options to the assembler"),
   cl::value_desc("option"));
 
+cl::alias AssemblerToolOptsAlias("Wa,", cl::ZeroOrMore,
+  cl::desc("Alias for -Tasm"), cl::aliasopt(AssemblerToolOpts));
+
 cl::list<std::string> OptimizerToolOpts("Topt", cl::ZeroOrMore,
   cl::desc("Pass specific options to the optimizer"),
   cl::value_desc("option"));
@@ -90,29 +96,65 @@ cl::list<std::string> LinkerToolOpts("Tlnk", cl::ZeroOrMore,
   cl::desc("Pass specific options to the linker"),
   cl::value_desc("option"));
 
+cl::alias LinkerToolOptsAlias("Wl,", cl::ZeroOrMore,
+  cl::desc("Alias for -Tlnk"), cl::aliasopt(LinkerToolOpts));
+
+cl::list<std::string> fOpts("f", cl::ZeroOrMore, cl::Prefix,
+  cl::desc("Pass through -f options to compiler tools"),
+  cl::value_desc("option"));
+
+cl::list<std::string> MOpts("M", cl::ZeroOrMore, cl::Prefix,
+  cl::desc("Pass through -M options to compiler tools"),
+  cl::value_desc("option"));
+
+cl::list<std::string> WOpts("W", cl::ZeroOrMore, cl::Prefix,
+  cl::desc("Pass through -W options to compiler tools"),
+  cl::value_desc("option"));
+
+cl::list<std::string> BOpt("B", cl::ZeroOrMore, cl::Prefix, 
+  cl::desc("Specify path to find llvmc sub-tools"),
+  cl::value_desc("dir"));
+
 //===------------------------------------------------------------------------===
 //===          INPUT OPTIONS
 //===------------------------------------------------------------------------===
 
 cl::list<std::string> LibPaths("L", cl::Prefix,
-  cl::desc("Specify a library search path"), cl::value_desc("directory"));
-                                                                                                                                            
+  cl::desc("Specify a library search path"), cl::value_desc("dir"));
+
 cl::list<std::string> Libraries("l", cl::Prefix,
-  cl::desc("Specify libraries to link to"), cl::value_desc("library prefix"));
+  cl::desc("Specify base name of libraries to link to"), cl::value_desc("lib"));
 
+cl::list<std::string> Includes("I", cl::Prefix,
+  cl::desc("Specify location to search for included source"), 
+  cl::value_desc("dir"));
+
+cl::list<std::string> Defines("D", cl::Prefix,
+  cl::desc("Specify a pre-processor symbol to define"),
+  cl::value_desc("symbol"));
 
 //===------------------------------------------------------------------------===
 //===          OUTPUT OPTIONS
 //===------------------------------------------------------------------------===
 
 cl::opt<std::string> OutputFilename("o", 
-  cl::desc("Override output filename"), cl::value_desc("filename"));
+  cl::desc("Override output filename"), cl::value_desc("file"));
 
 cl::opt<std::string> OutputMachine("m", cl::Prefix,
   cl::desc("Specify a target machine"), cl::value_desc("machine"));
-                                                                                                                                            
+
 cl::opt<bool> Native("native", cl::init(false),
-  cl::desc("Generative native object and executables instead of bytecode"));
+  cl::desc("Generative native code instead of bytecode"));
+
+cl::opt<bool> DebugOutput("g", cl::init(false),
+  cl::desc("Generate objects that include debug symbols"));
+
+cl::opt<bool> StripOutput("strip", cl::init(false),
+  cl::desc("Strip all symbols from linked output file"));
+
+cl::opt<std::string> PrintFileName("print-fname", cl::Optional,
+  cl::value_desc("file"),
+  cl::desc("Print the full path for the option's value"));
 
 //===------------------------------------------------------------------------===
 //===          INFORMATION OPTIONS
@@ -147,8 +189,8 @@ cl::opt<bool> ShowStats("stats", cl::Optional, cl::init(false),
 //===------------------------------------------------------------------------===
 
 static cl::opt<std::string> ConfigDir("config-dir", cl::Optional,
-  cl::desc("Specify configuration directory to override defaults"),
-  cl::value_desc("directory"));
+  cl::desc("Specify configuration directory to override defaults"),
+  cl::value_desc("dir"));
 
 static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional,
   cl::desc("Emit raw, unoptimized code"));
@@ -156,14 +198,14 @@ static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional,
 static cl::opt<bool> PipeCommands("pipe", cl::Optional,
   cl::desc("Invoke sub-commands by linking input/output with pipes"));
 
-static cl::opt<bool> KeepTemporaries("keep-temps", cl::Optional,
-  cl::desc("Don't delete the temporary files created during compilation"));
+static cl::opt<bool> KeepTemps("keep-temps", cl::Optional,
+  cl::desc("Don't delete temporary files created by llvmc"));
 
 //===------------------------------------------------------------------------===
 //===          POSITIONAL OPTIONS
 //===------------------------------------------------------------------------===
 
-static cl::list<std::string> Files(cl::Positional, cl::OneOrMore,
+static cl::list<std::string> Files(cl::Positional, cl::ZeroOrMore,
   cl::desc("[Sources/objects/libraries]"));
 
 static cl::list<std::string> Languages("x", cl::ZeroOrMore,
@@ -196,37 +238,46 @@ const std::string GetFileType(const std::string& fname, unsigned pos ) {
 
 } // end anonymous namespace
 
+void handleTerminatingOptions(CompilerDriver* CD) {
+  if (!PrintFileName.empty()) {
+    sys::Path path = CD->GetPathForLinkageItem(PrintFileName,false);
+    std::string p = path.toString();
+    if (p.empty())
+      std::cout << "Can't locate '" << PrintFileName << "'.\n";
+    else
+      std::cout << p << "\n";
+    exit(0);
+  }
+}
 
 /// @brief The main program for llvmc
 int main(int argc, char **argv) {
+  // Make sure we print stack trace if we get bad signals
+  sys::PrintStackTraceOnErrorSignal();
+
   try {
-    // Make sure we print stack trace if we get bad signals
-    PrintStackTraceOnErrorSignal();
 
     // Parse the command line options
     cl::ParseCommandLineOptions(argc, argv, 
-      " LLVM Compilation Driver (llvmc)\n\n"
+      " LLVM Compiler Driver (llvmc)\n\n"
       "  This program provides easy invocation of the LLVM tool set\n"
-      "  and source language compiler tools.\n"
+      "  and other compiler tools.\n"
     );
 
     // Deal with unimplemented options.
     if (PipeCommands)
-      throw "Not implemented yet: -pipe";
+      throw std::string("Not implemented yet: -pipe");
 
     if (OutputFilename.empty())
       if (OptLevel == CompilerDriver::LINKING)
         OutputFilename = "a.out";
-      else
-        throw "An output file must be specified. Please use the -o option";
-
 
     // Construct the ConfigDataProvider object
     LLVMC_ConfigDataProvider Provider;
-    Provider.setConfigDir(ConfigDir);
+    Provider.setConfigDir(sys::Path(ConfigDir));
 
     // Construct the CompilerDriver object
-    CompilerDriver CD(Provider);
+    CompilerDriver* CD = CompilerDriver::Get(Provider);
 
     // If the LLVM_LIB_SEARCH_PATH environment variable is
     // set, append it to the list of places to search for libraries
@@ -234,30 +285,46 @@ int main(int argc, char **argv) {
     if (!srchPath.empty())
       LibPaths.push_back(srchPath);
 
-    // Configure the driver based on options
-    CD.setVerbose(Verbose);
-    CD.setDebug(Debug);
-    CD.setDryRun(DryRun);
-    CD.setFinalPhase(FinalPhase);
-    CD.setOptimization(OptLevel);
-    CD.setOutputMachine(OutputMachine);
-    CD.setEmitNativeCode(Native);
-    CD.setEmitRawCode(EmitRawCode);
-    CD.setTimeActions(TimeActions);
-    CD.setTimePasses(TimePassesIsEnabled);
-    CD.setShowStats(ShowStats);
-    CD.setKeepTemporaries(KeepTemporaries);
-    CD.setLibraryPaths(LibPaths);
+    // Set the driver flags based on command line options
+    unsigned flags = 0;
+    if (Verbose)        flags |= CompilerDriver::VERBOSE_FLAG;
+    if (Debug)          flags |= CompilerDriver::DEBUG_FLAG;
+    if (DryRun)         flags |= CompilerDriver::DRY_RUN_FLAG;
+    if (Native)         flags |= CompilerDriver::EMIT_NATIVE_FLAG;
+    if (EmitRawCode)    flags |= CompilerDriver::EMIT_RAW_FLAG;
+    if (KeepTemps)      flags |= CompilerDriver::KEEP_TEMPS_FLAG;
+    if (ShowStats)      flags |= CompilerDriver::SHOW_STATS_FLAG;
+    if (TimeActions)    flags |= CompilerDriver::TIME_ACTIONS_FLAG;
+    if (TimePassesIsEnabled) flags |= CompilerDriver::TIME_PASSES_FLAG;
+    if (StripOutput)    flags |= CompilerDriver::STRIP_OUTPUT_FLAG;
+    CD->setDriverFlags(flags);
+
+    // Specify requred parameters
+    CD->setFinalPhase(FinalPhase);
+    CD->setOptimization(OptLevel);
+    CD->setOutputMachine(OutputMachine);
+    CD->setIncludePaths(Includes);
+    CD->setSymbolDefines(Defines);
+    CD->setLibraryPaths(LibPaths);
+    CD->setfPassThrough(fOpts);
+    CD->setMPassThrough(MOpts);
+    CD->setWPassThrough(WOpts);
+
+    // Provide additional tool arguments
     if (!PreprocessorToolOpts.empty())
-        CD.setPhaseArgs(CompilerDriver::PREPROCESSING, PreprocessorToolOpts);
+        CD->setPhaseArgs(CompilerDriver::PREPROCESSING, PreprocessorToolOpts);
     if (!TranslatorToolOpts.empty())
-        CD.setPhaseArgs(CompilerDriver::TRANSLATION, TranslatorToolOpts);
+        CD->setPhaseArgs(CompilerDriver::TRANSLATION, TranslatorToolOpts);
     if (!OptimizerToolOpts.empty())
-        CD.setPhaseArgs(CompilerDriver::OPTIMIZATION, OptimizerToolOpts);
+        CD->setPhaseArgs(CompilerDriver::OPTIMIZATION, OptimizerToolOpts);
     if (!AssemblerToolOpts.empty())
-        CD.setPhaseArgs(CompilerDriver::ASSEMBLY,AssemblerToolOpts);
+        CD->setPhaseArgs(CompilerDriver::ASSEMBLY,AssemblerToolOpts);
     if (!LinkerToolOpts.empty())
-        CD.setPhaseArgs(CompilerDriver::LINKING, LinkerToolOpts);
+        CD->setPhaseArgs(CompilerDriver::LINKING, LinkerToolOpts);
+
+    // Check for options that cause us to terminate before any significant work
+    // is done.
+    handleTerminatingOptions(CD);
 
     // Prepare the list of files to be compiled by the CompilerDriver.
     CompilerDriver::InputList InpList;
@@ -288,17 +355,18 @@ int main(int argc, char **argv) {
     }
 
     // Tell the driver to do its thing
-    int result = CD.execute(InpList,OutputFilename);
+    int result = CD->execute(InpList,sys::Path(OutputFilename));
     if (result != 0) {
-      throw "Error executing actions. Terminated.\n";
+      throw std::string("Error executing actions. Terminated.");
       return result;
     }
 
     // All is good, return success
     return 0;
-  } catch (std::string& msg) {
+  } catch (const std::string& msg) {
     std::cerr << argv[0] << ": " << msg << "\n";
   } catch (...) {
     std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
   }
+  return 1;
 }