From 232161b64d10cea8caeb70c4e91479506d912fab 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 c2016a4..d7a6533 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -420,6 +420,10 @@ if (CLANG_ANALYZER_Z3_INSTALL_DIR)
     message(FATAL_ERROR "Z3 4.7.1 has not been found in CLANG_ANALYZER_Z3_INSTALL_DIR: ${CLANG_ANALYZER_Z3_INSTALL_DIR}.")
   endif()
 endif()
+if(DEFINED HAIKU_HYBRID_SECONDARY)
+  add_definitions(-DHAIKU_HYBRID_SECONDARY=${HAIKU_HYBRID_SECONDARY})
+endif()
+
 
 set(CLANG_ANALYZER_ENABLE_Z3_SOLVER_DEFAULT "${Z3_FOUND}")
 
diff --git a/lib/Driver/ToolChains/Haiku.cpp b/lib/Driver/ToolChains/Haiku.cpp
index 12461ec..643b62b 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
 }
 
 void Haiku::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
@@ -30,6 +37,12 @@ void Haiku::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
 
 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 67842b5..f80bbc2 100644
--- a/lib/Frontend/InitHeaderSearch.cpp
+++ b/lib/Frontend/InitHeaderSearch.cpp
@@ -238,7 +238,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);
   }
@@ -273,7 +286,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);
@@ -305,6 +323,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 24a5b1a303ef3b899d5c30f833e0ae69a1b4814a 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 7563792bf072390c88e77df5776a936390ceb8fc 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 09867d8..7741410 100644
--- a/lib/Basic/Targets/OSTargets.h
+++ b/lib/Basic/Targets/OSTargets.h
@@ -277,7 +277,6 @@ public:
     this->IntPtrType = TargetInfo::SignedLong;
     this->PtrDiffType = TargetInfo::SignedLong;
     this->ProcessIDType = TargetInfo::SignedLong;
-    this->TLSSupported = false;
     switch (Triple.getArch()) {
     default:
       break;
diff --git a/lib/Driver/ToolChains/Haiku.h b/lib/Driver/ToolChains/Haiku.h
index a12a48e..9f6eb52 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; }
 
   void addLibCxxIncludePaths(
       const llvm::opt::ArgList &DriverArgs,
-- 
2.19.1


From 0caa55b50e4916e84eb3a2fb6e3836930092a077 Mon Sep 17 00:00:00 2001
From: Jerome Duval <jerome.duval@gmail.com>
Date: Tue, 12 Mar 2019 21:08:57 +0100
Subject: sysexits.h requires _BSD_SOURCE on Haiku beta1.


diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp
index a784e21..448639f 100644
--- a/lib/Driver/Driver.cpp
+++ b/lib/Driver/Driver.cpp
@@ -83,6 +83,9 @@
 #include <utility>
 #if LLVM_ON_UNIX
 #include <unistd.h> // getpid
+#ifdef __HAIKU__
+#define _BSD_SOURCE
+#endif
 #include <sysexits.h> // EX_IOERR
 #endif
 
-- 
2.19.1

