From 0130324ce3ce86cc0612b28a61abbc8e6f1605c9 Mon Sep 17 00:00:00 2001
From: Calvin Hill <calvin@hakobaito.co.uk>
Date: Wed, 10 Apr 2019 23:43:08 +0000
Subject: Haiku support for the Chromium build folder


diff --git a/build/build_config.h b/build/build_config.h
index 4d1ba77f2b..b36d2b34c3 100644
--- a/build/build_config.h
+++ b/build/build_config.h
@@ -53,6 +53,8 @@
 #define OS_FUCHSIA 1
 #elif defined(__FreeBSD__)
 #define OS_FREEBSD 1
+#elif defined(__HAIKU__)
+#define OS_HAIKU 1
 #elif defined(__NetBSD__)
 #define OS_NETBSD 1
 #elif defined(__OpenBSD__)
@@ -80,7 +82,7 @@
 // For access to standard POSIXish features, use OS_POSIX instead of a
 // more specific macro.
 #if defined(OS_AIX) || defined(OS_ANDROID) || defined(OS_ASMJS) ||    \
-    defined(OS_FREEBSD) || defined(OS_LINUX) || defined(OS_MACOSX) || \
+    defined(OS_FREEBSD) || defined(OS_HAIKU) || defined(OS_LINUX) || defined(OS_MACOSX) || \
     defined(OS_NACL) || defined(OS_NETBSD) || defined(OS_OPENBSD) ||  \
     defined(OS_QNX) || defined(OS_SOLARIS)
 #define OS_POSIX 1
diff --git a/build/config/BUILDCONFIG.gn b/build/config/BUILDCONFIG.gn
index 9e843f3c30..0c21df3e4d 100644
--- a/build/config/BUILDCONFIG.gn
+++ b/build/config/BUILDCONFIG.gn
@@ -213,7 +213,11 @@ if (host_toolchain == "") {
     }
   } else if (host_os == "aix") {
     host_toolchain = "//build/toolchain/aix:$host_cpu"
-  } else {
+  }
+   else if (host_os == "haiku") {
+    host_toolchain = "//build/toolchain/haiku:$host_cpu"
+  }
+   else {
     assert(false, "Unsupported host_os: $host_os")
   }
 }
