Clean up and document our hacks for building BSD source.

Change-Id: Ic591e22fa5b363bb68376b9f25814c0e5bd83fbf
diff --git a/HACKING.txt b/HACKING.txt
index 9f45903..fb93b5a 100644
--- a/HACKING.txt
+++ b/HACKING.txt
@@ -97,8 +97,14 @@
   upstream-openbsd/
     # These directories contain unmolested upstream source. Any time we can
     # just use a BSD implementation of something unmodified, we should.
-    # See files like netbsd-compat.h for various ways in which we manage to
-    # build BSD source in bionic.
+    # The structure under these directories mimics the upstream tree,
+    # but there's also...
+    android/
+      include/
+        # This is where we keep the hacks necessary to build BSD source
+        # in our world. The *-compat.h files are automatically included
+        # using -include, but we also provide equivalents for missing
+        # header/source files needed by the BSD implementation.
 
   bionic/
     # This is the biggest mess. The C++ files are files we own, typically
diff --git a/libc/Android.mk b/libc/Android.mk
index 0ec927a..507126e 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -306,7 +306,7 @@
     upstream-netbsd/lib/libc/unistd/killpg.c \
 
 libc_upstream_openbsd_gdtoa_src_files := \
-    upstream-openbsd/gdtoa_support.cpp \
+    upstream-openbsd/android/gdtoa_support.cpp \
     upstream-openbsd/lib/libc/gdtoa/dmisc.c \
     upstream-openbsd/lib/libc/gdtoa/dtoa.c \
     upstream-openbsd/lib/libc/gdtoa/gdtoa.c \
@@ -549,7 +549,7 @@
     -I$(LOCAL_PATH)/dns/include \
     -I$(LOCAL_PATH)/private \
     -I$(LOCAL_PATH)/upstream-netbsd/lib/libc/include \
-    -include upstream-netbsd/netbsd-compat.h
+    -include upstream-netbsd/android/include/netbsd-compat.h
 LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
@@ -573,9 +573,9 @@
 LOCAL_SRC_FILES := $(libc_upstream_freebsd_src_files)
 LOCAL_CFLAGS := \
     $(libc_common_cflags) \
-    -I$(LOCAL_PATH)/upstream-freebsd \
+    -I$(LOCAL_PATH)/upstream-freebsd/android/include \
     -I$(LOCAL_PATH)/upstream-freebsd/lib/libc/include \
-    -include upstream-freebsd/freebsd-compat.h
+    -include freebsd-compat.h
 LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
@@ -600,9 +600,9 @@
 LOCAL_CFLAGS := \
     $(libc_common_cflags) \
     -DPOSIX_MISTAKE \
-    -I$(LOCAL_PATH)/upstream-netbsd \
+    -I$(LOCAL_PATH)/upstream-netbsd/android/include \
     -I$(LOCAL_PATH)/upstream-netbsd/lib/libc/include \
-    -include upstream-netbsd/netbsd-compat.h
+    -include netbsd-compat.h
 LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
@@ -626,9 +626,9 @@
 LOCAL_SRC_FILES := $(libc_upstream_openbsd_src_files)
 LOCAL_CFLAGS := \
     $(libc_common_cflags) \
-    -I$(LOCAL_PATH)/upstream-openbsd \
+    -I$(LOCAL_PATH)/upstream-openbsd/android/include \
     -I$(LOCAL_PATH)/upstream-openbsd/lib/libc/include \
-    -include upstream-openbsd/openbsd-compat.h
+    -include openbsd-compat.h
 LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
@@ -653,9 +653,9 @@
 LOCAL_SRC_FILES_64 := $(libc_upstream_openbsd_gdtoa_src_files_64)
 LOCAL_CFLAGS := \
     $(libc_common_cflags) \
-    -I$(LOCAL_PATH)/upstream-openbsd \
+    -I$(LOCAL_PATH)/upstream-openbsd/android/include \
     -I$(LOCAL_PATH)/upstream-openbsd/lib/libc/include \
-    -include upstream-openbsd/openbsd-compat.h \
+    -include openbsd-compat.h \
 
 LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
diff --git a/libc/upstream-freebsd/freebsd-compat.h b/libc/upstream-freebsd/android/include/freebsd-compat.h
similarity index 100%
rename from libc/upstream-freebsd/freebsd-compat.h
rename to libc/upstream-freebsd/android/include/freebsd-compat.h
diff --git a/libc/upstream-freebsd/libc_private.h b/libc/upstream-freebsd/android/include/libc_private.h
similarity index 100%
rename from libc/upstream-freebsd/libc_private.h
rename to libc/upstream-freebsd/android/include/libc_private.h
diff --git a/libc/upstream-freebsd/namespace.h b/libc/upstream-freebsd/android/include/namespace.h
similarity index 100%
rename from libc/upstream-freebsd/namespace.h
rename to libc/upstream-freebsd/android/include/namespace.h
diff --git a/libc/upstream-freebsd/spinlock.h b/libc/upstream-freebsd/android/include/spinlock.h
similarity index 100%
rename from libc/upstream-freebsd/spinlock.h
rename to libc/upstream-freebsd/android/include/spinlock.h
diff --git a/libc/upstream-freebsd/un-namespace.h b/libc/upstream-freebsd/android/include/un-namespace.h
similarity index 100%
rename from libc/upstream-freebsd/un-namespace.h
rename to libc/upstream-freebsd/android/include/un-namespace.h
diff --git a/libc/upstream-netbsd/README.txt b/libc/upstream-netbsd/README.txt
index 86af6eb..e2d146e 100644
--- a/libc/upstream-netbsd/README.txt
+++ b/libc/upstream-netbsd/README.txt
@@ -2,8 +2,4 @@
 files directly. Make fixes upstream and then pull down the new version of
 the file.
 
