xref: /OK3568_Linux_fs/device/rockchip/common/linux-kbuild/armhf/linux-kbuild-4.4/scripts/setlocalversion (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/bin/sh
2*4882a593Smuzhiyun#
3*4882a593Smuzhiyun# This scripts adds local version information from the version
4*4882a593Smuzhiyun# control systems git, mercurial (hg) and subversion (svn).
5*4882a593Smuzhiyun#
6*4882a593Smuzhiyun# If something goes wrong, send a mail the kernel build mailinglist
7*4882a593Smuzhiyun# (see MAINTAINERS) and CC Nico Schottelius
8*4882a593Smuzhiyun# <nico-linuxsetlocalversion -at- schottelius.org>.
9*4882a593Smuzhiyun#
10*4882a593Smuzhiyun#
11*4882a593Smuzhiyun
12*4882a593Smuzhiyunusage() {
13*4882a593Smuzhiyun	echo "Usage: $0 [--save-scmversion] [srctree]" >&2
14*4882a593Smuzhiyun	exit 1
15*4882a593Smuzhiyun}
16*4882a593Smuzhiyun
17*4882a593Smuzhiyunscm_only=false
18*4882a593Smuzhiyunsrctree=.
19*4882a593Smuzhiyunif test "$1" = "--save-scmversion"; then
20*4882a593Smuzhiyun	scm_only=true
21*4882a593Smuzhiyun	shift
22*4882a593Smuzhiyunfi
23*4882a593Smuzhiyunif test $# -gt 0; then
24*4882a593Smuzhiyun	srctree=$1
25*4882a593Smuzhiyun	shift
26*4882a593Smuzhiyunfi
27*4882a593Smuzhiyunif test $# -gt 0 -o ! -d "$srctree"; then
28*4882a593Smuzhiyun	usage
29*4882a593Smuzhiyunfi
30*4882a593Smuzhiyun
31*4882a593Smuzhiyunscm_version()
32*4882a593Smuzhiyun{
33*4882a593Smuzhiyun	local short
34*4882a593Smuzhiyun	short=false
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun	cd "$srctree"
37*4882a593Smuzhiyun	if test -e .scmversion; then
38*4882a593Smuzhiyun		cat .scmversion
39*4882a593Smuzhiyun		return
40*4882a593Smuzhiyun	fi
41*4882a593Smuzhiyun	if test "$1" = "--short"; then
42*4882a593Smuzhiyun		short=true
43*4882a593Smuzhiyun	fi
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun	# Check for git and a git repo.
46*4882a593Smuzhiyun	if test -z "$(git rev-parse --show-cdup 2>/dev/null)" &&
47*4882a593Smuzhiyun	   head=`git rev-parse --verify --short HEAD 2>/dev/null`; then
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun		# If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
50*4882a593Smuzhiyun		# it, because this version is defined in the top level Makefile.
51*4882a593Smuzhiyun		if [ -z "`git describe --exact-match 2>/dev/null`" ]; then
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun			# If only the short version is requested, don't bother
54*4882a593Smuzhiyun			# running further git commands
55*4882a593Smuzhiyun			if $short; then
56*4882a593Smuzhiyun				echo "+"
57*4882a593Smuzhiyun				return
58*4882a593Smuzhiyun			fi
59*4882a593Smuzhiyun			# If we are past a tagged commit (like
60*4882a593Smuzhiyun			# "v2.6.30-rc5-302-g72357d5"), we pretty print it.
61*4882a593Smuzhiyun			if atag="`git describe 2>/dev/null`"; then
62*4882a593Smuzhiyun				echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}'
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun			# If we don't have a tag at all we print -g{commitish}.
65*4882a593Smuzhiyun			else
66*4882a593Smuzhiyun				printf '%s%s' -g $head
67*4882a593Smuzhiyun			fi
68*4882a593Smuzhiyun		fi
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun		# Is this git on svn?
71*4882a593Smuzhiyun		if git config --get svn-remote.svn.url >/dev/null; then
72*4882a593Smuzhiyun			printf -- '-svn%s' "`git svn find-rev $head`"
73*4882a593Smuzhiyun		fi
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun		# Check for uncommitted changes
76*4882a593Smuzhiyun		if git diff-index --name-only HEAD | grep -qv "^scripts/package"; then
77*4882a593Smuzhiyun			printf '%s' -dirty
78*4882a593Smuzhiyun		fi
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun		# All done with git
81*4882a593Smuzhiyun		return
82*4882a593Smuzhiyun	fi
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun	# Check for mercurial and a mercurial repo.
85*4882a593Smuzhiyun	if test -d .hg && hgid=`hg id 2>/dev/null`; then
86*4882a593Smuzhiyun		# Do we have an tagged version?  If so, latesttagdistance == 1
87*4882a593Smuzhiyun		if [ "`hg log -r . --template '{latesttagdistance}'`" == "1" ]; then
88*4882a593Smuzhiyun			id=`hg log -r . --template '{latesttag}'`
89*4882a593Smuzhiyun			printf '%s%s' -hg "$id"
90*4882a593Smuzhiyun		else
91*4882a593Smuzhiyun			tag=`printf '%s' "$hgid" | cut -d' ' -f2`
92*4882a593Smuzhiyun			if [ -z "$tag" -o "$tag" = tip ]; then
93*4882a593Smuzhiyun				id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
94*4882a593Smuzhiyun				printf '%s%s' -hg "$id"
95*4882a593Smuzhiyun			fi
96*4882a593Smuzhiyun		fi
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun		# Are there uncommitted changes?
99*4882a593Smuzhiyun		# These are represented by + after the changeset id.
100*4882a593Smuzhiyun		case "$hgid" in
101*4882a593Smuzhiyun			*+|*+\ *) printf '%s' -dirty ;;
102*4882a593Smuzhiyun		esac
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun		# All done with mercurial
105*4882a593Smuzhiyun		return
106*4882a593Smuzhiyun	fi
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun	# Check for svn and a svn repo.
109*4882a593Smuzhiyun	if rev=`LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'`; then
110*4882a593Smuzhiyun		rev=`echo $rev | awk '{print $NF}'`
111*4882a593Smuzhiyun		printf -- '-svn%s' "$rev"
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun		# All done with svn
114*4882a593Smuzhiyun		return
115*4882a593Smuzhiyun	fi
116*4882a593Smuzhiyun}
117*4882a593Smuzhiyun
118*4882a593Smuzhiyuncollect_files()
119*4882a593Smuzhiyun{
120*4882a593Smuzhiyun	local file res
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun	for file; do
123*4882a593Smuzhiyun		case "$file" in
124*4882a593Smuzhiyun		*\~*)
125*4882a593Smuzhiyun			continue
126*4882a593Smuzhiyun			;;
127*4882a593Smuzhiyun		esac
128*4882a593Smuzhiyun		if test -e "$file"; then
129*4882a593Smuzhiyun			res="$res$(cat "$file")"
130*4882a593Smuzhiyun		fi
131*4882a593Smuzhiyun	done
132*4882a593Smuzhiyun	echo "$res"
133*4882a593Smuzhiyun}
134*4882a593Smuzhiyun
135*4882a593Smuzhiyunif $scm_only; then
136*4882a593Smuzhiyun	if test ! -e .scmversion; then
137*4882a593Smuzhiyun		res=$(scm_version)
138*4882a593Smuzhiyun		echo "$res" >.scmversion
139*4882a593Smuzhiyun	fi
140*4882a593Smuzhiyun	exit
141*4882a593Smuzhiyunfi
142*4882a593Smuzhiyun
143*4882a593Smuzhiyunif test -e include/config/auto.conf; then
144*4882a593Smuzhiyun	. include/config/auto.conf
145*4882a593Smuzhiyunelse
146*4882a593Smuzhiyun	echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
147*4882a593Smuzhiyun	exit 1
148*4882a593Smuzhiyunfi
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun# localversion* files in the build and source directory
151*4882a593Smuzhiyunres="$(collect_files localversion*)"
152*4882a593Smuzhiyunif test ! "$srctree" -ef .; then
153*4882a593Smuzhiyun	res="$res$(collect_files "$srctree"/localversion*)"
154*4882a593Smuzhiyunfi
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun# CONFIG_LOCALVERSION and LOCALVERSION (if set)
157*4882a593Smuzhiyunres="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun# scm version string if not at a tagged commit
160*4882a593Smuzhiyunif test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
161*4882a593Smuzhiyun	# full scm version string
162*4882a593Smuzhiyun	res="$res$(scm_version)"
163*4882a593Smuzhiyunelse
164*4882a593Smuzhiyun	# append a plus sign if the repository is not in a clean
165*4882a593Smuzhiyun	# annotated or signed tagged state (as git describe only
166*4882a593Smuzhiyun	# looks at signed or annotated tags - git tag -a/-s) and
167*4882a593Smuzhiyun	# LOCALVERSION= is not specified
168*4882a593Smuzhiyun	if test "${LOCALVERSION+set}" != "set"; then
169*4882a593Smuzhiyun		scm=$(scm_version --short)
170*4882a593Smuzhiyun		res="$res${scm:++}"
171*4882a593Smuzhiyun	fi
172*4882a593Smuzhiyunfi
173*4882a593Smuzhiyun
174*4882a593Smuzhiyunecho "$res"
175