3 # SPDX-License-Identifier: BSD-3-Clause
5 # Copyright (c) 1984, 1986, 1990, 1993
6 # The Regents of the University of California. All rights reserved.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
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.
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
32 # @(#)newvers.sh 8.1 (Berkeley) 4/20/94
35 # Command line options:
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.
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.
47 # Note: usr.sbin/amd/include/newvers.sh assumes all variable assignments of
48 # upper case variables starting in column 1 are on one line w/o continuation.
53 if [ -n "${BRANCH_OVERRIDE}" ]; then
54 BRANCH=${BRANCH_OVERRIDE}
56 RELEASE="${REVISION}-${BRANCH}"
57 VERSION="${TYPE} ${RELEASE}"
61 # Looks up directory dir at world root and up the filesystem
69 while [ $(pwd) != "/" ]; do
70 if [ -e "./$1" ]; then
82 if [ -z "${SYSDIR}" ]; then
83 SYSDIR=$(dirname $0)/..
86 RELDATE=$(awk '/__FreeBSD_version.*propagated to newvers/ {print $3}' ${PARAMFILE:-${SYSDIR}/sys/param.h})
88 if [ -r "${SYSDIR}/../COPYRIGHT" ]; then
89 year=$(sed -Ee '/^Copyright .* The FreeBSD Project/!d;s/^.*1992-([0-9]*) .*$/\1/g' ${SYSDIR}/../COPYRIGHT)
93 # look for copyright template
94 b=share/examples/etc/bsd-style-copyright
95 for bsd_copyright in ../$b ../../$b ../../../$b /usr/src/$b /usr/$b
97 if [ -r "$bsd_copyright" ]; then
99 -e "s/\[year\]/1992-$year/" \
100 -e 's/\[your name here\]\.* /The FreeBSD Project./' \
101 -e 's/\[your name\]\.*/The FreeBSD Project./' \
102 -e '/\[id for your version control system, if any\]/d' \
108 # no copyright found, use a dummy
109 if [ -z "$COPYRIGHT" ]; then
111 * Copyright (c) 1992-$year The FreeBSD Project.
112 * All rights reserved.
118 COPYRIGHT="$COPYRIGHT
121 # VARS_ONLY means no files should be generated, this is just being
123 [ -n "$VARS_ONLY" ] && return 0
125 LC_ALL=C; export LC_ALL
135 h=${HOSTNAME:-`hostname`}
136 if [ -n "$SOURCE_DATE_EPOCH" ]; then
137 if ! t=`date -r $SOURCE_DATE_EPOCH 2>/dev/null`; then
138 echo "Invalid SOURCE_DATE_EPOCH" >&2
144 i=`${MAKE:-make} -V KERN_IDENT`
145 compiler_v=$($(${MAKE:-make} -V CC) -v 2>&1 | grep -w 'version')
147 for dir in /usr/bin /usr/local/bin; do
148 if [ ! -z "${svnversion}" ] ; then
151 if [ -x "${dir}/svnversion" ] && [ -z ${svnversion} ] ; then
152 # Run svnversion from ${dir} on this script; if return code
153 # is not zero, the checkout might not be compatible with the
154 # svnversion being used.
155 ${dir}/svnversion $(realpath ${0}) >/dev/null 2>&1
156 if [ $? -eq 0 ]; then
157 svnversion=${dir}/svnversion
163 if [ -z "${svnversion}" ] && [ -x /usr/bin/svnliteversion ] ; then
164 /usr/bin/svnliteversion $(realpath ${0}) >/dev/null 2>&1
165 if [ $? -eq 0 ]; then
166 svnversion=/usr/bin/svnliteversion
172 for dir in /usr/bin /usr/local/bin; do
173 if [ -x "${dir}/p4" ] && [ -z ${p4_cmd} ] ; then
178 if findvcs .git; then
179 for dir in /usr/bin /usr/local/bin; do
180 if [ -x "${dir}/git" ] ; then
181 git_cmd="${dir}/git -c help.autocorrect=0 --git-dir=${VCSDIR}"
188 for dir in /usr/bin /usr/local/bin; do
189 if [ -x "${dir}/hg" ] ; then
190 hg_cmd="${dir}/hg -R ${VCSDIR}"
196 if [ -n "$svnversion" ] ; then
197 svn=`cd ${SYSDIR} && $svnversion 2>/dev/null`
212 if [ -n "$git_cmd" ] ; then
213 git=`$git_cmd rev-parse --verify --short HEAD 2>/dev/null`
214 svn=`$git_cmd svn find-rev $git 2>/dev/null`
215 if [ -n "$svn" ] ; then
219 svn=`$git_cmd log --grep '^git-svn-id:' | \
220 grep '^ git-svn-id:' | head -1 | \
221 sed -n 's/^.*@\([0-9][0-9]*\).*$/\1/p'`
222 if [ -z "$svn" ] ; then
223 svn=`$git_cmd log --format='format:%N' | \
224 grep '^svn ' | head -1 | \
225 sed -n 's/^.*revision=\([0-9][0-9]*\).*$/\1/p'`
227 if [ -n "$svn" ] ; then
234 git_b=`$git_cmd rev-parse --abbrev-ref HEAD`
235 if [ -n "$git_b" ] ; then
236 git="${git}(${git_b})"
238 if $git_cmd --work-tree=${VCSTOP} diff-index \
239 --name-only HEAD | read dummy; then
245 if [ -n "$p4_cmd" ] ; then
246 p4version=`cd ${SYSDIR} && $p4_cmd changes -m1 "./...#have" 2>&1 | \
250 p4version=" ${p4version}"
251 p4opened=`cd ${SYSDIR} && $p4_cmd opened ./... 2>&1`
255 p4version="${p4version}+edit"
260 *) unset p4version ;;
264 if [ -n "$hg_cmd" ] ; then
265 hg=`$hg_cmd id 2>/dev/null`
266 svn=`$hg_cmd svn info 2>/dev/null | \
267 awk -F': ' '/Revision/ { print $2 }'`
268 if [ -n "$svn" ] ; then
271 if [ -n "$hg" ] ; then
276 include_metadata=true
277 while getopts rR opt; do
283 if [ -z "${modified}" ]; then
288 shift $((OPTIND - 1))
290 if [ -z "${include_metadata}" ]; then
291 VERINFO="${VERSION}${svn}${git}${hg}${p4version} ${i}"
292 VERSTR="${VERINFO}\\n"
294 VERINFO="${VERSION} #${v}${svn}${git}${hg}${p4version}: ${t}"
295 VERSTR="${VERINFO}\\n ${u}@${h}:${d}\\n"
300 #define SCCSSTR "@(#)${VERINFO}"
301 #define VERSTR "${VERSTR}"
302 #define RELSTR "${RELEASE}"
304 char sccs[sizeof(SCCSSTR) > 128 ? sizeof(SCCSSTR) : 128] = SCCSSTR;
305 char version[sizeof(VERSTR) > 256 ? sizeof(VERSTR) : 256] = VERSTR;
306 char compiler_version[] = "${compiler_v}";
307 char ostype[] = "${TYPE}";
308 char osrelease[sizeof(RELSTR) > 32 ? sizeof(RELSTR) : 32] = RELSTR;
309 int osreldate = ${RELDATE};
310 char kern_ident[] = "${i}";
313 echo $((v + 1)) > version