^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/bin/sh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # This scripts adds local version information from the version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) # control systems git, mercurial (hg) and subversion (svn).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) # If something goes wrong, send a mail the kernel build mailinglist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # (see MAINTAINERS) and CC Nico Schottelius
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) # <nico-linuxsetlocalversion -at- schottelius.org>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) usage() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) echo "Usage: $0 [--save-scmversion] [srctree] [branch] [kmi-generation]" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) scm_only=false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) srctree=.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) android_release=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) kmi_generation=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if test "$1" = "--save-scmversion"; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) scm_only=true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if test $# -gt 0; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) srctree=$1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if test $# -gt 0; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) # Extract the Android release version. If there is no match, then return 255
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) # and clear the var $android_release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) android_release=`echo "$1" | sed -e '/android[0-9]\{2,\}/!{q255}; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) s/^\(android[0-9]\{2,\}\)-.*/\1/'`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if test $? -ne 0; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) android_release=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if test $# -gt 0; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) kmi_generation=$1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) [ $(expr $kmi_generation : '^[0-9]\+$') -eq 0 ] && usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if test $# -gt 0 -o ! -d "$srctree"; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) scm_version()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) local short
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) short=false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) cd "$srctree"
555b945800 (kx 2023-10-28 16:30:02 +0300 56) if [ ! -e .git ] ; then
555b945800 (kx 2023-10-28 16:30:02 +0300 57) return
555b945800 (kx 2023-10-28 16:30:02 +0300 58) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if test -e .scmversion; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) cat .scmversion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if test "$1" = "--short"; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) short=true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) # Check for git and a git repo.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if head=$(git rev-parse --verify HEAD 2>/dev/null); then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if [ -n "$android_release" ] && [ -n "$kmi_generation" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) printf '%s' "-$android_release-$kmi_generation"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) elif [ -n "$android_release" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) printf '%s' "-$android_release"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) # it, because this version is defined in the top level Makefile.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) # If only the short version is requested, don't bother
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) # running further git commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if $short; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) # If we are past a tagged commit (like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) # "v2.6.30-rc5-302-g72357d5"), we pretty print it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) # Ensure the abbreviated sha1 has exactly 12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) # hex characters, to make the output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) # independent of git version, local
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) # core.abbrev settings and/or total number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) # objects in the current repository - passing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) # --abbrev=12 ensures a minimum of 12, and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) # awk substr() then picks the 'g' and first 12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) # hex chars.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if atag="$(git describe --abbrev=12 2>/dev/null)"; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),substr($(NF),0,13))}'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) # If we don't have a tag at all we print -g{commitish},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) # again using exactly 12 hex chars.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) head="$(echo $head | cut -c1-12)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) printf '%s%s' -g $head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) # Is this git on svn?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if git config --get svn-remote.svn.url >/dev/null; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) printf -- '-svn%s' "$(git svn find-rev $head)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) # Check for uncommitted changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) # First, with git-status, but --no-optional-locks is only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) # supported in git >= 2.14, so fall back to git-diff-index if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) # it fails. Note that git-diff-index does not refresh the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) # index, so it may give misleading results. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) # git-update-index(1), git-diff-index(1), and git-status(1).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) git --no-optional-locks status -uno --porcelain 2>/dev/null ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) git diff-index --name-only HEAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) } | grep -qvE '^(.. )?scripts/package'; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) printf '%s' -dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) # All done with git
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) # Check for mercurial and a mercurial repo.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if hgid=$(hg id 2>/dev/null); then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) # Do we have an tagged version? If so, latesttagdistance == 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if [ "$(hg log -r . --template '{latesttagdistance}')" = "1" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) id=$(hg log -r . --template '{latesttag}')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) printf '%s%s' -hg "$id"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) tag=$(printf '%s' "$hgid" | cut -d' ' -f2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if [ -z "$tag" -o "$tag" = tip ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) id=$(printf '%s' "$hgid" | sed 's/[+ ].*//')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) printf '%s%s' -hg "$id"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) # Are there uncommitted changes?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) # These are represented by + after the changeset id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) case "$hgid" in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) *+|*+\ *) printf '%s' -dirty ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) esac
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) # All done with mercurial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) # Check for svn and a svn repo.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if rev=$(LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'); then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) rev=$(echo $rev | awk '{print $NF}')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) printf -- '-svn%s' "$rev"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) # All done with svn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) collect_files()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) local file res=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) for file; do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) case "$file" in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) *\~*)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) esac
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if test -e "$file"; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) res="$res$(cat "$file")"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) echo "$res"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if $scm_only; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if test ! -e .scmversion; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) res=$(scm_version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) echo "$res" >.scmversion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if test -e include/config/auto.conf; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) . include/config/auto.conf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) # localversion* files in the build and source directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) res="$(collect_files localversion*)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if test ! "$srctree" -ef .; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) res="$res$(collect_files "$srctree"/localversion*)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) # CONFIG_LOCALVERSION and LOCALVERSION (if set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) # scm version string if not at a tagged commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) # full scm version string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) res="$res$(scm_version)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) # append a plus sign if the repository is not in a clean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) # annotated or signed tagged state (as git describe only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) # looks at signed or annotated tags - git tag -a/-s) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) # LOCALVERSION= is not specified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if test "${LOCALVERSION+set}" != "set"; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) scm=$(scm_version --short)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) res="$res${scm:++}"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) echo "$res"