#! /bin/sh -e

# DP: biarch-include.dpatch
# DP:
# DP: Adds biarch include directories
# DP:     /usr/local/include/<arch>-linux-gnu
# DP:     /usr/include/<arch>-linux-gnu
# DP: to the system include paths, depending on 32/64 bit mode.

dir=
if [ $# -eq 3 -a "$2" = '-d' ]; then
    pdir="-d $3"
    dir="$3/"
elif [ $# -ne 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi
case "$1" in
    -patch)
        patch $pdir -f --no-backup-if-mismatch -p0 < $0
        ;;
    -unpatch)
        patch $pdir -f --no-backup-if-mismatch -R -p0 < $0
        ;;
    *)
        echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
        exit 1
esac
exit 0

Index: gcc/cppdefault.c
===================================================================
--- gcc/cppdefault.c	(revision 112832)
+++ gcc/cppdefault.c	(working copy)
@@ -60,6 +60,7 @@
 #endif
 #ifdef LOCAL_INCLUDE_DIR
     /* /usr/local/include comes before the fixincluded header files.  */
+    { LOCAL_INCLUDE_DIR, 0, 0, 1, 1, 2 },
     { LOCAL_INCLUDE_DIR, 0, 0, 1, 1, 0 },
 #endif
 #ifdef PREFIX_INCLUDE_DIR
@@ -83,6 +84,7 @@
 #endif
 #ifdef STANDARD_INCLUDE_DIR
     /* /usr/include comes dead last.  */
+    { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0, 1, 2 },
     { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0, 1, 0 },
 #endif
     { 0, 0, 0, 0, 0, 0 }
Index: gcc/c-incpath.c
===================================================================
--- gcc/c-incpath.c	(revision 112832)
+++ gcc/c-incpath.c	(working copy)
@@ -30,6 +30,7 @@
 #include "intl.h"
 #include "c-incpath.h"
 #include "cppdefault.h"
+#include "errors.h"
 
 /* Windows does not natively support inodes, and neither does MSDOS.
    Cygwin's emulation can generate non-unique inodes, so don't use it.
@@ -121,6 +121,31 @@
     }
 }
 
+struct multiarch_mapping
+{
+  const char *const multilib;
+  const char *const multiarch;
+};
+
+const struct multiarch_mapping multiarch_mappings[]
+= {
+#include "multiarch.inc"
+  { 0, 0 }
+};
+
+static const char*
+multilib_to_multiarch (const char *imultilib)
+{
+  const struct multiarch_mapping *p;
+
+  for (p = multiarch_mappings; p->multiarch; p++)
+    {
+      if (!strcmp(p->multilib, imultilib ? imultilib : ""))
+	return p->multiarch;
+    }
+  internal_error("no multiarch mapping for multilib (%s)\n", imultilib);
+}
+
 /* Append the standard include chain defined in cppdefault.c.  */
 static void
 add_standard_paths (const char *sysroot, const char *iprefix,
@@ -128,6 +153,7 @@
 {
   const struct default_include *p;
   size_t len;
+  const char *multiarch;
 
   if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0)
     {
@@ -146,8 +172,15 @@
 	      if (!strncmp (p->fname, cpp_GCC_INCLUDE_DIR, len))
 		{
 		  char *str = concat (iprefix, p->fname + len, NULL);
-		  if (p->multilib && imultilib)
+		  if (p->multilib == 1 && imultilib)
 		    str = concat (str, dir_separator_str, imultilib, NULL);
+		  if (p->multilib == 2)
+		    {
+		      multiarch = multilib_to_multiarch (imultilib);
+		      if (!multiarch)
+			continue;
+		      str = concat (str, dir_separator_str, multiarch, NULL);
+		    }
 		  add_path (str, SYSTEM, p->cxx_aware, false);
 		}
 	    }
@@ -166,9 +199,17 @@
 	  else
 	    str = update_path (p->fname, p->component);
 
-	  if (p->multilib && imultilib)
+	  if (p->multilib == 1 && imultilib)
 	    str = concat (str, dir_separator_str, imultilib, NULL);
 
+	  if (p->multilib == 2)
+	    {
+	      multiarch = multilib_to_multiarch (imultilib);
+	      if (!multiarch)
+		continue;
+	      str = concat (str, dir_separator_str, multiarch, NULL);
+	    }
+
 	  add_path (str, SYSTEM, p->cxx_aware, false);
 	}
     }
