xref: /openbmc/linux/scripts/setlocalversion (revision 05e96e96)
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
533252572SNico Schottelius# control systems git, mercurial (hg) and subversion (svn).
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() {
14*05e96e96SMasahiro Yamada	echo "Usage: $0 [--no-local] [srctree]" >&2
15117a93dbSRene Scharfe	exit 1
16aaebf433SRyan Anderson}
17aaebf433SRyan Anderson
18*05e96e96SMasahiro Yamadano_local=false
19*05e96e96SMasahiro Yamadaif test "$1" = "--no-local"; then
20*05e96e96SMasahiro Yamada	no_local=true
21*05e96e96SMasahiro Yamada	shift
22*05e96e96SMasahiro Yamadafi
23*05e96e96SMasahiro 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{
35*05e96e96SMasahiro Yamada	local short=false
36*05e96e96SMasahiro Yamada	local no_dirty=false
376ab7e1f9SMasahiro Yamada	local tag
38*05e96e96SMasahiro Yamada
39*05e96e96SMasahiro Yamada	while [ $# -gt 0 ];
40*05e96e96SMasahiro Yamada	do
41*05e96e96SMasahiro Yamada		case "$1" in
42*05e96e96SMasahiro Yamada		--short)
43*05e96e96SMasahiro Yamada			short=true;;
44*05e96e96SMasahiro Yamada		--no-dirty)
45*05e96e96SMasahiro Yamada			no_dirty=true;;
46*05e96e96SMasahiro Yamada		esac
47*05e96e96SMasahiro Yamada		shift
48*05e96e96SMasahiro 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	# If a localversion*' file and the corresponding annotated tag exist,
616ab7e1f9SMasahiro Yamada	# use it. This is the case in linux-next.
626ab7e1f9SMasahiro Yamada	tag=${file_localversion#-}
636ab7e1f9SMasahiro Yamada	tag=$(git describe --exact-match --match=$tag $tag 2>/dev/null)
646ab7e1f9SMasahiro Yamada
656ab7e1f9SMasahiro Yamada	# Otherwise, default to the annotated tag derived from KERNELVERSION.
666ab7e1f9SMasahiro Yamada	#   mainline kernel:  6.2.0-rc5  ->  v6.2-rc5
676ab7e1f9SMasahiro Yamada	#   stable kernel:    6.1.7      ->  v6.1.7
686ab7e1f9SMasahiro Yamada	if [ -z "${tag}" ]; then
696ab7e1f9SMasahiro Yamada		tag=v$(echo "${KERNELVERSION}" | sed -E 's/^([0-9]+\.[0-9]+)\.0(.*)$/\1\2/')
706ab7e1f9SMasahiro Yamada	fi
716ab7e1f9SMasahiro Yamada
726ab7e1f9SMasahiro Yamada	# If we are at the tagged commit, we ignore it because the version is
736ab7e1f9SMasahiro Yamada	# well-defined.
746ab7e1f9SMasahiro Yamada	if [ -z "$(git describe --exact-match --match=$tag 2>/dev/null)" ]; then
7533252572SNico Schottelius
7609155120SMichal Marek		# If only the short version is requested, don't bother
7709155120SMichal Marek		# running further git commands
7809155120SMichal Marek		if $short; then
7909155120SMichal Marek			echo "+"
8009155120SMichal Marek			return
8109155120SMichal Marek		fi
826ab7e1f9SMasahiro Yamada		# If we are past the tagged commit, we pretty print it.
836ab7e1f9SMasahiro Yamada		# (like 6.1.0-14595-g292a089d78d3)
846ab7e1f9SMasahiro Yamada		if atag="$(git describe --match=$tag 2>/dev/null)"; then
85630ff0faSMasahiro Yamada			echo "$atag" | awk -F- '{printf("-%05d", $(NF-1))}'
8656b2f070SSebastian Siewior		fi
87630ff0faSMasahiro Yamada
88630ff0faSMasahiro Yamada		# Add -g and exactly 12 hex chars.
89630ff0faSMasahiro Yamada		printf '%s%s' -g "$(echo $head | cut -c1-12)"
9033252572SNico Schottelius	fi
91aaebf433SRyan Anderson
92*05e96e96SMasahiro Yamada	if ${no_dirty}; then
93*05e96e96SMasahiro Yamada		return
94*05e96e96SMasahiro Yamada	fi
95*05e96e96SMasahiro Yamada
96ff64dd48SBrian Norris	# Check for uncommitted changes.
9775280bdfSMasahiro Yamada	# This script must avoid any write attempt to the source tree, which
9875280bdfSMasahiro Yamada	# might be read-only.
9975280bdfSMasahiro Yamada	# You cannot use 'git describe --dirty' because it tries to create
10075280bdfSMasahiro Yamada	# .git/index.lock .
10175280bdfSMasahiro Yamada	# First, with git-status, but --no-optional-locks is only supported in
10275280bdfSMasahiro Yamada	# git >= 2.14, so fall back to git-diff-index if it fails. Note that
10375280bdfSMasahiro Yamada	# git-diff-index does not refresh the index, so it may give misleading
10475280bdfSMasahiro Yamada	# results.
10575280bdfSMasahiro Yamada	# See git-update-index(1), git-diff-index(1), and git-status(1).
106ff64dd48SBrian Norris	if {
107ff64dd48SBrian Norris		git --no-optional-locks status -uno --porcelain 2>/dev/null ||
108ff64dd48SBrian Norris		git diff-index --name-only HEAD
109a2be76a3SMasahiro Yamada	} | read dummy; then
11024d49756SRyan Anderson		printf '%s' -dirty
111117a93dbSRene Scharfe	fi
11209155120SMichal Marek}
11309155120SMichal Marek
11409155120SMichal Marekcollect_files()
11509155120SMichal Marek{
1167a82e3faSMasahiro Yamada	local file res=
11709155120SMichal Marek
11809155120SMichal Marek	for file; do
11909155120SMichal Marek		case "$file" in
12009155120SMichal Marek		*\~*)
12109155120SMichal Marek			continue
12209155120SMichal Marek			;;
12309155120SMichal Marek		esac
12409155120SMichal Marek		if test -e "$file"; then
12509155120SMichal Marek			res="$res$(cat "$file")"
12609155120SMichal Marek		fi
12709155120SMichal Marek	done
12809155120SMichal Marek	echo "$res"
12909155120SMichal Marek}
13009155120SMichal Marek
131ec31f868SMasahiro Yamadaif [ -z "${KERNELVERSION}" ]; then
132ec31f868SMasahiro Yamada	echo "KERNELVERSION is not set" >&2
133ec31f868SMasahiro Yamada	exit 1
134ec31f868SMasahiro Yamadafi
135ec31f868SMasahiro Yamada
13609155120SMichal Marek# localversion* files in the build and source directory
137eed36d77SMasahiro Yamadafile_localversion="$(collect_files localversion*)"
13809155120SMichal Marekif test ! "$srctree" -ef .; then
139eed36d77SMasahiro Yamada	file_localversion="${file_localversion}$(collect_files "$srctree"/localversion*)"
14009155120SMichal Marekfi
14109155120SMichal Marek
142*05e96e96SMasahiro Yamadaif ${no_local}; then
143*05e96e96SMasahiro Yamada	echo "${KERNELVERSION}$(scm_version --no-dirty)"
144*05e96e96SMasahiro Yamada	exit 0
145*05e96e96SMasahiro Yamadafi
146*05e96e96SMasahiro Yamada
147*05e96e96SMasahiro Yamadaif ! test -e include/config/auto.conf; then
148*05e96e96SMasahiro Yamada	echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
149*05e96e96SMasahiro Yamada	exit 1
150*05e96e96SMasahiro Yamadafi
151*05e96e96SMasahiro Yamada
152eed36d77SMasahiro Yamada# version string from CONFIG_LOCALVERSION
153129ab0d2SMasahiro Yamadaconfig_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf)
15409155120SMichal Marek
1556ab7e1f9SMasahiro Yamada# scm version string if not at the kernel version tag or at the file_localversion
1567d153696SMasahiro Yamadaif grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then
15709155120SMichal Marek	# full scm version string
158eed36d77SMasahiro Yamada	scm_version="$(scm_version)"
1595df99becSMikulas Patockaelif [ "${LOCALVERSION+set}" != "set" ]; then
1605df99becSMikulas Patocka	# If the variable LOCALVERSION is not set, append a plus
1615df99becSMikulas Patocka	# sign if the repository is not in a clean annotated or
1625df99becSMikulas Patocka	# signed tagged state (as git describe only looks at signed
1635df99becSMikulas Patocka	# or annotated tags - git tag -a/-s).
1645df99becSMikulas Patocka	#
1655df99becSMikulas Patocka	# If the variable LOCALVERSION is set (including being set
1665df99becSMikulas Patocka	# to an empty string), we don't want to append a plus sign.
167eed36d77SMasahiro Yamada	scm_version="$(scm_version --short)"
16809155120SMichal Marekfi
16909155120SMichal Marek
170eed36d77SMasahiro Yamadaecho "${KERNELVERSION}${file_localversion}${config_localversion}${LOCALVERSION}${scm_version}"
171