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