Driver: Sketch FreeBSD tool chain.
 - Patch by Ed Schouten!


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68061 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp
index 3f1a72d..a51e4bd 100644
--- a/lib/Driver/Driver.cpp
+++ b/lib/Driver/Driver.cpp
@@ -999,6 +999,9 @@
   if (memcmp(&OS[0], "darwin", 6) == 0)
     return createDarwinHostInfo(*this, Arch.c_str(), Platform.c_str(), 
                                 OS.c_str());
+  if (memcmp(&OS[0], "freebsd", 7) == 0)
+    return createFreeBSDHostInfo(*this, Arch.c_str(), Platform.c_str(), 
+                                 OS.c_str());
     
   return createUnknownHostInfo(*this, Arch.c_str(), Platform.c_str(), 
                                OS.c_str());
diff --git a/lib/Driver/HostInfo.cpp b/lib/Driver/HostInfo.cpp
index c02267f..07aa286 100644
--- a/lib/Driver/HostInfo.cpp
+++ b/lib/Driver/HostInfo.cpp
@@ -205,6 +205,68 @@
   return TC;
 }
 
+// FreeBSD Host Info
+
+/// FreeBSDHostInfo -  Similar to UnknownHostInfo, but doesn't depend on
+class FreeBSDHostInfo : public HostInfo {
+  /// Cache of tool chains we have created.
+  mutable llvm::StringMap<ToolChain*> ToolChains;
+
+public:
+  FreeBSDHostInfo(const Driver &D, const char *Arch, 
+                  const char *Platform, const char *OS);
+  ~FreeBSDHostInfo();
+
+  virtual bool useDriverDriver() const;
+
+  virtual types::ID lookupTypeForExtension(const char *Ext) const {
+    return types::lookupTypeForExtension(Ext);
+  }
+
+  virtual ToolChain *getToolChain(const ArgList &Args, 
+                                  const char *ArchName) const;
+};
+
+FreeBSDHostInfo::FreeBSDHostInfo(const Driver &D, const char *Arch, 
+                                 const char *Platform, const char *OS) 
+  : HostInfo(D, Arch, Platform, OS) {
+}
+
+FreeBSDHostInfo::~FreeBSDHostInfo() {
+  for (llvm::StringMap<ToolChain*>::iterator
+         it = ToolChains.begin(), ie = ToolChains.end(); it != ie; ++it)
+    delete it->second;
+}
+
+bool FreeBSDHostInfo::useDriverDriver() const { 
+  return false;
+}
+
+ToolChain *FreeBSDHostInfo::getToolChain(const ArgList &Args, 
+                                         const char *ArchName) const {
+  bool Lib32 = false;
+
+  assert(!ArchName && 
+         "Unexpected arch name on platform without driver driver support.");
+  
+  // On x86_64 we need to be able to compile 32-bits binaries as well.
+  // Compiling 64-bit binaries on i386 is not supported. We don't have a
+  // lib64.
+  ArchName = getArchName().c_str();
+  if (Args.hasArg(options::OPT_m32) && getArchName() == "x86_64") {
+    ArchName = "i386";
+    Lib32 = true;
+  } 
+  
+  ToolChain *&TC = ToolChains[ArchName];
+  if (!TC)
+    TC = new toolchains::FreeBSD(*this, ArchName, 
+                                 getPlatformName().c_str(), 
+                                 getOSName().c_str(), Lib32);
+
+  return TC;
+}
+
 }
 
 const HostInfo *clang::driver::createDarwinHostInfo(const Driver &D,
@@ -214,6 +276,13 @@
   return new DarwinHostInfo(D, Arch, Platform, OS);
 }
 
+const HostInfo *clang::driver::createFreeBSDHostInfo(const Driver &D,
+                                                     const char *Arch, 
+                                                     const char *Platform, 
+                                                     const char *OS) {
+  return new FreeBSDHostInfo(D, Arch, Platform, OS);
+}
+
 const HostInfo *clang::driver::createUnknownHostInfo(const Driver &D,
                                                      const char *Arch, 
                                                      const char *Platform, 
diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp
index 744882a..6d51273 100644
--- a/lib/Driver/ToolChains.cpp
+++ b/lib/Driver/ToolChains.cpp
@@ -367,3 +367,32 @@
 DerivedArgList *Generic_GCC::TranslateArgs(InputArgList &Args) const {
   return new DerivedArgList(Args, true);
 }
+
+/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
+
+FreeBSD::FreeBSD(const HostInfo &Host, const char *Arch, 
+                 const char *Platform, const char *OS, bool Lib32)
+  : Generic_GCC(Host, Arch, Platform, OS) {
+  if (Lib32)
+    getFilePaths().push_back(getHost().getDriver().Dir + "/../lib32");
+  else
+    getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
+}
+
+Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
+  Action::ActionClass Key;
+  if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getArchName()))
+    Key = Action::AnalyzeJobClass;
+  else
+    Key = JA.getKind();
+
+  Tool *&T = Tools[Key];
+  if (!T) {
+    switch (Key) {
+    default:
+      T = &Generic_GCC::SelectTool(C, JA);
+    }
+  }
+
+  return *T;
+}
diff --git a/lib/Driver/ToolChains.h b/lib/Driver/ToolChains.h
index 5e7bdb5..bbb27dc 100644
--- a/lib/Driver/ToolChains.h
+++ b/lib/Driver/ToolChains.h
@@ -26,6 +26,7 @@
   /// all subcommands; this relies on gcc translating the majority of
   /// command line options.
 class VISIBILITY_HIDDEN Generic_GCC : public ToolChain {
+protected:
   mutable llvm::DenseMap<unsigned, Tool*> Tools;
 
 public:
@@ -107,6 +108,14 @@
   virtual const char *GetDefaultRelocationModel() const { return "pic"; }
 };
 
+class VISIBILITY_HIDDEN FreeBSD : public Generic_GCC {
+public:
+  FreeBSD(const HostInfo &Host, const char *Arch, const char *Platform, 
+          const char *OS, bool Lib32);
+
+  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA) const;
+};
+
 } // end namespace toolchains
 } // end namespace driver
 } // end namespace clang