1 1.51 skrll #!/bin/sh - 2 1.16 cgd # 3 1.64 christos # $NetBSD: newvers.sh,v 1.64 2024/05/01 14:52:01 christos 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.62 christos # -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.62 christos # -r <timestamp> Reproducible build: Embed fixed information to 52 1.62 christos # the output file. 53 1.62 christos # This intended to allow two builds done at different 54 1.62 christos # times and even by different people on different 55 1.62 christos # hosts to produce identical output. 56 1.62 christos # 57 1.62 christos # -m <machine> Use the specified string as the value of the 58 1.62 christos # machine variable 59 1.62 christos # 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.64 christos | "${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.63 christos DATE=${TOOL_DATE:-date} 157 1.64 christos AWK=${TOOL_AWK:-awk} 158 1.62 christos Rflag=false 159 1.62 christos nflag=false 160 1.62 christos timestamp= 161 1.62 christos pwd=$(pwd) 162 1.62 christos 163 1.62 christos while getopts "Rr:m:i:n" OPT; do 164 1.62 christos case $OPT in 165 1.62 christos R) 166 1.62 christos # -R: Reproducible build 167 1.62 christos Rflag=true 168 1.62 christos ;; 169 1.62 christos r) 170 1.62 christos # -r <timestamp>: timestamp 171 1.62 christos timestamp="$OPTARG" 172 1.62 christos ;; 173 1.62 christos m) 174 1.62 christos # -m <machine>: machine 175 1.62 christos machine="$OPTARG" 176 1.55 pooka ;; 177 1.62 christos i) 178 1.59 apb # -i <id>: Use the secified string as the 179 1.59 apb # value of the kernel_ident variable 180 1.62 christos id="$OPTARG" 181 1.56 pooka ;; 182 1.62 christos n) 183 1.62 christos # -n: Do not include a ELF note section in the output file. 184 1.56 pooka nflag=true 185 1.56 pooka ;; 186 1.62 christos *) echo "Usage: newvers.sh [-Rn] [-r <timestamp>] [-m <machine>] [-i <kernel>]" >&2 187 1.62 christos exit 1;; 188 1.55 pooka esac 189 1.55 pooka done 190 1.55 pooka 191 1.56 pooka if [ -z "${id}" ]; then 192 1.56 pooka if [ -f ident ]; then 193 1.56 pooka id="$(cat ident)" 194 1.56 pooka else 195 1.62 christos id=$(basename "${pwd}") 196 1.56 pooka fi 197 1.58 apb # Append ".${BUILDID}" to the default value of <id>. 198 1.58 apb # If the "-i <id>" command line option was used then this 199 1.58 apb # branch is not taken, so the command-line value of <id> 200 1.58 apb # is used without change. 201 1.58 apb if [ -n "${BUILDID}" ]; then 202 1.58 apb id="${id}.${BUILDID}" 203 1.58 apb fi 204 1.30 hubertf fi 205 1.32 christos 206 1.62 christos if ${Rflag}; then 207 1.62 christos reproversion= 208 1.62 christos else 209 1.62 christos if [ -z "${timestamp}" ]; then 210 1.62 christos v=$(cat version) 211 1.63 christos t=$(LC_ALL=C ${DATE}) 212 1.62 christos u=${USER-root} 213 1.62 christos h=$(hostname) 214 1.62 christos d=$(pwd) 215 1.62 christos # Increment the serial number in the version file 216 1.62 christos echo $(expr ${v} + 1) > version 217 1.62 christos else 218 1.62 christos v=0 219 1.63 christos t=$(LC_ALL=C TZ=UTC ${DATE} -r "${timestamp}") 220 1.62 christos u=mkrepro 221 1.62 christos h=mkrepro.NetBSD.org 222 1.62 christos d="/usr/src/sys/arch/${machine}/compile/${id}" 223 1.62 christos fi 224 1.62 christos reproversion=" #${v}: ${t}\n\t${u}@${h}:${d}" 225 1.62 christos fi 226 1.62 christos 227 1.62 christos cwd=$(dirname "$0") 228 1.62 christos copyright="$(cat "${cwd}/copyright")" 229 1.37 lukem osrelcmd=${cwd}/osrelease.sh 230 1.12 cgd 231 1.12 cgd ost="NetBSD" 232 1.32 christos osr=$(sh $osrelcmd) 233 1.32 christos 234 1.62 christos fullversion="${ost} ${osr} (${id})${reproversion}\n" 235 1.36 lukem 236 1.60 apb # Convert multi-line strings to C source code. 237 1.60 apb # Also add an extra blank line to copyright. 238 1.60 apb # 239 1.60 apb copyright_source="$(printf "%s\n\n" "${copyright}" | source_lines)" 240 1.60 apb fullversion_source="$(printf "%b" "${fullversion}" | source_lines)" 241 1.60 apb buildinfo_source="$(printf "%b" "${BUILDINFO}" | source_lines)" 242 1.60 apb 243 1.61 justin # work around escaping issues with different shells 244 1.61 justin emptyq='""' 245 1.61 justin 246 1.32 christos cat << _EOF > vers.c 247 1.32 christos /* 248 1.32 christos * Automatically generated file from $0 249 1.32 christos * Do not edit. 250 1.32 christos */ 251 1.32 christos #include <sys/cdefs.h> 252 1.32 christos #include <sys/types.h> 253 1.32 christos #include <sys/param.h> 254 1.32 christos #include <sys/exec.h> 255 1.32 christos #include <sys/exec_elf.h> 256 1.32 christos 257 1.32 christos const char ostype[] = "${ost}"; 258 1.32 christos const char osrelease[] = "${osr}"; 259 1.60 apb const char sccs[] = "@(#)" ${fullversion_source}; 260 1.60 apb const char version[] = ${fullversion_source}; 261 1.61 justin const char buildinfo[] = ${buildinfo_source:-${emptyq}}; 262 1.41 thorpej const char kernel_ident[] = "${id}"; 263 1.60 apb const char copyright[] = ${copyright_source}; 264 1.55 pooka _EOF 265 1.55 pooka 266 1.62 christos ${nflag} && exit 0 267 1.55 pooka 268 1.55 pooka cat << _EOF >> vers.c 269 1.12 cgd 270 1.32 christos /* 271 1.32 christos * NetBSD identity note. 272 1.32 christos */ 273 1.52 skrll #ifdef __arm__ 274 1.52 skrll #define _SHT_NOTE %note 275 1.52 skrll #else 276 1.52 skrll #define _SHT_NOTE @note 277 1.52 skrll #endif 278 1.52 skrll 279 1.32 christos #define _S(TAG) __STRING(TAG) 280 1.32 christos __asm( 281 1.52 skrll ".section\t\".note.netbsd.ident\", \"\"," _S(_SHT_NOTE) "\n" 282 1.32 christos "\t.p2align\t2\n" 283 1.32 christos "\t.long\t" _S(ELF_NOTE_NETBSD_NAMESZ) "\n" 284 1.32 christos "\t.long\t" _S(ELF_NOTE_NETBSD_DESCSZ) "\n" 285 1.32 christos "\t.long\t" _S(ELF_NOTE_TYPE_NETBSD_TAG) "\n" 286 1.32 christos "\t.ascii\t" _S(ELF_NOTE_NETBSD_NAME) "\n" 287 1.32 christos "\t.long\t" _S(__NetBSD_Version__) "\n" 288 1.32 christos "\t.p2align\t2\n" 289 1.32 christos ); 290 1.4 cgd 291 1.32 christos _EOF 292