117a93db1dcd6 (Rene Scharfe 2006-01-04 20:42:03 +0100 1) #!/bin/sh
b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 2) # SPDX-License-Identifier: GPL-2.0
33252572e727c (Nico Schottelius 2009-05-16 14:00:56 +0200 3) #
33252572e727c (Nico Schottelius 2009-05-16 14:00:56 +0200 4) # This scripts adds local version information from the version
33252572e727c (Nico Schottelius 2009-05-16 14:00:56 +0200 5) # control systems git, mercurial (hg) and subversion (svn).
33252572e727c (Nico Schottelius 2009-05-16 14:00:56 +0200 6) #
33252572e727c (Nico Schottelius 2009-05-16 14:00:56 +0200 7) # If something goes wrong, send a mail the kernel build mailinglist
33252572e727c (Nico Schottelius 2009-05-16 14:00:56 +0200 8) # (see MAINTAINERS) and CC Nico Schottelius
33252572e727c (Nico Schottelius 2009-05-16 14:00:56 +0200 9) # <nico-linuxsetlocalversion -at- schottelius.org>.
33252572e727c (Nico Schottelius 2009-05-16 14:00:56 +0200 10) #
33252572e727c (Nico Schottelius 2009-05-16 14:00:56 +0200 11) #
aaebf43320189 (Ryan Anderson 2005-07-31 04:57:49 -0400 12)
117a93db1dcd6 (Rene Scharfe 2006-01-04 20:42:03 +0100 13) usage() {
b003afe32f608 (Michal Marek 2010-07-15 10:36:37 +0200 14) echo "Usage: $0 [--save-scmversion] [srctree]" >&2
117a93db1dcd6 (Rene Scharfe 2006-01-04 20:42:03 +0100 15) exit 1
aaebf43320189 (Ryan Anderson 2005-07-31 04:57:49 -0400 16) }
aaebf43320189 (Ryan Anderson 2005-07-31 04:57:49 -0400 17)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 18) scm_only=false
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 19) srctree=.
b003afe32f608 (Michal Marek 2010-07-15 10:36:37 +0200 20) if test "$1" = "--save-scmversion"; then
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 21) scm_only=true
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 22) shift
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 23) fi
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 24) if test $# -gt 0; then
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 25) srctree=$1
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 26) shift
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 27) fi
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 28) if test $# -gt 0 -o ! -d "$srctree"; then
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 29) usage
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 30) fi
aaebf43320189 (Ryan Anderson 2005-07-31 04:57:49 -0400 31)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 32) scm_version()
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 33) {
6dc0c2f3384fe (Michał Górny 2010-07-18 10:26:40 +0200 34) local short
6dc0c2f3384fe (Michał Górny 2010-07-18 10:26:40 +0200 35) short=false
33252572e727c (Nico Schottelius 2009-05-16 14:00:56 +0200 36)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 37) cd "$srctree"
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 38) if test -e .scmversion; then
6dc0c2f3384fe (Michał Górny 2010-07-18 10:26:40 +0200 39) cat .scmversion
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 40) return
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 41) fi
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 42) if test "$1" = "--short"; then
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 43) short=true
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 44) fi
33252572e727c (Nico Schottelius 2009-05-16 14:00:56 +0200 45)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 46) # Check for git and a git repo.
7593e0902bc41 (Franck Bui-Huu 2013-12-02 16:34:29 +0100 47) if test -z "$(git rev-parse --show-cdup 2>/dev/null)" &&
548b8b5168c90 (Rasmus Villemoes 2020-09-17 08:56:11 +0200 48) head=$(git rev-parse --verify HEAD 2>/dev/null); then
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 49)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 50) # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 51) # it, because this version is defined in the top level Makefile.
3c96bdd0ebfaf (Bhaskar Chowdhury 2019-10-23 07:24:27 +0530 52) if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 53)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 54) # If only the short version is requested, don't bother
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 55) # running further git commands
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 56) if $short; then
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 57) echo "+"
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 58) return
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 59) fi
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 60) # If we are past a tagged commit (like
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 61) # "v2.6.30-rc5-302-g72357d5"), we pretty print it.
630ff0faf84ea (Masahiro Yamada 2021-05-23 12:14:27 +0900 62) if atag="$(git describe 2>/dev/null)"; then
630ff0faf84ea (Masahiro Yamada 2021-05-23 12:14:27 +0900 63) echo "$atag" | awk -F- '{printf("-%05d", $(NF-1))}'
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 64) fi
630ff0faf84ea (Masahiro Yamada 2021-05-23 12:14:27 +0900 65)
630ff0faf84ea (Masahiro Yamada 2021-05-23 12:14:27 +0900 66) # Add -g and exactly 12 hex chars.
630ff0faf84ea (Masahiro Yamada 2021-05-23 12:14:27 +0900 67) printf '%s%s' -g "$(echo $head | cut -c1-12)"
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 68) fi
33252572e727c (Nico Schottelius 2009-05-16 14:00:56 +0200 69)
ff64dd4857303 (Brian Norris 2018-11-14 18:11:18 -0800 70) # Check for uncommitted changes.
ffaf62a8050b5 (Masahiro Yamada 2021-05-23 12:14:26 +0900 71) # This script must avoid any write attempt to the source tree,
ffaf62a8050b5 (Masahiro Yamada 2021-05-23 12:14:26 +0900 72) # which might be read-only.
ffaf62a8050b5 (Masahiro Yamada 2021-05-23 12:14:26 +0900 73) # You cannot use 'git describe --dirty' because it tries to
ffaf62a8050b5 (Masahiro Yamada 2021-05-23 12:14:26 +0900 74) # create .git/index.lock .
ff64dd4857303 (Brian Norris 2018-11-14 18:11:18 -0800 75) # First, with git-status, but --no-optional-locks is only
ff64dd4857303 (Brian Norris 2018-11-14 18:11:18 -0800 76) # supported in git >= 2.14, so fall back to git-diff-index if
ff64dd4857303 (Brian Norris 2018-11-14 18:11:18 -0800 77) # it fails. Note that git-diff-index does not refresh the
ff64dd4857303 (Brian Norris 2018-11-14 18:11:18 -0800 78) # index, so it may give misleading results. See
ff64dd4857303 (Brian Norris 2018-11-14 18:11:18 -0800 79) # git-update-index(1), git-diff-index(1), and git-status(1).
ff64dd4857303 (Brian Norris 2018-11-14 18:11:18 -0800 80) if {
ff64dd4857303 (Brian Norris 2018-11-14 18:11:18 -0800 81) git --no-optional-locks status -uno --porcelain 2>/dev/null ||
ff64dd4857303 (Brian Norris 2018-11-14 18:11:18 -0800 82) git diff-index --name-only HEAD
a2be76a352f10 (Masahiro Yamada 2021-05-23 12:14:25 +0900 83) } | read dummy; then
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 84) printf '%s' -dirty
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 85) fi
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 86) fi
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 87) }
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 88)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 89) collect_files()
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 90) {
7a82e3fa28f17 (Masahiro Yamada 2019-10-01 21:17:24 +0900 91) local file res=
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 92)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 93) for file; do
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 94) case "$file" in
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 95) *\~*)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 96) continue
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 97) ;;
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 98) esac
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 99) if test -e "$file"; then
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 100) res="$res$(cat "$file")"
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 101) fi
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 102) done
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 103) echo "$res"
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 104) }
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 105)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 106) if $scm_only; then
b003afe32f608 (Michal Marek 2010-07-15 10:36:37 +0200 107) if test ! -e .scmversion; then
b003afe32f608 (Michal Marek 2010-07-15 10:36:37 +0200 108) res=$(scm_version)
b003afe32f608 (Michal Marek 2010-07-15 10:36:37 +0200 109) echo "$res" >.scmversion
b003afe32f608 (Michal Marek 2010-07-15 10:36:37 +0200 110) fi
3dce174cfcba1 (Aron Griffis 2007-11-28 16:55:44 -0500 111) exit
117a93db1dcd6 (Rene Scharfe 2006-01-04 20:42:03 +0100 112) fi
ba3d05fb6369e (Bryan Wu 2008-02-03 14:13:26 +0800 113)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 114) if test -e include/config/auto.conf; then
6dc0c2f3384fe (Michał Górny 2010-07-18 10:26:40 +0200 115) . include/config/auto.conf
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 116) else
78283edf2c01c (Wolfram Sang 2016-06-06 21:00:38 +0200 117) echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 118) exit 1
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 119) fi
ba3d05fb6369e (Bryan Wu 2008-02-03 14:13:26 +0800 120)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 121) # localversion* files in the build and source directory
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 122) res="$(collect_files localversion*)"
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 123) if test ! "$srctree" -ef .; then
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 124) res="$res$(collect_files "$srctree"/localversion*)"
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 125) fi
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 126)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 127) # CONFIG_LOCALVERSION and LOCALVERSION (if set)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 128) res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 129)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 130) # scm version string if not at a tagged commit
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 131) if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 132) # full scm version string
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 133) res="$res$(scm_version)"
5df99bec210a2 (Mikulas Patocka 2021-07-12 15:35:46 -0400 134) elif [ "${LOCALVERSION+set}" != "set" ]; then
5df99bec210a2 (Mikulas Patocka 2021-07-12 15:35:46 -0400 135) # If the variable LOCALVERSION is not set, append a plus
5df99bec210a2 (Mikulas Patocka 2021-07-12 15:35:46 -0400 136) # sign if the repository is not in a clean annotated or
5df99bec210a2 (Mikulas Patocka 2021-07-12 15:35:46 -0400 137) # signed tagged state (as git describe only looks at signed
5df99bec210a2 (Mikulas Patocka 2021-07-12 15:35:46 -0400 138) # or annotated tags - git tag -a/-s).
5df99bec210a2 (Mikulas Patocka 2021-07-12 15:35:46 -0400 139) #
5df99bec210a2 (Mikulas Patocka 2021-07-12 15:35:46 -0400 140) # If the variable LOCALVERSION is set (including being set
5df99bec210a2 (Mikulas Patocka 2021-07-12 15:35:46 -0400 141) # to an empty string), we don't want to append a plus sign.
042da426f8ebd (Masahiro Yamada 2021-05-23 12:14:28 +0900 142) scm=$(scm_version --short)
042da426f8ebd (Masahiro Yamada 2021-05-23 12:14:28 +0900 143) res="$res${scm:++}"
ba3d05fb6369e (Bryan Wu 2008-02-03 14:13:26 +0800 144) fi
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 145)
09155120cf0ff (Michal Marek 2010-06-17 15:14:58 +0200 146) echo "$res"