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