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