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