xref: /freebsd/sys/conf/newvers.sh (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
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="13.0"
57BRANCH=${BRANCH_OVERRIDE:-CURRENT}
58RELEASE="${REVISION}-${BRANCH}"
59VERSION="${TYPE} ${RELEASE}"
60
61if [ -z "${SYSDIR}" ]; then
62    SYSDIR=$(dirname $0)/..
63fi
64
65RELDATE=$(awk '/__FreeBSD_version.*propagated to newvers/ {print $3}' ${PARAMFILE:-${SYSDIR}/sys/param.h})
66
67if [ -r "${SYSDIR}/../COPYRIGHT" ]; then
68	year=$(sed -Ee '/^Copyright .* The FreeBSD Project/!d;s/^.*1992-([0-9]*) .*$/\1/g' ${SYSDIR}/../COPYRIGHT)
69else
70	year=$(date +%Y)
71fi
72# look for copyright template
73b=share/examples/etc/bsd-style-copyright
74for bsd_copyright in $b ../$b ../../$b ../../../$b /usr/src/$b /usr/$b
75do
76	if [ -r "$bsd_copyright" ]; then
77		COPYRIGHT=$(sed \
78		    -e "s/\[year\]/1992-$year/" \
79		    -e 's/\[your name here\]\.* /The FreeBSD Project./' \
80		    -e 's/\[your name\]\.*/The FreeBSD Project./' \
81		    -e '/\[id for your version control system, if any\]/d' \
82		    $bsd_copyright)
83		break
84	fi
85done
86
87# no copyright found, use a dummy
88if [ -z "$COPYRIGHT" ]; then
89	COPYRIGHT="/*-
90 * Copyright (c) 1992-$year The FreeBSD Project.
91 *
92 */"
93fi
94
95# add newline
96COPYRIGHT="$COPYRIGHT
97"
98
99# We expand include_metadata later since we may set it to the
100# future value of modified.
101include_metadata=yes
102modified=no
103while getopts crRvV: opt; do
104	case "$opt" in
105	c)
106		echo "$COPYRIGHT"
107		exit 0
108		;;
109	r)
110		include_metadata=no
111		;;
112	R)
113		include_metadata=if-modified
114		;;
115	v)
116		# Only put variables that are single lines here.
117		for v in TYPE REVISION BRANCH RELEASE VERSION RELDATE; do
118			eval val=\$${v}
119			echo ${v}=\"${val}\"
120		done
121		exit 0
122		;;
123	V)
124		v=$OPTARG
125		eval val=\$${v}
126		echo ${v}=\"${val}\"
127		exit 0
128		;;
129	esac
130done
131shift $((OPTIND - 1))
132
133# VARS_ONLY means no files should be generated, this is just being
134# included.
135[ -n "$VARS_ONLY" ] && return 0
136
137#
138# findvcs dir
139#	Looks up directory dir at world root and up the filesystem
140#
141findvcs()
142{
143	local savedir
144
145	savedir=$(pwd)
146	cd ${SYSDIR}/..
147	while [ $(pwd) != "/" ]; do
148		if [ -e "./$1" ]; then
149			VCSTOP=$(pwd)
150			VCSDIR=${VCSTOP}"/$1"
151			cd ${savedir}
152			return 0
153		fi
154		cd ..
155	done
156	cd ${savedir}
157	return 1
158}
159
160git_tree_modified()
161{
162	# git diff-index lists both files that are known to have changes as
163	# well as those with metadata that does not match what is recorded in
164	# git's internal state.  The latter case is indicated by an all-zero
165	# destination file hash.
166
167	local fifo
168
169	fifo=$(mktemp -u)
170	mkfifo -m 600 $fifo || exit 1
171	$git_cmd --work-tree=${VCSTOP} diff-index HEAD > $fifo &
172	while read smode dmode ssha dsha status file; do
173		if ! expr $dsha : '^00*$' >/dev/null; then
174			rm $fifo
175			return 0
176		fi
177		if ! $git_cmd --work-tree=${VCSTOP} diff --quiet -- "${file}"; then
178			rm $fifo
179			return 0
180		fi
181	done < $fifo
182	# No files with content differences.
183	rm $fifo
184	return 1
185}
186
187LC_ALL=C; export LC_ALL
188if [ ! -r version ]
189then
190	echo 0 > version
191fi
192
193touch version
194v=$(cat version)
195u=${USER:-root}
196d=$(pwd)
197h=${HOSTNAME:-$(hostname)}
198if [ -n "$SOURCE_DATE_EPOCH" ]; then
199	if ! t=$(date -r $SOURCE_DATE_EPOCH 2>/dev/null); then
200		echo "Invalid SOURCE_DATE_EPOCH" >&2
201		exit 1
202	fi
203else
204	t=$(date)
205fi
206i=$(${MAKE:-make} -V KERN_IDENT)
207compiler_v=$($(${MAKE:-make} -V CC) -v 2>&1 | grep -w 'version')
208
209for dir in /usr/bin /usr/local/bin; do
210	if [ ! -z "${svnversion}" ] ; then
211		break
212	fi
213	if [ -x "${dir}/svnversion" ] && [ -z ${svnversion} ] ; then
214		# Run svnversion from ${dir} on this script; if return code
215		# is not zero, the checkout might not be compatible with the
216		# svnversion being used.
217		${dir}/svnversion $(realpath ${0}) >/dev/null 2>&1
218		if [ $? -eq 0 ]; then
219			svnversion=${dir}/svnversion
220			break
221		fi
222	fi
223done
224
225if [ -z "${svnversion}" ] && [ -x /usr/bin/svnliteversion ] ; then
226	/usr/bin/svnliteversion $(realpath ${0}) >/dev/null 2>&1
227	if [ $? -eq 0 ]; then
228		svnversion=/usr/bin/svnliteversion
229	else
230		svnversion=
231	fi
232fi
233
234if findvcs .git; then
235	for dir in /usr/bin /usr/local/bin; do
236		if [ -x "${dir}/git" ] ; then
237			git_cmd="${dir}/git -c help.autocorrect=0 --git-dir=${VCSDIR}"
238			break
239		fi
240	done
241fi
242
243if findvcs .hg; then
244	for dir in /usr/bin /usr/local/bin; do
245		if [ -x "${dir}/hg" ] ; then
246			hg_cmd="${dir}/hg -R ${VCSDIR}"
247			break
248		fi
249	done
250fi
251
252if [ -n "$svnversion" ] ; then
253	svn=$(cd ${SYSDIR} && $svnversion 2>/dev/null)
254	case "$svn" in
255	[0-9]*[MSP]|*:*)
256		svn=" r${svn}"
257		modified=yes
258		;;
259	[0-9]*)
260		svn=" r${svn}"
261		;;
262	*)
263		unset svn
264		;;
265	esac
266fi
267
268if [ -n "$git_cmd" ] ; then
269	git=$($git_cmd rev-parse --verify --short HEAD 2>/dev/null)
270	gitsvn=$($git_cmd svn find-rev $git 2>/dev/null)
271	if [ -n "$gitsvn" ] ; then
272		svn=" r${gitsvn}"
273		git="=${git}"
274	else
275#		Log searches are limited to 10k commits to speed up failures.
276#		We assume that if a tree is more than 10k commits out-of-sync
277#		with FreeBSD, it has forked the the OS and the SVN rev no
278#		longer matters.
279		gitsvn=$($git_cmd log -n 10000 |
280		    grep '^    git-svn-id:' | head -1 | \
281		    sed -n 's/^.*@\([0-9][0-9]*\).*$/\1/p')
282		if [ -z "$gitsvn" ] ; then
283			gitsvn=$($git_cmd log -n 10000 --format='format:%N' | \
284			     grep '^svn ' | head -1 | \
285			     sed -n 's/^.*revision=\([0-9][0-9]*\).*$/\1/p')
286		fi
287		if [ -n "$gitsvn" ] ; then
288			svn=" r${gitsvn}"
289			git="+${git}"
290		else
291			git=" ${git}"
292		fi
293	fi
294	git_cnt=$($git_cmd rev-list --count HEAD 2>/dev/null)
295	if [ -n "$git_cnt" ] ; then
296		git="${git}-c${git_cnt}"
297	fi
298	git_b=$($git_cmd rev-parse --abbrev-ref HEAD)
299	if [ -n "$git_b" ] ; then
300		git="${git}(${git_b})"
301	fi
302	if git_tree_modified; then
303		git="${git}-dirty"
304		modified=yes
305	fi
306fi
307
308if [ -n "$hg_cmd" ] ; then
309	hg=$($hg_cmd id 2>/dev/null)
310	hgsvn=$($hg_cmd svn info 2>/dev/null | \
311		awk -F': ' '/Revision/ { print $2 }')
312	if [ -n "$hgsvn" ] ; then
313		svn=" r${hgsvn}"
314	fi
315	if [ -n "$hg" ] ; then
316		hg=" ${hg}"
317	fi
318fi
319
320[ ${include_metadata} = "if-modified" -a ${modified} = "yes" ] && include_metadata=yes
321if [ ${include_metadata} != "yes" ]; then
322	VERINFO="${VERSION}${svn}${git}${hg} ${i}"
323	VERSTR="${VERINFO}\\n"
324else
325	VERINFO="${VERSION} #${v}${svn}${git}${hg}: ${t}"
326	VERSTR="${VERINFO}\\n    ${u}@${h}:${d}\\n"
327fi
328
329vers_content_new=$(cat << EOF
330$COPYRIGHT
331#define SCCSSTR "@(#)${VERINFO}"
332#define VERSTR "${VERSTR}"
333#define RELSTR "${RELEASE}"
334
335char sccs[sizeof(SCCSSTR) > 128 ? sizeof(SCCSSTR) : 128] = SCCSSTR;
336char version[sizeof(VERSTR) > 256 ? sizeof(VERSTR) : 256] = VERSTR;
337char compiler_version[] = "${compiler_v}";
338char ostype[] = "${TYPE}";
339char osrelease[sizeof(RELSTR) > 32 ? sizeof(RELSTR) : 32] = RELSTR;
340int osreldate = ${RELDATE};
341char kern_ident[] = "${i}";
342EOF
343)
344vers_content_old=$(cat vers.c 2>/dev/null || true)
345if [ "$vers_content_new" != "$vers_content_old" ]; then
346	echo "$vers_content_new" > vers.c
347fi
348
349echo $((v + 1)) > version
350