Home | History | Annotate | Line # | Download | only in conf
newvers.sh revision 1.61.8.1
      1      1.51     skrll #!/bin/sh -
      2      1.16       cgd #
      3  1.61.8.1  pgoyette #	$NetBSD: newvers.sh,v 1.61.8.1 2017/04/26 02:53:10 pgoyette Exp $
      4       1.1       cgd #
      5      1.12       cgd # Copyright (c) 1984, 1986, 1990, 1993
      6      1.12       cgd #	The Regents of the University of California.  All rights reserved.
      7       1.1       cgd #
      8       1.1       cgd # Redistribution and use in source and binary forms, with or without
      9       1.1       cgd # modification, are permitted provided that the following conditions
     10       1.1       cgd # are met:
     11       1.1       cgd # 1. Redistributions of source code must retain the above copyright
     12       1.1       cgd #    notice, this list of conditions and the following disclaimer.
     13       1.1       cgd # 2. Redistributions in binary form must reproduce the above copyright
     14       1.1       cgd #    notice, this list of conditions and the following disclaimer in the
     15       1.1       cgd #    documentation and/or other materials provided with the distribution.
     16      1.57       snj # 3. Neither the name of the University nor the names of its contributors
     17       1.1       cgd #    may be used to endorse or promote products derived from this software
     18       1.1       cgd #    without specific prior written permission.
     19       1.1       cgd #
     20       1.1       cgd # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21       1.1       cgd # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22       1.1       cgd # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23       1.1       cgd # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24       1.1       cgd # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25       1.1       cgd # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26       1.1       cgd # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27       1.1       cgd # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28       1.1       cgd # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29       1.1       cgd # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30       1.1       cgd # SUCH DAMAGE.
     31       1.1       cgd #
     32      1.15       cgd #	@(#)newvers.sh	8.1 (Berkeley) 4/20/94
     33       1.1       cgd 
     34      1.59       apb # newvers.sh -- Create a "vers.c" file containing version information.
     35      1.59       apb #
     36      1.59       apb # The "vers.c" file in the current directory is the primary output.  It
     37      1.59       apb # contains C source code with several variables containing information
     38      1.59       apb # about the build.  This file is expected to be incorporated into a
     39      1.59       apb # kernel, and when that kernel is booted then the information can be
     40      1.59       apb # queried by the uname(8) command.
     41      1.59       apb #
     42      1.59       apb # Command line options:
     43      1.59       apb #
     44  1.61.8.1  pgoyette #     -R                Reproducible build: Do not embed directory
     45      1.59       apb #                       names, user names, time stamps, or other dynamic
     46      1.59       apb #                       information into the output file.  This intended
     47      1.59       apb #                       to allow two builds done at different times and
     48      1.59       apb #                       even by different people on different hosts to
     49      1.59       apb #                       produce identical output.
     50      1.59       apb #
     51  1.61.8.1  pgoyette #     -r <timestamp>    Reproducible build: Embed fixed information to
     52  1.61.8.1  pgoyette #			the output file.
     53  1.61.8.1  pgoyette #			This intended to allow two builds done at different
     54  1.61.8.1  pgoyette #			times and even by different people on different
     55  1.61.8.1  pgoyette #			hosts to produce identical output.
     56  1.61.8.1  pgoyette #
     57  1.61.8.1  pgoyette #     -m <machine>      Use the specified string as the value of the
     58  1.61.8.1  pgoyette #                       machine variable
     59  1.61.8.1  pgoyette #
     60      1.59       apb #     -i <id>           Use the specified string as the value of the
     61      1.59       apb #                       kernel_ident variable
     62      1.59       apb #
     63      1.59       apb #     -n                Do not include an ELF note section in the output
     64      1.59       apb #                       file.
     65      1.59       apb # Environment variables:
     66      1.59       apb #
     67      1.59       apb #     BUILDID		If defined, ${BUILDID} is appended to the
     68      1.59       apb #			default value of the kernel_ident string.
     69      1.59       apb #			(If the -i command line option is used, then
     70      1.59       apb #			BUILDID is not appended.)
     71      1.59       apb #
     72      1.60       apb #     BUILDINFO		A string to be stored in the kernel's buildinfo
     73      1.60       apb #			variable.  ${BUILDINFO} may be a multi-line string,
     74      1.60       apb #			and may use C-style backslash escapes.
     75      1.60       apb #			Lines may be separated by either literal newlines
     76      1.60       apb #			or "\n" escape sequences.
     77      1.60       apb #
     78      1.59       apb # Output files:
     79      1.59       apb #
     80      1.59       apb #     vers.c            The "vers.c" file in the current directory is
     81      1.59       apb #                       the primary output.
     82      1.59       apb #
     83      1.59       apb #     version           The "version" file in the current directory
     84      1.59       apb #                       is both an input and an output.  See the
     85      1.59       apb #                       description under "Input files".
     86      1.59       apb #
     87      1.59       apb # Input files:
     88      1.59       apb #
     89      1.59       apb #     version           The "version" file in the current directory
     90      1.59       apb #                       contains an integer counter, representing the
     91      1.59       apb #                       number of times this script has been executed in
     92      1.59       apb #                       this directory, starting with "0" if the file
     93      1.59       apb #                       does not exist.  The serial number in the file
     94      1.59       apb #                       is incremented after the file is read. so that
     95      1.59       apb #                       the incremented serial number is an output from
     96      1.59       apb #                       the present build and an input to the next build
     97      1.59       apb #                       that is performed in the same directory.
     98      1.59       apb #
     99      1.59       apb #     copyright         The "copyright" file (in the same directory as
    100      1.59       apb #                       this script itself) contains a copyright notice,
    101      1.59       apb #                       which is embedded in the copyright variable in
    102      1.59       apb #                       the output file.
    103      1.59       apb #
    104      1.59       apb #     ident             The "ident" file in the current directory is optional.
    105      1.59       apb #			If this file exists, then its contents override the
    106      1.59       apb #			default value of the kernel_ident string.
    107      1.59       apb #
    108      1.59       apb # Input from external commands:
    109      1.59       apb #
    110      1.59       apb #     osrelease.sh      This script is expected to print the OS revision.
    111      1.59       apb #                       The result is stored in the osrelease variable.
    112      1.59       apb #
    113      1.59       apb 
    114      1.60       apb # FUNCTIONS
    115      1.60       apb 
    116      1.60       apb # source_lines [input] --
    117      1.60       apb #
    118      1.60       apb # Convert a multi-line string to a format that's suitable for inclusion in
    119      1.60       apb # C source code.  The result should look like this:
    120      1.60       apb #
    121      1.60       apb # "first line\n"
    122      1.60       apb # "second line\n"
    123      1.60       apb #
    124      1.60       apb # with <backslash><letter n> inside the quotes for each line,
    125      1.60       apb # literal quotation marks around each line,
    126      1.60       apb # and a literal newline separating one line from the next.
    127      1.60       apb #
    128      1.60       apb # Input is from "$1" if that is defined, or from stdin if $1 is not defined.
    129      1.60       apb #
    130      1.60       apb source_lines()
    131      1.60       apb {
    132      1.60       apb 	if [ -n "${1+set}" ]; then
    133      1.60       apb 		printf "%s" "$1"
    134      1.60       apb 	else
    135      1.60       apb 		cat
    136      1.60       apb 	fi \
    137      1.60       apb 	| awk '{
    138      1.60       apb 		# awk does not care about whether or not the last line
    139      1.60       apb 		# of input ends with a newline.
    140      1.60       apb 		# Convert <backslash> to <backslash><backslash>.
    141      1.60       apb 		gsub("\\\\","\\\\");
    142      1.60       apb 		# Convert <quote> to <backslash><quote>
    143      1.60       apb 		gsub("\"","\\\"");
    144      1.60       apb 		# Add <backslash><letter n> to the end of each line,
    145      1.60       apb 		# and wrap each line in double quotes.
    146      1.60       apb 		printf("\"%s\\n\"\n", $0);
    147      1.60       apb 	}'
    148      1.60       apb }
    149      1.60       apb 
    150      1.60       apb # MAIN PROGRAM
    151      1.60       apb 
    152      1.37     lukem if [ ! -e version ]; then
    153       1.1       cgd 	echo 0 > version
    154       1.1       cgd fi
    155       1.1       cgd 
    156  1.61.8.1  pgoyette Rflag=false
    157  1.61.8.1  pgoyette nflag=false
    158  1.61.8.1  pgoyette timestamp=
    159  1.61.8.1  pgoyette pwd=$(pwd)
    160  1.61.8.1  pgoyette 
    161  1.61.8.1  pgoyette while getopts "Rr:m:i:n" OPT; do
    162  1.61.8.1  pgoyette 	case $OPT in
    163  1.61.8.1  pgoyette 	R)
    164  1.61.8.1  pgoyette 		# -R: Reproducible build
    165  1.61.8.1  pgoyette 		Rflag=true
    166      1.55     pooka 		;;
    167  1.61.8.1  pgoyette 	r)
    168  1.61.8.1  pgoyette 		# -r <timestamp>: timestamp
    169  1.61.8.1  pgoyette 		timestamp="$OPTARG"
    170  1.61.8.1  pgoyette 		;;
    171  1.61.8.1  pgoyette 	m)
    172  1.61.8.1  pgoyette 		# -m <machine>: machine
    173  1.61.8.1  pgoyette 		machine="$OPTARG"
    174  1.61.8.1  pgoyette 		;;
    175  1.61.8.1  pgoyette 	i)
    176      1.59       apb 		# -i <id>: Use the secified string as the
    177      1.59       apb 		# value of the kernel_ident variable
    178  1.61.8.1  pgoyette 		id="$OPTARG"
    179      1.56     pooka 		;;
    180  1.61.8.1  pgoyette 	n)
    181  1.61.8.1  pgoyette 		# -n: Do not include a ELF note section in the output file.
    182      1.56     pooka 		nflag=true
    183      1.56     pooka 		;;
    184  1.61.8.1  pgoyette 	*)	echo "Usage: newvers.sh [-Rn] [-r <timestamp>] [-m <machine>] [-i <kernel>]" >&2
    185  1.61.8.1  pgoyette 		exit 1;;
    186      1.55     pooka 	esac
    187      1.55     pooka done
    188      1.55     pooka 
    189      1.56     pooka if [ -z "${id}" ]; then
    190      1.56     pooka 	if [ -f ident ]; then
    191      1.56     pooka 		id="$(cat ident)"
    192      1.56     pooka 	else
    193  1.61.8.1  pgoyette 		id=$(basename "${pwd}")
    194      1.56     pooka 	fi
    195      1.58       apb 	# Append ".${BUILDID}" to the default value of <id>.
    196      1.58       apb 	# If the "-i <id>" command line option was used then this
    197      1.58       apb 	# branch is not taken, so the command-line value of <id>
    198      1.58       apb 	# is used without change.
    199      1.58       apb 	if [ -n "${BUILDID}" ]; then
    200      1.58       apb 		id="${id}.${BUILDID}"
    201      1.58       apb 	fi
    202      1.30   hubertf fi
    203      1.32  christos 
    204  1.61.8.1  pgoyette if ${Rflag}; then
    205  1.61.8.1  pgoyette 	reproversion=
    206  1.61.8.1  pgoyette else
    207  1.61.8.1  pgoyette 	if [ -z "${timestamp}" ]; then
    208  1.61.8.1  pgoyette 		v=$(cat version)
    209  1.61.8.1  pgoyette 		t=$(LC_ALL=C date)
    210  1.61.8.1  pgoyette 		u=${USER-root}
    211  1.61.8.1  pgoyette 		h=$(hostname)
    212  1.61.8.1  pgoyette 		d=$(pwd)
    213  1.61.8.1  pgoyette 		# Increment the serial number in the version file
    214  1.61.8.1  pgoyette 		echo $(expr ${v} + 1) > version
    215  1.61.8.1  pgoyette 	else
    216  1.61.8.1  pgoyette 		v=0
    217  1.61.8.1  pgoyette 		t=$(LC_ALL=C TZ=UTC date -r "${timestamp}")
    218  1.61.8.1  pgoyette 		u=mkrepro
    219  1.61.8.1  pgoyette 		h=mkrepro.NetBSD.org
    220  1.61.8.1  pgoyette 		d="/usr/src/sys/arch/${machine}/compile/${id}"
    221  1.61.8.1  pgoyette 	fi
    222  1.61.8.1  pgoyette 	reproversion=" #${v}: ${t}\n\t${u}@${h}:${d}"
    223  1.61.8.1  pgoyette fi
    224  1.61.8.1  pgoyette 
    225  1.61.8.1  pgoyette cwd=$(dirname "$0")
    226  1.61.8.1  pgoyette copyright="$(cat "${cwd}/copyright")"
    227      1.37     lukem osrelcmd=${cwd}/osrelease.sh
    228      1.12       cgd 
    229      1.12       cgd ost="NetBSD"
    230      1.32  christos osr=$(sh $osrelcmd)
    231      1.32  christos 
    232  1.61.8.1  pgoyette fullversion="${ost} ${osr} (${id})${reproversion}\n"
    233      1.36     lukem 
    234      1.60       apb # Convert multi-line strings to C source code.
    235      1.60       apb # Also add an extra blank line to copyright.
    236      1.60       apb #
    237      1.60       apb copyright_source="$(printf "%s\n\n" "${copyright}" | source_lines)"
    238      1.60       apb fullversion_source="$(printf "%b" "${fullversion}" | source_lines)"
    239      1.60       apb buildinfo_source="$(printf "%b" "${BUILDINFO}" | source_lines)"
    240      1.60       apb 
    241      1.61    justin # work around escaping issues with different shells
    242      1.61    justin emptyq='""'
    243      1.61    justin 
    244      1.32  christos cat << _EOF > vers.c
    245      1.32  christos /*
    246      1.32  christos  * Automatically generated file from $0
    247      1.32  christos  * Do not edit.
    248      1.32  christos  */
    249      1.32  christos #include <sys/cdefs.h>
    250      1.32  christos #include <sys/types.h>
    251      1.32  christos #include <sys/param.h>
    252      1.32  christos #include <sys/exec.h>
    253      1.32  christos #include <sys/exec_elf.h>
    254      1.32  christos 
    255      1.32  christos const char ostype[] = "${ost}";
    256      1.32  christos const char osrelease[] = "${osr}";
    257      1.60       apb const char sccs[] = "@(#)" ${fullversion_source};
    258      1.60       apb const char version[] = ${fullversion_source};
    259      1.61    justin const char buildinfo[] = ${buildinfo_source:-${emptyq}};
    260      1.41   thorpej const char kernel_ident[] = "${id}";
    261      1.60       apb const char copyright[] = ${copyright_source};
    262      1.55     pooka _EOF
    263      1.55     pooka 
    264  1.61.8.1  pgoyette ${nflag} && exit 0
    265      1.55     pooka 
    266      1.55     pooka cat << _EOF >> vers.c
    267      1.12       cgd 
    268      1.32  christos /*
    269      1.32  christos  * NetBSD identity note.
    270      1.32  christos  */
    271      1.52     skrll #ifdef __arm__
    272      1.52     skrll #define _SHT_NOTE	%note
    273      1.52     skrll #else
    274      1.52     skrll #define _SHT_NOTE	@note
    275      1.52     skrll #endif
    276      1.52     skrll 
    277      1.32  christos #define	_S(TAG)	__STRING(TAG)
    278      1.32  christos __asm(
    279      1.52     skrll 	".section\t\".note.netbsd.ident\", \"\"," _S(_SHT_NOTE) "\n"
    280      1.32  christos 	"\t.p2align\t2\n"
    281      1.32  christos 	"\t.long\t" _S(ELF_NOTE_NETBSD_NAMESZ) "\n"
    282      1.32  christos 	"\t.long\t" _S(ELF_NOTE_NETBSD_DESCSZ) "\n"
    283      1.32  christos 	"\t.long\t" _S(ELF_NOTE_TYPE_NETBSD_TAG) "\n"
    284      1.32  christos 	"\t.ascii\t" _S(ELF_NOTE_NETBSD_NAME) "\n"
    285      1.32  christos 	"\t.long\t" _S(__NetBSD_Version__) "\n"
    286      1.32  christos 	"\t.p2align\t2\n"
    287      1.32  christos );
    288       1.4       cgd 
    289      1.32  christos _EOF
    290