-Note that code in the other 'netbsd' directory contains Android modifications.
-We should work towards getting as many of those changes as possible upstream
-and then losing those files in favor of pure upstream copies here instead.
-
 TODO: write a script to make this process automated.
diff --git a/libc/upstream-netbsd/env.h b/libc/upstream-netbsd/android/include/env.h
similarity index 100%
rename from libc/upstream-netbsd/env.h
rename to libc/upstream-netbsd/android/include/env.h
diff --git a/libc/upstream-netbsd/extern.h b/libc/upstream-netbsd/android/include/extern.h
similarity index 100%
rename from libc/upstream-netbsd/extern.h
rename to libc/upstream-netbsd/android/include/extern.h
diff --git a/libc/upstream-netbsd/fd_setsize.h b/libc/upstream-netbsd/android/include/fd_setsize.h
similarity index 100%
rename from libc/upstream-netbsd/fd_setsize.h
rename to libc/upstream-netbsd/android/include/fd_setsize.h
diff --git a/libc/upstream-netbsd/namespace.h b/libc/upstream-netbsd/android/include/namespace.h
similarity index 100%
rename from libc/upstream-netbsd/namespace.h
rename to libc/upstream-netbsd/android/include/namespace.h
diff --git a/libc/upstream-netbsd/netbsd-compat.h b/libc/upstream-netbsd/android/include/netbsd-compat.h
similarity index 100%
rename from libc/upstream-netbsd/netbsd-compat.h
rename to libc/upstream-netbsd/android/include/netbsd-compat.h
diff --git a/libc/upstream-netbsd/port_after.h b/libc/upstream-netbsd/android/include/port_after.h
similarity index 100%
rename from libc/upstream-netbsd/port_after.h
rename to libc/upstream-netbsd/android/include/port_after.h
diff --git a/libc/upstream-netbsd/port_before.h b/libc/upstream-netbsd/android/include/port_before.h
similarity index 100%
rename from libc/upstream-netbsd/port_before.h
rename to libc/upstream-netbsd/android/include/port_before.h
diff --git a/libc/upstream-netbsd/rand48.h b/libc/upstream-netbsd/android/include/rand48.h
similarity index 100%
rename from libc/upstream-netbsd/rand48.h
rename to libc/upstream-netbsd/android/include/rand48.h
diff --git a/libc/upstream-netbsd/reentrant.h b/libc/upstream-netbsd/android/include/reentrant.h
similarity index 100%
rename from libc/upstream-netbsd/reentrant.h
rename to libc/upstream-netbsd/android/include/reentrant.h
diff --git a/libc/upstream-openbsd/README.txt b/libc/upstream-openbsd/README.txt
new file mode 100644
index 0000000..9db64ea
--- /dev/null
+++ b/libc/upstream-openbsd/README.txt
@@ -0,0 +1,5 @@
+This directory contains upstream OpenBSD source. You should not edit these
+files directly. Make fixes upstream and then pull down the new version of
+the file.
+
+TODO: write a script to make this process automated.
diff --git a/libc/upstream-openbsd/gdtoa_support.cpp b/libc/upstream-openbsd/android/gdtoa_support.cpp
similarity index 100%
rename from libc/upstream-openbsd/gdtoa_support.cpp
rename to libc/upstream-openbsd/android/gdtoa_support.cpp
diff --git a/libc/upstream-openbsd/arith.h b/libc/upstream-openbsd/android/include/arith.h
similarity index 100%
rename from libc/upstream-openbsd/arith.h
rename to libc/upstream-openbsd/android/include/arith.h
diff --git a/libc/upstream-openbsd/gd_qnan.h b/libc/upstream-openbsd/android/include/gd_qnan.h
similarity index 100%
rename from libc/upstream-openbsd/gd_qnan.h
rename to libc/upstream-openbsd/android/include/gd_qnan.h
diff --git a/libc/upstream-openbsd/openbsd-compat.h b/libc/upstream-openbsd/android/include/openbsd-compat.h
similarity index 100%
rename from libc/upstream-openbsd/openbsd-compat.h
rename to libc/upstream-openbsd/android/include/openbsd-compat.h
diff --git a/libc/upstream-openbsd/thread_private.h b/libc/upstream-openbsd/android/include/thread_private.h
similarity index 100%
rename from libc/upstream-openbsd/thread_private.h
rename to libc/upstream-openbsd/android/include/thread_private.h