Update script to copy in extra generated files.

There are files in generated/asm that simply include asm-generic files. The
script now copies any file in generated/asm that also exists in asm-generic.

Change-Id: I075161c68624e9e9e81797224831988ce02220eb
diff --git a/libc/kernel/tools/generate_uapi_headers.sh b/libc/kernel/tools/generate_uapi_headers.sh
index 2a85630..de89838 100755
--- a/libc/kernel/tools/generate_uapi_headers.sh
+++ b/libc/kernel/tools/generate_uapi_headers.sh
@@ -80,6 +80,25 @@
   fi
 }
 
+function copy_if_exists () {
+  local check_dir=$1
+  local src_dir=$2
+  local tgt_dir=$3
+
+  mkdir -p ${tgt_dir}
+
+  # This only works if none of the filenames have spaces.
+  for file in $(ls -d ${src_dir}/* 2> /dev/null); do
+    if [[ -f  "${file}" ]] && [[ "${file}" =~ .h$ ]]; then
+      # Check that this file exists in check_dir.
+      header=$(basename ${file})
+      if [[ -f "${check_dir}/${header}" ]]; then
+        cp ${file} ${tgt_dir}
+      fi
+    fi
+  done
+}
+
 trap cleanup EXIT
 # This automatically triggers a call to cleanup.
 trap "exit 1" HUP INT TERM TSTP
@@ -158,9 +177,11 @@
 # Copy all of the include/uapi files to the kernel headers uapi directory.
 copy_hdrs "${KERNEL_DIR}/common/include/uapi" "${ANDROID_KERNEL_DIR}/uapi"
 
+# Copy the staging files to uapi/linux.
 copy_hdrs "${KERNEL_DIR}/common/drivers/staging/android/uapi" \
           "${ANDROID_KERNEL_DIR}/uapi/linux" "no-copy-dirs"
 
+# Copy the generated headers.
 copy_hdrs "${KERNEL_DIR}/common/include/generated/uapi" \
           "${ANDROID_KERNEL_DIR}/uapi"
 
@@ -171,10 +192,16 @@
   else
     tgt_arch="asm-${arch}"
   fi
-  # Copy arch headers first.
+  # Copy arch headers.
   copy_hdrs "${KERNEL_DIR}/common/arch/${arch}/include/uapi" \
             "${ANDROID_KERNEL_DIR}/uapi/${tgt_arch}"
-  # Copy the generated arch header files.
+  # Copy the generated arch headers.
   copy_hdrs "${KERNEL_DIR}/common/arch/${arch}/include/generated/uapi" \
             "${ANDROID_KERNEL_DIR}/uapi/${tgt_arch}"
+
+  # Special copy of generated header files from arch/<ARCH>/generated/asm that
+  # also exist in uapi/asm-generic.
+  copy_if_exists "${KERNEL_DIR}/common/include/uapi/asm-generic" \
+                 "${KERNEL_DIR}/common/arch/${arch}/include/generated/asm" \
+                 "${ANDROID_KERNEL_DIR}/uapi/${tgt_arch}/asm"
 done