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