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