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