summaryrefslogtreecommitdiff
path: root/sys/conf/newvers.sh
blob: c226aa2e3616d6e0b3b22b64113063f2b9b3c7fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/sh -
#
#	$NetBSD: newvers.sh,v 1.62 2017/04/08 18:24:09 christos Exp $
#
# Copyright (c) 1984, 1986, 1990, 1993
#	The Regents of the University of California.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the University nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#	@(#)newvers.sh	8.1 (Berkeley) 4/20/94

# newvers.sh -- Create a "vers.c" file containing version information.
#
# The "vers.c" file in the current directory is the primary output.  It
# contains C source code with several variables containing information
# about the build.  This file is expected to be incorporated into a
# kernel, and when that kernel is booted then the information can be
# queried by the uname(8) command.
#
# Command line options:
#
#     -R                Reproducible build: Do not embed directory
#                       names, user names, time stamps, or other dynamic
#                       information into the output file.  This intended
#                       to allow two builds done at different times and
#                       even by different people on different hosts to
#                       produce identical output.
#
#     -r <timestamp>    Reproducible build: Embed fixed information to
#			the output file.
#			This intended to allow two builds done at different
#			times and even by different people on different
#			hosts to produce identical output.
#
#     -m <machine>      Use the specified string as the value of the
#                       machine variable
#
#     -i <id>           Use the specified string as the value of the
#                       kernel_ident variable
#
#     -n                Do not include an ELF note section in the output
#                       file.
# Environment variables:
#
#     BUILDID		If defined, ${BUILDID} is appended to the
#			default value of the kernel_ident string.
#			(If the -i command line option is used, then
#			BUILDID is not appended.)
#
#     BUILDINFO		A string to be stored in the kernel's buildinfo
#			variable.  ${BUILDINFO} may be a multi-line string,
#			and may use C-style backslash escapes.
#			Lines may be separated by either literal newlines
#			or "\n" escape sequences.
#
# Output files:
#
#     vers.c            The "vers.c" file in the current directory is
#                       the primary output.
#
#     version           The "version" file in the current directory
#                       is both an input and an output.  See the
#                       description under "Input files".
#
# Input files:
#
#     version           The "version" file in the current directory
#                       contains an integer counter, representing the
#                       number of times this script has been executed in
#                       this directory, starting with "0" if the file
#                       does not exist.  The serial number in the file
#                       is incremented after the file is read. so that
#                       the incremented serial number is an output from
#                       the present build and an input to the next build
#                       that is performed in the same directory.
#
#     copyright         The "copyright" file (in the same directory as
#                       this script itself) contains a copyright notice,
#                       which is embedded in the copyright variable in
#                       the output file.
#
#     ident             The "ident" file in the current directory is optional.
#			If this file exists, then its contents override the
#			default value of the kernel_ident string.
#
# Input from external commands:
#
#     osrelease.sh      This script is expected to print the OS revision.
#                       The result is stored in the osrelease variable.
#

# FUNCTIONS

# source_lines [input] --
#
# Convert a multi-line string to a format that's suitable for inclusion in
# C source code.  The result should look like this:
#
# "first line\n"
# "second line\n"
#
# with <backslash><letter n> inside the quotes for each line,
# literal quotation marks around each line,
# and a literal newline separating one line from the next.
#
# Input is from "$1" if that is defined, or from stdin if $1 is not defined.
#
source_lines()
{
	if [ -n "${1+set}" ]; then
		printf "%s" "$1"
	else
		cat
	fi \
	| awk '{
		# awk does not care about whether or not the last line
		# of input ends with a newline.
		# Convert <backslash> to <backslash><backslash>.
		gsub("\\\\","\\\\");
		# Convert <quote> to <backslash><quote>
		gsub("\"","\\\"");
		# Add <backslash><letter n> to the end of each line,
		# and wrap each line in double quotes.
		printf("\"%s\\n\"\n", $0);
	}'
}

# MAIN PROGRAM

if [ ! -e version ]; then
	echo 0 > version
fi

