xref: /freebsd/sys/conf/newvers.sh (revision da5432eda807c4b7232d030d5157d5b417ea4f52)
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 variables
52#		like the -V command
53#
54
55TYPE="FreeBSD"
56REVISION="14.0"
57BRANCH="ALPHA1"
58if [ -n "${BRANCH_OVERRIDE}" ]; then
59	BRANCH=${BRANCH_OVERRIDE}
60fi
61
62if [ -z "${SYSDIR}" ]; then
63	SYSDIR=$(dirname $0)/..
64fi
65
66# allow random overrides
67while :
68do
69	case "$1" in
70	*=*) eval "$1"; shift;;
71	*) break;;
72	esac
73done
74
75RELEASE="${RELEASE:-${REVISION}-${BRANCH}}"
76VERSION="${VERSION:-${TYPE} ${RELEASE}}"
77
78RELDATE=$(awk '/^#define[[:space:]]*__FreeBSD_version/ {print $3}' ${PARAMFILE:-${SYSDIR}/sys/param.h})
79
80if [ -r "${SYSDIR}/../COPYRIGHT" ]; then
81	year=$(sed -Ee '/^Copyright .* The FreeBSD Project/!d;s/^.*1992-([0-9]*) .*$/\1/g' ${SYSDIR}/../COPYRIGHT)
82else
83	year=$(date +%Y)
84fi
85# look for copyright template
86b=share/examples/etc/bsd-style-copyright
87for bsd_copyright in $b ../$b ../../$b ../../../$b /usr/src/$b /usr/$b
88do
89	if [ -r "$bsd_copyright" ]; then
90		COPYRIGHT=$(sed \
91		    -e "s/\[year\]/1992-$year/" \
92		    -e 's/\[your name here\]\.* /The FreeBSD Project./' \
93		    -e 's/\[your name\]\.*/The FreeBSD Project./' \
94		    -e '/\[id for your version control system, if any\]/d' \
95		    $bsd_copyright)
96		break
97	fi
98done
99
100# no copyright found, use a dummy
101if [ -z "$COPYRIGHT" ]; then
102	COPYRIGHT="/*-
103 * Copyright (c) 1992-$year The FreeBSD Project.
104 *
105 */"
106fi
107
108# add newline
109COPYRIGHT="$COPYRIGHT
110"
111
112# We expand include_metadata later since we may set it to the
113# future value of modified.
114include_metadata=yes
115modified=no
116while getopts crRvV: opt; do
117	case "$opt" in
118	c)
119		echo "$COPYRIGHT"
120		exit 0
121		;;
122	r)
123		include_metadata=no
124		;;
125	R)
126		include_metadata=if-modified
127		;;
128	v)
129		# Only put variables that are single lines here.
130		for v in TYPE REVISION BRANCH RELEASE VERSION RELDATE; do
131			eval val=\$${v}
132			echo ${v}=\"${val}\"
133		done
134		exit 0
135		;;
136	V)
137		v=$OPTARG
138		eval val=\$${v}
139		echo ${v}=\"${val}\"
140		VARS_ONLY_EXIT=1
141		;;
142	esac
143done
144shift $((OPTIND - 1))
145
146# VARS_ONLY means no files should be generated, this is just being
147# included.
148[ -n "$VARS_ONLY" ] && return 0
149
150# VARS_ONLY_EXIT means no files should be generated, only the value of
151# variables are being output.
152[ -n "$VARS_ONLY_EXIT" ] && exit 0
153
154#
155# findvcs dir
156#	Looks up directory dir at world root and up the filesystem
157#
158findvcs()
159{
160	local savedir
161
162	savedir=$(pwd)
163	cd ${SYSDIR}/..
164	while [ $(pwd) != "/" ]; do
165		if [ -e "./$1" ]; then
166			VCSTOP=$(pwd)
167			VCSDIR=${VCSTOP}"/$1"
168			cd ${savedir}
169			return 0
170		fi
171		cd ..
172	done
173	cd ${savedir}
174	return 1
175}
176
177git_tree_modified()
178{
179	! $git_cmd "--work-tree=${VCSTOP}" -c core.checkStat=minimal -c core.fileMode=off diff --quiet
180}
181
182LC_ALL=C; export LC_ALL
183if [ ! -r version ]
184then
185	echo 0 > version
186fi
187
188touch version
189v=$(cat version)
190u=${USER:-root}
191d=$(pwd)
192h=${HOSTNAME:-$(hostname)}
193if [ -n "$SOURCE_DATE_EPOCH" ]; then
194	if ! t=$(date -r $SOURCE_DATE_EPOCH 2>/dev/null); then
195		echo "Invalid SOURCE_DATE_EPOCH" >&2
196		exit 1
197	fi
198else
199	t=$(date)
200fi
201i=$(${MAKE:-make} -V KERN_IDENT)
202compiler_v=$($(${MAKE:-make} -V CC) -v 2>&1 | grep -w 'version')
203
204for dir in /usr/bin /usr/local/bin; do
205	if [ ! -z "${svnversion}" ] ; then
206		break
207	fi
208	if [ -x "${dir}/svnversion" ] && [ -z ${svnversion} ] ; then
209		# Run svnversion from ${dir} on this script; if return code
210		# is not zero, the checkout might not be compatible with the
211		# svnversion being used.
212		${dir}/svnversion $(realpath ${0}) >/dev/null 2>&1
213		if [ $? -eq 0 ]; then
214			svnversion=${dir}/svnversion
215			break
216		fi
217	fi
218done
219
220if [ -z "${svnversion}" ] && [ -x /usr/bin/svnliteversion ] ; then
221	/usr/bin/svnliteversion $(realpath ${0}) >/dev/null 2>&1
222	if [ $? -eq 0 ]; then
223		svnversion=/usr/bin/svnliteversion
224	else
225		svnversion=
226	fi
227fi
228
229if findvcs .git; then
230	for dir in /usr/bin /usr/local/bin; do
231		if [ -x "${dir}/git" ] ; then
232			git_cmd="${dir}/git -c help.autocorrect=0 --git-dir=${VCSDIR}"
233			break
234		fi
235	done
236fi
237
238if findvcs .gituprevision; then
239	gituprevision="${VCSTOP}/.gituprevision"
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=yes
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	if [ "$($git_cmd rev-parse --is-shallow-repository)" = false ] ; then
270		git_cnt=$($git_cmd rev-list --first-parent --count HEAD 2>/dev/null)
271		if [ -n "$git_cnt" ] ; then
272			git="n${git_cnt}-${git}"
273		fi
274	fi
275	git_b=$($git_cmd rev-parse --abbrev-ref HEAD)
276	if [ -n "$git_b" -a "$git_b" != "HEAD" ] ; then
277		git="${git_b}-${git}"
278	fi
279	if git_tree_modified; then
280		git="${git}-dirty"
281		modified=yes
282	fi
283	git=" ${git}"
284fi
285
286if [ -n "$gituprevision" ] ; then
287	gitup=" $(awk -F: '{print $2}' $gituprevision)"
288fi
289
290if [ -n "$hg_cmd" ] ; then
291	hg=$($hg_cmd id 2>/dev/null)
292	hgsvn=$($hg_cmd svn info 2>/dev/null | \
293		awk -F': ' '/Revision/ { print $2 }')
294	if [ -n "$hgsvn" ] ; then
295		svn=" r${hgsvn}"
296	fi
297	if [ -n "$hg" ] ; then
298		hg=" ${hg}"
299	fi
300fi
301
302[ ${include_metadata} = "if-modified" -a ${modified} = "yes" ] && include_metadata=yes
303if [ ${include_metadata} != "yes" ]; then
304	VERINFO="${VERSION}${svn}${git}${gitup}${hg} ${i}"
305	VERSTR="${VERINFO}\\n"
306else
307	VERINFO="${VERSION} #${v}${svn}${git}${gitup}${hg}: ${t}"
308	VERSTR="${VERINFO}\\n    ${u}@${h}:${d}\\n"
309fi
310
311vers_content_new=$(cat << EOF
312$COPYRIGHT
313#define SCCSSTR "@(#)${VERINFO}"
314#define VERSTR "${VERSTR}"
315#define RELSTR "${RELEASE}"
316
317char sccs[sizeof(SCCSSTR) > 128 ? sizeof(SCCSSTR) : 128] = SCCSSTR;
318char version[sizeof(VERSTR) > 256 ? sizeof(VERSTR) : 256] = VERSTR;
319char compiler_version[] = "${compiler_v}";
320char ostype[] = "${TYPE}";
321char osrelease[sizeof(RELSTR) > 32 ? sizeof(RELSTR) : 32] = RELSTR;
322int osreldate = ${RELDATE};
323char kern_ident[] = "${i}";
324EOF
325)
326vers_content_old=$(cat vers.c 2>/dev/null || true)
327if [ "$vers_content_new" != "$vers_content_old" ]; then
328	printf "%s\n" "$vers_content_new" > vers.c
329fi
330
331echo $((v + 1)) > version
332