xref: /linux/kernel/gen_kheaders.sh (revision b0f84a84fff180718995b1269da2988e5b28be42)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# This script generates an archive consisting of kernel headers
5# for CONFIG_IKHEADERS.
6set -e
7spath="$(dirname "$(readlink -f "$0")")"
8kroot="$spath/.."
9outdir="$(pwd)"
10tarfile=$1
11cpio_dir=$outdir/$tarfile.tmp
12
13# Script filename relative to the kernel source root
14# We add it to the archive because it is small and any changes
15# to this script will also cause a rebuild of the archive.
16sfile="$(realpath --relative-to $kroot "$(readlink -f "$0")")"
17
18src_file_list="
19include/
20arch/$SRCARCH/include/
21$sfile
22"
23
24obj_file_list="
25include/
26arch/$SRCARCH/include/
27"
28
29# Support incremental builds by skipping archive generation
30# if timestamps of files being archived are not changed.
31
32# This block is useful for debugging the incremental builds.
33# Uncomment it for debugging.
34# if [ ! -f /tmp/iter ]; then iter=1; echo 1 > /tmp/iter;
35# else iter=$(($(cat /tmp/iter) + 1)); echo $iter > /tmp/iter; fi
36# find $src_file_list -type f | xargs ls -lR > /tmp/src-ls-$iter
37# find $obj_file_list -type f | xargs ls -lR > /tmp/obj-ls-$iter
38
39# include/generated/compile.h is ignored because it is touched even when none
40# of the source files changed. This causes pointless regeneration, so let us
41# ignore them for md5 calculation.
42pushd $kroot > /dev/null
43src_files_md5="$(find $src_file_list -type f                       |
44		grep -v "include/generated/compile.h"		   |
45		grep -v "include/generated/autoconf.h"		   |
46		grep -v "include/config/auto.conf"		   |
47		grep -v "include/config/auto.conf.cmd"		   |
48		grep -v "include/config/tristate.conf"		   |
49		xargs ls -lR | md5sum | cut -d ' ' -f1)"
50popd > /dev/null
51obj_files_md5="$(find $obj_file_list -type f                       |
52		grep -v "include/generated/compile.h"		   |
53		grep -v "include/generated/autoconf.h"		   |
54		grep -v "include/config/auto.conf"                 |
55		grep -v "include/config/auto.conf.cmd"		   |
56		grep -v "include/config/tristate.conf"		   |
57		xargs ls -lR | md5sum | cut -d ' ' -f1)"
58
59if [ -f $tarfile ]; then tarfile_md5="$(md5sum $tarfile | cut -d ' ' -f1)"; fi
60if [ -f kernel/kheaders.md5 ] &&
61	[ "$(cat kernel/kheaders.md5|head -1)" == "$src_files_md5" ] &&
62	[ "$(cat kernel/kheaders.md5|head -2|tail -1)" == "$obj_files_md5" ] &&
63	[ "$(cat kernel/kheaders.md5|tail -1)" == "$tarfile_md5" ]; then
64		exit
65fi
66
67if [ "${quiet}" != "silent_" ]; then
68       echo "  GEN     $tarfile"
69fi
70
71rm -rf $cpio_dir
72mkdir $cpio_dir
73
74pushd $kroot > /dev/null
75for f in $src_file_list;
76	do find "$f" ! -name "*.cmd" ! -name ".*";
77done | cpio --quiet -pd $cpio_dir
78popd > /dev/null
79
80# The second CPIO can complain if files already exist which can
81# happen with out of tree builds. Just silence CPIO for now.
82for f in $obj_file_list;
83	do find "$f" ! -name "*.cmd" ! -name ".*";
84done | cpio --quiet -pd $cpio_dir >/dev/null 2>&1
85
86# Remove comments except SDPX lines
87find $cpio_dir -type f -print0 |
88	xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;'
89
90tar -Jcf $tarfile -C $cpio_dir/ . > /dev/null
91
92echo "$src_files_md5" >  kernel/kheaders.md5
93echo "$obj_files_md5" >> kernel/kheaders.md5
94echo "$(md5sum $tarfile | cut -d ' ' -f1)" >> kernel/kheaders.md5
95
96rm -rf $cpio_dir
97