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