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