build.sh: avoid unnecessary copies and reduce overall runtime

build.sh was staging the headers to be included in a temporary
directory. Not only did this lead to a huge amount of cp commands, in
general that approach did not scale very well. In addition, the tmp
directory used might have ended up repeatedly in the archive under
certain circumstances.

Therefore, teach build.sh to collect all header files to be archived in
one single `find` command and to create the final archive on the fly.

On my machine that reduced the runtime for this step from ~60s to ~2s.
That is just a rough measurement by observation.

Change-Id: Ib65aa7c5a76050e92d4f8dcb13d110886181cc53
Signed-off-by: Matthias Maennich <maennich@google.com>
diff --git a/build.sh b/build.sh
index 94eb2b5..7b9bc6f 100755
--- a/build.sh
+++ b/build.sh
@@ -314,20 +314,18 @@
 fi
 
 if [ -z "${SKIP_CP_KERNEL_HDR}" ] ; then
-	echo "========================================================"
-	KERNEL_HEADERS_TAR=${DIST_DIR}/kernel-headers.tar.gz
-	echo " Copying kernel headers to ${KERNEL_HEADERS_TAR}"
-	TMP_DIR="${OUT_DIR}/tmp"
-	TMP_KERNEL_HEADERS_CHILD="kernel-headers"
-	TMP_KERNEL_HEADERS_DIR=$TMP_DIR/$TMP_KERNEL_HEADERS_CHILD
-	CURDIR=$(pwd)
-	mkdir -p $TMP_KERNEL_HEADERS_DIR
-	cd $ROOT_DIR/$KERNEL_DIR; find arch -name *.h -exec cp --parents {} $TMP_KERNEL_HEADERS_DIR \;
-	cd $ROOT_DIR/$KERNEL_DIR; find include -name *.h -exec cp --parents {} $TMP_KERNEL_HEADERS_DIR \;
-	cd $OUT_DIR; find  -name *.h -exec cp --parents {} $TMP_KERNEL_HEADERS_DIR \;
-	tar -czvf $KERNEL_HEADERS_TAR --directory=$TMP_DIR $TMP_KERNEL_HEADERS_CHILD > /dev/null 2>&1
-	rm -rf $TMP_KERNEL_HEADERS_DIR
-	cd $CURDIR
+  echo "========================================================"
+  KERNEL_HEADERS_TAR=${DIST_DIR}/kernel-headers.tar.gz
+  echo " Copying kernel headers to ${KERNEL_HEADERS_TAR}"
+  pushd $ROOT_DIR/$KERNEL_DIR
+    find arch include $OUT_DIR -name *.h -print0               \
+            | tar -czf $KERNEL_HEADERS_TAR                     \
+              --absolute-names                                 \
+              --dereference                                    \
+              --transform "s,.*$OUT_DIR,,"                     \
+              --transform "s,^,kernel-headers/,"               \
+              --null -T -
+  popd
 fi
 
 echo "========================================================"