xref: /freebsd/sys/conf/newvers.sh (revision fe2494903422ba3b924eba82cb63a6a9188fad7a)
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 vcstop_abs
87
88	fifo=$(mktemp -u)
89	mkfifo -m 600 $fifo
90	vcstop_abs=$(realpath $VCSTOP)
91	$git_cmd --work-tree=${VCSTOP} diff-index HEAD > $fifo &
92	while read smode dmode ssha dsha status file; do
93		if ! expr $dsha : '^00*$' >/dev/null; then
94			rm $fifo
95			return 0
96		fi
97		if ! $git_cmd diff --quiet -- "${vcstop_abs}/${file}"; then
98			rm $fifo
99			return 0
100		fi
101	done < $fifo
102	# No files with content differences.
103	rm $fifo
104	return 1
105}
106
107
108if [ -z "${SYSDIR}" ]; then
109    SYSDIR=$(dirname $0)/..
110fi
111
112if [ -n "${PARAMFILE}" ]; then
113	RELDATE=$(awk '/__FreeBSD_version.*propagated to newvers/ {print $3}' \
114		${PARAMFILE})
115else
116	RELDATE=$(awk '/__FreeBSD_version.*propagated to newvers/ {print $3}' \
117		${SYSDIR}/sys/param.h)
118fi
119
120b=share/examples/etc/bsd-style-copyright
121if [ -r "${SYSDIR}/../COPYRIGHT" ]; then
122	year=$(sed -Ee '/^Copyright .* The FreeBSD Project/!d;s/^.*1992-([0-9]*) .*$/\1/g' ${SYSDIR}/../COPYRIGHT)
123else
124	year=$(date +%Y)
125fi
126# look for copyright template
127for bsd_copyright in ../$b ../../$b ../../../$b /usr/src/$b /usr/$b
128do
129	if [ -r "$bsd_copyright" ]; then
130		COPYRIGHT=`sed \
131		    -e "s/\[year\]/1992-$year/" \
132		    -e 's/\[your name here\]\.* /The FreeBSD Project./' \
133		    -e 's/\[your name\]\.*/The FreeBSD Project./' \
134		    -e '/\[id for your version control system, if any\]/d' \
135		    $bsd_copyright`
136		break
137	fi
138done
139
140# no copyright found, use a dummy
141if [ -z "$COPYRIGHT" ]; then
142	COPYRIGHT="/*-
143 * Copyright (c) 1992-$year The FreeBSD Project.
144 * All rights reserved.
145 *
146 */"
147fi
148
149# add newline
150COPYRIGHT="$COPYRIGHT
151"
152
153# VARS_ONLY means no files should be generated, this is just being
154# included.
155if [ -n "$VARS_ONLY" ]; then
156	return 0
157fi
158
159LC_ALL=C; export LC_ALL
160if [ ! -r version ]
161then
162	echo 0 > version
163fi
164
165touch version
166v=`cat version`
167u=${USER:-root}
168d=`pwd`
169h=${HOSTNAME:-`hostname`}
170if [ -n "$SOURCE_DATE_EPOCH" ]; then
171	if ! t=`date -r $SOURCE_DATE_EPOCH 2>/dev/null`; then
172		echo "Invalid SOURCE_DATE_EPOCH" >&2
173		exit 1
174	fi
175else
176	t=`date`
177fi
178i=`${MAKE:-make} -V KERN_IDENT`
179compiler_v=$($(${MAKE:-make} -V CC) -v 2>&1 | grep -w 'version')
180
181for dir in /usr/bin /usr/local/bin; do
182	if [ ! -z "${svnversion}" ] ; then
183		break
184	fi
185	if [ -x "${dir}/svnversion" ] && [ -z ${svnversion} ] ; then
186		# Run svnversion from ${dir} on this script; if return code
187		# is not zero, the checkout might not be compatible with the
188		# svnversion being used.
189		${dir}/svnversion $(realpath ${0}) >/dev/null 2>&1
190		if [ $? -eq 0 ]; then
191			svnversion=${dir}/svnversion
192			break
193		fi
194	fi
195done
196
197if [ -z "${svnversion}" ] && [ -x /usr/bin/svnliteversion ] ; then
198	/usr/bin/svnliteversion $(realpath ${0}) >/dev/null 2>&1
199	if [ $? -eq 0 ]; then
200		svnversion=/usr/bin/svnliteversion
201	else
202		svnversion=
203	fi
204fi
205
206for dir in /usr/bin /usr/local/bin; do
207	if [ -x "${dir}/p4" ] && [ -z ${p4_cmd} ] ; then
208		p4_cmd=${dir}/p4
209	fi
210done
211
212if findvcs .git; then
213	for dir in /usr/bin /usr/local/bin; do
214		if [ -x "${dir}/git" ] ; then
215			git_cmd="${dir}/git -c help.autocorrect=0 --git-dir=${VCSDIR}"
216			break
217		fi
218	done
219fi
220
221if findvcs .hg; then
222	for dir in /usr/bin /usr/local/bin; do
223		if [ -x "${dir}/hg" ] ; then
224			hg_cmd="${dir}/hg -R ${VCSDIR}"
225			break
226		fi
227	done
228fi
229
230if [ -n "$svnversion" ] ; then
231	svn=`cd ${SYSDIR} && $svnversion 2>/dev/null`
232	case "$svn" in
233	[0-9]*[MSP]|*:*)
234		svn=" r${svn}"
235		modified=true
236		;;
237	[0-9]*)
238		svn=" r${svn}"
239		;;
240	*)
241		unset svn
242		;;
243	esac
244fi
245
246if [ -n "$git_cmd" ] ; then
247	git=`$git_cmd rev-parse --verify --short HEAD 2>/dev/null`
248	svn=`$git_cmd svn find-rev $git 2>/dev/null`
249	if [ -n "$svn" ] ; then
250		svn=" r${svn}"
251		git="=${git}"
252	else
253		svn=`$git_cmd log --grep '^git-svn-id:' | \
254		    grep '^    git-svn-id:' | head -1 | \
255		    sed -n 's/^.*@\([0-9][0-9]*\).*$/\1/p'`
256		if [ -z "$svn" ] ; then
257			svn=`$git_cmd log --format='format:%N' | \
258			     grep '^svn ' | head -1 | \
259			     sed -n 's/^.*revision=\([0-9][0-9]*\).*$/\1/p'`
260		fi
261		if [ -n "$svn" ] ; then
262			svn=" r${svn}"
263			git="+${git}"
264		else
265			git=" ${git}"
266		fi
267	fi
268	git_b=`$git_cmd rev-parse --abbrev-ref HEAD`
269	if [ -n "$git_b" ] ; then
270		git="${git}(${git_b})"
271	fi
272	if git_tree_modified; then
273		git="${git}-dirty"
274		modified=true
275	fi
276fi
277
278if [ -n "$p4_cmd" ] ; then
279	p4version=`cd ${SYSDIR} && $p4_cmd changes -m1 "./...#have" 2>&1 | \
280		awk '{ print $2 }'`
281	case "$p4version" in
282	[0-9]*)
283		p4version=" ${p4version}"
284		p4opened=`cd ${SYSDIR} && $p4_cmd opened ./... 2>&1`
285		case "$p4opened" in
286		File*) ;;
287		//*)
288			p4version="${p4version}+edit"
289			modified=true
290			;;
291		esac
292		;;
293	*)	unset p4version ;;
294	esac
295fi
296
297if [ -n "$hg_cmd" ] ; then
298	hg=`$hg_cmd id 2>/dev/null`
299	svn=`$hg_cmd svn info 2>/dev/null | \
300		awk -F': ' '/Revision/ { print $2 }'`
301	if [ -n "$svn" ] ; then
302		svn=" r${svn}"
303	fi
304	if [ -n "$hg" ] ; then
305		hg=" ${hg}"
306	fi
307fi
308
309include_metadata=true
310while getopts rR opt; do
311	case "$opt" in
312	r)
313		include_metadata=
314		;;
315	R)
316		if [ -z "${modified}" ]; then
317			include_metadata=
318		fi
319	esac
320done
321shift $((OPTIND - 1))
322
323if [ -z "${include_metadata}" ]; then
324	VERINFO="${VERSION}${svn}${git}${hg}${p4version} ${i}"
325	VERSTR="${VERINFO}\\n"
326else
327	VERINFO="${VERSION} #${v}${svn}${git}${hg}${p4version}: ${t}"
328	VERSTR="${VERINFO}\\n    ${u}@${h}:${d}\\n"
329fi
330
331cat << EOF > vers.c
332$COPYRIGHT
333#define SCCSSTR "@(#)${VERINFO}"
334#define VERSTR "${VERSTR}"
335#define RELSTR "${RELEASE}"
336
337char sccs[sizeof(SCCSSTR) > 128 ? sizeof(SCCSSTR) : 128] = SCCSSTR;
338char version[sizeof(VERSTR) > 256 ? sizeof(VERSTR) : 256] = VERSTR;
339char compiler_version[] = "${compiler_v}";
340char ostype[] = "${TYPE}";
341char osrelease[sizeof(RELSTR) > 32 ? sizeof(RELSTR) : 32] = RELSTR;
342int osreldate = ${RELDATE};
343char kern_ident[] = "${i}";
344EOF
345
346echo $((v + 1)) > version
347