xref: /freebsd/sys/conf/newvers.sh (revision 43d1e6ee299ad4e143d90d3ad374d1c24bd3306f)
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
47# Note: usr.sbin/amd/include/newvers.sh assumes all variable assignments of
48# upper case variables starting in column 1 are on one line w/o continuation.
49
50TYPE="FreeBSD"
51REVISION="13.0"
52BRANCH=${BRANCH_OVERRIDE:-CURRENT}
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
111RELDATE=$(awk '/__FreeBSD_version.*propagated to newvers/ {print $3}' ${PARAMFILE:-${SYSDIR}/sys/param.h})
112
113if [ -r "${SYSDIR}/../COPYRIGHT" ]; then
114	year=$(sed -Ee '/^Copyright .* The FreeBSD Project/!d;s/^.*1992-([0-9]*) .*$/\1/g' ${SYSDIR}/../COPYRIGHT)
115else
116	year=$(date +%Y)
117fi
118# look for copyright template
119b=share/examples/etc/bsd-style-copyright
120for bsd_copyright in ../$b ../../$b ../../../$b /usr/src/$b /usr/$b
121do
122	if [ -r "$bsd_copyright" ]; then
123		COPYRIGHT=$(sed \
124		    -e "s/\[year\]/1992-$year/" \
125		    -e 's/\[your name here\]\.* /The FreeBSD Project./' \
126		    -e 's/\[your name\]\.*/The FreeBSD Project./' \
127		    -e '/\[id for your version control system, if any\]/d' \
128		    $bsd_copyright)
129		break
130	fi
131done
132
133# no copyright found, use a dummy
134if [ -z "$COPYRIGHT" ]; then
135	COPYRIGHT="/*-
136 * Copyright (c) 1992-$year The FreeBSD Project.
137 *
138 */"
139fi
140
141# add newline
142COPYRIGHT="$COPYRIGHT
143"
144
145# VARS_ONLY means no files should be generated, this is just being
146# included.
147[ -n "$VARS_ONLY" ] && return 0
148
149LC_ALL=C; export LC_ALL
150if [ ! -r version ]
151then
152	echo 0 > version
153fi
154
155touch version
156v=$(cat version)
157u=${USER:-root}
158d=$(pwd)
159h=${HOSTNAME:-$(hostname)}
160if [ -n "$SOURCE_DATE_EPOCH" ]; then
161	if ! t=$(date -r $SOURCE_DATE_EPOCH 2>/dev/null); then
162		echo "Invalid SOURCE_DATE_EPOCH" >&2
163		exit 1
164	fi
165else
166	t=$(date)
167fi
168i=$(${MAKE:-make} -V KERN_IDENT)
169compiler_v=$($(${MAKE:-make} -V CC) -v 2>&1 | grep -w 'version')
170
171for dir in /usr/bin /usr/local/bin; do
172	if [ ! -z "${svnversion}" ] ; then
173		break
174	fi
175	if [ -x "${dir}/svnversion" ] && [ -z ${svnversion} ] ; then
176		# Run svnversion from ${dir} on this script; if return code
177		# is not zero, the checkout might not be compatible with the
178		# svnversion being used.
179		${dir}/svnversion $(realpath ${0}) >/dev/null 2>&1
180		if [ $? -eq 0 ]; then
181			svnversion=${dir}/svnversion
182			break
183		fi
184	fi
185done
186
187if [ -z "${svnversion}" ] && [ -x /usr/bin/svnliteversion ] ; then
188	/usr/bin/svnliteversion $(realpath ${0}) >/dev/null 2>&1
189	if [ $? -eq 0 ]; then
190		svnversion=/usr/bin/svnliteversion
191	else
192		svnversion=
193	fi
194fi
195
196if findvcs .git; then
197	for dir in /usr/bin /usr/local/bin; do
198		if [ -x "${dir}/git" ] ; then
199			git_cmd="${dir}/git -c help.autocorrect=0 --git-dir=${VCSDIR}"
200			break
201		fi
202	done
203fi
204
205if findvcs .hg; then
206	for dir in /usr/bin /usr/local/bin; do
207		if [ -x "${dir}/hg" ] ; then
208			hg_cmd="${dir}/hg -R ${VCSDIR}"
209			break
210		fi
211	done
212fi
213
214if [ -n "$svnversion" ] ; then
215	svn=$(cd ${SYSDIR} && $svnversion 2>/dev/null)
216	case "$svn" in
217	[0-9]*[MSP]|*:*)
218		svn=" r${svn}"
219		modified=true
220		;;
221	[0-9]*)
222		svn=" r${svn}"
223		;;
224	*)
225		unset svn
226		;;
227	esac
228fi
229
230if [ -n "$git_cmd" ] ; then
231	git=$($git_cmd rev-parse --verify --short HEAD 2>/dev/null)
232	gitsvn=$($git_cmd svn find-rev $git 2>/dev/null)
233	if [ -n "$gitsvn" ] ; then
234		svn=" r${gitsvn}"
235		git="=${git}"
236	else
237#		Log searches are limited to 10k commits to speed up failures.
238#		We assume that if a tree is more than 10k commits out-of-sync
239#		with FreeBSD, it has forked the the OS and the SVN rev no
240#		longer matters.
241		gitsvn=$($git_cmd log -n 10000 |
242		    grep '^    git-svn-id:' | head -1 | \
243		    sed -n 's/^.*@\([0-9][0-9]*\).*$/\1/p')
244		if [ -z "$gitsvn" ] ; then
245			gitsvn=$($git_cmd log -n 10000 --format='format:%N' | \
246			     grep '^svn ' | head -1 | \
247			     sed -n 's/^.*revision=\([0-9][0-9]*\).*$/\1/p')
248		fi
249		if [ -n "$gitsvn" ] ; then
250			svn=" r${gitsvn}"
251			git="+${git}"
252		else
253			git=" ${git}"
254		fi
255	fi
256	git_b=$($git_cmd rev-parse --abbrev-ref HEAD)
257	if [ -n "$git_b" ] ; then
258		git="${git}(${git_b})"
259	fi
260	if git_tree_modified; then
261		git="${git}-dirty"
262		modified=true
263	fi
264fi
265
266if [ -n "$hg_cmd" ] ; then
267	hg=$($hg_cmd id 2>/dev/null)
268	hgsvn=$($hg_cmd svn info 2>/dev/null | \
269		awk -F': ' '/Revision/ { print $2 }')
270	if [ -n "$hgsvn" ] ; then
271		svn=" r${hgsvn}"
272	fi
273	if [ -n "$hg" ] ; then
274		hg=" ${hg}"
275	fi
276fi
277
278include_metadata=true
279while getopts rR opt; do
280	case "$opt" in
281	r)
282		include_metadata=
283		;;
284	R)
285		if [ -z "${modified}" ]; then
286			include_metadata=
287		fi
288	esac
289done
290shift $((OPTIND - 1))
291
292if [ -z "${include_metadata}" ]; then
293	VERINFO="${VERSION}${svn}${git}${hg} ${i}"
294	VERSTR="${VERINFO}\\n"
295else
296	VERINFO="${VERSION} #${v}${svn}${git}${hg}: ${t}"
297	VERSTR="${VERINFO}\\n    ${u}@${h}:${d}\\n"
298fi
299
300vers_content_new=$(cat << EOF
301$COPYRIGHT
302#define SCCSSTR "@(#)${VERINFO}"
303#define VERSTR "${VERSTR}"
304#define RELSTR "${RELEASE}"
305
306char sccs[sizeof(SCCSSTR) > 128 ? sizeof(SCCSSTR) : 128] = SCCSSTR;
307char version[sizeof(VERSTR) > 256 ? sizeof(VERSTR) : 256] = VERSTR;
308char compiler_version[] = "${compiler_v}";
309char ostype[] = "${TYPE}";
310char osrelease[sizeof(RELSTR) > 32 ? sizeof(RELSTR) : 32] = RELSTR;
311int osreldate = ${RELDATE};
312char kern_ident[] = "${i}";
313EOF
314)
315vers_content_old=$(cat vers.c 2>/dev/null || true)
316if [ "$vers_content_new" != "$vers_content_old" ]; then
317	echo "$vers_content_new" > vers.c
318fi
319
320echo $((v + 1)) > version
321