setlocalversion 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  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. #
  12. usage() {
  13. echo "Usage: $0 [--save-scmversion] [srctree] [branch] [kmi-generation] [sec-localversion]" >&2
  14. exit 1
  15. }
  16. scm_only=false
  17. srctree=.
  18. android_release=
  19. kmi_generation=
  20. if test "$1" = "--save-scmversion"; then
  21. scm_only=true
  22. shift
  23. fi
  24. if test $# -gt 0; then
  25. srctree=$1
  26. shift
  27. fi
  28. if test $# -gt 0; then
  29. # Extract the Android release version. If there is no match, then return 255
  30. # and clear the var $android_release
  31. android_release=`echo "$1" | sed -e '/android[0-9]\{2,\}/!{q255}; \
  32. s/^\(android[0-9]\{2,\}\)-.*/\1/'`
  33. if test $? -ne 0; then
  34. android_release=
  35. fi
  36. shift
  37. if test $# -gt 0; then
  38. kmi_generation=$1
  39. [ $(expr $kmi_generation : '^[0-9]\+$') -eq 0 ] && usage
  40. shift
  41. fi
  42. if test $# -gt 0; then
  43. SEC_LOCALVERSION=$1
  44. shift
  45. fi
  46. fi
  47. if test $# -gt 0 -o ! -d "$srctree"; then
  48. usage
  49. fi
  50. scm_version()
  51. {
  52. local short
  53. short=false
  54. cd "$srctree"
  55. if test -e .scmversion; then
  56. cat .scmversion
  57. return
  58. fi
  59. if test "$1" = "--short"; then
  60. short=true
  61. fi
  62. # Check for git and a git repo.
  63. if head=$(git rev-parse --verify HEAD 2>/dev/null); then
  64. if [ -n "$android_release" ] && [ -n "$kmi_generation" ]; then
  65. printf '%s' "-$android_release-$kmi_generation"
  66. elif [ -n "$android_release" ]; then
  67. printf '%s' "-$android_release"
  68. fi
  69. # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
  70. # it, because this version is defined in the top level Makefile.
  71. if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then
  72. # If only the short version is requested, don't bother
  73. # running further git commands
  74. if $short; then
  75. echo "+"
  76. return
  77. fi
  78. # If we are past a tagged commit (like
  79. # "v2.6.30-rc5-302-g72357d5"), we pretty print it.
  80. if atag="$(git describe 2>/dev/null)"; then
  81. echo "$atag" | awk -F- '{printf("-%05d", $(NF-1))}'
  82. fi
  83. # Add -g and exactly 12 hex chars.
  84. printf '%s%s' -g "$(echo $head | cut -c1-12)"
  85. fi
  86. # Check for uncommitted changes.
  87. # This script must avoid any write attempt to the source tree,
  88. # which might be read-only.
  89. # You cannot use 'git describe --dirty' because it tries to
  90. # create .git/index.lock .
  91. # First, with git-status, but --no-optional-locks is only
  92. # supported in git >= 2.14, so fall back to git-diff-index if
  93. # it fails. Note that git-diff-index does not refresh the
  94. # index, so it may give misleading results. See
  95. # git-update-index(1), git-diff-index(1), and git-status(1).
  96. if {
  97. git --no-optional-locks status -uno --porcelain 2>/dev/null ||
  98. git diff-index --name-only HEAD
  99. } | read dummy; then
  100. printf '%s' -dirty
  101. fi
  102. fi
  103. # Samsung does not use git, hg and svn.
  104. # Forcely use android_release and kmi_generation
  105. if [ -n "$android_release" ] && [ -n "$kmi_generation" ]; then
  106. printf '%s' "-$android_release-$kmi_generation"
  107. fi
  108. }
  109. collect_files()
  110. {
  111. local file res=
  112. for file; do
  113. case "$file" in
  114. *\~*)
  115. continue
  116. ;;
  117. esac
  118. if test -e "$file"; then
  119. res="$res$(cat "$file")"
  120. fi
  121. done
  122. echo "$res"
  123. }
  124. if $scm_only; then
  125. if test ! -e .scmversion; then
  126. res=$(scm_version)
  127. echo "$res" >.scmversion
  128. fi
  129. exit
  130. fi
  131. if ! test -e include/config/auto.conf; then
  132. echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
  133. exit 1
  134. fi
  135. # localversion* files in the build and source directory
  136. res="$(collect_files localversion*)"
  137. if test ! "$srctree" -ef .; then
  138. res="$res$(collect_files "$srctree"/localversion*)"
  139. fi
  140. # CONFIG_LOCALVERSION and LOCALVERSION (if set)
  141. config_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf)
  142. res="${res}${config_localversion}${LOCALVERSION}"
  143. # scm version string if not at a tagged commit
  144. if grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then
  145. # full scm version string
  146. res="$res$(scm_version)"
  147. elif [ "${LOCALVERSION+set}" != "set" ]; then
  148. # If the variable LOCALVERSION is not set, append a plus
  149. # sign if the repository is not in a clean annotated or
  150. # signed tagged state (as git describe only looks at signed
  151. # or annotated tags - git tag -a/-s).
  152. #
  153. # If the variable LOCALVERSION is set (including being set
  154. # to an empty string), we don't want to append a plus sign.
  155. scm=$(scm_version --short)
  156. res="$res${scm:++}"
  157. fi
  158. # finally, add the abXXX number if BUILD_NUMBER is set
  159. if test -n "${BUILD_NUMBER}"; then
  160. res="$res-ab${BUILD_NUMBER}"
  161. fi
  162. sec_scm_version()
  163. {
  164. # Forcely use android_release and kmi_generation.
  165. # And SEC build provide 'LOCALVERSION' to propagate extra version info.
  166. if [ -n "${android_release}" ] && [ -n "${kmi_generation}" ]; then
  167. printf '%s' "-${android_release}-${kmi_generation}${CONFIG_LOCALVERSION}-${SEC_LOCALVERSION}"
  168. else
  169. echo -n "$res"
  170. fi
  171. }
  172. res=$(sec_scm_version)
  173. echo "$res"