From e0361784072b954f39beaab5c4ccc3614c32e17e Mon Sep 17 00:00:00 2001
From: Calvin Hill <calvin@hakobaito.co.uk>
Date: Tue, 15 Aug 2017 21:55:36 +0100
Subject: swift-clang: reapply Haiku toolchain patches.


diff --git a/CMakeLists.txt b/CMakeLists.txt
index 04ab7b7..8d1bcf1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -390,6 +390,10 @@ endif()
 if(CLANG_ENABLE_ARCMT)
   set(CLANG_ENABLE_OBJC_REWRITER ON)
 endif()
+if(DEFINED HAIKU_HYBRID_SECONDARY)
+  add_definitions(-DHAIKU_HYBRID_SECONDARY=${HAIKU_HYBRID_SECONDARY})
+endif()
+
 
 # Clang version information
 set(CLANG_EXECUTABLE_VERSION
diff --git a/lib/Basic/Targets.cpp b/lib/Basic/Targets.cpp
index b0dc0d2..2e3157e 100644
--- a/lib/Basic/Targets.cpp
+++ b/lib/Basic/Targets.cpp
@@ -436,8 +436,6 @@ public:
     this->IntPtrType = TargetInfo::SignedLong;
     this->PtrDiffType = TargetInfo::SignedLong;
     this->ProcessIDType = TargetInfo::SignedLong;
-    this->TLSSupported = false;
-
   }
 };
 
