xref: /openbmc/linux/scripts/setlocalversion (revision c45cec53)
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
33try_tag() {
34	tag="$1"
35
36	# Is $tag an annotated tag?
37	[ "$(git cat-file -t "$tag" 2> /dev/null)" = tag ] || return 1
38
39	# Is it an ancestor of HEAD, and if so, how many commits are in $tag..HEAD?
40	# shellcheck disable=SC2046 # word splitting is the point here
41	set -- $(git rev-list --count --left-right "$tag"...HEAD 2> /dev/null)
42
43	# $1 is 0 if and only if $tag is an ancestor of HEAD. Use
44	# string comparison, because $1 is empty if the 'git rev-list'
45	# command somehow failed.
46	[ "$1" = 0 ] || return 1
47
48	# $2 is the number of commits in the range $tag..HEAD, possibly 0.
49	count="$2"
50
51	return 0
52}
53
54scm_version()
55{
56	local short=false
57	local no_dirty=false
58	local tag
59
60	while [ $# -gt 0 ];
61	do
62		case "$1" in
63		--short)
64			short=true;;
65		--no-dirty)
66			no_dirty=true;;
67		esac
68		shift
69	done
70
71	cd "$srctree"
72
73	if test -n "$(git rev-parse --show-cdup 2>/dev/null)"; then
74		return
75	fi
76
77	if ! head=$(git rev-parse --verify HEAD 2>/dev/null); then
78		return
79	fi
80
81	# mainline kernel:  6.2.0-rc5  ->  v6.2-rc5
82	# stable kernel:    6.1.7      ->  v6.1.7
83	version_tag=v$(echo "${KERNELVERSION}" | sed -E 's/^([0-9]+\.[0-9]+)\.0(.*)$/\1\2/')
84
85	# try_tag initializes count if the tag is usable.
86	count=
87
88	# If a localversion* file exists, and the corresponding
89	# annotated tag exists and is an ancestor of HEAD, use
90	# it. This is the case in linux-next.
91	if [ -n "${file_localversion#-}" ] ; then
92		try_tag "${file_localversion#-}"
93	fi
94
95	# Otherwise, if a localversion* file exists, and the tag
96	# obtained by appending it to the tag derived from
97	# KERNELVERSION exists and is an ancestor of HEAD, use
98	# it. This is e.g. the case in linux-rt.
99	if [ -z "${count}" ] && [ -n "${file_localversion}" ]; then
100		try_tag "${version_tag}${file_localversion}"
101	fi
102
103	# Otherwise, default to the annotated tag derived from KERNELVERSION.
104	if [ -z "${count}" ]; then
105		try_tag "${version_tag}"
106	fi
107
108	# If we are at the tagged commit, we ignore it because the
109	# version is well-defined. If none of the attempted tags exist
110	# or were usable, $count is still empty.
111	if [ -z "${count}" ] || [ "${count}" -gt 0 ]; then
112
113		# If only the short version is requested, don't bother
114		# running further git commands
115		if $short; then
116			echo "+"
117			return
118		fi
119
120		# If we are past the tagged commit, we pretty print it.
121		# (like 6.1.0-14595-g292a089d78d3)
122		if [ -n "${count}" ]; then
123			printf "%s%05d" "-" "${count}"
124		fi
125
126		# Add -g and exactly 12 hex chars.
127		printf '%s%.12s' -g "$head"
128	fi
129
130	if ${no_dirty}; then
131		return
132	fi
133
134	# Check for uncommitted changes.
135	# This script must avoid any write attempt to the source tree, which
136	# might be read-only.
137	# You cannot use 'git describe --dirty' because it tries to create
138	# .git/index.lock .
139	# First, with git-status, but --no-optional-locks is only supported in
140	# git >= 2.14, so fall back to git-diff-index if it fails. Note that
141	# git-diff-index does not refresh the index, so it may give misleading
142	# results.
143	# See git-update-index(1), git-diff-index(1), and git-status(1).
144	if {
145		git --no-optional-locks status -uno --porcelain 2>/dev/null ||
146		git diff-index --name-only HEAD
147	} | read dummy; then
148		printf '%s' -dirty
149	fi
150}
151
152collect_files()
153{
154	local file res=
155
156	for file; do
157		case "$file" in
158		*\~*)
159			continue
160			;;
161		esac
162		if test -e "$file"; then
163			res="$res$(cat "$file")"
164		fi
165	done
166	echo "$res"
167}
168
169if [ -z "${KERNELVERSION}" ]; then
170	echo "KERNELVERSION is not set" >&2
171	exit 1
172fi
173
174# localversion* files in the build and source directory
175file_localversion="$(collect_files localversion*)"
176if test ! "$srctree" -ef .; then
177	file_localversion="${file_localversion}$(collect_files "$srctree"/localversion*)"
178fi
179
180if ${no_local}; then
181	echo "${KERNELVERSION}$(scm_version --no-dirty)"
182	exit 0
183fi
184
185if ! test -e include/config/auto.conf; then
186	echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
187	exit 1
188fi
189
190# version string from CONFIG_LOCALVERSION
191config_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf)
192
193# scm version string if not at the kernel version tag or at the file_localversion
194if grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then
195	# full scm version string
196	scm_version="$(scm_version)"
197elif [ "${LOCALVERSION+set}" != "set" ]; then
198	# If the variable LOCALVERSION is not set, append a plus
199	# sign if the repository is not in a clean annotated or
200	# signed tagged state (as git describe only looks at signed
201	# or annotated tags - git tag -a/-s).
202	#
203	# If the variable LOCALVERSION is set (including being set
204	# to an empty string), we don't want to append a plus sign.
205	scm_version="$(scm_version --short)"
206fi
207
208echo "${KERNELVERSION}${file_localversion}${config_localversion}${LOCALVERSION}${scm_version}"
209