xref: /freebsd/sys/conf/newvers.sh (revision 8b4e4c2737305df8807abc6cd054a32586085c93)
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# Command line options:
33#
34#	-c	Print the copyright / license statement as a C comment and exit
35#
36#	-r	Reproducible build.  Do not embed directory names, user	names,
37#		time stamps or other dynamic information into the output file.
38#		This is intended to allow two builds done at different times
39#		and even by different people on different hosts to produce
40#		identical output.
41#
42#	-R	Reproducible build if the tree represents an unmodified
43#		checkout from a version control system.  Metadata is included
44#		if the tree is modified.
45#
46#	-V var	Print ${var}="${val-of-var}" and exit
47#
48#	-v	Print TYPE REVISION BRANCH RELEASE VERSION RELDATE variables
49#		like the -V command
50#
51
52TYPE="FreeBSD"
53REVISION="16.0"
54BRANCH="CURRENT"
55if [ -n "${BRANCH_OVERRIDE}" ]; then
56	BRANCH=${BRANCH_OVERRIDE}
57fi
58unset RELEASE
59unset VERSION
60
61if [ -z "${SYSDIR}" ]; then
62	SYSDIR=$(dirname $0)/..
63fi
64
65# allow random overrides
66while :
67do
68	case "$1" in
69	*=*) eval "$1"; shift;;
70	*) break;;
71	esac
72done
73
74RELEASE="${RELEASE:-${REVISION}-${BRANCH}}"
75VERSION="${VERSION:-${TYPE} ${RELEASE}}"
76
77RELDATE=$(awk '/^#define[[:space:]]*__FreeBSD_version/ {print $3}' ${PARAMFILE:-${SYSDIR}/sys/param.h})
78
79if [ -r "${SYSDIR}/../COPYRIGHT" ]; then
80	year=$(sed -Ee '/^Copyright .* The FreeBSD Project/!d;s/^.*1992-([0-9]*) .*$/\1/g' ${SYSDIR}/../COPYRIGHT)
81else
82	year=$(date +%Y)
83fi
84# look for copyright template
85b=share/examples/etc/bsd-style-copyright
86for bsd_copyright in $b ../$b ../../$b ../../../$b /usr/src/$b /usr/$b
87do
88	if [ -r "$bsd_copyright" ]; then
89		COPYRIGHT=$(sed \
90		    -e "s/\[year\]/1992-$year/" \
91		    -e 's/\[your name here\]\.* /The FreeBSD Project./' \
92		    -e 's/\[your name\]\.*/The FreeBSD Project./' \
93		    -e '/\[id for your version control system, if any\]/d' \
94		    $bsd_copyright)
95		break
96	fi
97done
98
99# no copyright found, use a dummy
100if [ -z "$COPYRIGHT" ]; then
101	COPYRIGHT="/*-
102 * Copyright (c) 1992-$year The FreeBSD Project.
103 *
104 */"
105fi
106
107# add newline
108COPYRIGHT="$COPYRIGHT
109"
110
111# We expand include_metadata later since we may set it to the
112# future value of modified.
113builddir=$(pwd)
114include_metadata=yes
115modified=no
116while getopts cd:rRvV: opt; do
117	case "$opt" in
118	c)
119		echo "$COPYRIGHT"
120		exit 0
121		;;
122	d)
123		builddir=$OPTARG
124		;;
125	r)
126		include_metadata=no
127		;;
128	R)
129		include_metadata=if-modified
130		;;
131	v)
132		# Only put variables that are single lines here.
133		for v in TYPE REVISION BRANCH RELEASE VERSION RELDATE; do
134			eval val=\$${v}
135			echo ${v}=\"${val}\"
136		done
137		exit 0
138		;;
139	V)
140		v=$OPTARG
141		eval val=\$${v}
142		echo ${v}=\"${val}\"
143		VARS_ONLY_EXIT=1
144		;;
145	esac
146done
147shift $((OPTIND - 1))
148
149# VARS_ONLY means no files should be generated, this is just being
150# included.
151[ -n "$VARS_ONLY" ] && return 0
152
153# VARS_ONLY_EXIT means no files should be generated, only the value of
154# variables are being output.
155[ -n "$VARS_ONLY_EXIT" ] && exit 0
156
157#
158# findvcs dir
159#	Looks up directory dir at world root and up the filesystem
160#
161findvcs()
162{
163	local savedir
164
165	savedir=$(pwd)
166	cd ${SYSDIR}/..
167	while [ $(pwd) != "/" ]; do
168		if [ -e "./$1" ]; then
169			VCSTOP=$(pwd)
170			VCSDIR=${VCSTOP}"/$1"
171			cd ${savedir}
172			return 0
173		fi
174		cd ..
175	done
176	cd ${savedir}
177	return 1
178}
179
180git_tree_modified()
181{
182	! $git_cmd "--work-tree=${VCSTOP}" -c core.checkStat=minimal -c core.fileMode=off diff --quiet
183}
184
185LC_ALL=C; export LC_ALL
186if [ ! -r version ]
187then
188	echo 0 > version
189fi
190
191touch version
192v=$(cat version)
193u=${USER:-root}
194d=$builddir
195h=${HOSTNAME:-$(hostname)}
196if [ -n "$SOURCE_DATE_EPOCH" ]; then
197	if ! t=$(date -ur $SOURCE_DATE_EPOCH 2>/dev/null); then
198		echo "Invalid SOURCE_DATE_EPOCH" >&2
199		exit 1
200	fi
201else
202	t=$(date)
203fi
204i=$(${MAKE:-make} -V KERN_IDENT)
205compiler_v=$($(${MAKE:-make} -V CC) -v 2>&1 | grep -w 'version')
206
207for dir in /usr/bin /usr/local/bin; do
208	if [ ! -z "${svnversion}" ] ; then
209		break
210	fi
211	if [ -x "${dir}/svnversion" ] && [ -z ${svnversion} ] ; then
212		# Run svnversion from ${dir} on this script; if return code
213		# is not zero, the checkout might not be compatible with the
214		# svnversion being used.
215		${dir}/svnversion $(realpath ${0}) >/dev/null 2>&1
216		if [ $? -eq 0 ]; then
217			svnversion=${dir}/svnversion
218			break
219		fi
220	fi
221done
222
223if findvcs .git; then
224	for dir in /usr/bin /usr/local/bin; do
225		if [ -x "${dir}/git" ] ; then
226			git_cmd="${dir}/git -c help.autocorrect=0 --git-dir=${VCSDIR}"
227			break
228		fi
229	done
230fi
231
232if findvcs .gituprevision; then
233	gituprevision="${VCSTOP}/.gituprevision"
234fi
235
236if findvcs .hg; then
237	for dir in /usr/bin /usr/local/bin; do
238		if [ -x "${dir}/hg" ] ; then
239			hg_cmd="${dir}/hg -R ${VCSDIR}"
240			break
241		fi
242	done
243fi
244
245if [ -n "$svnversion" ] ; then
246	svn=$(cd ${SYSDIR} && $svnversion 2>/dev/null)
247	case "$svn" in
248	[0-9]*[MSP]|*:*)
249		svn=" r${svn}"
250		modified=yes
251		;;
252	[0-9]*)
253		svn=" r${svn}"
254		;;
255	*)
256		unset svn
257		;;
258	esac
259fi
260
261if [ -n "$git_cmd" ] ; then
262	git=$($git_cmd rev-parse --verify --short=12 HEAD 2>/dev/null)
263	if [ "$($git_cmd rev-parse --is-shallow-repository)" = false ] ; then
264		git_cnt=$($git_cmd rev-list --first-parent --count HEAD 2>/dev/null)
265		if [ -n "$git_cnt" ] ; then
266			git="n${git_cnt}-${git}"
267		fi
268	fi
269	git_b=$($git_cmd rev-parse --abbrev-ref HEAD)
270	if [ -n "$git_b" -a "$git_b" != "HEAD" ] ; then
271		git="${git_b}-${git}"
272	fi
273	if git_tree_modified; then
274		git="${git}-dirty"
275		modified=yes
276	fi
277	git=" ${git}"
278fi
279
280if [ -n "$gituprevision" ] ; then
281	gitup=" $(awk -F: '{print $2}' $gituprevision)"
282fi
283
284if [ -n "$hg_cmd" ] ; then
285	hg=$($hg_cmd id 2>/dev/null)
286	hgsvn=$($hg_cmd svn info 2>/dev/null | \
287		awk -F': ' '/Revision/ { print $2 }')
288	if [ -n "$hgsvn" ] ; then
289		svn=" r${hgsvn}"
290	fi
291	if [ -n "$hg" ] ; then
292		hg=" ${hg}"
293	fi
294fi
295
296[ ${include_metadata} = "if-modified" -a ${modified} = "yes" ] && include_metadata=yes
297if [ ${include_metadata} != "yes" ]; then
298	VERINFO="${VERSION}${svn}${git}${gitup}${hg} ${i}"
299	VERSTR="${VERINFO}\\n"
300else
301	VERINFO="${VERSION} #${v}${svn}${git}${gitup}${hg}: ${t}"
302	VERSTR="${VERINFO}\\n    ${u}@${h}:${d}\\n"
303fi
304
305vers_content_new=$(cat << EOF
306$COPYRIGHT
307/*
308 * The SCCS stuff is a marker that by convention identifies the kernel.  While
309 * the convention originated with SCCS, the current use is more generic and is
310 * used by different organizations to identify the kernel, the crash dump,
311 * etc. The what(1) utility prints these markers. Better methods exist, so this
312 * method is deprecated and will be removed in a future version of FreeBSD. Orgs
313 * that use it are encouraged to migrate before then.
314 */
315#define SCCSSTR "@(#)${VERINFO}"
316#define VERSTR "${VERSTR}"
317#define RELSTR "${RELEASE}"
318
319const char sccs[sizeof(SCCSSTR) > 128 ? sizeof(SCCSSTR) : 128] = SCCSSTR;
320const char version[sizeof(VERSTR) > 256 ? sizeof(VERSTR) : 256] = VERSTR;
321const char compiler_version[] = "${compiler_v}";
322const char ostype[] = "${TYPE}";
323const char osrelease[sizeof(RELSTR) > 32 ? sizeof(RELSTR) : 32] = RELSTR;
324const int osreldate = ${RELDATE};
325const char kern_ident[] = "${i}";
326EOF
327)
328vers_content_old=$(cat vers.c 2>/dev/null || true)
329if [ "$vers_content_new" != "$vers_content_old" ]; then
330	printf "%s\n" "$vers_content_new" > vers.c
331fi
332
333echo $((v + 1)) > version
334