llvm-mc: Add -triple, and start fetching the target asm printer.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76257 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/llvm-mc/CMakeLists.txt b/tools/llvm-mc/CMakeLists.txt
index b21a4b1..d2e9e71 100644
--- a/tools/llvm-mc/CMakeLists.txt
+++ b/tools/llvm-mc/CMakeLists.txt
@@ -1,4 +1,4 @@
-set(LLVM_LINK_COMPONENTS support MC)
+set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} support MC)
 
 add_llvm_tool(llvm-mc
   llvm-mc.cpp
diff --git a/tools/llvm-mc/Makefile b/tools/llvm-mc/Makefile
index 3c327da..ab9c5b6 100644
--- a/tools/llvm-mc/Makefile
+++ b/tools/llvm-mc/Makefile
@@ -9,9 +9,15 @@
 
 LEVEL = ../..
 TOOLNAME = llvm-mc
-LINK_COMPONENTS := support MC
 
 # This tool has no plugins, optimize startup time.
 TOOL_NO_EXPORTS = 1
 
-include $(LEVEL)/Makefile.common
+# Include this here so we can get the configuration of the targets
+# that have been configured for construction. We have to do this 
+# early so we can set up LINK_COMPONENTS before including Makefile.rules
+include $(LEVEL)/Makefile.config
+
+LINK_COMPONENTS := $(TARGETS_TO_BUILD) MC support
+
+include $(LLVM_SRC_ROOT)/Makefile.rules
diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp
index b52edd1..c7f3996 100644
--- a/tools/llvm-mc/llvm-mc.cpp
+++ b/tools/llvm-mc/llvm-mc.cpp
@@ -22,6 +22,8 @@
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/System/Signals.h"
+#include "llvm/Target/TargetRegistry.h"
+#include "llvm/Target/TargetSelect.h"
 #include "AsmParser.h"
 using namespace llvm;
 
@@ -36,6 +38,11 @@
 IncludeDirs("I", cl::desc("Directory of include files"),
             cl::value_desc("directory"), cl::Prefix);
 
+static cl::opt<std::string>
+Triple("triple", cl::desc("Target triple to assemble for,"
+                          "see -version for available targets"),
+       cl::init(""));
+
 enum ActionType {
   AC_AsLex,
   AC_Assemble
@@ -137,6 +144,23 @@
 }
 
 static int AssembleInput(const char *ProgName) {
+  // Get the target specific parser.
+  std::string Error;
+  const Target *TheTarget =
+    TargetRegistry::getClosestStaticTargetForTriple(Triple, Error);
+  if (TheTarget == 0) {
+    errs() << ProgName << ": error: unable to get target for '" << Triple
+           << "', see --version and --triple.\n";
+    return 1;
+  }
+
+  TargetAsmParser *TAP = TheTarget->createAsmParser();
+  if (!TAP) {
+    errs() << ProgName 
+           << ": error: this target does not support assembly parsing.\n";
+    return 1;    
+  }
+
   std::string ErrorMessage;
   MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
                                                       &ErrorMessage);
@@ -174,6 +198,11 @@
   sys::PrintStackTraceOnErrorSignal();
   PrettyStackTraceProgram X(argc, argv);
   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
+
+  // Initialize targets and assembly parsers.
+  llvm::InitializeAllTargetInfos();
+  llvm::InitializeAllAsmParsers();
+  
   cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
 
   switch (Action) {