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