xref: /linux-6.15/kernel/gen_kheaders.sh (revision 479fde49)
11463f74fSMasahiro Yamada#!/bin/sh
2f7b101d3SJoel Fernandes (Google)# SPDX-License-Identifier: GPL-2.0
3f7b101d3SJoel Fernandes (Google)
4f7b101d3SJoel Fernandes (Google)# This script generates an archive consisting of kernel headers
5f7b101d3SJoel Fernandes (Google)# for CONFIG_IKHEADERS.
6f7b101d3SJoel Fernandes (Google)set -e
77199ff7dSMasahiro Yamadasfile="$(readlink -f "$0")"
8f7b101d3SJoel Fernandes (Google)outdir="$(pwd)"
9f7b101d3SJoel Fernandes (Google)tarfile=$1
10fd2a118cSMasahiro Yamadatmpdir=$outdir/${tarfile%/*}/.tmp_dir
11f7b101d3SJoel Fernandes (Google)
127199ff7dSMasahiro Yamadadir_list="
13f7b101d3SJoel Fernandes (Google)include/
14f7b101d3SJoel Fernandes (Google)arch/$SRCARCH/include/
15f7b101d3SJoel Fernandes (Google)"
16f7b101d3SJoel Fernandes (Google)
17f7b101d3SJoel Fernandes (Google)# Support incremental builds by skipping archive generation
18f7b101d3SJoel Fernandes (Google)# if timestamps of files being archived are not changed.
19f7b101d3SJoel Fernandes (Google)
20f7b101d3SJoel Fernandes (Google)# This block is useful for debugging the incremental builds.
21f7b101d3SJoel Fernandes (Google)# Uncomment it for debugging.
221457dc9eSJoel Fernandes (Google)# if [ ! -f /tmp/iter ]; then iter=1; echo 1 > /tmp/iter;
231457dc9eSJoel Fernandes (Google)# else iter=$(($(cat /tmp/iter) + 1)); echo $iter > /tmp/iter; fi
240e11773eSMasahiro Yamada# find $all_dirs -name "*.h" | xargs ls -l > /tmp/ls-$iter
250e11773eSMasahiro Yamada
260e11773eSMasahiro Yamadaall_dirs=
270e11773eSMasahiro Yamadaif [ "$building_out_of_srctree" ]; then
280e11773eSMasahiro Yamada	for d in $dir_list; do
290e11773eSMasahiro Yamada		all_dirs="$all_dirs $srctree/$d"
300e11773eSMasahiro Yamada	done
310e11773eSMasahiro Yamadafi
320e11773eSMasahiro Yamadaall_dirs="$all_dirs $dir_list"
33f7b101d3SJoel Fernandes (Google)
342df8220cSMasahiro Yamada# include/generated/utsversion.h is ignored because it is generated after this
352df8220cSMasahiro Yamada# script is executed. (utsversion.h is unneeded for kheaders)
36f276031bSMasahiro Yamada#
37f276031bSMasahiro Yamada# When Kconfig regenerates include/generated/autoconf.h, its timestamp is
38f276031bSMasahiro Yamada# updated, but the contents might be still the same. When any CONFIG option is
390e0345b7SAlexey Dobriyan# changed, Kconfig touches the corresponding timestamp file include/config/*.
40f276031bSMasahiro Yamada# Hence, the md5sum detects the configuration change anyway. We do not need to
41f276031bSMasahiro Yamada# check include/generated/autoconf.h explicitly.
42f276031bSMasahiro Yamada#
43f276031bSMasahiro Yamada# Ignore them for md5 calculation to avoid pointless regeneration.
44de0cae92SMasahiro Yamadaheaders_md5="$(find $all_dirs -name "*.h" -a			\
45de0cae92SMasahiro Yamada		! -path include/generated/utsversion.h -a	\
46de0cae92SMasahiro Yamada		! -path include/generated/autoconf.h		|
47b60b7c2eSMasahiro Yamada		xargs ls -l | md5sum | cut -d ' ' -f1)"
480e11773eSMasahiro Yamada
497199ff7dSMasahiro Yamada# Any changes to this script will also cause a rebuild of the archive.
507199ff7dSMasahiro Yamadathis_file_md5="$(ls -l $sfile | md5sum | cut -d ' ' -f1)"
51f7b101d3SJoel Fernandes (Google)if [ -f $tarfile ]; then tarfile_md5="$(md5sum $tarfile | cut -d ' ' -f1)"; fi
52f7b101d3SJoel Fernandes (Google)if [ -f kernel/kheaders.md5 ] &&
530e11773eSMasahiro Yamada	[ "$(head -n 1 kernel/kheaders.md5)" = "$headers_md5" ] &&
540e11773eSMasahiro Yamada	[ "$(head -n 2 kernel/kheaders.md5 | tail -n 1)" = "$this_file_md5" ] &&
559a066357SMasahiro Yamada	[ "$(tail -n 1 kernel/kheaders.md5)" = "$tarfile_md5" ]; then
56f7b101d3SJoel Fernandes (Google)		exit
57f7b101d3SJoel Fernandes (Google)fi
58f7b101d3SJoel Fernandes (Google)
59f7b101d3SJoel Fernandes (Google)echo "  GEN     $tarfile"
60f7b101d3SJoel Fernandes (Google)
61fd2a118cSMasahiro Yamadarm -rf "${tmpdir}"
62fd2a118cSMasahiro Yamadamkdir "${tmpdir}"
63f7b101d3SJoel Fernandes (Google)
64ea79e516SMasahiro Yamadaif [ "$building_out_of_srctree" ]; then
651463f74fSMasahiro Yamada	(
661463f74fSMasahiro Yamada		cd $srctree
67ea79e516SMasahiro Yamada		for f in $dir_list
687199ff7dSMasahiro Yamada			do find "$f" -name "*.h";
6982a1978dSMasahiro Yamada		done | tar -c -f - -T - | tar -xf - -C "${tmpdir}"
701463f74fSMasahiro Yamada	)
71ea79e516SMasahiro Yamadafi
72f7b101d3SJoel Fernandes (Google)
737199ff7dSMasahiro Yamadafor f in $dir_list;
747199ff7dSMasahiro Yamada	do find "$f" -name "*.h";
7582a1978dSMasahiro Yamadadone | tar -c -f - -T - | tar -xf - -C "${tmpdir}"
76f7b101d3SJoel Fernandes (Google)
7741e86fe7SMasahiro Yamada# Always exclude include/generated/utsversion.h
7841e86fe7SMasahiro Yamada# Otherwise, the contents of the tarball may vary depending on the build steps.
79fd2a118cSMasahiro Yamadarm -f "${tmpdir}/include/generated/utsversion.h"
80f7b101d3SJoel Fernandes (Google)
81f7b101d3SJoel Fernandes (Google)# Remove comments except SDPX lines
82*41a00051SHONG Yifan# Use a temporary file to store directory contents to prevent find/xargs from
83*41a00051SHONG Yifan# seeing temporary files created by perl.
84*41a00051SHONG Yifanfind "${tmpdir}" -type f -print0 > "${tmpdir}.contents.txt"
85*41a00051SHONG Yifanxargs -0 -P8 -n1 \
86*41a00051SHONG Yifan	perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;' \
87*41a00051SHONG Yifan	< "${tmpdir}.contents.txt"
88*41a00051SHONG Yifanrm -f "${tmpdir}.contents.txt"
89f7b101d3SJoel Fernandes (Google)
90700dea5aSDmitry Goldin# Create archive and try to normalize metadata for reproducibility.
9186cdd2fdSDmitry Goldintar "${KBUILD_BUILD_TIMESTAMP:+--mtime=$KBUILD_BUILD_TIMESTAMP}" \
923bd27a84SMatthias Maennich    --owner=0 --group=0 --sort=name --numeric-owner --mode=u=rw,go=r,a+X \
93fd2a118cSMasahiro Yamada    -I $XZ -cf $tarfile -C "${tmpdir}/" . > /dev/null
94f7b101d3SJoel Fernandes (Google)
950e11773eSMasahiro Yamadaecho $headers_md5 > kernel/kheaders.md5
967199ff7dSMasahiro Yamadaecho "$this_file_md5" >> kernel/kheaders.md5
97f7b101d3SJoel Fernandes (Google)echo "$(md5sum $tarfile | cut -d ' ' -f1)" >> kernel/kheaders.md5
98f7b101d3SJoel Fernandes (Google)
99fd2a118cSMasahiro Yamadarm -rf "${tmpdir}"
100