gen_kheaders.sh 3.2 KB

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