Rflag=false
nflag=false
timestamp=
pwd=$(pwd)

while getopts "Rr:m:i:n" OPT; do
	case $OPT in
	R)
		# -R: Reproducible build
		Rflag=true
		;;
	r)
		# -r <timestamp>: timestamp
		timestamp="$OPTARG"
		;;
	m)
		# -m <machine>: machine
		machine="$OPTARG"
		;;
	i)
		# -i <id>: Use the secified string as the
		# value of the kernel_ident variable
		id="$OPTARG"
		;;
	n)
		# -n: Do not include a ELF note section in the output file.
		nflag=true
		;;
	*)	echo "Usage: newvers.sh [-Rn] [-r <timestamp>] [-m <machine>] [-i <kernel>]" >&2
		exit 1;;
	esac
done

if [ -z "${id}" ]; then
	if [ -f ident ]; then
		id="$(cat ident)"
	else
		id=$(basename "${pwd}")
	fi
	# Append ".${BUILDID}" to the default value of <id>.
	# If the "-i <id>" command line option was used then this
	# branch is not taken, so the command-line value of <id>
	# is used without change.
	if [ -n "${BUILDID}" ]; then
		id="${id}.${BUILDID}"
	fi
fi

if ${Rflag}; then
	reproversion=
else
	if [ -z "${timestamp}" ]; then
		v=$(cat version)
		t=$(LC_ALL=C date)
		u=${USER-root}
		h=$(hostname)
		d=$(pwd)
		# Increment the serial number in the version file
		echo $(expr ${v} + 1) > version
	else
		v=0
		t=$(LC_ALL=C TZ=UTC date -r "${timestamp}")
		u=mkrepro
		h=mkrepro.NetBSD.org
		d="/usr/src/sys/arch/${machine}/compile/${id}"
	fi
	reproversion=" #${v}: ${t}\n\t${u}@${h}:${d}"
fi

cwd=$(dirname "$0")
copyright="$(cat "${cwd}/copyright")"
osrelcmd=${cwd}/osrelease.sh

ost="NetBSD"
osr=$(sh $osrelcmd)

fullversion="${ost} ${osr} (${id})${reproversion}\n"

# Convert multi-line strings to C source code.
# Also add an extra blank line to copyright.
#
copyright_source="$(printf "%s\n\n" "${copyright}" | source_lines)"
fullversion_source="$(printf "%b" "${fullversion}" | source_lines)"
buildinfo_source="$(printf "%b" "${BUILDINFO}" | source_lines)"

# work around escaping issues with different shells
emptyq='""'

cat << _EOF > vers.c
/*
 * Automatically generated file from $0
 * Do not edit.
 */
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/exec.h>
#include <sys/exec_elf.h>

const char ostype[] = "${ost}";
const char osrelease[] = "${osr}";
const char sccs[] = "@(#)" ${fullversion_source};
const char version[] = ${fullversion_source};
const char buildinfo[] = ${buildinfo_source:-${emptyq}};
const char kernel_ident[] = "${id}";
const char copyright[] = ${copyright_source};
_EOF

${nflag} && exit 0

cat << _EOF >> vers.c

/*
 * NetBSD identity note.
 */
#ifdef __arm__
#define _SHT_NOTE	%note
#else
#define _SHT_NOTE	@note
#endif

#define	_S(TAG)	__STRING(TAG)
__asm(
	".section\t\".note.netbsd.ident\", \"\"," _S(_SHT_NOTE) "\n"
	"\t.p2align\t2\n"
	"\t.long\t" _S(ELF_NOTE_NETBSD_NAMESZ) "\n"
	"\t.long\t" _S(ELF_NOTE_NETBSD_DESCSZ) "\n"
	"\t.long\t" _S(ELF_NOTE_TYPE_NETBSD_TAG) "\n"
	"\t.ascii\t" _S(ELF_NOTE_NETBSD_NAME) "\n"
	"\t.long\t" _S(__NetBSD_Version__) "\n"
	"\t.p2align\t2\n"
);

_EOF