Home | History | Annotate | Line # | Download | only in conf
      1 #!/bin/sh -
      2 #
      3 #	$NetBSD: newvers.sh,v 1.64 2024/05/01 14:52:01 christos Exp $
      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 
     34 # newvers.sh -- Create a "vers.c" file containing version information.
     35 #
     36 # The "vers.c" file in the current directory is the primary output.  It
     37 # contains C source code with several variables containing information
     38 # about the build.  This file is expected to be incorporated into a
     39 # kernel, and when that kernel is booted then the information can be
     40 # queried by the uname(8) command.
     41 #
     42 # Command line options:
     43 #
     44 #     -R                Reproducible build: Do not embed directory
     45 #                       names, user names, time stamps, or other dynamic
     46 #                       information into the output file.  This intended
     47 #                       to allow two builds done at different times and
     48 #                       even by different people on different hosts to
     49 #                       produce identical output.
     50 #
     51 #     -r <timestamp>    Reproducible build: Embed fixed information to
     52 #			the output file.
     53 #			This intended to allow two builds done at different
     54 #			times and even by different people on different
     55 #			hosts to produce identical output.
     56 #
     57 #     -m <machine>      Use the specified string as the value of the
     58 #                       machine variable
     59 #
     60 #     -i <id>           Use the specified string as the value of the
     61 #                       kernel_ident variable
     62 #
     63 #     -n                Do not include an ELF note section in the output
     64 #                       file.
     65 # Environment variables:
     66 #
     67 #     BUILDID		If defined, ${BUILDID} is appended to the
     68 #			default value of the kernel_ident string.
     69 #			(If the -i command line option is used, then
     70 #			BUILDID is not appended.)
     71 #
     72 #     BUILDINFO		A string to be stored in the kernel's buildinfo
     73 #			variable.  ${BUILDINFO} may be a multi-line string,
     74 #			and may use C-style backslash escapes.
     75 #			Lines may be separated by either literal newlines
     76 #			or "\n" escape sequences.
     77 #
     78 # Output files:
     79 #
     80 #     vers.c            The "vers.c" file in the current directory is
     81 #                       the primary output.
     82 #
     83 #     version           The "version" file in the current directory
     84 #                       is both an input and an output.  See the
     85 #                       description under "Input files".
     86 #
     87 # Input files:
     88 #
     89 #     version           The "version" file in the current directory
     90 #                       contains an integer counter, representing the
     91 #                       number of times this script has been executed in
     92 #                       this directory, starting with "0" if the file
     93 #                       does not exist.  The serial number in the file
     94 #                       is incremented after the file is read. so that
     95 #                       the incremented serial number is an output from
     96 #                       the present build and an input to the next build
     97 #                       that is performed in the same directory.
     98 #
     99 #     copyright         The "copyright" file (in the same directory as
    100 #                       this script itself) contains a copyright notice,
    101 #                       which is embedded in the copyright variable in
    102 #                       the output file.
    103 #
    104 #     ident             The "ident" file in the current directory is optional.
    105 #			If this file exists, then its contents override the
    106 #			default value of the kernel_ident string.
    107 #
    108 # Input from external commands:
    109 #
    110 #     osrelease.sh      This script is expected to print the OS revision.
    111 #                       The result is stored in the osrelease variable.
    112 #
    113 
    114 # FUNCTIONS
    115 
    116 # source_lines [input] --
    117 #
    118 # Convert a multi-line string to a format that's suitable for inclusion in
    119 # C source code.  The result should look like this:
    120 #
    121 # "first line\n"
    122 # "second line\n"
    123 #
    124 # with <backslash><letter n> inside the quotes for each line,
    125 # literal quotation marks around each line,
    126 # and a literal newline separating one line from the next.
    127 #
    128 # Input is from "$1" if that is defined, or from stdin if $1 is not defined.
    129 #
    130 source_lines()
    131 {
    132 	if [ -n "${1+set}" ]; then
    133 		printf "%s" "$1"
    134 	else
    135 		cat
    136 	fi \
    137 	| "${AWK}" '{
    138 		# awk does not care about whether or not the last line
    139 		# of input ends with a newline.
    140 		# Convert <backslash> to <backslash><backslash>.
    141 		gsub("\\\\","\\\\");
    142 		# Convert <quote> to <backslash><quote>
    143 		gsub("\"","\\\"");
    144 		# Add <backslash><letter n> to the end of each line,
    145 		# and wrap each line in double quotes.
    146 		printf("\"%s\\n\"\n", $0);
    147 	}'
    148 }
    149 
    150 # MAIN PROGRAM
    151 
    152 if [ ! -e version ]; then
    153 	echo 0 > version
    154 fi
    155 
    156 DATE=${TOOL_DATE:-date}
    157 AWK=${TOOL_AWK:-awk}
    158 Rflag=false
    159 nflag=false
    160 timestamp=
    161 pwd=$(pwd)
    162 
    163 while getopts "Rr:m:i:n" OPT; do
    164 	case $OPT in
    165 	R)
    166 		# -R: Reproducible build
    167 		Rflag=true
    168 		;;
    169 	r)
    170 		# -r <timestamp>: timestamp
    171 		timestamp="$OPTARG"
    172 		;;
    173 	m)
    174 		# -m <machine>: machine
    175 		machine="$OPTARG"
    176 		;;
    177 	i)
    178 		# -i <id>: Use the secified string as the
    179 		# value of the kernel_ident variable
    180 		id="$OPTARG"
    181 		;;
    182 	n)
    183 		# -n: Do not include a ELF note section in the output file.
    184 		nflag=true
    185 		;;
    186 	*)	echo "Usage: newvers.sh [-Rn] [-r <timestamp>] [-m <machine>] [-i <kernel>]" >&2
    187 		exit 1;;
    188 	esac
    189 done
    190 
    191 if [ -z "${id}" ]; then
    192 	if [ -f ident ]; then
    193 		id="$(cat ident)"
    194 	else
    195 		id=$(basename "${pwd}")
    196 	fi
    197 	# Append ".${BUILDID}" to the default value of <id>.
    198 	# If the "-i <id>" command line option was used then this
    199 	# branch is not taken, so the command-line value of <id>
    200 	# is used without change.
    201 	if [ -n "${BUILDID}" ]; then
    202 		id="${id}.${BUILDID}"
    203 	fi
    204 fi
    205 
    206 if ${Rflag}; then
    207 	reproversion=
    208 else
    209 	if [ -z "${timestamp}" ]; then
    210 		v=$(cat version)
    211 		t=$(LC_ALL=C ${DATE})
    212 		u=${USER-root}
    213 		h=$(hostname)
    214 		d=$(pwd)
    215 		# Increment the serial number in the version file
    216 		echo $(expr ${v} + 1) > version
    217 	else
    218 		v=0
    219 		t=$(LC_ALL=C TZ=UTC ${DATE} -r "${timestamp}")
    220 		u=mkrepro
    221 		h=mkrepro.NetBSD.org
    222 		d="/usr/src/sys/arch/${machine}/compile/${id}"
    223 	fi
    224 	reproversion=" #${v}: ${t}\n\t${u}@${h}:${d}"
    225 fi
    226 
    227 cwd=$(dirname "$0")
    228 copyright="$(cat "${cwd}/copyright")"
    229 osrelcmd=${cwd}/osrelease.sh
    230 
    231 ost="NetBSD"
    232 osr=$(sh $osrelcmd)
    233 
    234 fullversion="${ost} ${osr} (${id})${reproversion}\n"
    235 
    236 # Convert multi-line strings to C source code.
    237 # Also add an extra blank line to copyright.
    238 #
    239 copyright_source="$(printf "%s\n\n" "${copyright}" | source_lines)"
    240 fullversion_source="$(printf "%b" "${fullversion}" | source_lines)"
    241 buildinfo_source="$(printf "%b" "${BUILDINFO}" | source_lines)"
    242 
    243 # work around escaping issues with different shells
    244 emptyq='""'
    245 
    246 cat << _EOF > vers.c
    247 /*
    248  * Automatically generated file from $0
    249  * Do not edit.
    250  */
    251 #include <sys/cdefs.h>
    252 #include <sys/types.h>
    253 #include <sys/param.h>
    254 #include <sys/exec.h>
    255 #include <sys/exec_elf.h>
    256 
    257 const char ostype[] = "${ost}";
    258 const char osrelease[] = "${osr}";
    259 const char sccs[] = "@(#)" ${fullversion_source};
    260 const char version[] = ${fullversion_source};
    261 const char buildinfo[] = ${buildinfo_source:-${emptyq}};
    262 const char kernel_ident[] = "${id}";
    263 const char copyright[] = ${copyright_source};
    264 _EOF
    265 
    266 ${nflag} && exit 0
    267 
    268 cat << _EOF >> vers.c
    269 
    270 /*
    271  * NetBSD identity note.
    272  */
    273 #ifdef __arm__
    274 #define _SHT_NOTE	%note
    275 #else
    276 #define _SHT_NOTE	@note
    277 #endif
    278 
    279 #define	_S(TAG)	__STRING(TAG)
    280 __asm(
    281 	".section\t\".note.netbsd.ident\", \"\"," _S(_SHT_NOTE) "\n"
    282 	"\t.p2align\t2\n"
    283 	"\t.long\t" _S(ELF_NOTE_NETBSD_NAMESZ) "\n"
    284 	"\t.long\t" _S(ELF_NOTE_NETBSD_DESCSZ) "\n"
    285 	"\t.long\t" _S(ELF_NOTE_TYPE_NETBSD_TAG) "\n"
    286 	"\t.ascii\t" _S(ELF_NOTE_NETBSD_NAME) "\n"
    287 	"\t.long\t" _S(__NetBSD_Version__) "\n"
    288 	"\t.p2align\t2\n"
    289 );
    290 
    291 _EOF
    292