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