blob: 165786b2ab6dd0cc2d526bd5be93e918227ad2a4 [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
Will McVickerf0bec7a2020-07-09 15:33:45 -070044 fi
45fi
Michal Marek09155122010-06-17 15:14:58 +020046if test $# -gt 0 -o ! -d "$srctree"; then
47 usage
48fi
Ryan Andersonaaebf432005-07-31 04:57:49 -040049
Michal Marek09155122010-06-17 15:14:58 +020050scm_version()
51{
Michał Górny6dc0c2f2010-07-18 10:26:40 +020052 local short
53 short=false
Nico Schottelius33252572009-05-16 14:00:56 +020054
Michal Marek09155122010-06-17 15:14:58 +020055 cd "$srctree"
56 if test -e .scmversion; then
Michał Górny6dc0c2f2010-07-18 10:26:40 +020057 cat .scmversion
Michal Marek09155122010-06-17 15:14:58 +020058 return
59 fi
60 if test "$1" = "--short"; then
61 short=true
62 fi
Nico Schottelius33252572009-05-16 14:00:56 +020063
Michal Marek09155122010-06-17 15:14:58 +020064 # Check for git and a git repo.
Will McVicker47a17412021-02-11 11:33:42 -080065 if head=$(git rev-parse --verify HEAD 2>/dev/null); then
Nico Schottelius33252572009-05-16 14:00:56 +020066
Will McVickerf0bec7a2020-07-09 15:33:45 -070067 if [ -n "$android_release" ] && [ -n "$kmi_generation" ]; then
68 printf '%s' "-$android_release-$kmi_generation"
Alistair Delva4c4e1542021-11-17 09:58:10 -080069 elif [ -n "$android_release" ]; then
70 printf '%s' "-$android_release"
Will McVickerf0bec7a2020-07-09 15:33:45 -070071 fi
72
Michal Marek09155122010-06-17 15:14:58 +020073 # 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.
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +053075 if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then
Michal Marek09155122010-06-17 15:14:58 +020076
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.
Masahiro Yamada630ff0f2021-05-23 12:14:27 +090085 if atag="$(git describe 2>/dev/null)"; then
86 echo "$atag" | awk -F- '{printf("-%05d", $(NF-1))}'
Michal Marek09155122010-06-17 15:14:58 +020087 fi
Michal Marek09155122010-06-17 15:14:58 +020088
Masahiro Yamada630ff0f2021-05-23 12:14:27 +090089 # Add -g and exactly 12 hex chars.
90 printf '%s%s' -g "$(echo $head | cut -c1-12)"
Michal Marek09155122010-06-17 15:14:58 +020091 fi
92
Brian Norrisff64dd42018-11-14 18:11:18 -080093 # Check for uncommitted changes.
Masahiro Yamadaffaf62a2021-05-23 12:14:26 +090094 # 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 .
Brian Norrisff64dd42018-11-14 18:11:18 -080098 # 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
Masahiro Yamadaa2be76a2021-05-23 12:14:25 +0900106 } | read dummy; then
Michal Marek09155122010-06-17 15:14:58 +0200107 printf '%s' -dirty
108 fi
Rene Scharfe117a93d2006-01-04 20:42:03 +0100109 fi
Michal Marek09155122010-06-17 15:14:58 +0200110}
Aron Griffis3dce1742007-11-28 16:55:44 -0500111
Michal Marek09155122010-06-17 15:14:58 +0200112collect_files()
113{
Masahiro Yamada7a82e3f2019-10-01 21:17:24 +0900114 local file res=
Michal Marek09155122010-06-17 15:14:58 +0200115
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
Michal Marekb003afe2010-07-15 10:36:37 +0200130 if test ! -e .scmversion; then
131 res=$(scm_version)
132 echo "$res" >.scmversion
133 fi
Aron Griffis3dce1742007-11-28 16:55:44 -0500134 exit
135fi
136
Michal Marek09155122010-06-17 15:14:58 +0200137if test -e include/config/auto.conf; then
Michał Górny6dc0c2f2010-07-18 10:26:40 +0200138 . include/config/auto.conf
Michal Marek09155122010-06-17 15:14:58 +0200139else
Wolfram Sang78283edf2016-06-06 21:00:38 +0200140 echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
Michal Marek09155122010-06-17 15:14:58 +0200141 exit 1
142fi
Aron Griffis3dce1742007-11-28 16:55:44 -0500143
Michal Marek09155122010-06-17 15:14:58 +0200144# 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
Dongjin Kim59f4cc1f2024-09-07 00:17:28 +0900156 res="$res"
Mikulas Patocka5df99be2021-07-12 15:35:46 -0400157elif [ "${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.
Masahiro Yamada042da422021-05-23 12:14:28 +0900165 scm=$(scm_version --short)
166 res="$res${scm:++}"
Rene Scharfe117a93d2006-01-04 20:42:03 +0100167fi
Bryan Wuba3d05f2008-02-03 14:13:26 +0800168
Alistair Delva68f36312021-07-20 12:16:00 -0700169# finally, add the abXXX number if BUILD_NUMBER is set
170if test -n "${BUILD_NUMBER}"; then
171 res="$res-ab${BUILD_NUMBER}"
172fi
173
Michal Marek09155122010-06-17 15:14:58 +0200174echo "$res"