xref: /freebsd/sys/conf/newvers.sh (revision 2ad756a6bbb30fc98ee9000fba5bceec916a6c70)
1#!/bin/sh -
2#
3# SPDX-License-Identifier: BSD-3-Clause
4#
5# Copyright (c) 1984, 1986, 1990, 1993
6#	The Regents of the University of California.  All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16# 3. Neither the name of the University nor the names of its contributors
17#    may be used to endorse or promote products derived from this software
18#    without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30# SUCH DAMAGE.
31#
32#	@(#)newvers.sh	8.1 (Berkeley) 4/20/94
33
34# Command line options:
35#
36#	-c	Print the copyright / license statement as a C comment and exit
37#
38#	-r	Reproducible build.  Do not embed directory names, user	names,
39#		time stamps or other dynamic information into the output file.
40#		This is intended to allow two builds done at different times
41#		and even by different people on different hosts to produce
42#		identical output.
43#
44#	-R	Reproducible build if the tree represents an unmodified
45#		checkout from a version control system.  Metadata is included
46#		if the tree is modified.
47#
48#	-V var	Print ${var}="${val-of-var}" and exit
49#
50#	-v	Print TYPE REVISION BRANCH RELEASE VERSION RELDATE variables
51#		like the -V command
52#
53
54TYPE="FreeBSD"
55REVISION="15.0"
56BRANCH="CURRENT"
57if [ -n "${BRANCH_OVERRIDE}" ]; then
58	BRANCH=${BRANCH_OVERRIDE}
59fi
60
61if [ -z "${SYSDIR}" ]; then
62	SYSDIR=$(dirname $0)/..
63fi
64
65# allow random overrides
66while :
67do
68	case "$1" in
69	*=*) eval "$1"; shift;;
70	*) break;;
71	esac
72done
73
74RELEASE="${RELEASE:-${REVISION}-${BRANCH}}"
75VERSION="${VERSION:-${TYPE} ${RELEASE}}"
76
77RELDATE=$(awk '/^#define[[:space:]]*__FreeBSD_version/ {print $3}' ${PARAMFILE:-${SYSDIR}/sys/param.h})
78
79if [ -r "${SYSDIR}/../COPYRIGHT" ]; then
80	year=$(sed -Ee '/^Copyright .* The FreeBSD Project/!d;s/^.*1992-([0-9]*) .*$/\1/g' ${SYSDIR}/../COPYRIGHT)
81else
82	year=$(date +%Y)
83fi
84# look for copyright template
85b=share/examples/etc/bsd-style-copyright
86for bsd_copyright in $b ../$b ../../$b ../../../$b /usr/src/$b /usr/$b
87do
88	if [ -r "$bsd_copyright" ]; then
89		COPYRIGHT=$(sed \
90		    -e "s/\[year\]/1992-$year/" \
91		    -e 's/\[your name here\]\.* /The FreeBSD Project./' \
92		    -e 's/\[your name\]\.*/The FreeBSD Project./' \
93		    -e '/\[id for your version control system, if any\]/d' \
94		    $bsd_copyright)
95		break
96	fi
97done
98
99# no copyright found, use a dummy
100if [ -z "$COPYRIGHT" ]; then
101	COPYRIGHT="/*-
102 * Copyright (c) 1992-$year The FreeBSD Project.
103 *
104 */"
105fi
106
107# add newline
108COPYRIGHT="$COPYRIGHT
109"
110
111# We expand include_metadata later since we may set it to the
112# future value of modified.
113include_metadata=yes
114modified=no
115while getopts crRvV: opt; do
116	case "$opt" in
117	c)
118		echo "$COPYRIGHT"
119		exit 0
120		;;
121	r)
122		include_metadata=no
123		;;
124	R)
125		include_metadata=if-modified
126		;;
127	v)
128		# Only put variables that are single lines here.
129		for v in TYPE REVISION BRANCH RELEASE VERSION RELDATE; do
130			eval val=\$${v}
131			echo ${v}=\"${val}\"
132		done
133		exit 0
134		;;
135	V)
136		v=$OPTARG
137		eval val=\$${v}
138		echo ${v}=\"${val}\"
139		VARS_ONLY_EXIT=1
140		;;
141	esac
142done
143shift $((OPTIND - 1))
144
145# VARS_ONLY means no files should be generated, this is just being
146# included.
147[ -n "$VARS_ONLY" ] && return 0
148
149# VARS_ONLY_EXIT means no files should be generated, only the value of
150# variables are being output.
151[ -n "$VARS_ONLY_EXIT" ] && exit 0
152
153#
154# findvcs dir
155#	Looks up directory dir at world root and up the filesystem
156#
157findvcs()
158{
159	local savedir
160
161	savedir=$(pwd)
162	cd ${SYSDIR}/..
163	while [ $(pwd) != "/" ]; do
164		if [ -e "./$1" ]; then
165			VCSTOP=$(pwd)
166			VCSDIR=${VCSTOP}"/$1"
167			cd ${savedir}
168			return 0
169		fi
170		cd ..
171	done
172	cd ${savedir}
173	return 1
174}
175
176git_tree_modified()
177{
178	! $git_cmd "--work-tree=${VCSTOP}" -c core.checkStat=minimal -c core.fileMode=off diff --quiet
179}
180
181LC_ALL=C; export LC_ALL
182if [ ! -r version ]
183then
184	echo 0 > version
185fi
186
187touch version
188v=$(cat version)
189u=${USER:-root}
190d=$(pwd)
191h=${HOSTNAME:-$(hostname)}
192if [ -n "$SOURCE_DATE_EPOCH" ]; then
193	if ! t=$(date -r $SOURCE_DATE_EPOCH 2>/dev/null); then
194		echo "Invalid SOURCE_DATE_EPOCH" >&2
195		exit 1
196	fi
197else
198	t=$(date)
199fi
200i=$(${MAKE:-make} -V KERN_IDENT)
201compiler_v=$($(${MAKE:-make} -V CC) -v 2>&1 | grep -w 'version')
202
203for dir in /usr/bin /usr/local/bin; do
204	if [ ! -z "${svnversion}" ] ; then
205		break
206	fi
207	if [ -x "${dir}/svnversion" ] && [ -z ${svnversion} ] ; then
208		# Run svnversion from ${dir} on this script; if return code
209		# is not zero, the checkout might not be compatible with the
210		# svnversion being used.
211		${dir}/svnversion $(realpath ${0}) >/dev/null 2>&1
212		if [ $? -eq 0 ]; then
213			svnversion=${dir}/svnversion
214			break
215		fi
216	fi
217done
218
219if [ -z "${svnversion}" ] && [ -x /usr/bin/svnliteversion ] ; then
220	/usr/bin/svnliteversion $(realpath ${0}) >/dev/null 2>&1
221	if [ $? -eq 0 ]; then
222		svnversion=/usr/bin/svnliteversion
223	else
224		svnversion=
225	fi
226fi
227
228if findvcs .git; then
229	for dir in /usr/bin /usr/local/bin; do
230		if [ -x "${dir}/git" ] ; then
231			git_cmd="${dir}/git -c help.autocorrect=0 --git-dir=${VCSDIR}"
232			break
233		fi
234	done
235fi
236
237if findvcs .gituprevision; then
238	gituprevision="${VCSTOP}/.gituprevision"
239fi
240
241if findvcs .hg; then
242	for dir in /usr/bin /usr/local/bin; do
243		if [ -x "${dir}/hg" ] ; then
244			hg_cmd="${dir}/hg -R ${VCSDIR}"
245			break
246		fi
247	done
248fi
249
250if [ -n "$svnversion" ] ; then
251	svn=$(cd ${SYSDIR} && $svnversion 2>/dev/null)
252	case "$svn" in
253	[0-9]*[MSP]|*:*)
254		svn=" r${svn}"
255		modified=yes
256		;;
257	[0-9]*)
258		svn=" r${svn}"
259		;;
260	*)
261		unset svn
262		;;
263	esac
264fi
265
266if [ -n "$git_cmd" ] ; then
267	git=$($git_cmd rev-parse --verify --short HEAD 2>/dev/null)
268	if [ "$($git_cmd rev-parse --is-shallow-repository)" = false ] ; then
269		git_cnt=$($git_cmd rev-list --first-parent --count HEAD 2>/dev/null)
270		if [ -n "$git_cnt" ] ; then
271			git="n${git_cnt}-${git}"
272		fi
273	fi
274	git_b=$($git_cmd rev-parse --abbrev-ref HEAD)
275	if [ -n "$git_b" -a "$git_b" != "HEAD" ] ; then
276		git="${git_b}-${git}"
277	fi
278	if git_tree_modified; then
279		git="${git}-dirty"
280		modified=yes
281	fi
282	git=" ${git}"
283fi
284
285if [ -n "$gituprevision" ] ; then
286	gitup=" $(awk -F: '{print $2}' $gituprevision)"
287fi
288
289if [ -n "$hg_cmd" ] ; then
290	hg=$($hg_cmd id 2>/dev/null)
291	hgsvn=$($hg_cmd svn info 2>/dev/null | \
292		awk -F': ' '/Revision/ { print $2 }')
293	if [ -n "$hgsvn" ] ; then
294		svn=" r${hgsvn}"
295	fi
296	if [ -n "$hg" ] ; then
297		hg=" ${hg}"
298	fi
299fi
300
301[ ${include_metadata} = "if-modified" -a ${modified} = "yes" ] && include_metadata=yes
302if [ ${include_metadata} != "yes" ]; then
303	VERINFO="${VERSION}${svn}${git}${gitup}${hg} ${i}"
304	VERSTR="${VERINFO}\\n"
305else
306	VERINFO="${VERSION} #${v}${svn}${git}${gitup}${hg}: ${t}"
307	VERSTR="${VERINFO}\\n    ${u}@${h}:${d}\\n"
308fi
309
310vers_content_new=$(cat << EOF
311$COPYRIGHT
312#define SCCSSTR "@(#)${VERINFO}"
313#define VERSTR "${VERSTR}"
314#define RELSTR "${RELEASE}"
315
316char sccs[sizeof(SCCSSTR) > 128 ? sizeof(SCCSSTR) : 128] = SCCSSTR;
317char version[sizeof(VERSTR) > 256 ? sizeof(VERSTR) : 256] = VERSTR;
318char compiler_version[] = "${compiler_v}";
319char ostype[] = "${TYPE}";
320char osrelease[sizeof(RELSTR) > 32 ? sizeof(RELSTR) : 32] = RELSTR;
321int osreldate = ${RELDATE};
322char kern_ident[] = "${i}";
323EOF
324)
325vers_content_old=$(cat vers.c 2>/dev/null || true)
326if [ "$vers_content_new" != "$vers_content_old" ]; then
327	printf "%s\n" "$vers_content_new" > vers.c
328fi
329
330echo $((v + 1)) > version
331