From 2224160e6b72457a3b386f412831f48c21710bf3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= <jerome.duval@gmail.com>
Date: Mon, 18 Jul 2016 14:13:19 +0200
Subject: support for secondary arch.


diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2eee8e6..1a3b180 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -402,6 +402,10 @@ option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
 if(NOT CLANG_ENABLE_STATIC_ANALYZER AND (CLANG_ENABLE_ARCMT OR CLANG_ANALYZER_BUILD_Z3))
   message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT or Z3")
 endif()
+if(DEFINED HAIKU_HYBRID_SECONDARY)
+  add_definitions(-DHAIKU_HYBRID_SECONDARY=${HAIKU_HYBRID_SECONDARY})
+endif()
+
 
 if(CLANG_ANALYZER_BUILD_Z3)
   find_package(Z3 4.5)
diff --git a/lib/Driver/ToolChains/Haiku.cpp b/lib/Driver/ToolChains/Haiku.cpp
index 284d269..3a816aa 100644
--- a/lib/Driver/ToolChains/Haiku.cpp
+++ b/lib/Driver/ToolChains/Haiku.cpp
@@ -20,6 +20,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 +35,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);
+                     getTriple().str(), "", "", "", DriverArgs, CC1Args);
+#endif
 }
diff --git a/lib/Frontend/InitHeaderSearch.cpp b/lib/Frontend/InitHeaderSearch.cpp
index 8c6face..4c04668 100644
--- a/lib/Frontend/InitHeaderSearch.cpp
+++ b/lib/Frontend/InitHeaderSearch.cpp
@@ -233,7 +233,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, Clang, Ver);
+#else
     SmallString<128> P = StringRef(HSOpts.ResourceDir);
+#endif
+
     llvm::sys::path::append(P, "include");
     AddUnmappedPath(P, ExternCSystem, false);
   }
@@ -266,7 +279,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);
@@ -298,6 +316,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:
-- 
2.19.1


From 01c867dbd8530f5531532cba7d07f1e3e47f38c5 Mon Sep 17 00:00:00 2001
From: Jerome Duval <jerome.duval@gmail.com>
Date: Thu, 7 Apr 2016 18:30:52 +0000
Subject: add a test for haiku driver.

* upstream wants a case for libcxx in ToolChains.cpp, so add it.

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.19.1


From c889c9ad0249e680afd209729ee87259f78f59c3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= <jerome.duval@gmail.com>
Date: Mon, 18 Jul 2016 14:13:19 +0200
Subject: Enable thread-local storage and disable PIE by default


diff --git a/lib/Basic/Targets/OSTargets.h b/lib/Basic/Targets/OSTargets.h
index 5af6361..790732b 100644
--- a/lib/Basic/Targets/OSTargets.h
+++ b/lib/Basic/Targets/OSTargets.h
@@ -260,7 +260,6 @@ public:
     this->IntPtrType = TargetInfo::SignedLong;
     this->PtrDiffType = TargetInfo::SignedLong;
     this->ProcessIDType = TargetInfo::SignedLong;
-    this->TLSSupported = false;
   }
 };
 
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(
-- 
2.19.1


From 90022e621a8d44fd7f3d865f34568cb2c9213ad5 Mon Sep 17 00:00:00 2001
From: Calvin Hill <calvin@hakobaito.co.uk>
Date: Wed, 24 Oct 2018 10:47:00 +0100
Subject: clang: Enable float128 support in clang with tests.


diff --git a/lib/Basic/Targets/OSTargets.h b/lib/Basic/Targets/OSTargets.h
index 790732b..11692b6 100644
--- a/lib/Basic/Targets/OSTargets.h
+++ b/lib/Basic/Targets/OSTargets.h
@@ -251,6 +251,10 @@ protected:
     Builder.defineMacro("__HAIKU__");
     Builder.defineMacro("__ELF__");
     DefineStd(Builder, "unix", Opts);
+    if (this->HasFloat128) {
+      Builder.defineMacro("__FLOAT128__");
+      Builder.defineMacro("_GLIBCXX_USE_FLOAT128");
+    }
   }
 
 public:
@@ -260,6 +264,14 @@ public:
     this->IntPtrType = TargetInfo::SignedLong;
     this->PtrDiffType = TargetInfo::SignedLong;
     this->ProcessIDType = TargetInfo::SignedLong;
+    switch (Triple.getArch()) {
+    default:
+      break;
+    case llvm::Triple::x86:
+    case llvm::Triple::x86_64:
+      this->HasFloat128 = true;
+      break;
+    }
   }
 };
 
diff --git a/test/CodeGenCXX/float128-declarations.cpp b/test/CodeGenCXX/float128-declarations.cpp
index f1db8f4..11a49c9 100644
--- a/test/CodeGenCXX/float128-declarations.cpp
+++ b/test/CodeGenCXX/float128-declarations.cpp
@@ -12,6 +12,10 @@
 // RUN:   %s -o - | FileCheck %s -check-prefix=CHECK-X86
 // RUN: %clang_cc1 -emit-llvm -triple amd64-pc-openbsd -std=c++11 \
 // RUN:   %s -o - | FileCheck %s -check-prefix=CHECK-X86
+// RUN: %clang_cc1 -emit-llvm -triple i586-pc-haiku -std=c++11 \
+// RUN:   %s -o - | FileCheck %s -check-prefix=CHECK-X86
+// RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-haiku -std=c++11 \
+// RUN:   %s -o - | FileCheck %s -check-prefix=CHECK-X86
 //
 /*  Various contexts where type __float128 can appear. The different check
     prefixes are due to different mangling on X86 and different calling
-- 
2.19.1

