xref: /linux-6.15/kernel/gen_kheaders.sh (revision ea79e516)
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
7sfile="$(readlink -f "$0")"
8outdir="$(pwd)"
9tarfile=$1
10cpio_dir=$outdir/$tarfile.tmp
11
12dir_list="
13include/
14arch/$SRCARCH/include/
15"
16
17# Support incremental builds by skipping archive generation
18# if timestamps of files being archived are not changed.
19
20# This block is useful for debugging the incremental builds.
21# Uncomment it for debugging.
22# if [ ! -f /tmp/iter ]; then iter=1; echo 1 > /tmp/iter;
23# else iter=$(($(cat /tmp/iter) + 1)); echo $iter > /tmp/iter; fi
24# find $all_dirs -name "*.h" | xargs ls -l > /tmp/ls-$iter
25
26all_dirs=
27if [ "$building_out_of_srctree" ]; then
28	for d in $dir_list; do
29		all_dirs="$all_dirs $srctree/$d"
30	done
31fi
32all_dirs="$all_dirs $dir_list"
33
34# include/generated/compile.h is ignored because it is touched even when none
35# of the source files changed. This causes pointless regeneration, so let us
36# ignore them for md5 calculation.
37headers_md5="$(find $all_dirs -name "*.h"			|
38		grep -v "include/generated/compile.h"	|
39		grep -v "include/generated/autoconf.h"	|
40		xargs ls -l | md5sum | cut -d ' ' -f1)"
41
42# Any changes to this script will also cause a rebuild of the archive.
43this_file_md5="$(ls -l $sfile | md5sum | cut -d ' ' -f1)"
44if [ -f $tarfile ]; then tarfile_md5="$(md5sum $tarfile | cut -d ' ' -f1)"; fi
45if [ -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
50fi
51
52if [ "${quiet}" != "silent_" ]; then
53       echo "  GEN     $tarfile"
54fi
55
56rm -rf $cpio_dir
57mkdir $cpio_dir
58
59if [ "$building_out_of_srctree" ]; then
60	pushd $srctree > /dev/null
61	for f in $dir_list
62		do find "$f" -name "*.h";
63	done | cpio --quiet -pd $cpio_dir
64	popd > /dev/null
65fi
66
67# The second CPIO can complain if files already exist which can happen with out
68# of tree builds having stale headers in srctree. Just silence CPIO for now.
69for f in $dir_list;
70	do find "$f" -name "*.h";
71done | cpio --quiet -pd $cpio_dir >/dev/null 2>&1
72
73# Remove comments except SDPX lines
74find $cpio_dir -type f -print0 |
75	xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;'
76
77# Create archive and try to normalize metadata for reproducibility.
78# For compatibility with older versions of tar, files are fed to tar
79# pre-sorted, as --sort=name might not be available.
80find $cpio_dir -printf "./%P\n" | LC_ALL=C sort | \
81    tar "${KBUILD_BUILD_TIMESTAMP:+--mtime=$KBUILD_BUILD_TIMESTAMP}" \
82    --owner=0 --group=0 --numeric-owner --no-recursion \
83    -Jcf $tarfile -C $cpio_dir/ -T - > /dev/null
84
85echo $headers_md5 > kernel/kheaders.md5
86echo "$this_file_md5" >> kernel/kheaders.md5
87echo "$(md5sum $tarfile | cut -d ' ' -f1)" >> kernel/kheaders.md5
88
89rm -rf $cpio_dir
90