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 # -c Print the copyright / license statement as a C comment and exit
39 # -r Reproducible build. Do not embed directory names, user names,
40 # time stamps or other dynamic information into the output file.
41 # This is intended to allow two builds done at different times
42 # and even by different people on different hosts to produce
45 # -R Reproducible build if the tree represents an unmodified
46 # checkout from a version control system. Metadata is included
47 # if the tree is modified.
49 # -V var Print ${var}="${val-of-var}" and exit
51 # -v Print TYPE REVISION BRANCH RELEASE VERSION RELDATE variables
57 BRANCH=${BRANCH_OVERRIDE:-CURRENT}
58 RELEASE="${REVISION}-${BRANCH}"
59 VERSION="${TYPE} ${RELEASE}"
61 if [ -z "${SYSDIR}" ]; then
62 SYSDIR=$(dirname $0)/..
65 RELDATE=$(awk '/__FreeBSD_version.*propagated to newvers/ {print $3}' ${PARAMFILE:-${SYSDIR}/sys/param.h})
67 if [ -r "${SYSDIR}/../COPYRIGHT" ]; then
68 year=$(sed -Ee '/^Copyright .* The FreeBSD Project/!d;s/^.*1992-([0-9]*) .*$/\1/g' ${SYSDIR}/../COPYRIGHT)
72 # look for copyright template
73 b=share/examples/etc/bsd-style-copyright
74 for bsd_copyright in $b ../$b ../../$b ../../../$b /usr/src/$b /usr/$b
76 if [ -r "$bsd_copyright" ]; then
78 -e "s/\[year\]/1992-$year/" \
79 -e 's/\[your name here\]\.* /The FreeBSD Project./' \
80 -e 's/\[your name\]\.*/The FreeBSD Project./' \
81 -e '/\[id for your version control system, if any\]/d' \
87 # no copyright found, use a dummy
88 if [ -z "$COPYRIGHT" ]; then
90 * Copyright (c) 1992-$year The FreeBSD Project.
99 # We expand include_metadata later since we may set it to the
100 # future value of modified.
103 while getopts crRvV: opt; do
113 include_metadata=if-modified
116 # Only put variables that are single lines here.
117 for v in TYPE REVISION BRANCH RELEASE VERSION RELDATE; do
131 shift $((OPTIND - 1))
133 # VARS_ONLY means no files should be generated, this is just being
135 [ -n "$VARS_ONLY" ] && return 0
139 # Looks up directory dir at world root and up the filesystem
147 while [ $(pwd) != "/" ]; do
148 if [ -e "./$1" ]; then
150 VCSDIR=${VCSTOP}"/$1"
162 # git diff-index lists both files that are known to have changes as
163 # well as those with metadata that does not match what is recorded in
164 # git's internal state. The latter case is indicated by an all-zero
165 # destination file hash.
170 mkfifo -m 600 $fifo || exit 1
171 $git_cmd --work-tree=${VCSTOP} diff-index HEAD > $fifo &
172 while read smode dmode ssha dsha status file; do
173 if ! expr $dsha : '^00*$' >/dev/null; then
177 if ! $git_cmd --work-tree=${VCSTOP} diff --quiet -- "${file}"; then
182 # No files with content differences.
187 LC_ALL=C; export LC_ALL
197 h=${HOSTNAME:-$(hostname)}
198 if [ -n "$SOURCE_DATE_EPOCH" ]; then
199 if ! t=$(date -r $SOURCE_DATE_EPOCH 2>/dev/null); then
200 echo "Invalid SOURCE_DATE_EPOCH" >&2
206 i=$(${MAKE:-make} -V KERN_IDENT)
207 compiler_v=$($(${MAKE:-make} -V CC) -v 2>&1 | grep -w 'version')
209 for dir in /usr/bin /usr/local/bin; do
210 if [ ! -z "${svnversion}" ] ; then
213 if [ -x "${dir}/svnversion" ] && [ -z ${svnversion} ] ; then
214 # Run svnversion from ${dir} on this script; if return code
215 # is not zero, the checkout might not be compatible with the
216 # svnversion being used.
217 ${dir}/svnversion $(realpath ${0}) >/dev/null 2>&1
218 if [ $? -eq 0 ]; then
219 svnversion=${dir}/svnversion
225 if [ -z "${svnversion}" ] && [ -x /usr/bin/svnliteversion ] ; then
226 /usr/bin/svnliteversion $(realpath ${0}) >/dev/null 2>&1
227 if [ $? -eq 0 ]; then
228 svnversion=/usr/bin/svnliteversion
234 if findvcs .git; then
235 for dir in /usr/bin /usr/local/bin; do
236 if [ -x "${dir}/git" ] ; then
237 git_cmd="${dir}/git -c help.autocorrect=0 --git-dir=${VCSDIR}"
244 for dir in /usr/bin /usr/local/bin; do
245 if [ -x "${dir}/hg" ] ; then
246 hg_cmd="${dir}/hg -R ${VCSDIR}"
252 if [ -n "$svnversion" ] ; then
253 svn=$(cd ${SYSDIR} && $svnversion 2>/dev/null)
268 if [ -n "$git_cmd" ] ; then
269 git=$($git_cmd rev-parse --verify --short HEAD 2>/dev/null)
270 gitsvn=$($git_cmd svn find-rev $git 2>/dev/null)
271 if [ -n "$gitsvn" ] ; then
275 # Log searches are limited to 10k commits to speed up failures.
276 # We assume that if a tree is more than 10k commits out-of-sync
277 # with FreeBSD, it has forked the the OS and the SVN rev no
279 gitsvn=$($git_cmd log -n 10000 |
280 grep '^ git-svn-id:' | head -1 | \
281 sed -n 's/^.*@\([0-9][0-9]*\).*$/\1/p')
282 if [ -z "$gitsvn" ] ; then
283 gitsvn=$($git_cmd log -n 10000 --format='format:%N' | \
284 grep '^svn ' | head -1 | \
285 sed -n 's/^.*revision=\([0-9][0-9]*\).*$/\1/p')
287 if [ -n "$gitsvn" ] ; then
294 git_cnt=$($git_cmd rev-list --count HEAD 2>/dev/null)
295 if [ -n "$git_cnt" ] ; then
296 git="${git}-c${git_cnt}"
298 git_b=$($git_cmd rev-parse --abbrev-ref HEAD)
299 if [ -n "$git_b" ] ; then
300 git="${git}(${git_b})"
302 if git_tree_modified; then
308 if [ -n "$hg_cmd" ] ; then
309 hg=$($hg_cmd id 2>/dev/null)
310 hgsvn=$($hg_cmd svn info 2>/dev/null | \
311 awk -F': ' '/Revision/ { print $2 }')
312 if [ -n "$hgsvn" ] ; then
315 if [ -n "$hg" ] ; then
320 [ ${include_metadata} = "if-modified" -a ${modified} = "yes" ] && include_metadata=yes
321 if [ ${include_metadata} != "yes" ]; then
322 VERINFO="${VERSION}${svn}${git}${hg} ${i}"
323 VERSTR="${VERINFO}\\n"
325 VERINFO="${VERSION} #${v}${svn}${git}${hg}: ${t}"
326 VERSTR="${VERINFO}\\n ${u}@${h}:${d}\\n"
329 vers_content_new=$(cat << EOF
331 #define SCCSSTR "@(#)${VERINFO}"
332 #define VERSTR "${VERSTR}"
333 #define RELSTR "${RELEASE}"
335 char sccs[sizeof(SCCSSTR) > 128 ? sizeof(SCCSSTR) : 128] = SCCSSTR;
336 char version[sizeof(VERSTR) > 256 ? sizeof(VERSTR) : 256] = VERSTR;
337 char compiler_version[] = "${compiler_v}";
338 char ostype[] = "${TYPE}";
339 char osrelease[sizeof(RELSTR) > 32 ? sizeof(RELSTR) : 32] = RELSTR;
340 int osreldate = ${RELDATE};
341 char kern_ident[] = "${i}";
344 vers_content_old=$(cat vers.c 2>/dev/null || true)
345 if [ "$vers_content_new" != "$vers_content_old" ]; then
346 echo "$vers_content_new" > vers.c
349 echo $((v + 1)) > version