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
34 # Command line options:
36 # -c Print the copyright / license statement as a C comment and exit
38 # -r Reproducible build. Do not embed directory names, user names,
39 # time stamps or other dynamic information into the output file.
40 # This is intended to allow two builds done at different times
41 # and even by different people on different hosts to produce
44 # -R Reproducible build if the tree represents an unmodified
45 # checkout from a version control system. Metadata is included
46 # if the tree is modified.
48 # -V var Print ${var}="${val-of-var}" and exit
50 # -v Print TYPE REVISION BRANCH RELEASE VERSION RELDATE variables
57 if [ -n "${BRANCH_OVERRIDE}" ]; then
58 BRANCH=${BRANCH_OVERRIDE}
63 if [ -z "${SYSDIR}" ]; then
64 SYSDIR=$(dirname $0)/..
67 # allow random overrides
71 *=*) eval "$1"; shift;;
76 RELEASE="${RELEASE:-${REVISION}-${BRANCH}}"
77 VERSION="${VERSION:-${TYPE} ${RELEASE}}"
79 RELDATE=$(awk '/^#define[[:space:]]*__FreeBSD_version/ {print $3}' ${PARAMFILE:-${SYSDIR}/sys/param.h})
81 if [ -r "${SYSDIR}/../COPYRIGHT" ]; then
82 year=$(sed -Ee '/^Copyright .* The FreeBSD Project/!d;s/^.*1992-([0-9]*) .*$/\1/g' ${SYSDIR}/../COPYRIGHT)
86 # look for copyright template
87 b=share/examples/etc/bsd-style-copyright
88 for bsd_copyright in $b ../$b ../../$b ../../../$b /usr/src/$b /usr/$b
90 if [ -r "$bsd_copyright" ]; then
92 -e "s/\[year\]/1992-$year/" \
93 -e 's/\[your name here\]\.* /The FreeBSD Project./' \
94 -e 's/\[your name\]\.*/The FreeBSD Project./' \
95 -e '/\[id for your version control system, if any\]/d' \
101 # no copyright found, use a dummy
102 if [ -z "$COPYRIGHT" ]; then
104 * Copyright (c) 1992-$year The FreeBSD Project.
110 COPYRIGHT="$COPYRIGHT
113 # We expand include_metadata later since we may set it to the
114 # future value of modified.
117 while getopts crRvV: opt; do
127 include_metadata=if-modified
130 # Only put variables that are single lines here.
131 for v in TYPE REVISION BRANCH RELEASE VERSION RELDATE; do
145 shift $((OPTIND - 1))
147 # VARS_ONLY means no files should be generated, this is just being
149 [ -n "$VARS_ONLY" ] && return 0
151 # VARS_ONLY_EXIT means no files should be generated, only the value of
152 # variables are being output.
153 [ -n "$VARS_ONLY_EXIT" ] && exit 0
157 # Looks up directory dir at world root and up the filesystem
165 while [ $(pwd) != "/" ]; do
166 if [ -e "./$1" ]; then
168 VCSDIR=${VCSTOP}"/$1"
180 ! $git_cmd "--work-tree=${VCSTOP}" -c core.checkStat=minimal -c core.fileMode=off diff --quiet
183 LC_ALL=C; export LC_ALL
193 h=${HOSTNAME:-$(hostname)}
194 if [ -n "$SOURCE_DATE_EPOCH" ]; then
195 if ! t=$(date -r $SOURCE_DATE_EPOCH 2>/dev/null); then
196 echo "Invalid SOURCE_DATE_EPOCH" >&2
202 i=$(${MAKE:-make} -V KERN_IDENT)
203 compiler_v=$($(${MAKE:-make} -V CC) -v 2>&1 | grep -w 'version')
205 for dir in /usr/bin /usr/local/bin; do
206 if [ ! -z "${svnversion}" ] ; then
209 if [ -x "${dir}/svnversion" ] && [ -z ${svnversion} ] ; then
210 # Run svnversion from ${dir} on this script; if return code
211 # is not zero, the checkout might not be compatible with the
212 # svnversion being used.
213 ${dir}/svnversion $(realpath ${0}) >/dev/null 2>&1
214 if [ $? -eq 0 ]; then
215 svnversion=${dir}/svnversion
221 if [ -z "${svnversion}" ] && [ -x /usr/bin/svnliteversion ] ; then
222 /usr/bin/svnliteversion $(realpath ${0}) >/dev/null 2>&1
223 if [ $? -eq 0 ]; then
224 svnversion=/usr/bin/svnliteversion
230 if findvcs .git; then
231 for dir in /usr/bin /usr/local/bin; do
232 if [ -x "${dir}/git" ] ; then
233 git_cmd="${dir}/git -c help.autocorrect=0 --git-dir=${VCSDIR}"
239 if findvcs .gituprevision; then
240 gituprevision="${VCSTOP}/.gituprevision"
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 if [ "$($git_cmd rev-parse --is-shallow-repository)" = false ] ; then
271 git_cnt=$($git_cmd rev-list --first-parent --count HEAD 2>/dev/null)
272 if [ -n "$git_cnt" ] ; then
273 git="n${git_cnt}-${git}"
276 git_b=$($git_cmd rev-parse --abbrev-ref HEAD)
277 if [ -n "$git_b" -a "$git_b" != "HEAD" ] ; then
278 git="${git_b}-${git}"
280 if git_tree_modified; then
287 if [ -n "$gituprevision" ] ; then
288 gitup=" $(awk -F: '{print $2}' $gituprevision)"
291 if [ -n "$hg_cmd" ] ; then
292 hg=$($hg_cmd id 2>/dev/null)
293 hgsvn=$($hg_cmd svn info 2>/dev/null | \
294 awk -F': ' '/Revision/ { print $2 }')
295 if [ -n "$hgsvn" ] ; then
298 if [ -n "$hg" ] ; then
303 [ ${include_metadata} = "if-modified" -a ${modified} = "yes" ] && include_metadata=yes
304 if [ ${include_metadata} != "yes" ]; then
305 VERINFO="${VERSION}${svn}${git}${gitup}${hg} ${i}"
306 VERSTR="${VERINFO}\\n"
308 VERINFO="${VERSION} #${v}${svn}${git}${gitup}${hg}: ${t}"
309 VERSTR="${VERINFO}\\n ${u}@${h}:${d}\\n"
312 vers_content_new=$(cat << EOF
314 #define VERSTR "${VERSTR}"
315 #define RELSTR "${RELEASE}"
317 char version[sizeof(VERSTR) > 256 ? sizeof(VERSTR) : 256] = VERSTR;
318 char compiler_version[] = "${compiler_v}";
319 char ostype[] = "${TYPE}";
320 char osrelease[sizeof(RELSTR) > 32 ? sizeof(RELSTR) : 32] = RELSTR;
321 int osreldate = ${RELDATE};
322 char kern_ident[] = "${i}";
325 vers_content_old=$(cat vers.c 2>/dev/null || true)
326 if [ "$vers_content_new" != "$vers_content_old" ]; then
327 printf "%s\n" "$vers_content_new" > vers.c
330 echo $((v + 1)) > version