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