setlocalversion 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # This scripts adds local version information from the version
  5. # control system git.
  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. usage() {
  13. echo "Usage: $0 [--no-local] [srctree]" >&2
  14. exit 1
  15. }
  16. no_local=false
  17. if test "$1" = "--no-local"; then
  18. no_local=true
  19. shift
  20. fi
  21. srctree=.
  22. if test $# -gt 0; then
  23. srctree=$1
  24. shift
  25. fi
  26. if test $# -gt 0 -o ! -d "$srctree"; then
  27. usage
  28. fi
  29. try_tag() {
  30. tag="$1"
  31. # Is $tag an annotated tag?
  32. [ "$(git cat-file -t "$tag" 2> /dev/null)" = tag ] || return 1
  33. # Is it an ancestor of HEAD, and if so, how many commits are in $tag..HEAD?
  34. # shellcheck disable=SC2046 # word splitting is the point here
  35. set -- $(git rev-list --count --left-right "$tag"...HEAD 2> /dev/null)
  36. # $1 is 0 if and only if $tag is an ancestor of HEAD. Use
  37. # string comparison, because $1 is empty if the 'git rev-list'
  38. # command somehow failed.
  39. [ "$1" = 0 ] || return 1
  40. # $2 is the number of commits in the range $tag..HEAD, possibly 0.
  41. count="$2"
  42. return 0
  43. }
  44. scm_version()
  45. {
  46. local short=false
  47. local no_dirty=false
  48. local tag
  49. while [ $# -gt 0 ];
  50. do
  51. case "$1" in
  52. --short)
  53. short=true;;
  54. --no-dirty)
  55. no_dirty=true;;
  56. esac
  57. shift
  58. done
  59. cd "$srctree"
  60. if test -n "$(git rev-parse --show-cdup 2>/dev/null)"; then
  61. return
  62. fi
  63. if ! head=$(git rev-parse --verify HEAD 2>/dev/null); then
  64. return
  65. fi
  66. # mainline kernel: 6.2.0-rc5 -> v6.2-rc5
  67. # stable kernel: 6.1.7 -> v6.1.7
  68. version_tag=v$(echo "${KERNELVERSION}" | sed -E 's/^([0-9]+\.[0-9]+)\.0(.*)$/\1\2/')
  69. # try_tag initializes count if the tag is usable.
  70. count=
  71. # If a localversion* file exists, and the corresponding
  72. # annotated tag exists and is an ancestor of HEAD, use
  73. # it. This is the case in linux-next.
  74. if [ -n "${file_localversion#-}" ] ; then
  75. try_tag "${file_localversion#-}"
  76. fi
  77. # Otherwise, if a localversion* file exists, and the tag
  78. # obtained by appending it to the tag derived from
  79. # KERNELVERSION exists and is an ancestor of HEAD, use
  80. # it. This is e.g. the case in linux-rt.
  81. if [ -z "${count}" ] && [ -n "${file_localversion}" ]; then
  82. try_tag "${version_tag}${file_localversion}"
  83. fi
  84. # Otherwise, default to the annotated tag derived from KERNELVERSION.
  85. if [ -z "${count}" ]; then
  86. try_tag "${version_tag}"
  87. fi
  88. # If we are at the tagged commit, we ignore it because the
  89. # version is well-defined. If none of the attempted tags exist
  90. # or were usable, $count is still empty.
  91. if [ -z "${count}" ] || [ "${count}" -gt 0 ]; then
  92. # If only the short version is requested, don't bother
  93. # running further git commands
  94. if $short; then
  95. echo "+"
  96. return
  97. fi
  98. # If we are past the tagged commit, we pretty print it.
  99. # (like 6.1.0-14595-g292a089d78d3)
  100. if [ -n "${count}" ]; then
  101. printf "%s%05d" "-" "${count}"
  102. fi
  103. # Add -g and exactly 12 hex chars.
  104. printf '%s%.12s' -g "$head"
  105. fi
  106. if ${no_dirty}; then
  107. return
  108. fi
  109. # Check for uncommitted changes.
  110. # This script must avoid any write attempt to the source tree, which
  111. # might be read-only.
  112. # You cannot use 'git describe --dirty' because it tries to create
  113. # .git/index.lock .
  114. # First, with git-status, but --no-optional-locks is only supported in
  115. # git >= 2.14, so fall back to git-diff-index if it fails. Note that
  116. # git-diff-index does not refresh the index, so it may give misleading
  117. # results.
  118. # See git-update-index(1), git-diff-index(1), and git-status(1).
  119. if {
  120. git --no-optional-locks status -uno --porcelain 2>/dev/null ||
  121. git diff-index --name-only HEAD
  122. } | read dummy; then
  123. printf '%s' -dirty
  124. fi
  125. }
  126. collect_files()
  127. {
  128. local file res=
  129. for file; do
  130. case "$file" in
  131. *\~*)
  132. continue
  133. ;;
  134. esac
  135. if test -e "$file"; then
  136. res="$res$(cat "$file")"
  137. fi
  138. done
  139. echo "$res"
  140. }
  141. if [ -z "${KERNELVERSION}" ]; then
  142. echo "KERNELVERSION is not set" >&2
  143. exit 1
  144. fi
  145. # localversion* files in the build and source directory
  146. file_localversion="$(collect_files localversion*)"
  147. if test ! "$srctree" -ef .; then
  148. file_localversion="${file_localversion}$(collect_files "$srctree"/localversion*)"
  149. fi
  150. if ${no_local}; then
  151. echo "${KERNELVERSION}$(scm_version --no-dirty)"
  152. exit 0
  153. fi
  154. if ! test -e include/config/auto.conf; then
  155. echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
  156. exit 1
  157. fi
  158. # version string from CONFIG_LOCALVERSION
  159. config_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf)
  160. # scm version string if not at the kernel version tag or at the file_localversion
  161. if grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then
  162. # full scm version string
  163. scm_version="$(scm_version)"
  164. elif [ "${LOCALVERSION+set}" != "set" ]; then
  165. # If the variable LOCALVERSION is not set, append a plus
  166. # sign if the repository is not in a clean annotated or
  167. # signed tagged state (as git describe only looks at signed
  168. # or annotated tags - git tag -a/-s).
  169. #
  170. # If the variable LOCALVERSION is set (including being set
  171. # to an empty string), we don't want to append a plus sign.
  172. scm_version="$(scm_version --short)"
  173. fi
  174. echo "${KERNELVERSION}${file_localversion}${config_localversion}${LOCALVERSION}${scm_version}"