From 73bc3f26265dc46cba578c1da080a2d4002de195 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Draszik?= <andre.draszik@st.com>
Date: Mon, 15 Jul 2013 16:42:15 +0100
Subject: [PATCH] linux-dtb: allow to build dtb using kernel build system
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The kernel build system has native support for device tree
source files that /include/ additional files from standard
directories, and it also supports the pre-processing of
source file(s) using CPP.

This commits lets OE use the kbuild system if
$KERNEL_DEVICETREE points to file(s) that are located in
arch/${ARCH}/boot/dts/

Still missing is a change to the dependency tracking python
code after which we would only add the dtc-native dependency
if the kernel build system is not used.

Signed-off-by: André Draszik <andre.draszik@st.com>
---
 meta/recipes-kernel/linux/linux-dtb.inc | 60 +++++++++++++++++++++++++++------
 1 file changed, 50 insertions(+), 10 deletions(-)

diff --git a/meta/recipes-kernel/linux/linux-dtb.inc b/meta/recipes-kernel/linux/linux-dtb.inc
index 7747718..7eea83a 100644
--- a/meta/recipes-kernel/linux/linux-dtb.inc
+++ b/meta/recipes-kernel/linux/linux-dtb.inc
@@ -1,3 +1,5 @@
+inherit kernel-arch
+
 # Support for device tree generation
 FILES_kernel-devicetree = "/boot/devicetree*"
 KERNEL_DEVICETREE_FLAGS ?= "-R 8 -p 0x3000"
@@ -5,24 +7,49 @@ KERNEL_DEVICETREE_FLAGS ?= "-R 8 -p 0x3000"
 python __anonymous () {
     devicetree = d.getVar("KERNEL_DEVICETREE", True) or ''
     if devicetree:
+        # fixme: add dependency only if file not found
         depends = d.getVar("DEPENDS", True)
         d.setVar("DEPENDS", "%s dtc-native" % depends)
         packages = d.getVar("PACKAGES", True)
         d.setVar("PACKAGES", "%s kernel-devicetree" % packages)
 }
 
-do_install_append() {
+do_compile_append() {
 	if test -n "${KERNEL_DEVICETREE}"; then
 		for DTS_FILE in ${KERNEL_DEVICETREE}; do
 			if [ ! -f ${DTS_FILE} ]; then
-				echo "Warning: ${DTS_FILE} is not available!"
-				continue
+				if [ ! -f arch/${ARCH}/boot/dts/${DTS_FILE} ]; then
+					echo "Warning: ${DTS_FILE} is not available!"
+					continue
+				fi
+
+				# standard build using kbuild
+				echo "Info: standard kbuild for device tree blob"
+				DTS_BASE_NAME=`basename ${DTS_FILE} | awk -F "." '{print $1}'`
+
+				unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
+				oe_runmake ${DTS_BASE_NAME}.dtb CC="${KERNEL_CC}" LD="${KERNEL_LD}" ${KERNEL_EXTRA_ARGS}
 			fi
+		done
+	fi
+}
+
+do_install_append() {
+	if test -n "${KERNEL_DEVICETREE}"; then
+		for DTS_FILE in ${KERNEL_DEVICETREE}; do
 			DTS_BASE_NAME=`basename ${DTS_FILE} | awk -F "." '{print $1}'`
-			DTB_NAME=`echo ${KERNEL_IMAGE_BASE_NAME} | sed "s/${MACHINE}/${DTS_BASE_NAME}/g"`
 			DTB_SYMLINK_NAME=`echo ${KERNEL_IMAGE_SYMLINK_NAME} | sed "s/${MACHINE}/${DTS_BASE_NAME}/g"`
-			dtc -I dts -O dtb ${KERNEL_DEVICETREE_FLAGS} -o ${DTS_BASE_NAME} ${DTS_FILE}
-			install -m 0644 ${DTS_BASE_NAME} ${D}/boot/devicetree-${DTB_SYMLINK_NAME}.dtb
+			if [ ! -f ${DTS_FILE} ]; then
+				if [ ! -f arch/${ARCH}/boot/dts/${DTS_FILE} ]; then
+					echo "Warning: ${DTS_FILE} is not available!"
+					continue
+				fi
+
+				install -m 0644 arch/${ARCH}/boot/${DTS_BASE_NAME}.dtb ${D}/boot/devicetree-${DTB_SYMLINK_NAME}.dtb
+				continue
+			fi
+			dtc -I dts -O dtb ${KERNEL_DEVICETREE_FLAGS} -o ${DTS_BASE_NAME}.dtb ${DTS_FILE}
+			install -m 0644 ${DTS_BASE_NAME}.dtb ${D}/boot/devicetree-${DTB_SYMLINK_NAME}.dtb
 		done
 	fi
 }
@@ -30,15 +57,28 @@ do_install_append() {
 do_deploy_append() {
 	if test -n "${KERNEL_DEVICETREE}"; then
 		for DTS_FILE in ${KERNEL_DEVICETREE}; do
+			DTS_BASE_NAME=`basename ${DTS_FILE} | awk -F "." '{print $1}'`
 			if [ ! -f ${DTS_FILE} ]; then
-				echo "Warning: ${DTS_FILE} is not available!"
-				continue
+				if [ ! -f arch/${ARCH}/boot/dts/${DTS_FILE} ]; then
+					echo "Warning: ${DTS_FILE} is not available!"
+					continue
+				fi
+
+				# standard build using kbuild
+				echo "Info: standard kbuild for device tree blob"
 			fi
-			DTS_BASE_NAME=`basename ${DTS_FILE} | awk -F "." '{print $1}'`
+
 			DTB_NAME=`echo ${KERNEL_IMAGE_BASE_NAME} | sed "s/${MACHINE}/${DTS_BASE_NAME}/g"`
 			DTB_SYMLINK_NAME=`echo ${KERNEL_IMAGE_SYMLINK_NAME} | sed "s/${MACHINE}/${DTS_BASE_NAME}/g"`
+
 			install -d ${DEPLOYDIR}
-			install -m 0644 ${B}/${DTS_BASE_NAME} ${DEPLOYDIR}/${DTB_NAME}.dtb
+
+			if [ -f arch/${ARCH}/boot/dts/${DTS_FILE} ]; then
+				install -m 0644 ${B}/arch/${ARCH}/boot/${DTS_BASE_NAME}.dtb ${DEPLOYDIR}/${DTB_NAME}.dtb
+			else
+				install -m 0644 ${B}/${DTS_BASE_NAME}.dtb ${DEPLOYDIR}/${DTB_NAME}.dtb
+			fi
+
 			cd ${DEPLOYDIR}
 			ln -sf ${DTB_NAME}.dtb ${DTB_SYMLINK_NAME}.dtb
 			cd -
-- 
1.8.2

