gen_kheaders.sh 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. # This script generates an archive consisting of kernel headers
  4. # for CONFIG_IKHEADERS.
  5. set -e
  6. sfile="$(readlink -f "$0")"
  7. outdir="$(pwd)"
  8. tarfile=$1
  9. cpio_dir=$outdir/$tarfile.tmp
  10. dir_list="
  11. include/
  12. arch/$SRCARCH/include/
  13. "
  14. # Support incremental builds by skipping archive generation
  15. # if timestamps of files being archived are not changed.
  16. # This block is useful for debugging the incremental builds.
  17. # Uncomment it for debugging.
  18. # if [ ! -f /tmp/iter ]; then iter=1; echo 1 > /tmp/iter;
  19. # else iter=$(($(cat /tmp/iter) + 1)); echo $iter > /tmp/iter; fi
  20. # find $all_dirs -name "*.h" | xargs ls -l > /tmp/ls-$iter
  21. all_dirs=
  22. if [ "$building_out_of_srctree" ]; then
  23. for d in $dir_list; do
  24. all_dirs="$all_dirs $srctree/$d"
  25. done
  26. fi
  27. all_dirs="$all_dirs $dir_list"
  28. # include/generated/compile.h is ignored because it is touched even when none
  29. # of the source files changed.
  30. #
  31. # When Kconfig regenerates include/generated/autoconf.h, its timestamp is
  32. # updated, but the contents might be still the same. When any CONFIG option is
  33. # changed, Kconfig touches the corresponding timestamp file include/config/*.h.
  34. # Hence, the md5sum detects the configuration change anyway. We do not need to
  35. # check include/generated/autoconf.h explicitly.
  36. #
  37. # Ignore them for md5 calculation to avoid pointless regeneration.
  38. headers_md5="$(find $all_dirs -name "*.h" |
  39. grep -v "include/generated/compile.h" |
  40. grep -v "include/generated/autoconf.h" |
  41. xargs ls -l | md5sum | cut -d ' ' -f1)"
  42. # Any changes to this script will also cause a rebuild of the archive.
  43. this_file_md5="$(ls -l $sfile | md5sum | cut -d ' ' -f1)"
  44. if [ -f $tarfile ]; then tarfile_md5="$(md5sum $tarfile | cut -d ' ' -f1)"; fi
  45. if [ -f kernel/kheaders.md5 ] &&
  46. [ "$(head -n 1 kernel/kheaders.md5)" = "$headers_md5" ] &&
  47. [ "$(head -n 2 kernel/kheaders.md5 | tail -n 1)" = "$this_file_md5" ] &&
  48. [ "$(tail -n 1 kernel/kheaders.md5)" = "$tarfile_md5" ]; then
  49. exit
  50. fi
  51. if [ "${quiet}" != "silent_" ]; then
  52. echo " GEN $tarfile"
  53. fi
  54. rm -rf $cpio_dir
  55. mkdir $cpio_dir
  56. if [ "$building_out_of_srctree" ]; then
  57. (
  58. cd $srctree
  59. for f in $dir_list
  60. do find "$f" -name "*.h";
  61. done | cpio --quiet -pd $cpio_dir
  62. )
  63. fi
  64. # The second CPIO can complain if files already exist which can happen with out
  65. # of tree builds having stale headers in srctree. Just silence CPIO for now.
  66. for f in $dir_list;
  67. do find "$f" -name "*.h";
  68. done | cpio --quiet -pd $cpio_dir >/dev/null 2>&1
  69. # Remove comments except SDPX lines
  70. find $cpio_dir -type f -print0 |
  71. xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;'
  72. # Create archive and try to normalize metadata for reproducibility.
  73. # For compatibility with older versions of tar, files are fed to tar
  74. # pre-sorted, as --sort=name might not be available.
  75. find $cpio_dir -printf "./%P\n" | LC_ALL=C sort | \
  76. tar "${KBUILD_BUILD_TIMESTAMP:+--mtime=$KBUILD_BUILD_TIMESTAMP}" \
  77. --owner=0 --group=0 --numeric-owner --no-recursion \
  78. -I $XZ -cf $tarfile -C $cpio_dir/ -T - > /dev/null
  79. echo $headers_md5 > kernel/kheaders.md5
  80. echo "$this_file_md5" >> kernel/kheaders.md5
  81. echo "$(md5sum $tarfile | cut -d ' ' -f1)" >> kernel/kheaders.md5
  82. rm -rf $cpio_dir