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