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