• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# This scripts adds local version information from the version
5# control systems git, mercurial (hg) and subversion (svn).
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 [--save-scmversion] [srctree] [branch] [kmi-generation]" >&2
15	exit 1
16}
17
18scm_only=false
19srctree=.
20android_release=
21kmi_generation=
22if test "$1" = "--save-scmversion"; then
23	scm_only=true
24	shift
25fi
26if test $# -gt 0; then
27	srctree=$1
28	shift
29fi
30if test $# -gt 0; then
31	# Extract the Android release version. If there is no match, then return 255
32	# and clear the var $android_release
33	android_release=`echo "$1" | sed -e '/android[0-9]\{2,\}/!{q255}; \
34		s/^\(android[0-9]\{2,\}\)-.*/\1/'`
35	if test $? -ne 0; then
36		android_release=
37	fi
38	shift
39
40	if test $# -gt 0; then
41		kmi_generation=$1
42		[ $(expr $kmi_generation : '^[0-9]\+$') -eq 0 ] && usage
43		shift
44	fi
45fi
46if test $# -gt 0 -o ! -d "$srctree"; then
47	usage
48fi
49
50scm_version()
51{
52	local short
53	short=false
54
55	cd "$srctree"
56	if test -e .scmversion; then
57		cat .scmversion
58		return
59	fi
60	if test "$1" = "--short"; then
61		short=true
62	fi
63
64	# Check for git and a git repo.
65	if head=$(git rev-parse --verify HEAD 2>/dev/null); then
66
67		if [ -n "$android_release" ] && [ -n "$kmi_generation" ]; then
68			printf '%s' "-$android_release-$kmi_generation"
69		elif [ -n "$android_release" ]; then
70			printf '%s' "-$android_release"
71		fi
72
73		# If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
74		# it, because this version is defined in the top level Makefile.
75		if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then
76
77			# If only the short version is requested, don't bother
78			# running further git commands
79			if $short; then
80				echo "+"
81				return
82			fi
83			# If we are past a tagged commit (like
84			# "v2.6.30-rc5-302-g72357d5"), we pretty print it.
85			if atag="$(git describe 2>/dev/null)"; then
86				echo "$atag" | awk -F- '{printf("-%05d", $(NF-1))}'
87			fi
88
89			# Add -g and exactly 12 hex chars.
90			printf '%s%s' -g "$(echo $head | cut -c1-12)"
91		fi
92
93		# Check for uncommitted changes.
94		# This script must avoid any write attempt to the source tree,
95		# which might be read-only.
96		# You cannot use 'git describe --dirty' because it tries to
97		# create .git/index.lock .
98		# First, with git-status, but --no-optional-locks is only
99		# supported in git >= 2.14, so fall back to git-diff-index if
100		# it fails. Note that git-diff-index does not refresh the
101		# index, so it may give misleading results. See
102		# git-update-index(1), git-diff-index(1), and git-status(1).
103		if {
104			git --no-optional-locks status -uno --porcelain 2>/dev/null ||
105			git diff-index --name-only HEAD
106		} | read dummy; then
107			printf '%s' -dirty
108		fi
109	fi
110}
111
112collect_files()
113{
114	local file res=
115
116	for file; do
117		case "$file" in
118		*\~*)
119			continue
120			;;
121		esac
122		if test -e "$file"; then
123			res="$res$(cat "$file")"
124		fi
125	done
126	echo "$res"
127}
128
129if $scm_only; then
130	if test ! -e .scmversion; then
131		res=$(scm_version)
132		echo "$res" >.scmversion
133	fi
134	exit
135fi
136
137if test -e include/config/auto.conf; then
138	. include/config/auto.conf
139else
140	echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
141	exit 1
142fi
143
144# localversion* files in the build and source directory
145res="$(collect_files localversion*)"
146if test ! "$srctree" -ef .; then
147	res="$res$(collect_files "$srctree"/localversion*)"
148fi
149
150# CONFIG_LOCALVERSION and LOCALVERSION (if set)
151res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
152
153# scm version string if not at a tagged commit
154if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
155	# full scm version string
156	res="$res$(scm_version)"
157elif [ "${LOCALVERSION+set}" != "set" ]; then
158	# If the variable LOCALVERSION is not set, append a plus
159	# sign if the repository is not in a clean annotated or
160	# signed tagged state (as git describe only looks at signed
161	# or annotated tags - git tag -a/-s).
162	#
163	# If the variable LOCALVERSION is set (including being set
164	# to an empty string), we don't want to append a plus sign.
165	scm=$(scm_version --short)
166	res="$res${scm:++}"
167fi
168
169# finally, add the abXXX number if BUILD_NUMBER is set
170if test -n "${BUILD_NUMBER}"; then
171	res="$res-ab${BUILD_NUMBER}"
172fi
173
174echo "$res"
175