@@ -233,7 +237,10 @@ if (target_os == "android") {
   }
 } else if (target_os == "fuchsia") {
   _default_toolchain = "//build/toolchain/fuchsia:$target_cpu"
-} else if (target_os == "ios") {
+} else if (target_os == "haiku") {
+  _default_toolchain = "//build/toolchain/haiku:$target_cpu"
+}
+ else if (target_os == "ios") {
   _default_toolchain = "//build/toolchain/mac:ios_clang_$target_cpu"
 } else if (target_os == "mac") {
   assert(host_os == "mac", "Mac cross-compiles are unsupported.")
@@ -290,6 +297,7 @@ if (custom_toolchain != "") {
 is_android = current_os == "android"
 is_chromeos = current_os == "chromeos"
 is_fuchsia = current_os == "fuchsia"
+is_haiku = current_os == "haiku"
 is_ios = current_os == "ios"
 is_linux = current_os == "chromeos" || current_os == "linux"
 is_mac = current_os == "mac"
diff --git a/build/config/clang/BUILD.gn b/build/config/clang/BUILD.gn
index 960726e1e5..b5e2f2e206 100644
--- a/build/config/clang/BUILD.gn
+++ b/build/config/clang/BUILD.gn
@@ -31,6 +31,7 @@ config("find_bad_constructs") {
 # Enables some extra Clang-specific warnings. Some third-party code won't
 # compile with these so may want to remove this config.
 config("extra_warnings") {
+   if (!is_haiku) {
   cflags = [
     "-Wheader-hygiene",
 
@@ -39,4 +40,5 @@ config("extra_warnings") {
 
     "-Wtautological-overlap-compare",
   ]
+   }
 }
diff --git a/build/config/clang/clang.gni b/build/config/clang/clang.gni
index 2c2d76fd4b..9ded4f4cca 100644
--- a/build/config/clang/clang.gni
+++ b/build/config/clang/clang.gni
@@ -6,6 +6,9 @@ import("//build/toolchain/toolchain.gni")
 
 default_clang_base_path = "//third_party/llvm-build/Release+Asserts"
 
+if(is_haiku){
+  default_clang_base_path = ""
+}
 declare_args() {
   # Indicates if the build should use the Chrome-specific plugins for enforcing
   # coding guidelines, etc. Only used when compiling with Clang.
diff --git a/build/config/compiler/BUILD.gn b/build/config/compiler/BUILD.gn
index ca6ff2b138..e66eeac213 100644
--- a/build/config/compiler/BUILD.gn
+++ b/build/config/compiler/BUILD.gn
@@ -101,7 +101,7 @@ declare_args() {
   # So only enable this on linux if use_goma.  Elsewhere, there are no
   # drawbacks to using it, so always enable it there.
   strip_absolute_paths_from_debug_symbols =
-      is_android || is_fuchsia || is_nacl || (is_win && use_lld) ||
+      is_android || is_fuchsia || is_nacl || is_haiku || (is_win && use_lld) ||
       (use_goma && is_linux)
 
   # Allow projects that wish to stay on C++11 to override Chromium's default.
@@ -236,6 +236,8 @@ config("compiler") {
     configs += [ "//build/config/fuchsia:compiler" ]
   } else if (current_os == "aix") {
     configs += [ "//build/config/aix:compiler" ]
+  } else if (current_os == "haiku") {
+    configs += [ "//build/config/haiku:compiler" ]
   }
 
   configs += [
@@ -359,7 +361,7 @@ config("compiler") {
 
   # Linux/Android/Fuchsia common flags setup.
   # ---------------------------------
-  if (is_linux || is_android || is_fuchsia) {
+  if (is_linux || is_android || is_fuchsia || is_haiku) {
     asmflags += [ "-fPIC" ]
     cflags += [ "-fPIC" ]
     ldflags += [ "-fPIC" ]
@@ -464,7 +466,7 @@ config("compiler") {
 
   # Clang-specific compiler flags setup.
   # ------------------------------------
-  if (is_clang) {
+  if (is_clang && !is_haiku) {
     cflags += [ "-fcolor-diagnostics" ]
 
     # Enable -fmerge-all-constants. This used to be the default in clang
@@ -1139,7 +1141,7 @@ config("compiler_deterministic") {
   # Tells the compiler not to use absolute paths when passing the default
   # paths to the tools it invokes. We don't want this because we don't
   # really need it and it can mess up the goma cache entries.
-  if (is_clang && !is_nacl) {
+  if (is_clang && !is_nacl && !is_haiku) {
     cflags += [ "-no-canonical-prefixes" ]
   }
 }
@@ -1707,7 +1709,7 @@ config("thin_archive") {
   # Mac and iOS use the mac-specific "libtool" command, not ar, which doesn't
   # have a "thin archive" mode (it does accept -T, but it means truncating
   # archive names to 16 characters, which is not what we want).
-  if ((is_posix && !is_nacl && !is_mac && !is_ios) || is_fuchsia) {
+  if ((is_posix && !is_nacl && !is_mac && !is_ios && !is_haiku) || is_fuchsia) {
     arflags = [ "-T" ]
   } else if (is_win && use_lld) {
     arflags = [ "/llvmlibthin" ]
diff --git a/build/config/haiku/BUILD.gn b/build/config/haiku/BUILD.gn
new file mode 100644
index 0000000000..2e25200459
--- /dev/null
+++ b/build/config/haiku/BUILD.gn
@@ -0,0 +1,61 @@
+# Copyright 2017 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import("//build/config/sanitizers/sanitizers.gni")
+import("//build/toolchain/toolchain.gni")
+
+# This is included by reference in the //build/config/compiler config that
+# is applied to all targets. It is here to separate out the logic.
+
+config("compiler") {
+  # These flags are shared between the C compiler and linker.
+
+  defines = [
+    "__STDC_FORMAT_MACROS",
+  ]
+
+  cflags = [
+    "-Wall",
+    "-Wno-unused-parameter",
+    "-Wmissing-field-initializers",
+    "-Wno-uninitialized",
+    "-O3",
+    "-Wno-unused-local-typedefs",
+    "-Wno-deprecated-declarations",
+    "-fno-delete-null-pointer-checks",
+    "-Wno-comments",
+    "-fomit-frame-pointer",
+    "-g0",
+    "-fvisibility=hidden",
+    "-Wno-strict-overflow",
+    "-Wno-return-type",
+    "-fno-ident",
+    "-fdata-sections",
+    "-ffunction-sections",
+    "-Wno-narrowing",
+    "-fno-exceptions",
+    "-fno-rtti",
+    "-fvisibility-inlines-hidden",
+    "-fstack-protector",
+    "--param=ssp-buffer-size=4",
+    "-fstack-protector",
+    "-Wno-builtin-macro-redefined"
+    # "-Werror"
+    # We need to find a way to fix the TOC warnings if we want to enable this.
+  ]
+
+  cflags_cc = [
+    "-std=gnu++11",
+
+  ]
+
+  ldflags = [
+    "-lroot",
+    "-lstdc++",
+    "-lbsd",
+    "-lssp",
+    "-lnetwork",
+  ]
+
+}
diff --git a/build/toolchain/gcc_toolchain.gni b/build/toolchain/gcc_toolchain.gni
index 7326a840dc..e10ba7f9dc 100644
--- a/build/toolchain/gcc_toolchain.gni
+++ b/build/toolchain/gcc_toolchain.gni
@@ -448,9 +448,9 @@ template("gcc_toolchain") {
       command = "$python_path \"$solink_wrapper\" --readelf=\"$readelf\" --nm=\"$nm\" $strip_switch--sofile=\"$unstripped_sofile\" --tocfile=\"$tocfile\"$map_switch --output=\"$sofile\" -- $link_command"
 
       if (target_cpu == "mipsel" && is_component_build && is_android) {
-        rspfile_content = "-Wl,--start-group -Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix -Wl,--end-group"
+        rspfile_content = "-Wl,--start-group -Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix -Wl,--end-group -lssp"
       } else {
-        rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
+        rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix -lssp -lexecinfo"
       }
 
       description = "SOLINK $sofile"
@@ -501,13 +501,13 @@ template("gcc_toolchain") {
         unstripped_sofile = sofile
       }
 
-      command = "$ld -shared {{ldflags}}${extra_ldflags} -o \"$unstripped_sofile\" -Wl,-soname=\"$soname\" @\"$rspfile\""
+      command = "$ld -shared {{ldflags}}${extra_ldflags} -o \"$unstripped_sofile\" -Wl,-soname=\"$soname\" @\"$rspfile\" -lssp"
 
       if (defined(invoker.strip)) {
         strip_command = "${invoker.strip} -o \"$sofile\" \"$unstripped_sofile\""
         command += " && " + strip_command
       }
-      rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
+      rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} -lexecinfo $solink_libs_section_postfix"
 
       description = "SOLINK_MODULE $sofile"
 
@@ -567,9 +567,10 @@ template("gcc_toolchain") {
       end_group_flag = ""
       if (current_os != "aix") {
         # the "--start-group .. --end-group" feature isn't available on the aix ld.
-        start_group_flag = "-Wl,--start-group"
-        end_group_flag = "-Wl,--end-group "
+        start_group_flag = "-Wl,--start-group -lssp -lexecinfo -lnetwork"
+        end_group_flag = "-Wl,--end-group -lssp -lexecinfo -lnetwork"
       }
+
       link_command = "$ld {{ldflags}}${extra_ldflags} -o \"$unstripped_outfile\" $start_group_flag @\"$rspfile\" {{solibs}} $end_group_flag $libs_section_prefix {{libs}} $libs_section_postfix"
 
       strip_switch = ""
diff --git a/build/toolchain/haiku/BUILD.gn b/build/toolchain/haiku/BUILD.gn
new file mode 100644
index 0000000000..0b31eade9f
--- /dev/null
+++ b/build/toolchain/haiku/BUILD.gn
@@ -0,0 +1,76 @@
+# Copyright 2017 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import("//build/toolchain/gcc_toolchain.gni")
+
+
+gcc_toolchain("x86") {
+  cc = "gcc"
+  cxx = "g++"
+
+  readelf = "readelf"
+  nm = "nm"
+  ar = "ar"
+  ld = cxx
+
+  # Output linker map files for binary size analysis.
+  enable_linker_map = true
+
+  is_clang = false
+
+  extra_ldflags="-lroot -lbsd -lnetwork -lssp"
+
+
+  toolchain_args = {
+    current_cpu = "x86"
+    current_os = "haiku"
+    is_clang = false
+  }
+}
+
+gcc_toolchain("x64") {
+  cc = "gcc"
+  cxx = "g++"
+
+  readelf = "readelf"
+  nm = "nm"
+  ar = "ar"
+  ld = cxx
+
+  # Output linker map files for binary size analysis.
+  enable_linker_map = true
+
+  is_clang = false
+
+  extra_ldflags="-lroot -lbsd -lnetwork -lssp"
+
+
+  toolchain_args = {
+    current_cpu = "x64"
+    current_os = "haiku"
+    is_clang = false
+  
+  }
+}
+
+clang_toolchain("clang_x64") {
+  # Output linker map files for binary size analysis.
+  enable_linker_map = true
+  
+  
+  toolchain_args = {
+    current_cpu = "x64"
+    current_os = "haiku"
+  }
+}
+
+clang_toolchain("clang_x86") {
+  # Output linker map files for binary size analysis.
+  enable_linker_map = true
+  
+  toolchain_args = {
+    current_cpu = "x86"
+    current_os = "haiku"
+  }
+}
diff --git a/third_party/googletest/src/googletest/include/gtest/internal/gtest-port-arch.h b/third_party/googletest/src/googletest/include/gtest/internal/gtest-port-arch.h
index 779872de6a..aaeff5d9d6 100644
--- a/third_party/googletest/src/googletest/include/gtest/internal/gtest-port-arch.h
+++ b/third_party/googletest/src/googletest/include/gtest/internal/gtest-port-arch.h
@@ -79,6 +79,8 @@
 # define GTEST_OS_FUCHSIA 1
 #elif defined(__GLIBC__) && defined(__FreeBSD_kernel__)
 # define GTEST_OS_GNU_KFREEBSD 1
+#elif defined(__HAIKU__)
+# define GTEST_OS_HAIKU 1
 #elif defined __linux__
 # define GTEST_OS_LINUX 1
 # if defined __ANDROID__
diff --git a/third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h b/third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h
index 4932947b49..ab430447e1 100644
--- a/third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h
+++ b/third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h
@@ -121,6 +121,7 @@
 //   GTEST_OS_FREEBSD  - FreeBSD
 //   GTEST_OS_FUCHSIA  - Fuchsia
 //   GTEST_OS_GNU_KFREEBSD - GNU/kFreeBSD
+//   GTEST_OS_HAIKU    - Haiku
 //   GTEST_OS_HPUX     - HP-UX
 //   GTEST_OS_LINUX    - Linux
 //     GTEST_OS_LINUX_ANDROID - Google Android
@@ -548,7 +549,7 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
 #define GTEST_HAS_PTHREAD                                             \
   (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX || GTEST_OS_QNX || \
    GTEST_OS_FREEBSD || GTEST_OS_NACL || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \
-   GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_OPENBSD)
+   GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_OPENBSD || GTEST_OS_HAIKU)
 #endif  // GTEST_HAS_PTHREAD
 
 #if GTEST_HAS_PTHREAD
@@ -608,7 +609,7 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
      GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX || GTEST_OS_HPUX || \
      GTEST_OS_OPENBSD || GTEST_OS_QNX || GTEST_OS_FREEBSD || \
      GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || GTEST_OS_DRAGONFLY || \
-     GTEST_OS_GNU_KFREEBSD)
+     GTEST_OS_GNU_KFREEBSD || GTEST_OS_HAIKU)
 # define GTEST_HAS_DEATH_TEST 1
 #endif
 
-- 
2.21.0

