blob: 3b13445239ef59971941f6b2ee55632eda955f12 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4TARGET=$1
5ARCH=$2
6SMP=$3
7PREEMPT=$4
8PREEMPT_RT=$5
9CC_VERSION="$6"
10LD=$7
11
12vecho() { [ "${quiet}" = "silent_" ] || echo "$@" ; }
13
14# If compile.h exists already and we don't own autoconf.h
15# (i.e. we're not the same user who did make *config), don't
16# modify compile.h
17# So "sudo make install" won't change the "compiled by <user>"
18# do "compiled by root"
19
20if [ -r $TARGET -a ! -O include/generated/autoconf.h ]; then
21 vecho " SKIPPED $TARGET"
22 exit 0
23fi
24
25# Do not expand names
26set -f
27
28# Fix the language to get consistent output
29LC_ALL=C
30export LC_ALL
31
32if [ -z "$KBUILD_BUILD_VERSION" ]; then
33 VERSION=$(cat .version 2>/dev/null || echo 1)
34else
35 VERSION=$KBUILD_BUILD_VERSION
36fi
37
38if [ -z "$KBUILD_BUILD_TIMESTAMP" ]; then
39 TIMESTAMP=`date`
40else
41 TIMESTAMP=$KBUILD_BUILD_TIMESTAMP
42fi
43if test -z "$KBUILD_BUILD_USER"; then
44 LINUX_COMPILE_BY=$(whoami | sed 's/\\/\\\\/')
45else
46 LINUX_COMPILE_BY=$KBUILD_BUILD_USER
47fi
48if test -z "$KBUILD_BUILD_HOST"; then
49 LINUX_COMPILE_HOST=`uname -n`
50else
51 LINUX_COMPILE_HOST=$KBUILD_BUILD_HOST
52fi
53
54UTS_VERSION="#$VERSION"
55CONFIG_FLAGS=""
56if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
57if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi
58if [ -n "$PREEMPT_RT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT_RT"; fi
59UTS_VERSION="$UTS_VERSION $CONFIG_FLAGS $TIMESTAMP"
60
61# Truncate to maximum length
62
63UTS_LEN=64
64UTS_TRUNCATE="cut -b -$UTS_LEN"
65
66# Generate a temporary compile.h
67
68{ echo /\* This file is auto generated, version $VERSION \*/
69 if [ -n "$CONFIG_FLAGS" ] ; then echo "/* $CONFIG_FLAGS */"; fi
70
71 echo \#define UTS_MACHINE \"$ARCH\"
72
73 echo \#define UTS_VERSION \"`echo $UTS_VERSION | $UTS_TRUNCATE`\"
74
75 echo \#define LINUX_COMPILE_BY \"`echo $LINUX_COMPILE_BY | $UTS_TRUNCATE`\"
76 echo \#define LINUX_COMPILE_HOST \"`echo $LINUX_COMPILE_HOST | $UTS_TRUNCATE`\"
77
78 LD_VERSION=$($LD -v | head -n1 | sed 's/(compatible with [^)]*)//' \
79 | sed 's/[[:space:]]*$//')
80 printf '#define LINUX_COMPILER "%s"\n' "$CC_VERSION, $LD_VERSION"
81} > .tmpcompile
82
83# Only replace the real compile.h if the new one is different,
84# in order to preserve the timestamp and avoid unnecessary
85# recompilations.
86# We don't consider the file changed if only the date/time changed,
87# unless KBUILD_BUILD_TIMESTAMP was explicitly set (e.g. for
88# reproducible builds with that value referring to a commit timestamp).
89# A kernel config change will increase the generation number, thus
90# causing compile.h to be updated (including date/time) due to the
91# changed comment in the
92# first line.
93
94if [ -z "$KBUILD_BUILD_TIMESTAMP" ]; then
95 IGNORE_PATTERN="UTS_VERSION"
96else
97 IGNORE_PATTERN="NOT_A_PATTERN_TO_BE_MATCHED"
98fi
99
100if [ -r $TARGET ] && \
101 grep -v $IGNORE_PATTERN $TARGET > .tmpver.1 && \
102 grep -v $IGNORE_PATTERN .tmpcompile > .tmpver.2 && \
103 cmp -s .tmpver.1 .tmpver.2; then
104 rm -f .tmpcompile
105else
106 vecho " UPD $TARGET"
107 mv -f .tmpcompile $TARGET
108fi
109rm -f .tmpver.1 .tmpver.2