xref: /freebsd/sys/conf/newvers.sh (revision 16f3346309757259b3613e43a2eb6404659cdda9)
1df8bae1dSRodney W. Grimes#!/bin/sh -
251369649SPedro F. Giffuni#
3981e34b9SPedro F. Giffuni# SPDX-License-Identifier: BSD-3-Clause
4df8bae1dSRodney W. Grimes#
5df8bae1dSRodney W. Grimes# Copyright (c) 1984, 1986, 1990, 1993
6df8bae1dSRodney W. Grimes#	The Regents of the University of California.  All rights reserved.
7df8bae1dSRodney W. Grimes#
8df8bae1dSRodney W. Grimes# Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes# modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes# are met:
11df8bae1dSRodney W. Grimes# 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes#    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes# 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes#    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes#    documentation and/or other materials provided with the distribution.
16f232b391SEd Maste# 3. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes#    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes#    without specific prior written permission.
19df8bae1dSRodney W. Grimes#
20df8bae1dSRodney W. Grimes# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes# SUCH DAMAGE.
31df8bae1dSRodney W. Grimes
327e9de36cSEd Maste# Command line options:
337e9de36cSEd Maste#
349afea54aSWarner Losh#	-c	Print the copyright / license statement as a C comment and exit
359afea54aSWarner Losh#
369afea54aSWarner Losh#	-r	Reproducible build.  Do not embed directory names, user	names,
379afea54aSWarner Losh#		time stamps or other dynamic information into the output file.
389afea54aSWarner Losh#		This is intended to allow two builds done at different times
399afea54aSWarner Losh#		and even by different people on different hosts to produce
409afea54aSWarner Losh#		identical output.
4161ae7e24SEd Maste#
4261ae7e24SEd Maste#	-R	Reproducible build if the tree represents an unmodified
439afea54aSWarner Losh#		checkout from a version control system.  Metadata is included
449afea54aSWarner Losh#		if the tree is modified.
459afea54aSWarner Losh#
469afea54aSWarner Losh#	-V var	Print ${var}="${val-of-var}" and exit
479afea54aSWarner Losh#
481eb8dfc6SEd Maste#	-v	Print TYPE REVISION BRANCH RELEASE VERSION RELDATE variables
499afea54aSWarner Losh#		like the -V command
509afea54aSWarner Losh#
514ff9f3e6SWarner Losh
52c0ac91feSPoul-Henning KampTYPE="FreeBSD"
53aee253d8SGlen BarberREVISION="15.0"
54aee253d8SGlen BarberBRANCH="CURRENT"
55bfdd0a34SGordon Tetlowif [ -n "${BRANCH_OVERRIDE}" ]; then
56bfdd0a34SGordon Tetlow	BRANCH=${BRANCH_OVERRIDE}
57bfdd0a34SGordon Tetlowfi
589f16abf8SDag-Erling Smørgravunset RELEASE
599f16abf8SDag-Erling Smørgravunset VERSION
600210509aSIan Lepore
619afea54aSWarner Loshif [ -z "${SYSDIR}" ]; then
629afea54aSWarner Losh	SYSDIR=$(dirname $0)/..
639afea54aSWarner Loshfi
649afea54aSWarner Losh
6589f361f7SSimon J. Gerraty# allow random overrides
6689f361f7SSimon J. Gerratywhile :
6789f361f7SSimon J. Gerratydo
6889f361f7SSimon J. Gerraty	case "$1" in
6989f361f7SSimon J. Gerraty	*=*) eval "$1"; shift;;
7089f361f7SSimon J. Gerraty	*) break;;
7189f361f7SSimon J. Gerraty	esac
7289f361f7SSimon J. Gerratydone
7389f361f7SSimon J. Gerraty
7489f361f7SSimon J. GerratyRELEASE="${RELEASE:-${REVISION}-${BRANCH}}"
7589f361f7SSimon J. GerratyVERSION="${VERSION:-${TYPE} ${RELEASE}}"
7689f361f7SSimon J. Gerraty
772c19beeeSWarner LoshRELDATE=$(awk '/^#define[[:space:]]*__FreeBSD_version/ {print $3}' ${PARAMFILE:-${SYSDIR}/sys/param.h})
789afea54aSWarner Losh
799afea54aSWarner Loshif [ -r "${SYSDIR}/../COPYRIGHT" ]; then
809afea54aSWarner Losh	year=$(sed -Ee '/^Copyright .* The FreeBSD Project/!d;s/^.*1992-([0-9]*) .*$/\1/g' ${SYSDIR}/../COPYRIGHT)
819afea54aSWarner Loshelse
829afea54aSWarner Losh	year=$(date +%Y)
839afea54aSWarner Loshfi
849afea54aSWarner Losh# look for copyright template
859afea54aSWarner Loshb=share/examples/etc/bsd-style-copyright
869afea54aSWarner Loshfor bsd_copyright in $b ../$b ../../$b ../../../$b /usr/src/$b /usr/$b
879afea54aSWarner Loshdo
889afea54aSWarner Losh	if [ -r "$bsd_copyright" ]; then
899afea54aSWarner Losh		COPYRIGHT=$(sed \
909afea54aSWarner Losh		    -e "s/\[year\]/1992-$year/" \
919afea54aSWarner Losh		    -e 's/\[your name here\]\.* /The FreeBSD Project./' \
929afea54aSWarner Losh		    -e 's/\[your name\]\.*/The FreeBSD Project./' \
939afea54aSWarner Losh		    -e '/\[id for your version control system, if any\]/d' \
949afea54aSWarner Losh		    $bsd_copyright)
959afea54aSWarner Losh		break
969afea54aSWarner Losh	fi
979afea54aSWarner Loshdone
989afea54aSWarner Losh
999afea54aSWarner Losh# no copyright found, use a dummy
1009afea54aSWarner Loshif [ -z "$COPYRIGHT" ]; then
1019afea54aSWarner Losh	COPYRIGHT="/*-
1029afea54aSWarner Losh * Copyright (c) 1992-$year The FreeBSD Project.
1039afea54aSWarner Losh *
1049afea54aSWarner Losh */"
1059afea54aSWarner Loshfi
1069afea54aSWarner Losh
1079afea54aSWarner Losh# add newline
1089afea54aSWarner LoshCOPYRIGHT="$COPYRIGHT
1099afea54aSWarner Losh"
1109afea54aSWarner Losh
11189afd39cSWarner Losh# We expand include_metadata later since we may set it to the
11289afd39cSWarner Losh# future value of modified.
11389afd39cSWarner Loshinclude_metadata=yes
11489afd39cSWarner Loshmodified=no
1159afea54aSWarner Loshwhile getopts crRvV: opt; do
1169afea54aSWarner Losh	case "$opt" in
1179afea54aSWarner Losh	c)
1189afea54aSWarner Losh		echo "$COPYRIGHT"
1199afea54aSWarner Losh		exit 0
1209afea54aSWarner Losh		;;
1219afea54aSWarner Losh	r)
12289afd39cSWarner Losh		include_metadata=no
1239afea54aSWarner Losh		;;
1249afea54aSWarner Losh	R)
12589afd39cSWarner Losh		include_metadata=if-modified
1269afea54aSWarner Losh		;;
1279afea54aSWarner Losh	v)
1289afea54aSWarner Losh		# Only put variables that are single lines here.
1299afea54aSWarner Losh		for v in TYPE REVISION BRANCH RELEASE VERSION RELDATE; do
1309afea54aSWarner Losh			eval val=\$${v}
1319afea54aSWarner Losh			echo ${v}=\"${val}\"
1329afea54aSWarner Losh		done
1339afea54aSWarner Losh		exit 0
1349afea54aSWarner Losh		;;
1359afea54aSWarner Losh	V)
1369afea54aSWarner Losh		v=$OPTARG
1379afea54aSWarner Losh		eval val=\$${v}
1389afea54aSWarner Losh		echo ${v}=\"${val}\"
1396ab35c78SJohn Baldwin		VARS_ONLY_EXIT=1
1409afea54aSWarner Losh		;;
1419afea54aSWarner Losh	esac
1429afea54aSWarner Loshdone
1439afea54aSWarner Loshshift $((OPTIND - 1))
1449afea54aSWarner Losh
1459afea54aSWarner Losh# VARS_ONLY means no files should be generated, this is just being
1469afea54aSWarner Losh# included.
1479afea54aSWarner Losh[ -n "$VARS_ONLY" ] && return 0
1489afea54aSWarner Losh
1496ab35c78SJohn Baldwin# VARS_ONLY_EXIT means no files should be generated, only the value of
1506ab35c78SJohn Baldwin# variables are being output.
1516ab35c78SJohn Baldwin[ -n "$VARS_ONLY_EXIT" ] && exit 0
1526ab35c78SJohn Baldwin
1535e680acdSGleb Smirnoff#
1545e680acdSGleb Smirnoff# findvcs dir
1555e680acdSGleb Smirnoff#	Looks up directory dir at world root and up the filesystem
1565e680acdSGleb Smirnoff#
1575e680acdSGleb Smirnofffindvcs()
1585e680acdSGleb Smirnoff{
1595e680acdSGleb Smirnoff	local savedir
1605e680acdSGleb Smirnoff
1615e680acdSGleb Smirnoff	savedir=$(pwd)
1625e680acdSGleb Smirnoff	cd ${SYSDIR}/..
1635e680acdSGleb Smirnoff	while [ $(pwd) != "/" ]; do
164495947bcSEd Maste		if [ -e "./$1" ]; then
165030efb3cSEd Maste			VCSTOP=$(pwd)
166030efb3cSEd Maste			VCSDIR=${VCSTOP}"/$1"
1675e680acdSGleb Smirnoff			cd ${savedir}
1685e680acdSGleb Smirnoff			return 0
1695e680acdSGleb Smirnoff		fi
1705e680acdSGleb Smirnoff		cd ..
1715e680acdSGleb Smirnoff	done
1725e680acdSGleb Smirnoff	cd ${savedir}
1735e680acdSGleb Smirnoff	return 1
1745e680acdSGleb Smirnoff}
1755e680acdSGleb Smirnoff
17650b53a8dSEd Mastegit_tree_modified()
17750b53a8dSEd Maste{
17817eba5e3SEd Maste	! $git_cmd "--work-tree=${VCSTOP}" -c core.checkStat=minimal -c core.fileMode=off diff --quiet
17950b53a8dSEd Maste}
18050b53a8dSEd Maste
181afcf05e4SRuslan ErmilovLC_ALL=C; export LC_ALL
182df8bae1dSRodney W. Grimesif [ ! -r version ]
183df8bae1dSRodney W. Grimesthen
184df8bae1dSRodney W. Grimes	echo 0 > version
185df8bae1dSRodney W. Grimesfi
186df8bae1dSRodney W. Grimes
187df8bae1dSRodney W. Grimestouch version
18879beb716SWarner Loshv=$(cat version)
18958eaee06SEd Masteu=${USER:-root}
19079beb716SWarner Loshd=$(pwd)
19179beb716SWarner Loshh=${HOSTNAME:-$(hostname)}
192484df459SEd Masteif [ -n "$SOURCE_DATE_EPOCH" ]; then
193*16f33463SColin Percival	if ! t=$(date -ur $SOURCE_DATE_EPOCH 2>/dev/null); then
194484df459SEd Maste		echo "Invalid SOURCE_DATE_EPOCH" >&2
195484df459SEd Maste		exit 1
196484df459SEd Maste	fi
197484df459SEd Masteelse
19879beb716SWarner Losh	t=$(date)
199484df459SEd Mastefi
20079beb716SWarner Loshi=$(${MAKE:-make} -V KERN_IDENT)
20172b2ef06SBaptiste Daroussincompiler_v=$($(${MAKE:-make} -V CC) -v 2>&1 | grep -w 'version')
2021125f273SPoul-Henning Kamp
20392994bd3SGlen Barberfor dir in /usr/bin /usr/local/bin; do
20492994bd3SGlen Barber	if [ ! -z "${svnversion}" ] ; then
20592994bd3SGlen Barber		break
20692994bd3SGlen Barber	fi
207878a25dbSRui Paulo	if [ -x "${dir}/svnversion" ] && [ -z ${svnversion} ] ; then
208a6191444SGlen Barber		# Run svnversion from ${dir} on this script; if return code
209a6191444SGlen Barber		# is not zero, the checkout might not be compatible with the
210a6191444SGlen Barber		# svnversion being used.
2117c0af95aSGlen Barber		${dir}/svnversion $(realpath ${0}) >/dev/null 2>&1
212a6191444SGlen Barber		if [ $? -eq 0 ]; then
213e652e59bSMax Laier			svnversion=${dir}/svnversion
21492994bd3SGlen Barber			break
215878a25dbSRui Paulo		fi
216a6191444SGlen Barber	fi
21792994bd3SGlen Barberdone
218a6191444SGlen Barber
2195e680acdSGleb Smirnoffif findvcs .git; then
22092994bd3SGlen Barber	for dir in /usr/bin /usr/local/bin; do
2210782493bSAndriy Gapon		if [ -x "${dir}/git" ] ; then
2228385e87cSMateusz Piotrowski			git_cmd="${dir}/git -c help.autocorrect=0 --git-dir=${VCSDIR}"
223e0976d1aSDoug Barton			break
224e0976d1aSDoug Barton		fi
225e0976d1aSDoug Barton	done
2260782493bSAndriy Gaponfi
227e0976d1aSDoug Barton
228a9fc14fbSMichael Osipovif findvcs .gituprevision; then
229a9fc14fbSMichael Osipov	gituprevision="${VCSTOP}/.gituprevision"
230a9fc14fbSMichael Osipovfi
231a9fc14fbSMichael Osipov
2325e680acdSGleb Smirnoffif findvcs .hg; then
233a4108819SRui Paulo	for dir in /usr/bin /usr/local/bin; do
234a4108819SRui Paulo		if [ -x "${dir}/hg" ] ; then
2355e680acdSGleb Smirnoff			hg_cmd="${dir}/hg -R ${VCSDIR}"
236a4108819SRui Paulo			break
237a4108819SRui Paulo		fi
238a4108819SRui Paulo	done
239a4108819SRui Paulofi
240a4108819SRui Paulo
241e652e59bSMax Laierif [ -n "$svnversion" ] ; then
24279beb716SWarner Losh	svn=$(cd ${SYSDIR} && $svnversion 2>/dev/null)
243cec62135SDoug Barton	case "$svn" in
2447f582d62SEd Maste	[0-9]*[MSP]|*:*)
2457f582d62SEd Maste		svn=" r${svn}"
24689afd39cSWarner Losh		modified=yes
2477f582d62SEd Maste		;;
2487f582d62SEd Maste	[0-9]*)
2497f582d62SEd Maste		svn=" r${svn}"
2507f582d62SEd Maste		;;
2517f582d62SEd Maste	*)
2527f582d62SEd Maste		unset svn
2537f582d62SEd Maste		;;
254cec62135SDoug Barton	esac
255bee7f5e6SBjoern A. Zeebfi
256b26f77e5SWarner Losh
257e652e59bSMax Laierif [ -n "$git_cmd" ] ; then
258a1097094SPat Maddox	git=$($git_cmd rev-parse --verify --short=12 HEAD 2>/dev/null)
2599e98065cSEd Maste	if [ "$($git_cmd rev-parse --is-shallow-repository)" = false ] ; then
2608a51f14aSWarner Losh		git_cnt=$($git_cmd rev-list --first-parent --count HEAD 2>/dev/null)
261ecb2bbc0SEd Maste		if [ -n "$git_cnt" ] ; then
2628a51f14aSWarner Losh			git="n${git_cnt}-${git}"
263ecb2bbc0SEd Maste		fi
264d7134682SRui Paulo	fi
265ace7209cSMateusz Guzik	git_b=$($git_cmd rev-parse --abbrev-ref HEAD)
266ace7209cSMateusz Guzik	if [ -n "$git_b" -a "$git_b" != "HEAD" ] ; then
267ace7209cSMateusz Guzik		git="${git_b}-${git}"
268ace7209cSMateusz Guzik	fi
26950b53a8dSEd Maste	if git_tree_modified; then
270e652e59bSMax Laier		git="${git}-dirty"
27189afd39cSWarner Losh		modified=yes
272e652e59bSMax Laier	fi
2738d405efdSUlrich Spörlein	git=" ${git}"
274e652e59bSMax Laierfi
2751125f273SPoul-Henning Kamp
276a9fc14fbSMichael Osipovif [ -n "$gituprevision" ] ; then
277a9fc14fbSMichael Osipov	gitup=" $(awk -F: '{print $2}' $gituprevision)"
278a9fc14fbSMichael Osipovfi
279a9fc14fbSMichael Osipov
280a4108819SRui Pauloif [ -n "$hg_cmd" ] ; then
28179beb716SWarner Losh	hg=$($hg_cmd id 2>/dev/null)
28279beb716SWarner Losh	hgsvn=$($hg_cmd svn info 2>/dev/null | \
28379beb716SWarner Losh		awk -F': ' '/Revision/ { print $2 }')
2842cb541bfSEd Maste	if [ -n "$hgsvn" ] ; then
2852cb541bfSEd Maste		svn=" r${hgsvn}"
286a4108819SRui Paulo	fi
287a4108819SRui Paulo	if [ -n "$hg" ] ; then
288a4108819SRui Paulo		hg=" ${hg}"
289a4108819SRui Paulo	fi
290a4108819SRui Paulofi
291878a25dbSRui Paulo
29289afd39cSWarner Losh[ ${include_metadata} = "if-modified" -a ${modified} = "yes" ] && include_metadata=yes
29389afd39cSWarner Loshif [ ${include_metadata} != "yes" ]; then
294a9fc14fbSMichael Osipov	VERINFO="${VERSION}${svn}${git}${gitup}${hg} ${i}"
2957e9de36cSEd Maste	VERSTR="${VERINFO}\\n"
2967e9de36cSEd Masteelse
297a9fc14fbSMichael Osipov	VERINFO="${VERSION} #${v}${svn}${git}${gitup}${hg}: ${t}"
2987e9de36cSEd Maste	VERSTR="${VERINFO}\\n    ${u}@${h}:${d}\\n"
2997e9de36cSEd Mastefi
3007e9de36cSEd Maste
301179460e1SEd Mastevers_content_new=$(cat << EOF
302b3a415b1SJordan K. Hubbard$COPYRIGHT
303fc4a6768SWarner Losh/*
304fc4a6768SWarner Losh * The SCCS stuff is a marker that by convention identifies the kernel.  While
305fc4a6768SWarner Losh * the convention originated with SCCS, the current use is more generic and is
306fc4a6768SWarner Losh * used by different organizations to identify the kernel, the crash dump,
307fc4a6768SWarner Losh * etc. The what(1) utility prints these markers. Better methods exist, so this
308fc4a6768SWarner Losh * method is deprecated and will be removed in a future version of FreeBSD. Orgs
309fc4a6768SWarner Losh * that use it are encouraged to migrate before then.
310fc4a6768SWarner Losh */
311cb668587SWarner Losh#define SCCSSTR "@(#)${VERINFO}"
3127e9de36cSEd Maste#define VERSTR "${VERSTR}"
3134f9dc742SColin Percival#define RELSTR "${RELEASE}"
3144f9dc742SColin Percival
315356be134SZhenlei Huangconst char sccs[sizeof(SCCSSTR) > 128 ? sizeof(SCCSSTR) : 128] = SCCSSTR;
316356be134SZhenlei Huangconst char version[sizeof(VERSTR) > 256 ? sizeof(VERSTR) : 256] = VERSTR;
317356be134SZhenlei Huangconst char compiler_version[] = "${compiler_v}";
318356be134SZhenlei Huangconst char ostype[] = "${TYPE}";
319356be134SZhenlei Huangconst char osrelease[sizeof(RELSTR) > 32 ? sizeof(RELSTR) : 32] = RELSTR;
320356be134SZhenlei Huangconst int osreldate = ${RELDATE};
321356be134SZhenlei Huangconst char kern_ident[] = "${i}";
322b3a415b1SJordan K. HubbardEOF
323179460e1SEd Maste)
324179460e1SEd Mastevers_content_old=$(cat vers.c 2>/dev/null || true)
325179460e1SEd Masteif [ "$vers_content_new" != "$vers_content_old" ]; then
32685646602SEd Maste	printf "%s\n" "$vers_content_new" > vers.c
327179460e1SEd Mastefi
328df8bae1dSRodney W. Grimes
329e28b1e2dSRuslan Ermilovecho $((v + 1)) > version
330