diff --git a/lib/Driver/ToolChains/Haiku.cpp b/lib/Driver/ToolChains/Haiku.cpp
index 284d269..bc33eea 100644
--- a/lib/Driver/ToolChains/Haiku.cpp
+++ b/lib/Driver/ToolChains/Haiku.cpp
@@ -19,7 +19,13 @@ using namespace llvm::opt;
 
 Haiku::Haiku(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
   : Generic_ELF(D, Triple, Args) {
-
+#ifdef HAIKU_HYBRID_SECONDARY
+  getProgramPaths().insert(getProgramPaths().begin(), getDriver().SysRoot
+                           + "/system/bin/" HAIKU_HYBRID_SECONDARY);
+  getFilePaths().clear();
+  getFilePaths().push_back(getDriver().SysRoot + "/system/lib/"
+                           HAIKU_HYBRID_SECONDARY);
+#endif
 }
 
 std::string Haiku::findLibCxxIncludePath() const {
@@ -28,6 +34,12 @@ std::string Haiku::findLibCxxIncludePath() const {
 
 void Haiku::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
                                      llvm::opt::ArgStringList &CC1Args) const {
+#ifdef HAIKU_HYBRID_SECONDARY
+  addLibStdCXXIncludePaths(getDriver().SysRoot, "/system/develop/headers"
+                     HAIKU_HYBRID_SECONDARY "/c++", getTriple().str(), "", "", "", 
+                     DriverArgs, CC1Args);
+#else
   addLibStdCXXIncludePaths(getDriver().SysRoot, "/system/develop/headers/c++",
                            getTriple().str(), "", "", "", DriverArgs, CC1Args);
+#endif
 }
diff --git a/lib/Driver/ToolChains/Haiku.h b/lib/Driver/ToolChains/Haiku.h
index 8b5b48e..ccd851f 100644
--- a/lib/Driver/ToolChains/Haiku.h
+++ b/lib/Driver/ToolChains/Haiku.h
@@ -23,9 +23,7 @@ public:
   Haiku(const Driver &D, const llvm::Triple &Triple,
           const llvm::opt::ArgList &Args);
 
-  bool isPIEDefault() const override {
-    return getTriple().getArch() == llvm::Triple::x86_64;
-  }
+  bool isPIEDefault() const override { return false; }
 
   std::string findLibCxxIncludePath() const override;
   void addLibStdCxxIncludePaths(
diff --git a/lib/Frontend/InitHeaderSearch.cpp b/lib/Frontend/InitHeaderSearch.cpp
index 1d7c8a0..e8ccd4e 100644
--- a/lib/Frontend/InitHeaderSearch.cpp
+++ b/lib/Frontend/InitHeaderSearch.cpp
@@ -234,7 +234,20 @@ void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
   if (HSOpts.UseBuiltinIncludes) {
     // Ignore the sys root, we *always* look for clang headers relative to
     // supplied path.
+#ifdef HAIKU_HYBRID_SECONDARY
+    // Remove version from foo/lib/clang/version
+    StringRef Ver = llvm::sys::path::filename(HSOpts.ResourceDir);
+    StringRef NoVer = llvm::sys::path::parent_path(HSOpts.ResourceDir);
+    // Remove clang from foo/lib/clang
+    StringRef Clang = llvm::sys::path::filename(NoVer);
+    SmallString<128> P = llvm::sys::path::parent_path(NoVer);
+
+    // Get foo/include/c++/v1
+    llvm::sys::path::append(P, HAIKU_HYBRID_SECONDARY, Clang, Ver);
+#else
     SmallString<128> P = StringRef(HSOpts.ResourceDir);
+#endif
+
     llvm::sys::path::append(P, "include");
     AddUnmappedPath(P, ExternCSystem, false);
   }
@@ -267,7 +280,12 @@ void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
   }
 
   case llvm::Triple::Haiku:
+#ifdef HAIKU_HYBRID_SECONDARY
+    AddPath("/boot/system/non-packaged/develop/headers/" HAIKU_HYBRID_SECONDARY,
+            System, false);
+#else
     AddPath("/boot/system/non-packaged/develop/headers", System, false);
+#endif
     AddPath("/boot/system/develop/headers/os", System, false);
     AddPath("/boot/system/develop/headers/os/app", System, false);
     AddPath("/boot/system/develop/headers/os/arch", System, false);
@@ -299,6 +317,13 @@ void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
     AddPath("/boot/system/develop/headers/bsd", System, false);
     AddPath("/boot/system/develop/headers/glibc", System, false);
     AddPath("/boot/system/develop/headers/posix", System, false);
+#ifdef HAIKU_HYBRID_SECONDARY
+    AddPath("/boot/system/develop/headers/" HAIKU_HYBRID_SECONDARY, System, false);
+    AddPath("/boot/system/develop/headers/" HAIKU_HYBRID_SECONDARY "/os", System,
+            false);
+    AddPath("/boot/system/develop/headers/" HAIKU_HYBRID_SECONDARY "/os/opengl",
+            System, false);
+#endif
     AddPath("/boot/system/develop/headers",  System, false);
     break;
   case llvm::Triple::RTEMS:
diff --git a/test/Driver/haiku.c b/test/Driver/haiku.c
new file mode 100644
index 0000000..9591739
--- /dev/null
+++ b/test/Driver/haiku.c
@@ -0,0 +1,12 @@
+// RUN: %clang -no-canonical-prefixes -target x86_64-unknown-haiku %s -### 2> %t.log
+// RUN: FileCheck --check-prefix=CHECK-X86_64 -input-file %t.log %s
+
+// CHECK-X86_64: clang{{.*}}" "-cc1" "-triple" "x86_64-unknown-haiku"
+// CHECK-X86_64: gcc{{.*}}" "-o" "a.out" "{{.*}}.o"
+
+// RUN: %clang -no-canonical-prefixes -target i586-pc-haiku %s -### 2> %t.log
+// RUN: FileCheck --check-prefix=CHECK-X86 -input-file %t.log %s
+
+// CHECK-X86: clang{{.*}}" "-cc1" "-triple" "i586-pc-haiku"
+// CHECK-X86: gcc{{.*}}" "-o" "a.out" "{{.*}}.o"
+
-- 
2.14.2


From 8b814e4c5bac68c07990d5e65279395dd76a1102 Mon Sep 17 00:00:00 2001
From: Jerome Duval <jerome.duval@gmail.com>
Date: Sat, 11 Nov 2017 14:02:31 +0100
Subject: default to PIC


diff --git a/lib/Driver/ToolChains/Haiku.h b/lib/Driver/ToolChains/Haiku.h
index ccd851f..df98cd6 100644
--- a/lib/Driver/ToolChains/Haiku.h
+++ b/lib/Driver/ToolChains/Haiku.h
@@ -25,6 +25,8 @@ public:
 
   bool isPIEDefault() const override { return false; }
 
+  bool isPICDefault() const override { return true; }
+
   std::string findLibCxxIncludePath() const override;
   void addLibStdCxxIncludePaths(
       const llvm::opt::ArgList &DriverArgs,
-- 
2.14.2

