core: allow devices to provide their own device specific kernel headers

Currently, we need to pollute the common bionic/libc/kernel/...
directory with device specific kernel headers. This has created a giant
dumping ground of headers from all the projects we have ever done.

Allow each device to provide their own headers and thus eventually allow
bionic/libc/kernel to become *just* the common linux kernel headers.

This change will automatically add device/oem/name/kernel-headers to
the include list and also allow the BoardConfig.mk to specify
additional directories with TARGET_BOARD_KERNEL_HEADERS variable.
The requirement is that the final dir name must be kernel-headers to
mark explicitly where kernel headers are kept.

Change-Id: I7d32a979eef1f464db06c1bc448a04ddc3575efd
Signed-off-by: Dima Zavin <dima@android.com>
diff --git a/core/config.mk b/core/config.mk
index afbddae..f9cab54 100644
--- a/core/config.mk
+++ b/core/config.mk
@@ -151,6 +151,28 @@
 TARGET_DEVICE_DIR := $(patsubst %/,%,$(dir $(board_config_mk)))
 board_config_mk :=
 
+# pull in device specific kernel headers. Files should be in
+# $(TARGET_DEVICE_DIR)/kernel-headers, e.g.
+# $(TARGET_DEVICE_DIR)/kernel-headers/linux/
+# $(TARGET_DEVICE_DIR)/kernel-headers/media/
+# $(TARGET_DEVICE_DIR)/kernel-headers/video/
+# etc.
+TARGET_DEVICE_KERNEL_HEADERS := $(strip $(wildcard $(TARGET_DEVICE_DIR)/kernel-headers))
+
+# also allow the board config to provide additional directories since
+# there could be device/oem/base_hw and device/oem/derived_hw
+# that both are valid devices but derived_hw needs to use kernel headers
+# from base_hw.
+TARGET_BOARD_KERNEL_HEADERS := $(strip $(wildcard $(TARGET_BOARD_KERNEL_HEADERS)))
+TARGET_BOARD_KERNEL_HEADERS := $(patsubst %/,%,$(TARGET_BOARD_KERNEL_HEADERS))
+_bad_kernel_hdr_dirs := \
+	$(foreach hdr_dir,$(TARGET_BOARD_KERNEL_HEADERS),\
+		$(filter-out kernel-headers,$(notdir $(hdr_dir))))
+ifneq ($(words $(_bad_kernel_hdr_dirs)),0)
+  $(error Board kernel header dirs must be end in kernel-headers: $(TARGET_BOARD_KERNEL_HEADERS))
+endif
+_bad_kernel_hdr_dirs :=
+
 # Clean up/verify variables defined by the board config file.
 TARGET_BOOTLOADER_BOARD_NAME := $(strip $(TARGET_BOOTLOADER_BOARD_NAME))
 TARGET_CPU_ABI := $(strip $(TARGET_CPU_ABI))
@@ -330,7 +352,8 @@
 TARGET_GLOBAL_LD_DIRS += -L$(TARGET_OUT_INTERMEDIATE_LIBRARIES)
 
 HOST_PROJECT_INCLUDES:= $(SRC_HEADERS) $(SRC_HOST_HEADERS) $(HOST_OUT_HEADERS)
-TARGET_PROJECT_INCLUDES:= $(SRC_HEADERS) $(TARGET_OUT_HEADERS)
+TARGET_PROJECT_INCLUDES:= $(SRC_HEADERS) $(TARGET_OUT_HEADERS) \
+		$(TARGET_DEVICE_KERNEL_HEADERS) $(TARGET_BOARD_KERNEL_HEADERS)
 
 # Many host compilers don't support these flags, so we have to make
 # sure to only specify them for the target compilers checked in to