blob: 0c352525a4675a5eb24e3086adf8e2d32c1fdf82 [file] [log] [blame]
Rene Scharfe117a93d2006-01-04 20:42:03 +01001#!/bin/sh
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01002# SPDX-License-Identifier: GPL-2.0
Nico Schottelius33252572009-05-16 14:00:56 +02003#
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#
Ryan Andersonaaebf432005-07-31 04:57:49 -040012
Rene Scharfe117a93d2006-01-04 20:42:03 +010013usage() {
Will McVickerf0bec7a2020-07-09 15:33:45 -070014 echo "Usage: $0 [--save-scmversion] [srctree] [branch] [kmi-generation]" >&2
Rene Scharfe117a93d2006-01-04 20:42:03 +010015 exit 1
Ryan Andersonaaebf432005-07-31 04:57:49 -040016}
17
Michal Marek09155122010-06-17 15:14:58 +020018scm_only=false
19srctree=.
Will McVickerf0bec7a2020-07-09 15:33:45 -070020android_release=
21kmi_generation=
Michal Marekb003afe2010-07-15 10:36:37 +020022if test "$1" = "--save-scmversion"; then
Michal Marek09155122010-06-17 15:14:58 +020023 scm_only=true
24 shift
25fi
26if test $# -gt 0; then
27 srctree=$1
28 shift
29fi
Will McVickerf0bec7a2020-07-09 15:33:45 -070030if 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 else
45 usage
46 fi
47fi
Michal Marek09155122010-06-17 15:14:58 +020048if test $# -gt 0 -o ! -d "$srctree"; then
49 usage
50fi
Ryan Andersonaaebf432005-07-31 04:57:49 -040051
Michal Marek09155122010-06-17 15:14:58 +020052scm_version()
53{
Michał Górny6dc0c2f2010-07-18 10:26:40 +020054 local short
55 short=false
Nico Schottelius33252572009-05-16 14:00:56 +020056
Michal Marek09155122010-06-17 15:14:58 +020057 cd "$srctree"
58 if test -e .scmversion; then
Michał Górny6dc0c2f2010-07-18 10:26:40 +020059 cat .scmversion
Michal Marek09155122010-06-17 15:14:58 +020060 return
61 fi
62 if test "$1" = "--short"; then
63 short=true
64 fi
Nico Schottelius33252572009-05-16 14:00:56 +020065
Michal Marek09155122010-06-17 15:14:58 +020066 # Check for git and a git repo.
Franck Bui-Huu7593e092013-12-02 16:34:29 +010067 if test -z "$(git rev-parse --show-cdup 2>/dev/null)" &&
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +053068 head=$(git rev-parse --verify --short HEAD 2>/dev/null); then
Nico Schottelius33252572009-05-16 14:00:56 +020069
Will McVickerf0bec7a2020-07-09 15:33:45 -070070 if [ -n "$android_release" ] && [ -n "$kmi_generation" ]; then
71 printf '%s' "-$android_release-$kmi_generation"
72 fi
73
Michal Marek09155122010-06-17 15:14:58 +020074 # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
75 # it, because this version is defined in the top level Makefile.
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +053076 if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then
Michal Marek09155122010-06-17 15:14:58 +020077
78 # If only the short version is requested, don't bother
79 # running further git commands
80 if $short; then
81 echo "+"
82 return
83 fi
84 # If we are past a tagged commit (like
85 # "v2.6.30-rc5-302-g72357d5"), we pretty print it.
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +053086 if atag="$(git describe 2>/dev/null)"; then
Michal Marek09155122010-06-17 15:14:58 +020087 echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}'
88
89 # If we don't have a tag at all we print -g{commitish}.
90 else
91 printf '%s%s' -g $head
92 fi
Nico Schottelius33252572009-05-16 14:00:56 +020093 fi
Michal Marek09155122010-06-17 15:14:58 +020094
95 # Is this git on svn?
96 if git config --get svn-remote.svn.url >/dev/null; then
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +053097 printf -- '-svn%s' "$(git svn find-rev $head)"
Michal Marek09155122010-06-17 15:14:58 +020098 fi
99
Brian Norrisff64dd42018-11-14 18:11:18 -0800100 # Check for uncommitted changes.
101 # First, with git-status, but --no-optional-locks is only
102 # supported in git >= 2.14, so fall back to git-diff-index if
103 # it fails. Note that git-diff-index does not refresh the
104 # index, so it may give misleading results. See
105 # git-update-index(1), git-diff-index(1), and git-status(1).
106 if {
107 git --no-optional-locks status -uno --porcelain 2>/dev/null ||
108 git diff-index --name-only HEAD
109 } | grep -qvE '^(.. )?scripts/package'; then
Michal Marek09155122010-06-17 15:14:58 +0200110 printf '%s' -dirty
111 fi
112
113 # All done with git
114 return
Rene Scharfe117a93d2006-01-04 20:42:03 +0100115 fi
Ryan Andersonaaebf432005-07-31 04:57:49 -0400116
Michal Marek09155122010-06-17 15:14:58 +0200117 # Check for mercurial and a mercurial repo.
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +0530118 if test -d .hg && hgid=$(hg id 2>/dev/null); then
Mike Crowe38b34392011-01-12 00:53:52 -0500119 # Do we have an tagged version? If so, latesttagdistance == 1
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +0530120 if [ "$(hg log -r . --template '{latesttagdistance}')" = "1" ]; then
121 id=$(hg log -r . --template '{latesttag}')
Michal Marek09155122010-06-17 15:14:58 +0200122 printf '%s%s' -hg "$id"
Mike Crowe38b34392011-01-12 00:53:52 -0500123 else
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +0530124 tag=$(printf '%s' "$hgid" | cut -d' ' -f2)
Mike Crowe38b34392011-01-12 00:53:52 -0500125 if [ -z "$tag" -o "$tag" = tip ]; then
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +0530126 id=$(printf '%s' "$hgid" | sed 's/[+ ].*//')
Mike Crowe38b34392011-01-12 00:53:52 -0500127 printf '%s%s' -hg "$id"
128 fi
Michal Marek09155122010-06-17 15:14:58 +0200129 fi
130
131 # Are there uncommitted changes?
132 # These are represented by + after the changeset id.
133 case "$hgid" in
134 *+|*+\ *) printf '%s' -dirty ;;
135 esac
136
137 # All done with mercurial
138 return
Peter Korsgaardff80aa92008-12-02 21:58:06 +0100139 fi
140
Michal Marek09155122010-06-17 15:14:58 +0200141 # Check for svn and a svn repo.
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +0530142 if rev=$(LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'); then
143 rev=$(echo $rev | awk '{print $NF}')
Michal Marek09155122010-06-17 15:14:58 +0200144 printf -- '-svn%s' "$rev"
Nico Schotteliusa2bb90a2009-06-12 09:59:52 +0200145
Michal Marek09155122010-06-17 15:14:58 +0200146 # All done with svn
147 return
Rene Scharfe117a93d2006-01-04 20:42:03 +0100148 fi
Michal Marek09155122010-06-17 15:14:58 +0200149}
Aron Griffis3dce1742007-11-28 16:55:44 -0500150
Michal Marek09155122010-06-17 15:14:58 +0200151collect_files()
152{
Masahiro Yamada7a82e3f2019-10-01 21:17:24 +0900153 local file res=
Michal Marek09155122010-06-17 15:14:58 +0200154
155 for file; do
156 case "$file" in
157 *\~*)
158 continue
159 ;;
160 esac
161 if test -e "$file"; then
162 res="$res$(cat "$file")"
163 fi
164 done
165 echo "$res"
166}
167
168if $scm_only; then
Michal Marekb003afe2010-07-15 10:36:37 +0200169 if test ! -e .scmversion; then
170 res=$(scm_version)
171 echo "$res" >.scmversion
172 fi
Aron Griffis3dce1742007-11-28 16:55:44 -0500173 exit
174fi
175
Michal Marek09155122010-06-17 15:14:58 +0200176if test -e include/config/auto.conf; then
Michał Górny6dc0c2f2010-07-18 10:26:40 +0200177 . include/config/auto.conf
Michal Marek09155122010-06-17 15:14:58 +0200178else
Wolfram Sang78283edf2016-06-06 21:00:38 +0200179 echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
Michal Marek09155122010-06-17 15:14:58 +0200180 exit 1
181fi
Aron Griffis3dce1742007-11-28 16:55:44 -0500182
Michal Marek09155122010-06-17 15:14:58 +0200183# localversion* files in the build and source directory
184res="$(collect_files localversion*)"
185if test ! "$srctree" -ef .; then
186 res="$res$(collect_files "$srctree"/localversion*)"
187fi
188
189# CONFIG_LOCALVERSION and LOCALVERSION (if set)
190res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
191
192# scm version string if not at a tagged commit
193if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
194 # full scm version string
195 res="$res$(scm_version)"
196else
Michael Prokopc3e2f192010-09-06 11:57:19 +0200197 # append a plus sign if the repository is not in a clean
198 # annotated or signed tagged state (as git describe only
199 # looks at signed or annotated tags - git tag -a/-s) and
200 # LOCALVERSION= is not specified
Michal Marek09155122010-06-17 15:14:58 +0200201 if test "${LOCALVERSION+set}" != "set"; then
202 scm=$(scm_version --short)
203 res="$res${scm:++}"
Aron Griffis3dce1742007-11-28 16:55:44 -0500204 fi
Rene Scharfe117a93d2006-01-04 20:42:03 +0100205fi
Bryan Wuba3d05f2008-02-03 14:13:26 +0800206
Michal Marek09155122010-06-17 15:14:58 +0200207echo "$res"