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 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 <>
diff --git a/core/ b/core/
index afbddae..f9cab54 100644
--- a/core/
+++ b/core/
@@ -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.
+_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))
+_bad_kernel_hdr_dirs :=
 # Clean up/verify variables defined by the board config file.
@@ -330,7 +352,8 @@
 # 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