1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# This scripts adds local version information from the version
5# control system git.
6#
7# If something goes wrong, send a mail the kernel build mailinglist
8# (see MAINTAINERS) and CC Nico Schottelius
9# <nico-linuxsetlocalversion -at- schottelius.org>.
10#
11#
12
13usage() {
14	echo "Usage: $0 [--no-local] [srctree]" >&2
15	exit 1
16}
17
18no_local=false
19if test "$1" = "--no-local"; then
20	no_local=true
21	shift
22fi
23
24srctree=.
25if test $# -gt 0; then
26	srctree=$1
27	shift
28fi
29if test $# -gt 0 -o ! -d "$srctree"; then
30	usage
31fi
32
33scm_version()
34{
35	local short=false
36	local no_dirty=false
37	local tag
38
39	while [ $# -gt 0 ];
40	do
41		case "$1" in
42		--short)
43			short=true;;
44		--no-dirty)
45			no_dirty=true;;
46		esac
47		shift
48	done
49
50	cd "$srctree"
51
52	if test -n "$(git rev-parse --show-cdup 2>/dev/null)"; then
53		return
54	fi
55
56	if ! head=$(git rev-parse --verify HEAD 2>/dev/null); then
57		return
58	fi
59
60	# mainline kernel:  6.2.0-rc5  ->  v6.2-rc5
61	# stable kernel:    6.1.7      ->  v6.1.7
62	version_tag=v$(echo "${KERNELVERSION}" | sed -E 's/^([0-9]+\.[0-9]+)\.0(.*)$/\1\2/')
63
64	# If a localversion* file exists, and the corresponding
65	# annotated tag exists and is an ancestor of HEAD, use
66	# it. This is the case in linux-next.
67	tag=${file_localversion#-}
68	desc=
69	if [ -n "${tag}" ]; then
70		desc=$(git describe --match=$tag 2>/dev/null)
71	fi
72
73	# Otherwise, if a localversion* file exists, and the tag
74	# obtained by appending it to the tag derived from
75	# KERNELVERSION exists and is an ancestor of HEAD, use
76	# it. This is e.g. the case in linux-rt.
77	if [ -z "${desc}" ] && [ -n "${file_localversion}" ]; then
78		tag="${version_tag}${file_localversion}"
79		desc=$(git describe --match=$tag 2>/dev/null)
80	fi
81
82	# Otherwise, default to the annotated tag derived from KERNELVERSION.
83	if [ -z "${desc}" ]; then
84		tag="${version_tag}"
85		desc=$(git describe --match=$tag 2>/dev/null)
86	fi
87
88	# If we are at the tagged commit, we ignore it because the version is
89	# well-defined.
90	if [ "${tag}" != "${desc}" ]; then
91
92		# If only the short version is requested, don't bother
93		# running further git commands
94		if $short; then
95			echo "+"
96			return
97		fi
98		# If we are past the tagged commit, we pretty print it.
99		# (like 6.1.0-14595-g292a089d78d3)
100		if [ -n "${desc}" ]; then
101			echo "${desc}" | awk -F- '{printf("-%05d", $(NF-1))}'
102		fi
103
104		# Add -g and exactly 12 hex chars.
105		printf '%s%s' -g "$(echo $head | cut -c1-12)"
106	fi
107
108	if ${no_dirty}; then
109		return
110	fi
111
112	# Check for uncommitted changes.
113	# This script must avoid any write attempt to the source tree, which
114	# might be read-only.
115	# You cannot use 'git describe --dirty' because it tries to create
116	# .git/index.lock .
117	# First, with git-status, but --no-optional-locks is only supported in
118	# git >= 2.14, so fall back to git-diff-index if it fails. Note that
119	# git-diff-index does not refresh the index, so it may give misleading
120	# results.
121	# See git-update-index(1), git-diff-index(1), and git-status(1).
122	if {
123		git --no-optional-locks status -uno --porcelain 2>/dev/null ||
124		git diff-index --name-only HEAD
125	} | read dummy; then
126		printf '%s' -dirty
127	fi
128}
129
130collect_files()
131{
132	local file res=
133
134	for file; do
135		case "$file" in
136		*\~*)
137			continue
138			;;
139		esac
140		if test -e "$file"; then
141			res="$res$(cat "$file")"
142		fi
143	done
144	echo "$res"
145}
146
147if [ -z "${KERNELVERSION}" ]; then
148	echo "KERNELVERSION is not set" >&2
149	exit 1
150fi
151
152# localversion* files in the build and source directory
153file_localversion="$(collect_files localversion*)"
154if test ! "$srctree" -ef .; then
155	file_localversion="${file_localversion}$(collect_files "$srctree"/localversion*)"
156fi
157
158if ${no_local}; then
159	echo "${KERNELVERSION}$(scm_version --no-dirty)"
160	exit 0
161fi
162
163if ! test -e include/config/auto.conf; then
164	echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
165	exit 1
166fi
167
168# version string from CONFIG_LOCALVERSION
169config_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf)
170
171# scm version string if not at the kernel version tag or at the file_localversion
172if grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then
173	# full scm version string
174	scm_version="$(scm_version)"
175elif [ "${LOCALVERSION+set}" != "set" ]; then
176	# If the variable LOCALVERSION is not set, append a plus
177	# sign if the repository is not in a clean annotated or
178	# signed tagged state (as git describe only looks at signed
179	# or annotated tags - git tag -a/-s).
180	#
181	# If the variable LOCALVERSION is set (including being set
182	# to an empty string), we don't want to append a plus sign.
183	scm_version="$(scm_version --short)"
184fi
185
186echo "${KERNELVERSION}${file_localversion}${config_localversion}${LOCALVERSION}${scm_version}"
187