xref: /openbmc/linux/scripts/setlocalversion (revision 01e89a4a)
1117a93dbSRene Scharfe#!/bin/sh
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
333252572SNico Schottelius#
433252572SNico Schottelius# This scripts adds local version information from the version
53354c64dSRasmus Villemoes# control system git.
633252572SNico Schottelius#
733252572SNico Schottelius# If something goes wrong, send a mail the kernel build mailinglist
833252572SNico Schottelius# (see MAINTAINERS) and CC Nico Schottelius
933252572SNico Schottelius# <nico-linuxsetlocalversion -at- schottelius.org>.
1033252572SNico Schottelius#
1133252572SNico Schottelius#
12aaebf433SRyan Anderson
13117a93dbSRene Scharfeusage() {
1405e96e96SMasahiro Yamada	echo "Usage: $0 [--no-local] [srctree]" >&2
15117a93dbSRene Scharfe	exit 1
16aaebf433SRyan Anderson}
17aaebf433SRyan Anderson
1805e96e96SMasahiro Yamadano_local=false
1905e96e96SMasahiro Yamadaif test "$1" = "--no-local"; then
2005e96e96SMasahiro Yamada	no_local=true
2105e96e96SMasahiro Yamada	shift
2205e96e96SMasahiro Yamadafi
2305e96e96SMasahiro Yamada
2409155120SMichal Mareksrctree=.
2509155120SMichal Marekif test $# -gt 0; then
2609155120SMichal Marek	srctree=$1
2709155120SMichal Marek	shift
2809155120SMichal Marekfi
2909155120SMichal Marekif test $# -gt 0 -o ! -d "$srctree"; then
3009155120SMichal Marek	usage
3109155120SMichal Marekfi
3209155120SMichal Marek
3309155120SMichal Marekscm_version()
3409155120SMichal Marek{
3505e96e96SMasahiro Yamada	local short=false
3605e96e96SMasahiro Yamada	local no_dirty=false
376ab7e1f9SMasahiro Yamada	local tag
3805e96e96SMasahiro Yamada
3905e96e96SMasahiro Yamada	while [ $# -gt 0 ];
4005e96e96SMasahiro Yamada	do
4105e96e96SMasahiro Yamada		case "$1" in
4205e96e96SMasahiro Yamada		--short)
4305e96e96SMasahiro Yamada			short=true;;
4405e96e96SMasahiro Yamada		--no-dirty)
4505e96e96SMasahiro Yamada			no_dirty=true;;
4605e96e96SMasahiro Yamada		esac
4705e96e96SMasahiro Yamada		shift
4805e96e96SMasahiro Yamada	done
4909155120SMichal Marek
5009155120SMichal Marek	cd "$srctree"
51aaebf433SRyan Anderson
5275280bdfSMasahiro Yamada	if test -n "$(git rev-parse --show-cdup 2>/dev/null)"; then
5375280bdfSMasahiro Yamada		return
5475280bdfSMasahiro Yamada	fi
5533252572SNico Schottelius
5675280bdfSMasahiro Yamada	if ! head=$(git rev-parse --verify HEAD 2>/dev/null); then
5775280bdfSMasahiro Yamada		return
5875280bdfSMasahiro Yamada	fi
5975280bdfSMasahiro Yamada
606ab7e1f9SMasahiro Yamada	# mainline kernel:  6.2.0-rc5  ->  v6.2-rc5
616ab7e1f9SMasahiro Yamada	# stable kernel:    6.1.7      ->  v6.1.7
62*01e89a4aSRasmus Villemoes	version_tag=v$(echo "${KERNELVERSION}" | sed -E 's/^([0-9]+\.[0-9]+)\.0(.*)$/\1\2/')
63*01e89a4aSRasmus Villemoes
64*01e89a4aSRasmus Villemoes	# If a localversion* file exists, and the corresponding
65*01e89a4aSRasmus Villemoes	# annotated tag exists and is an ancestor of HEAD, use
66*01e89a4aSRasmus Villemoes	# it. This is the case in linux-next.
67*01e89a4aSRasmus Villemoes	tag=${file_localversion#-}
68*01e89a4aSRasmus Villemoes	desc=
69*01e89a4aSRasmus Villemoes	if [ -n "${tag}" ]; then
70*01e89a4aSRasmus Villemoes		desc=$(git describe --match=$tag 2>/dev/null)
71*01e89a4aSRasmus Villemoes	fi
72*01e89a4aSRasmus Villemoes
73*01e89a4aSRasmus Villemoes	# Otherwise, if a localversion* file exists, and the tag
74*01e89a4aSRasmus Villemoes	# obtained by appending it to the tag derived from
75*01e89a4aSRasmus Villemoes	# KERNELVERSION exists and is an ancestor of HEAD, use
76*01e89a4aSRasmus Villemoes	# it. This is e.g. the case in linux-rt.
77*01e89a4aSRasmus Villemoes	if [ -z "${desc}" ] && [ -n "${file_localversion}" ]; then
78*01e89a4aSRasmus Villemoes		tag="${version_tag}${file_localversion}"
79*01e89a4aSRasmus Villemoes		desc=$(git describe --match=$tag 2>/dev/null)
80*01e89a4aSRasmus Villemoes	fi
81*01e89a4aSRasmus Villemoes
82*01e89a4aSRasmus Villemoes	# Otherwise, default to the annotated tag derived from KERNELVERSION.
83*01e89a4aSRasmus Villemoes	if [ -z "${desc}" ]; then
84*01e89a4aSRasmus Villemoes		tag="${version_tag}"
85*01e89a4aSRasmus Villemoes		desc=$(git describe --match=$tag 2>/dev/null)
866ab7e1f9SMasahiro Yamada	fi
876ab7e1f9SMasahiro Yamada
886ab7e1f9SMasahiro Yamada	# If we are at the tagged commit, we ignore it because the version is
896ab7e1f9SMasahiro Yamada	# well-defined.
90*01e89a4aSRasmus Villemoes	if [ "${tag}" != "${desc}" ]; then
9133252572SNico Schottelius
9209155120SMichal Marek		# If only the short version is requested, don't bother
9309155120SMichal Marek		# running further git commands
9409155120SMichal Marek		if $short; then
9509155120SMichal Marek			echo "+"
9609155120SMichal Marek			return
9709155120SMichal Marek		fi
986ab7e1f9SMasahiro Yamada		# If we are past the tagged commit, we pretty print it.
996ab7e1f9SMasahiro Yamada		# (like 6.1.0-14595-g292a089d78d3)
100*01e89a4aSRasmus Villemoes		if [ -n "${desc}" ]; then
101*01e89a4aSRasmus Villemoes			echo "${desc}" | awk -F- '{printf("-%05d", $(NF-1))}'
10256b2f070SSebastian Siewior		fi
103630ff0faSMasahiro Yamada
104630ff0faSMasahiro Yamada		# Add -g and exactly 12 hex chars.
105630ff0faSMasahiro Yamada		printf '%s%s' -g "$(echo $head | cut -c1-12)"
10633252572SNico Schottelius	fi
107aaebf433SRyan Anderson
10805e96e96SMasahiro Yamada	if ${no_dirty}; then
10905e96e96SMasahiro Yamada		return
11005e96e96SMasahiro Yamada	fi
11105e96e96SMasahiro Yamada
112ff64dd48SBrian Norris	# Check for uncommitted changes.
11375280bdfSMasahiro Yamada	# This script must avoid any write attempt to the source tree, which
11475280bdfSMasahiro Yamada	# might be read-only.
11575280bdfSMasahiro Yamada	# You cannot use 'git describe --dirty' because it tries to create
11675280bdfSMasahiro Yamada	# .git/index.lock .
11775280bdfSMasahiro Yamada	# First, with git-status, but --no-optional-locks is only supported in
11875280bdfSMasahiro Yamada	# git >= 2.14, so fall back to git-diff-index if it fails. Note that
11975280bdfSMasahiro Yamada	# git-diff-index does not refresh the index, so it may give misleading
12075280bdfSMasahiro Yamada	# results.
12175280bdfSMasahiro Yamada	# See git-update-index(1), git-diff-index(1), and git-status(1).
122ff64dd48SBrian Norris	if {
123ff64dd48SBrian Norris		git --no-optional-locks status -uno --porcelain 2>/dev/null ||
124ff64dd48SBrian Norris		git diff-index --name-only HEAD
125a2be76a3SMasahiro Yamada	} | read dummy; then
12624d49756SRyan Anderson		printf '%s' -dirty
127117a93dbSRene Scharfe	fi
12809155120SMichal Marek}
12909155120SMichal Marek
13009155120SMichal Marekcollect_files()
13109155120SMichal Marek{
1327a82e3faSMasahiro Yamada	local file res=
13309155120SMichal Marek
13409155120SMichal Marek	for file; do
13509155120SMichal Marek		case "$file" in
13609155120SMichal Marek		*\~*)
13709155120SMichal Marek			continue
13809155120SMichal Marek			;;
13909155120SMichal Marek		esac
14009155120SMichal Marek		if test -e "$file"; then
14109155120SMichal Marek			res="$res$(cat "$file")"
14209155120SMichal Marek		fi
14309155120SMichal Marek	done
14409155120SMichal Marek	echo "$res"
14509155120SMichal Marek}
14609155120SMichal Marek
147ec31f868SMasahiro Yamadaif [ -z "${KERNELVERSION}" ]; then
148ec31f868SMasahiro Yamada	echo "KERNELVERSION is not set" >&2
149ec31f868SMasahiro Yamada	exit 1
150ec31f868SMasahiro Yamadafi
151ec31f868SMasahiro Yamada
15209155120SMichal Marek# localversion* files in the build and source directory
153eed36d77SMasahiro Yamadafile_localversion="$(collect_files localversion*)"
15409155120SMichal Marekif test ! "$srctree" -ef .; then
155eed36d77SMasahiro Yamada	file_localversion="${file_localversion}$(collect_files "$srctree"/localversion*)"
15609155120SMichal Marekfi
15709155120SMichal Marek
15805e96e96SMasahiro Yamadaif ${no_local}; then
15905e96e96SMasahiro Yamada	echo "${KERNELVERSION}$(scm_version --no-dirty)"
16005e96e96SMasahiro Yamada	exit 0
16105e96e96SMasahiro Yamadafi
16205e96e96SMasahiro Yamada
16305e96e96SMasahiro Yamadaif ! test -e include/config/auto.conf; then
16405e96e96SMasahiro Yamada	echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
16505e96e96SMasahiro Yamada	exit 1
16605e96e96SMasahiro Yamadafi
16705e96e96SMasahiro Yamada
168eed36d77SMasahiro Yamada# version string from CONFIG_LOCALVERSION
169129ab0d2SMasahiro Yamadaconfig_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf)
17009155120SMichal Marek
1716ab7e1f9SMasahiro Yamada# scm version string if not at the kernel version tag or at the file_localversion
1727d153696SMasahiro Yamadaif grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then
17309155120SMichal Marek	# full scm version string
174eed36d77SMasahiro Yamada	scm_version="$(scm_version)"
1755df99becSMikulas Patockaelif [ "${LOCALVERSION+set}" != "set" ]; then
1765df99becSMikulas Patocka	# If the variable LOCALVERSION is not set, append a plus
1775df99becSMikulas Patocka	# sign if the repository is not in a clean annotated or
1785df99becSMikulas Patocka	# signed tagged state (as git describe only looks at signed
1795df99becSMikulas Patocka	# or annotated tags - git tag -a/-s).
1805df99becSMikulas Patocka	#
1815df99becSMikulas Patocka	# If the variable LOCALVERSION is set (including being set
1825df99becSMikulas Patocka	# to an empty string), we don't want to append a plus sign.
183eed36d77SMasahiro Yamada	scm_version="$(scm_version --short)"
18409155120SMichal Marekfi
18509155120SMichal Marek
186eed36d77SMasahiro Yamadaecho "${KERNELVERSION}${file_localversion}${config_localversion}${LOCALVERSION}${scm_version}"
187