1117a93dbSRene Scharfe#!/bin/sh 2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 333252572SNico Schottelius# 433252572SNico Schottelius# This scripts adds local version information from the version 533252572SNico Schottelius# control systems git, mercurial (hg) and subversion (svn). 633252572SNico Schottelius# 733252572SNico Schottelius# If something goes wrong, send a mail the kernel build mailinglist 833252572SNico Schottelius# (see MAINTAINERS) and CC Nico Schottelius 933252572SNico Schottelius# <nico-linuxsetlocalversion -at- schottelius.org>. 1033252572SNico Schottelius# 1133252572SNico Schottelius# 12aaebf433SRyan Anderson 13117a93dbSRene Scharfeusage() { 14f6e09b07SMasahiro Yamada echo "Usage: $0 [srctree]" >&2 15117a93dbSRene Scharfe exit 1 16aaebf433SRyan Anderson} 17aaebf433SRyan Anderson 1809155120SMichal Mareksrctree=. 1909155120SMichal Marekif test $# -gt 0; then 2009155120SMichal Marek srctree=$1 2109155120SMichal Marek shift 2209155120SMichal Marekfi 2309155120SMichal Marekif test $# -gt 0 -o ! -d "$srctree"; then 2409155120SMichal Marek usage 2509155120SMichal Marekfi 2609155120SMichal Marek 2709155120SMichal Marekscm_version() 2809155120SMichal Marek{ 296dc0c2f3SMichał Górny local short 30*6ab7e1f9SMasahiro Yamada local tag 316dc0c2f3SMichał Górny short=false 3209155120SMichal Marek 3309155120SMichal Marek cd "$srctree" 3409155120SMichal Marek if test "$1" = "--short"; then 3509155120SMichal Marek short=true 3609155120SMichal Marek fi 37aaebf433SRyan Anderson 3875280bdfSMasahiro Yamada if test -n "$(git rev-parse --show-cdup 2>/dev/null)"; then 3975280bdfSMasahiro Yamada return 4075280bdfSMasahiro Yamada fi 4133252572SNico Schottelius 4275280bdfSMasahiro Yamada if ! head=$(git rev-parse --verify HEAD 2>/dev/null); then 4375280bdfSMasahiro Yamada return 4475280bdfSMasahiro Yamada fi 4575280bdfSMasahiro Yamada 46*6ab7e1f9SMasahiro Yamada # If a localversion*' file and the corresponding annotated tag exist, 47*6ab7e1f9SMasahiro Yamada # use it. This is the case in linux-next. 48*6ab7e1f9SMasahiro Yamada tag=${file_localversion#-} 49*6ab7e1f9SMasahiro Yamada tag=$(git describe --exact-match --match=$tag $tag 2>/dev/null) 50*6ab7e1f9SMasahiro Yamada 51*6ab7e1f9SMasahiro Yamada # Otherwise, default to the annotated tag derived from KERNELVERSION. 52*6ab7e1f9SMasahiro Yamada # mainline kernel: 6.2.0-rc5 -> v6.2-rc5 53*6ab7e1f9SMasahiro Yamada # stable kernel: 6.1.7 -> v6.1.7 54*6ab7e1f9SMasahiro Yamada if [ -z "${tag}" ]; then 55*6ab7e1f9SMasahiro Yamada tag=v$(echo "${KERNELVERSION}" | sed -E 's/^([0-9]+\.[0-9]+)\.0(.*)$/\1\2/') 56*6ab7e1f9SMasahiro Yamada fi 57*6ab7e1f9SMasahiro Yamada 58*6ab7e1f9SMasahiro Yamada # If we are at the tagged commit, we ignore it because the version is 59*6ab7e1f9SMasahiro Yamada # well-defined. 60*6ab7e1f9SMasahiro Yamada if [ -z "$(git describe --exact-match --match=$tag 2>/dev/null)" ]; then 6133252572SNico Schottelius 6209155120SMichal Marek # If only the short version is requested, don't bother 6309155120SMichal Marek # running further git commands 6409155120SMichal Marek if $short; then 6509155120SMichal Marek echo "+" 6609155120SMichal Marek return 6709155120SMichal Marek fi 68*6ab7e1f9SMasahiro Yamada # If we are past the tagged commit, we pretty print it. 69*6ab7e1f9SMasahiro Yamada # (like 6.1.0-14595-g292a089d78d3) 70*6ab7e1f9SMasahiro Yamada if atag="$(git describe --match=$tag 2>/dev/null)"; then 71630ff0faSMasahiro Yamada echo "$atag" | awk -F- '{printf("-%05d", $(NF-1))}' 7256b2f070SSebastian Siewior fi 73630ff0faSMasahiro Yamada 74630ff0faSMasahiro Yamada # Add -g and exactly 12 hex chars. 75630ff0faSMasahiro Yamada printf '%s%s' -g "$(echo $head | cut -c1-12)" 7633252572SNico Schottelius fi 77aaebf433SRyan Anderson 78ff64dd48SBrian Norris # Check for uncommitted changes. 7975280bdfSMasahiro Yamada # This script must avoid any write attempt to the source tree, which 8075280bdfSMasahiro Yamada # might be read-only. 8175280bdfSMasahiro Yamada # You cannot use 'git describe --dirty' because it tries to create 8275280bdfSMasahiro Yamada # .git/index.lock . 8375280bdfSMasahiro Yamada # First, with git-status, but --no-optional-locks is only supported in 8475280bdfSMasahiro Yamada # git >= 2.14, so fall back to git-diff-index if it fails. Note that 8575280bdfSMasahiro Yamada # git-diff-index does not refresh the index, so it may give misleading 8675280bdfSMasahiro Yamada # results. 8775280bdfSMasahiro Yamada # See git-update-index(1), git-diff-index(1), and git-status(1). 88ff64dd48SBrian Norris if { 89ff64dd48SBrian Norris git --no-optional-locks status -uno --porcelain 2>/dev/null || 90ff64dd48SBrian Norris git diff-index --name-only HEAD 91a2be76a3SMasahiro Yamada } | read dummy; then 9224d49756SRyan Anderson printf '%s' -dirty 93117a93dbSRene Scharfe fi 9409155120SMichal Marek} 9509155120SMichal Marek 9609155120SMichal Marekcollect_files() 9709155120SMichal Marek{ 987a82e3faSMasahiro Yamada local file res= 9909155120SMichal Marek 10009155120SMichal Marek for file; do 10109155120SMichal Marek case "$file" in 10209155120SMichal Marek *\~*) 10309155120SMichal Marek continue 10409155120SMichal Marek ;; 10509155120SMichal Marek esac 10609155120SMichal Marek if test -e "$file"; then 10709155120SMichal Marek res="$res$(cat "$file")" 10809155120SMichal Marek fi 10909155120SMichal Marek done 11009155120SMichal Marek echo "$res" 11109155120SMichal Marek} 11209155120SMichal Marek 1137d153696SMasahiro Yamadaif ! test -e include/config/auto.conf; then 11478283edfSWolfram Sang echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2 11509155120SMichal Marek exit 1 11609155120SMichal Marekfi 11709155120SMichal Marek 118ec31f868SMasahiro Yamadaif [ -z "${KERNELVERSION}" ]; then 119ec31f868SMasahiro Yamada echo "KERNELVERSION is not set" >&2 120ec31f868SMasahiro Yamada exit 1 121ec31f868SMasahiro Yamadafi 122ec31f868SMasahiro Yamada 12309155120SMichal Marek# localversion* files in the build and source directory 124eed36d77SMasahiro Yamadafile_localversion="$(collect_files localversion*)" 12509155120SMichal Marekif test ! "$srctree" -ef .; then 126eed36d77SMasahiro Yamada file_localversion="${file_localversion}$(collect_files "$srctree"/localversion*)" 12709155120SMichal Marekfi 12809155120SMichal Marek 129eed36d77SMasahiro Yamada# version string from CONFIG_LOCALVERSION 130129ab0d2SMasahiro Yamadaconfig_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf) 13109155120SMichal Marek 132*6ab7e1f9SMasahiro Yamada# scm version string if not at the kernel version tag or at the file_localversion 1337d153696SMasahiro Yamadaif grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then 13409155120SMichal Marek # full scm version string 135eed36d77SMasahiro Yamada scm_version="$(scm_version)" 1365df99becSMikulas Patockaelif [ "${LOCALVERSION+set}" != "set" ]; then 1375df99becSMikulas Patocka # If the variable LOCALVERSION is not set, append a plus 1385df99becSMikulas Patocka # sign if the repository is not in a clean annotated or 1395df99becSMikulas Patocka # signed tagged state (as git describe only looks at signed 1405df99becSMikulas Patocka # or annotated tags - git tag -a/-s). 1415df99becSMikulas Patocka # 1425df99becSMikulas Patocka # If the variable LOCALVERSION is set (including being set 1435df99becSMikulas Patocka # to an empty string), we don't want to append a plus sign. 144eed36d77SMasahiro Yamada scm_version="$(scm_version --short)" 14509155120SMichal Marekfi 14609155120SMichal Marek 147eed36d77SMasahiro Yamadaecho "${KERNELVERSION}${file_localversion}${config_localversion}${LOCALVERSION}${scm_version}" 148