162306a36Sopenharmony_ci#!/bin/sh 262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0 362306a36Sopenharmony_ci# 462306a36Sopenharmony_ci# This scripts adds local version information from the version 562306a36Sopenharmony_ci# control system git. 662306a36Sopenharmony_ci# 762306a36Sopenharmony_ci# If something goes wrong, send a mail the kernel build mailinglist 862306a36Sopenharmony_ci# (see MAINTAINERS) and CC Nico Schottelius 962306a36Sopenharmony_ci# <nico-linuxsetlocalversion -at- schottelius.org>. 1062306a36Sopenharmony_ci# 1162306a36Sopenharmony_ci# 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ciusage() { 1462306a36Sopenharmony_ci echo "Usage: $0 [--no-local] [srctree]" >&2 1562306a36Sopenharmony_ci exit 1 1662306a36Sopenharmony_ci} 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_cino_local=false 1962306a36Sopenharmony_ciif test "$1" = "--no-local"; then 2062306a36Sopenharmony_ci no_local=true 2162306a36Sopenharmony_ci shift 2262306a36Sopenharmony_cifi 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_cisrctree=. 2562306a36Sopenharmony_ciif test $# -gt 0; then 2662306a36Sopenharmony_ci srctree=$1 2762306a36Sopenharmony_ci shift 2862306a36Sopenharmony_cifi 2962306a36Sopenharmony_ciif test $# -gt 0 -o ! -d "$srctree"; then 3062306a36Sopenharmony_ci usage 3162306a36Sopenharmony_cifi 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ciscm_version() 3462306a36Sopenharmony_ci{ 3562306a36Sopenharmony_ci local short=false 3662306a36Sopenharmony_ci local no_dirty=false 3762306a36Sopenharmony_ci local tag 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci while [ $# -gt 0 ]; 4062306a36Sopenharmony_ci do 4162306a36Sopenharmony_ci case "$1" in 4262306a36Sopenharmony_ci --short) 4362306a36Sopenharmony_ci short=true;; 4462306a36Sopenharmony_ci --no-dirty) 4562306a36Sopenharmony_ci no_dirty=true;; 4662306a36Sopenharmony_ci esac 4762306a36Sopenharmony_ci shift 4862306a36Sopenharmony_ci done 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci cd "$srctree" 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci if test -n "$(git rev-parse --show-cdup 2>/dev/null)"; then 5362306a36Sopenharmony_ci return 5462306a36Sopenharmony_ci fi 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci if ! head=$(git rev-parse --verify HEAD 2>/dev/null); then 5762306a36Sopenharmony_ci return 5862306a36Sopenharmony_ci fi 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci # mainline kernel: 6.2.0-rc5 -> v6.2-rc5 6162306a36Sopenharmony_ci # stable kernel: 6.1.7 -> v6.1.7 6262306a36Sopenharmony_ci version_tag=v$(echo "${KERNELVERSION}" | sed -E 's/^([0-9]+\.[0-9]+)\.0(.*)$/\1\2/') 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci # If a localversion* file exists, and the corresponding 6562306a36Sopenharmony_ci # annotated tag exists and is an ancestor of HEAD, use 6662306a36Sopenharmony_ci # it. This is the case in linux-next. 6762306a36Sopenharmony_ci tag=${file_localversion#-} 6862306a36Sopenharmony_ci desc= 6962306a36Sopenharmony_ci if [ -n "${tag}" ]; then 7062306a36Sopenharmony_ci desc=$(git describe --match=$tag 2>/dev/null) 7162306a36Sopenharmony_ci fi 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci # Otherwise, if a localversion* file exists, and the tag 7462306a36Sopenharmony_ci # obtained by appending it to the tag derived from 7562306a36Sopenharmony_ci # KERNELVERSION exists and is an ancestor of HEAD, use 7662306a36Sopenharmony_ci # it. This is e.g. the case in linux-rt. 7762306a36Sopenharmony_ci if [ -z "${desc}" ] && [ -n "${file_localversion}" ]; then 7862306a36Sopenharmony_ci tag="${version_tag}${file_localversion}" 7962306a36Sopenharmony_ci desc=$(git describe --match=$tag 2>/dev/null) 8062306a36Sopenharmony_ci fi 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci # Otherwise, default to the annotated tag derived from KERNELVERSION. 8362306a36Sopenharmony_ci if [ -z "${desc}" ]; then 8462306a36Sopenharmony_ci tag="${version_tag}" 8562306a36Sopenharmony_ci desc=$(git describe --match=$tag 2>/dev/null) 8662306a36Sopenharmony_ci fi 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci # If we are at the tagged commit, we ignore it because the version is 8962306a36Sopenharmony_ci # well-defined. 9062306a36Sopenharmony_ci if [ "${tag}" != "${desc}" ]; then 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci # If only the short version is requested, don't bother 9362306a36Sopenharmony_ci # running further git commands 9462306a36Sopenharmony_ci if $short; then 9562306a36Sopenharmony_ci echo "+" 9662306a36Sopenharmony_ci return 9762306a36Sopenharmony_ci fi 9862306a36Sopenharmony_ci # If we are past the tagged commit, we pretty print it. 9962306a36Sopenharmony_ci # (like 6.1.0-14595-g292a089d78d3) 10062306a36Sopenharmony_ci if [ -n "${desc}" ]; then 10162306a36Sopenharmony_ci echo "${desc}" | awk -F- '{printf("-%05d", $(NF-1))}' 10262306a36Sopenharmony_ci fi 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci # Add -g and exactly 12 hex chars. 10562306a36Sopenharmony_ci printf '%s%s' -g "$(echo $head | cut -c1-12)" 10662306a36Sopenharmony_ci fi 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci if ${no_dirty}; then 10962306a36Sopenharmony_ci return 11062306a36Sopenharmony_ci fi 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci # Check for uncommitted changes. 11362306a36Sopenharmony_ci # This script must avoid any write attempt to the source tree, which 11462306a36Sopenharmony_ci # might be read-only. 11562306a36Sopenharmony_ci # You cannot use 'git describe --dirty' because it tries to create 11662306a36Sopenharmony_ci # .git/index.lock . 11762306a36Sopenharmony_ci # First, with git-status, but --no-optional-locks is only supported in 11862306a36Sopenharmony_ci # git >= 2.14, so fall back to git-diff-index if it fails. Note that 11962306a36Sopenharmony_ci # git-diff-index does not refresh the index, so it may give misleading 12062306a36Sopenharmony_ci # results. 12162306a36Sopenharmony_ci # See git-update-index(1), git-diff-index(1), and git-status(1). 12262306a36Sopenharmony_ci if { 12362306a36Sopenharmony_ci git --no-optional-locks status -uno --porcelain 2>/dev/null || 12462306a36Sopenharmony_ci git diff-index --name-only HEAD 12562306a36Sopenharmony_ci } | read dummy; then 12662306a36Sopenharmony_ci printf '%s' -dirty 12762306a36Sopenharmony_ci fi 12862306a36Sopenharmony_ci} 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_cicollect_files() 13162306a36Sopenharmony_ci{ 13262306a36Sopenharmony_ci local file res= 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci for file; do 13562306a36Sopenharmony_ci case "$file" in 13662306a36Sopenharmony_ci *\~*) 13762306a36Sopenharmony_ci continue 13862306a36Sopenharmony_ci ;; 13962306a36Sopenharmony_ci esac 14062306a36Sopenharmony_ci if test -e "$file"; then 14162306a36Sopenharmony_ci res="$res$(cat "$file")" 14262306a36Sopenharmony_ci fi 14362306a36Sopenharmony_ci done 14462306a36Sopenharmony_ci echo "$res" 14562306a36Sopenharmony_ci} 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ciif [ -z "${KERNELVERSION}" ]; then 14862306a36Sopenharmony_ci echo "KERNELVERSION is not set" >&2 14962306a36Sopenharmony_ci exit 1 15062306a36Sopenharmony_cifi 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci# localversion* files in the build and source directory 15362306a36Sopenharmony_cifile_localversion="$(collect_files localversion*)" 15462306a36Sopenharmony_ciif test ! "$srctree" -ef .; then 15562306a36Sopenharmony_ci file_localversion="${file_localversion}$(collect_files "$srctree"/localversion*)" 15662306a36Sopenharmony_cifi 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ciif ${no_local}; then 15962306a36Sopenharmony_ci echo "${KERNELVERSION}$(scm_version --no-dirty)" 16062306a36Sopenharmony_ci exit 0 16162306a36Sopenharmony_cifi 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ciif ! test -e include/config/auto.conf; then 16462306a36Sopenharmony_ci echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2 16562306a36Sopenharmony_ci exit 1 16662306a36Sopenharmony_cifi 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci# version string from CONFIG_LOCALVERSION 16962306a36Sopenharmony_ciconfig_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf) 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci# scm version string if not at the kernel version tag or at the file_localversion 17262306a36Sopenharmony_ciif grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then 17362306a36Sopenharmony_ci # full scm version string 17462306a36Sopenharmony_ci scm_version="$(scm_version)" 17562306a36Sopenharmony_cielif [ "${LOCALVERSION+set}" != "set" ]; then 17662306a36Sopenharmony_ci # If the variable LOCALVERSION is not set, append a plus 17762306a36Sopenharmony_ci # sign if the repository is not in a clean annotated or 17862306a36Sopenharmony_ci # signed tagged state (as git describe only looks at signed 17962306a36Sopenharmony_ci # or annotated tags - git tag -a/-s). 18062306a36Sopenharmony_ci # 18162306a36Sopenharmony_ci # If the variable LOCALVERSION is set (including being set 18262306a36Sopenharmony_ci # to an empty string), we don't want to append a plus sign. 18362306a36Sopenharmony_ci scm_version="$(scm_version --short)" 18462306a36Sopenharmony_cifi 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ciecho "${KERNELVERSION}${file_localversion}${config_localversion}${LOCALVERSION}${scm_version}" 187