xref: /freebsd/sys/conf/newvers.sh (revision 81ad6265)
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# $FreeBSD$
34
35# Command line options:
36#
37#	-c	Print the copyright / license statement as a C comment and exit
38#
39#	-r	Reproducible build.  Do not embed directory names, user	names,
40#		time stamps or other dynamic information into the output file.
41#		This is intended to allow two builds done at different times
42#		and even by different people on different hosts to produce
43#		identical output.
44#
45#	-R	Reproducible build if the tree represents an unmodified
46#		checkout from a version control system.  Metadata is included
47#		if the tree is modified.
48#
49#	-V var	Print ${var}="${val-of-var}" and exit
50#
51#	-v	Print TYPE REVISION BRANCH RELEASE VERSION RELDATE variables
52#		like the -V command
53#
54
55TYPE="FreeBSD"
56REVISION="14.0"
57BRANCH="CURRENT"
58if [ -n "${BRANCH_OVERRIDE}" ]; then
59	BRANCH=${BRANCH_OVERRIDE}
60fi
61RELEASE="${REVISION}-${BRANCH}"
62VERSION="${TYPE} ${RELEASE}"
63
64if [ -z "${SYSDIR}" ]; then
65    SYSDIR=$(dirname $0)/..
66fi
67
68RELDATE=$(awk '/^\#define[[:space:]]*__FreeBSD_version/ {print $3}' ${PARAMFILE:-${SYSDIR}/sys/param.h})
69
70if [ -r "${SYSDIR}/../COPYRIGHT" ]; then
71	year=$(sed -Ee '/^Copyright .* The FreeBSD Project/!d;s/^.*1992-([0-9]*) .*$/\1/g' ${SYSDIR}/../COPYRIGHT)
72else
73	year=$(date +%Y)
74fi
75# look for copyright template
76b=share/examples/etc/bsd-style-copyright
77for bsd_copyright in $b ../$b ../../$b ../../../$b /usr/src/$b /usr/$b
78do
79	if [ -r "$bsd_copyright" ]; then
80		COPYRIGHT=$(sed \
81		    -e "s/\[year\]/1992-$year/" \
82		    -e 's/\[your name here\]\.* /The FreeBSD Project./' \
83		    -e 's/\[your name\]\.*/The FreeBSD Project./' \
84		    -e '/\[id for your version control system, if any\]/d' \
85		    $bsd_copyright)
86		break
87	fi
88done
89
90# no copyright found, use a dummy
91if [ -z "$COPYRIGHT" ]; then
92	COPYRIGHT="/*-
93 * Copyright (c) 1992-$year The FreeBSD Project.
94 *
95 */"
96fi
97
98# add newline
99COPYRIGHT="$COPYRIGHT
100"
101
102# We expand include_metadata later since we may set it to the
103# future value of modified.
104include_metadata=yes
105modified=no
106while getopts crRvV: opt; do
107	case "$opt" in
108	c)
109		echo "$COPYRIGHT"
110		exit 0
111		;;
112	r)
113		include_metadata=no
114		;;
115	R)
116		include_metadata=if-modified
117		;;
118	v)
119		# Only put variables that are single lines here.
120		for v in TYPE REVISION BRANCH RELEASE VERSION RELDATE; do
121			eval val=\$${v}
122			echo ${v}=\"${val}\"
123		done
124		exit 0
125		;;
126	V)
127		v=$OPTARG
128		eval val=\$${v}
129		echo ${v}=\"${val}\"
130		VARS_ONLY_EXIT=1
131		;;
132	esac
133done
134shift $((OPTIND - 1))
135
136# VARS_ONLY means no files should be generated, this is just being
137# included.
138[ -n "$VARS_ONLY" ] && return 0
139
140# VARS_ONLY_EXIT means no files should be generated, only the value of
141# variables are being output.
142[ -n "$VARS_ONLY_EXIT" ] && exit 0
143
144#
145# findvcs dir
146#	Looks up directory dir at world root and up the filesystem
147#
148findvcs()
149{
150	local savedir
151
152	savedir=$(pwd)
153	cd ${SYSDIR}/..
154	while [ $(pwd) != "/" ]; do
155		if [ -e "./$1" ]; then
156			VCSTOP=$(pwd)
157			VCSDIR=${VCSTOP}"/$1"
158			cd ${savedir}
159			return 0
160		fi
161		cd ..
162	done
163	cd ${savedir}
164	return 1
165}
166
167git_tree_modified()
168{
169	! $git_cmd "--work-tree=${VCSTOP}" -c core.checkStat=minimal -c core.fileMode=off diff --quiet
170}
171
172LC_ALL=C; export LC_ALL
173if [ ! -r version ]
174then
175	echo 0 > version
176fi
177
178touch version
179v=$(cat version)
180u=${USER:-root}
181d=$(pwd)
182h=${HOSTNAME:-$(hostname)}
183if [ -n "$SOURCE_DATE_EPOCH" ]; then
184	if ! t=$(date -r $SOURCE_DATE_EPOCH 2>/dev/null); then
185		echo "Invalid SOURCE_DATE_EPOCH" >&2
186		exit 1
187	fi
188else
189	t=$(date)
190fi
191i=$(${MAKE:-make} -V KERN_IDENT)
192compiler_v=$($(${MAKE:-make} -V CC) -v 2>&1 | grep -w 'version')
193
194for dir in /usr/bin /usr/local/bin; do
195	if [ ! -z "${svnversion}" ] ; then
196		break
197	fi
198	if [ -x "${dir}/svnversion" ] && [ -z ${svnversion} ] ; then
199		# Run svnversion from ${dir} on this script; if return code
200		# is not zero, the checkout might not be compatible with the
201		# svnversion being used.
202		${dir}/svnversion $(realpath ${0}) >/dev/null 2>&1
203		if [ $? -eq 0 ]; then
204			svnversion=${dir}/svnversion
205			break
206		fi
207	fi
208done
209
210if [ -z "${svnversion}" ] && [ -x /usr/bin/svnliteversion ] ; then
211	/usr/bin/svnliteversion $(realpath ${0}) >/dev/null 2>&1
212	if [ $? -eq 0 ]; then
213		svnversion=/usr/bin/svnliteversion
214	else
215		svnversion=
216	fi
217fi
218
219if findvcs .git; then
220	for dir in /usr/bin /usr/local/bin; do
221		if [ -x "${dir}/git" ] ; then
222			git_cmd="${dir}/git -c help.autocorrect=0 --git-dir=${VCSDIR}"
223			break
224		fi
225	done
226fi
227
228if findvcs .gituprevision; then
229	gituprevision="${VCSTOP}/.gituprevision"
230fi
231
232if findvcs .hg; then
233	for dir in /usr/bin /usr/local/bin; do
234		if [ -x "${dir}/hg" ] ; then
235			hg_cmd="${dir}/hg -R ${VCSDIR}"
236			break
237		fi
238	done
239fi
240
241if [ -n "$svnversion" ] ; then
242	svn=$(cd ${SYSDIR} && $svnversion 2>/dev/null)
243	case "$svn" in
244	[0-9]*[MSP]|*:*)
245		svn=" r${svn}"
246		modified=yes
247		;;
248	[0-9]*)
249		svn=" r${svn}"
250		;;
251	*)
252		unset svn
253		;;
254	esac
255fi
256
257if [ -n "$git_cmd" ] ; then
258	git=$($git_cmd rev-parse --verify --short HEAD 2>/dev/null)
259	if [ "$($git_cmd rev-parse --is-shallow-repository)" = false ] ; then
260		git_cnt=$($git_cmd rev-list --first-parent --count HEAD 2>/dev/null)
261		if [ -n "$git_cnt" ] ; then
262			git="n${git_cnt}-${git}"
263		fi
264	fi
265	git_b=$($git_cmd rev-parse --abbrev-ref HEAD)
266	if [ -n "$git_b" -a "$git_b" != "HEAD" ] ; then
267		git="${git_b}-${git}"
268	fi
269	if git_tree_modified; then
270		git="${git}-dirty"
271		modified=yes
272	fi
273	git=" ${git}"
274fi
275
276if [ -n "$gituprevision" ] ; then
277	gitup=" $(awk -F: '{print $2}' $gituprevision)"
278fi
279
280if [ -n "$hg_cmd" ] ; then
281	hg=$($hg_cmd id 2>/dev/null)
282	hgsvn=$($hg_cmd svn info 2>/dev/null | \
283		awk -F': ' '/Revision/ { print $2 }')
284	if [ -n "$hgsvn" ] ; then
285		svn=" r${hgsvn}"
286	fi
287	if [ -n "$hg" ] ; then
288		hg=" ${hg}"
289	fi
290fi
291
292[ ${include_metadata} = "if-modified" -a ${modified} = "yes" ] && include_metadata=yes
293if [ ${include_metadata} != "yes" ]; then
294	VERINFO="${VERSION}${svn}${git}${gitup}${hg} ${i}"
295	VERSTR="${VERINFO}\\n"
296else
297	VERINFO="${VERSION} #${v}${svn}${git}${gitup}${hg}: ${t}"
298	VERSTR="${VERINFO}\\n    ${u}@${h}:${d}\\n"
299fi
300
301vers_content_new=$(cat << EOF
302$COPYRIGHT
303#define SCCSSTR "@(#)${VERINFO}"
304#define VERSTR "${VERSTR}"
305#define RELSTR "${RELEASE}"
306
307char sccs[sizeof(SCCSSTR) > 128 ? sizeof(SCCSSTR) : 128] = SCCSSTR;
308char version[sizeof(VERSTR) > 256 ? sizeof(VERSTR) : 256] = VERSTR;
309char compiler_version[] = "${compiler_v}";
310char ostype[] = "${TYPE}";
311char osrelease[sizeof(RELSTR) > 32 ? sizeof(RELSTR) : 32] = RELSTR;
312int osreldate = ${RELDATE};
313char kern_ident[] = "${i}";
314EOF
315)
316vers_content_old=$(cat vers.c 2>/dev/null || true)
317if [ "$vers_content_new" != "$vers_content_old" ]; then
318	printf "%s\n" "$vers_content_new" > vers.c
319fi
320
321echo $((v + 1)) > version
322