1*8d741a5dSApple OSS Distributions#!/usr/bin/perl 2*8d741a5dSApple OSS Distributions# 3*8d741a5dSApple OSS Distributions# This tool is used to stamp kernel version information into files at kernel 4*8d741a5dSApple OSS Distributions# build time. Each argument provided on the command line is the path to a file 5*8d741a5dSApple OSS Distributions# that needs to be updated with the current version information. The file 6*8d741a5dSApple OSS Distributions# $OBJROOT/xnuVersion is read to determine the version number to use. 7*8d741a5dSApple OSS Distributions# Each file is read, and all occurrences of the following strings are replaced 8*8d741a5dSApple OSS Distributions# in-place like so: 9*8d741a5dSApple OSS Distributions# ###KERNEL_VERSION_LONG### 1.2.3b4 10*8d741a5dSApple OSS Distributions# ###KERNEL_VERSION_SHORT### 1.2.3 11*8d741a5dSApple OSS Distributions# ###KERNEL_VERSION_MAJOR### 1 12*8d741a5dSApple OSS Distributions# ###KERNEL_VERSION_MINOR### 2 13*8d741a5dSApple OSS Distributions# ###KERNEL_VERSION_VARIANT### 3b4 14*8d741a5dSApple OSS Distributions# ###KERNEL_VERSION_REVISION### 3 15*8d741a5dSApple OSS Distributions# ###KERNEL_VERSION_STAGE### VERSION_STAGE_BETA (see libkern/version.h) 16*8d741a5dSApple OSS Distributions# ###KERNEL_VERSION_PRERELEASE_LEVEL### 4 17*8d741a5dSApple OSS Distributions# ###KERNEL_BUILD_CONFIG### development 18*8d741a5dSApple OSS Distributions# ###KERNEL_BUILDER### root 19*8d741a5dSApple OSS Distributions# ###KERNEL_BUILD_OBJROOT### xnu/xnu-690.obj~2/RELEASE_PPC 20*8d741a5dSApple OSS Distributions# ###KERNEL_BUILD_DATE### Sun Oct 24 05:33:28 PDT 2004 21*8d741a5dSApple OSS Distributions 22*8d741a5dSApple OSS Distributionsuse File::Basename; 23*8d741a5dSApple OSS Distributions 24*8d741a5dSApple OSS Distributionsuse strict; 25*8d741a5dSApple OSS Distributions 26*8d741a5dSApple OSS Distributionssub ReadFile { 27*8d741a5dSApple OSS Distributions my ($fileName) = @_; 28*8d741a5dSApple OSS Distributions my $data; 29*8d741a5dSApple OSS Distributions local $/ = undef; # Read complete files 30*8d741a5dSApple OSS Distributions 31*8d741a5dSApple OSS Distributions if (open(IN, "<$fileName")) { 32*8d741a5dSApple OSS Distributions $data=<IN>; 33*8d741a5dSApple OSS Distributions close IN; 34*8d741a5dSApple OSS Distributions return $data; 35*8d741a5dSApple OSS Distributions } 36*8d741a5dSApple OSS Distributions die "newvers: Can't read file \"$fileName\"\n"; 37*8d741a5dSApple OSS Distributions} 38*8d741a5dSApple OSS Distributions 39*8d741a5dSApple OSS Distributionssub WriteFile { 40*8d741a5dSApple OSS Distributions my ($fileName, $data) = @_; 41*8d741a5dSApple OSS Distributions 42*8d741a5dSApple OSS Distributions open (OUT, ">$fileName") or die "newvers: Can't write file \"$fileName\"\n"; 43*8d741a5dSApple OSS Distributions print OUT $data; 44*8d741a5dSApple OSS Distributions close(OUT); 45*8d741a5dSApple OSS Distributions} 46*8d741a5dSApple OSS Distributions 47*8d741a5dSApple OSS Distributionsdie("SRCROOT not defined") unless defined($ENV{'SRCROOT'}); 48*8d741a5dSApple OSS Distributionsdie("OBJROOT not defined") unless defined($ENV{'OBJROOT'}); 49*8d741a5dSApple OSS Distributions 50*8d741a5dSApple OSS Distributionsmy $versfile = "xnuVersion"; 51*8d741a5dSApple OSS Distributions$versfile = "$ENV{'OBJROOT'}/$versfile" if ($ENV{'OBJROOT'}); 52*8d741a5dSApple OSS Distributionsmy $BUILD_SRCROOT=$ENV{'SRCROOT'}; 53*8d741a5dSApple OSS Distributions$BUILD_SRCROOT =~ s,/+$,,; 54*8d741a5dSApple OSS Distributionsmy $BUILD_OBJROOT=$ENV{'OBJROOT'}; 55*8d741a5dSApple OSS Distributions$BUILD_OBJROOT =~ s,/+$,,; 56*8d741a5dSApple OSS Distributionsmy $BUILD_OBJPATH=$ENV{'TARGET'} || $ENV{'OBJROOT'}; 57*8d741a5dSApple OSS Distributions$BUILD_OBJPATH =~ s,/+$,,; 58*8d741a5dSApple OSS Distributionsmy $BUILD_DATE = `date`; 59*8d741a5dSApple OSS Distributions$BUILD_DATE =~ s/[\n\t]//g; 60*8d741a5dSApple OSS Distributionsmy $BUILD_CONFIG = "unknown"; 61*8d741a5dSApple OSS Distributions$BUILD_CONFIG = $ENV{'CURRENT_KERNEL_CONFIG_LC'} if defined($ENV{'CURRENT_KERNEL_CONFIG_LC'}); 62*8d741a5dSApple OSS Distributionsmy $BUILDER=`whoami`; 63*8d741a5dSApple OSS Distributions$BUILDER =~ s/[\n\t]//g; 64*8d741a5dSApple OSS Distributionsmy $RC_STRING = $ENV{'RC_ProjectNameAndSourceVersion'} . "~" . $ENV{'RC_ProjectBuildVersion'} if defined($ENV{'RC_XBS'}); 65*8d741a5dSApple OSS Distributions 66*8d741a5dSApple OSS Distributions# Handle four scenarios: 67*8d741a5dSApple OSS Distributions# SRCROOT=/tmp/xnu 68*8d741a5dSApple OSS Distributions# OBJROOT=/tmp/xnu/BUILD/obj 69*8d741a5dSApple OSS Distributions# OBJPATH=/tmp/xnu/BUILD/obj/RELEASE_X86_64 70*8d741a5dSApple OSS Distributions# 71*8d741a5dSApple OSS Distributions# SRCROOT=/SourceCache/xnu/xnu-2706 72*8d741a5dSApple OSS Distributions# OBJROOT=/BinaryCache/xnu/xnu-2706~3/Objects 73*8d741a5dSApple OSS Distributions# OBJPATH=/BinaryCache/xnu/xnu-2706~3/Objects/DEVELOPMENT_X86_64 74*8d741a5dSApple OSS Distributions# RC_XBS=YES (XBS-16.3+) 75*8d741a5dSApple OSS Distributions# RC_ProjectNameAndSourceVersion=xnu-2706 76*8d741a5dSApple OSS Distributions# RC_ProjectBuildVersion=3 77*8d741a5dSApple OSS Distributions# 78*8d741a5dSApple OSS Distributions# SRCROOT=/SourceCache/xnu/xnu-2706 79*8d741a5dSApple OSS Distributions# OBJROOT=/private/var/tmp/xnu/xnu-2706~2 80*8d741a5dSApple OSS Distributions# OBJPATH=/private/var/tmp/xnu/xnu-2706~2/DEVELOPMENT_ARM_S5L8940X 81*8d741a5dSApple OSS Distributions# RC_XBS=YES (<XBS-16.3) 82*8d741a5dSApple OSS Distributions# RC_ProjectNameAndSourceVersion=xnu-2706 83*8d741a5dSApple OSS Distributions# RC_ProjectBuildVersion=2 84*8d741a5dSApple OSS Distributions# 85*8d741a5dSApple OSS Distributions# SRCROOT=/tmp/xnu-2800.0.1_xnu-svn.roots/Sources/xnu-2800.0.1 86*8d741a5dSApple OSS Distributions# OBJROOT=/private/tmp/xnu-2800.0.1_xnu-svn.roots/BuildRecords/xnu-2800.0.1_install/Objects 87*8d741a5dSApple OSS Distributions# OBJPATH=/private/tmp/xnu-2800.0.1_xnu-svn.roots/BuildRecords/xnu-2800.0.1_install/Objects/DEVELOPMENT_X86_64 88*8d741a5dSApple OSS Distributions# RC_XBS=YES (buildit) 89*8d741a5dSApple OSS Distributions# RC_BUILDIT=YES 90*8d741a5dSApple OSS Distributions# RC_ProjectNameAndSourceVersion=xnu-2800.0.1 91*8d741a5dSApple OSS Distributions# RC_ProjectBuildVersion=1 92*8d741a5dSApple OSS Distributions# 93*8d741a5dSApple OSS Distributions# 94*8d741a5dSApple OSS Distributions# If SRCROOT is a strict prefix of OBJPATH, we 95*8d741a5dSApple OSS Distributions# want to preserve the "interesting" part 96*8d741a5dSApple OSS Distributions# starting with "xnu". If it's not a prefix, 97*8d741a5dSApple OSS Distributions# the basename of OBJROOT itself is "interesting". 98*8d741a5dSApple OSS Distributions# Newer versions of XBS just set this to "Objects", so we 99*8d741a5dSApple OSS Distributions# need to synthesize the directory name to be more interesting. 100*8d741a5dSApple OSS Distributions# 101*8d741a5dSApple OSS Distributions 102*8d741a5dSApple OSS Distributionssub describe { 103*8d741a5dSApple OSS Distributions my ($basename) = @_; 104*8d741a5dSApple OSS Distributions 105*8d741a5dSApple OSS Distributions # get a git tag if we can 106*8d741a5dSApple OSS Distributions my $tag = `git describe --dirty 2>/dev/null`; 107*8d741a5dSApple OSS Distributions chomp $tag; 108*8d741a5dSApple OSS Distributions if ($? != 0 or $tag !~ /^xnu-([^\s\n]+)$/) { 109*8d741a5dSApple OSS Distributions return $basename; 110*8d741a5dSApple OSS Distributions } 111*8d741a5dSApple OSS Distributions 112*8d741a5dSApple OSS Distributions # If basename is just 'xnu' then replace it with the tag. Otherwise add 113*8d741a5dSApple OSS Distributions # the tag in brackets. 114*8d741a5dSApple OSS Distributions if ($basename eq 'xnu') { 115*8d741a5dSApple OSS Distributions return $tag 116*8d741a5dSApple OSS Distributions } else { 117*8d741a5dSApple OSS Distributions return "${basename}[$tag]" 118*8d741a5dSApple OSS Distributions } 119*8d741a5dSApple OSS Distributions} 120*8d741a5dSApple OSS Distributions 121*8d741a5dSApple OSS Distributionsif ($BUILD_OBJPATH =~ m,^$BUILD_SRCROOT/(.*)$,) { 122*8d741a5dSApple OSS Distributions $BUILD_OBJROOT = describe(basename($BUILD_SRCROOT)) . "/" . $1; 123*8d741a5dSApple OSS Distributions} elsif ($BUILD_OBJPATH =~ m,^$BUILD_OBJROOT/(.*)$,) { 124*8d741a5dSApple OSS Distributions if (defined($RC_STRING)) { 125*8d741a5dSApple OSS Distributions $BUILD_OBJROOT = $RC_STRING . "/" . $1; 126*8d741a5dSApple OSS Distributions } else { 127*8d741a5dSApple OSS Distributions $BUILD_OBJROOT = describe(basename($BUILD_OBJROOT)) . "/" . $1; 128*8d741a5dSApple OSS Distributions } 129*8d741a5dSApple OSS Distributions} else { 130*8d741a5dSApple OSS Distributions # Use original OBJROOT 131*8d741a5dSApple OSS Distributions} 132*8d741a5dSApple OSS Distributions 133*8d741a5dSApple OSS Distributionsmy $rawvers = &ReadFile($versfile); 134*8d741a5dSApple OSS Distributions#$rawvers =~ s/\s//g; 135*8d741a5dSApple OSS Distributions($rawvers) = split "\n", $rawvers; 136*8d741a5dSApple OSS Distributionsmy ($VERSION_MAJOR, $VERSION_MINOR, $VERSION_VARIANT) = split /\./, $rawvers; 137*8d741a5dSApple OSS Distributionsdie "newvers: Invalid xnuVersion \"$rawvers\"!!! " if (!$VERSION_MAJOR); 138*8d741a5dSApple OSS Distributions$VERSION_MINOR = "0" unless ($VERSION_MINOR); 139*8d741a5dSApple OSS Distributions$VERSION_VARIANT = "0" unless ($VERSION_VARIANT); 140*8d741a5dSApple OSS Distributions$VERSION_VARIANT =~ tr/A-Z/a-z/; 141*8d741a5dSApple OSS Distributions$VERSION_VARIANT =~ m/(\d+)((?:d|a|b|r|fc)?)(\d*)/; 142*8d741a5dSApple OSS Distributionsmy $VERSION_REVISION = $1; 143*8d741a5dSApple OSS Distributionsmy $stage = $2; 144*8d741a5dSApple OSS Distributionsmy $VERSION_PRERELEASE_LEVEL = $3; 145*8d741a5dSApple OSS Distributions$VERSION_REVISION ="0" unless ($VERSION_REVISION); 146*8d741a5dSApple OSS Distributions$stage = "r" if (!$stage || ($stage eq "fc")); 147*8d741a5dSApple OSS Distributions$VERSION_PRERELEASE_LEVEL = "0" unless ($VERSION_PRERELEASE_LEVEL); 148*8d741a5dSApple OSS Distributions 149*8d741a5dSApple OSS Distributionsmy $VERSION_STAGE; 150*8d741a5dSApple OSS Distributions$VERSION_STAGE = 'VERSION_STAGE_DEV' if ($stage eq 'd'); 151*8d741a5dSApple OSS Distributions$VERSION_STAGE = 'VERSION_STAGE_ALPHA' if ($stage eq 'a'); 152*8d741a5dSApple OSS Distributions$VERSION_STAGE = 'VERSION_STAGE_BETA' if ($stage eq 'b'); 153*8d741a5dSApple OSS Distributions$VERSION_STAGE = 'VERSION_STAGE_RELEASE' if ($stage eq 'r'); 154*8d741a5dSApple OSS Distributions 155*8d741a5dSApple OSS Distributionsmy $VERSION_SHORT = "$VERSION_MAJOR.$VERSION_MINOR.$VERSION_REVISION"; 156*8d741a5dSApple OSS Distributionsmy $VERSION_LONG = $VERSION_SHORT; 157*8d741a5dSApple OSS Distributions$VERSION_LONG .= "$stage$VERSION_PRERELEASE_LEVEL" if (($stage ne "r") || ($VERSION_PRERELEASE_LEVEL != 0)); 158*8d741a5dSApple OSS Distributions 159*8d741a5dSApple OSS Distributions# Allow environment to override some versioning information (allowing for 160*8d741a5dSApple OSS Distributions# reproducible builds or building with changes that shouldn't affect the 161*8d741a5dSApple OSS Distributions# build artifacts). 162*8d741a5dSApple OSS Distributions$BUILD_DATE = $ENV{'KERNEL_BUILD_DATE'} ? $ENV{'KERNEL_BUILD_DATE'} : $BUILD_DATE; 163*8d741a5dSApple OSS Distributions$BUILD_OBJROOT = $ENV{'KERNEL_BUILD_OBJROOT'} ? $ENV{'KERNEL_BUILD_OBJROOT'} : $BUILD_OBJROOT; 164*8d741a5dSApple OSS Distributions 165*8d741a5dSApple OSS Distributionsmy $file; 166*8d741a5dSApple OSS Distributionsforeach $file (@ARGV) { 167*8d741a5dSApple OSS Distributions print "newvers.pl: Stamping version \"$VERSION_LONG\" into \"$file\" ..."; 168*8d741a5dSApple OSS Distributions my $data = &ReadFile($file); 169*8d741a5dSApple OSS Distributions my $count=0; 170*8d741a5dSApple OSS Distributions $count += $data =~ s/###KERNEL_VERSION_LONG###/$VERSION_LONG/g; 171*8d741a5dSApple OSS Distributions $count += $data =~ s/###KERNEL_VERSION_SHORT###/$VERSION_SHORT/g; 172*8d741a5dSApple OSS Distributions $count += $data =~ s/###KERNEL_VERSION_MAJOR###/$VERSION_MAJOR/g; 173*8d741a5dSApple OSS Distributions $count += $data =~ s/###KERNEL_VERSION_MINOR###/$VERSION_MINOR/g; 174*8d741a5dSApple OSS Distributions $count += $data =~ s/###KERNEL_VERSION_VARIANT###/$VERSION_VARIANT/g; 175*8d741a5dSApple OSS Distributions $count += $data =~ s/###KERNEL_VERSION_REVISION###/$VERSION_REVISION/g; 176*8d741a5dSApple OSS Distributions $count += $data =~ s/###KERNEL_VERSION_STAGE###/$VERSION_STAGE/g; 177*8d741a5dSApple OSS Distributions $count += $data =~ s/###KERNEL_VERSION_PRERELEASE_LEVEL###/$VERSION_PRERELEASE_LEVEL/g; 178*8d741a5dSApple OSS Distributions $count += $data =~ s/###KERNEL_BUILD_CONFIG###/$BUILD_CONFIG/g; 179*8d741a5dSApple OSS Distributions $count += $data =~ s/###KERNEL_BUILDER###/$BUILDER/g; 180*8d741a5dSApple OSS Distributions $count += $data =~ s/###KERNEL_BUILD_OBJROOT###/$BUILD_OBJROOT/g; 181*8d741a5dSApple OSS Distributions $count += $data =~ s/###KERNEL_BUILD_DATE###/$BUILD_DATE/g; 182*8d741a5dSApple OSS Distributions print " $count replacements\n"; 183*8d741a5dSApple OSS Distributions &WriteFile($file, $data); 184*8d741a5dSApple OSS Distributions} 185*8d741a5dSApple OSS Distributions 186*8d741a5dSApple OSS Distributionsif (0==scalar @ARGV) { 187*8d741a5dSApple OSS Distributions print "newvers.pl: read version \"$rawvers\" from \"$versfile\"\n"; 188*8d741a5dSApple OSS Distributions print "newvers.pl: ###KERNEL_VERSION_LONG### = $VERSION_LONG\n"; 189*8d741a5dSApple OSS Distributions print "newvers.pl: ###KERNEL_VERSION_SHORT### = $VERSION_SHORT\n"; 190*8d741a5dSApple OSS Distributions print "newvers.pl: ###KERNEL_VERSION_MAJOR### = $VERSION_MAJOR\n"; 191*8d741a5dSApple OSS Distributions print "newvers.pl: ###KERNEL_VERSION_MINOR### = $VERSION_MINOR\n"; 192*8d741a5dSApple OSS Distributions print "newvers.pl: ###KERNEL_VERSION_VARIANT### = $VERSION_VARIANT\n"; 193*8d741a5dSApple OSS Distributions print "newvers.pl: ###KERNEL_VERSION_REVISION### = $VERSION_REVISION\n"; 194*8d741a5dSApple OSS Distributions print "newvers.pl: ###KERNEL_VERSION_STAGE### = $VERSION_STAGE\n"; 195*8d741a5dSApple OSS Distributions print "newvers.pl: ###KERNEL_VERSION_PRERELEASE_LEVEL### = $VERSION_PRERELEASE_LEVEL\n"; 196*8d741a5dSApple OSS Distributions print "newvers.pl: ###KERNEL_BUILD_CONFIG### = $BUILD_CONFIG\n"; 197*8d741a5dSApple OSS Distributions print "newvers.pl: ###KERNEL_BUILDER### = $BUILDER\n"; 198*8d741a5dSApple OSS Distributions print "newvers.pl: ###KERNEL_BUILD_OBJROOT### = $BUILD_OBJROOT\n"; 199*8d741a5dSApple OSS Distributions print "newvers.pl: ###KERNEL_BUILD_DATE### = $BUILD_DATE\n"; 200*8d741a5dSApple OSS Distributions} 201