Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / scripts / version_tag.pl.in
1 #!/usr/bin/perl
2 # -*- Mode: perl; indent-tabs-mode: nil; cperl-indent-level: 4 -*-
3
4 use strict;
5 use diagnostics;
6 use IO::File;
7 use Time::Local;
8
9 my $pristine = 1;
10 my $kernver;
11
12 # Use the CVS tag first otherwise use the portals version
13 sub get_tag()
14 {
15     my $tag;
16     my $line;
17
18     my $tagfile = new IO::File;
19     if (!$tagfile->open("lustre/CVS/Tag")) {
20         my $verfile = new IO::File;
21         if (!$verfile->open("config.h")) {
22           return "UNKNOWN";
23         }
24         while(defined($line = <$verfile>)) {
25             $line =~ /\#define VERSION "(.*)"/;
26             if ($1) {
27                 $tag = $1;
28                 last;
29             }
30         }
31         $verfile->close();
32         return $tag
33     } else {
34         my $tmp = <$tagfile>;
35         $tagfile->close();
36
37         $tmp =~ m/T(.*)/;
38         return $1;
39     }
40 }
41
42 sub get_latest_mtime()
43 {
44     my %months=("Jan" => 0, "Feb" => 1, "Mar" => 2, "Apr" => 3, "May" => 4,
45                 "Jun" => 5, "Jul" => 6, "Aug" => 7, "Sep" => 8, "Oct" => 9,
46                 "Nov" => 10, "Dec" => 11);
47
48     my $last_mtime = 0;
49     my @entries = `find . -name Entries`;
50     my $entry_file;
51     foreach $entry_file (@entries) {
52         chomp($entry_file);
53         my $entry = new IO::File;
54         if (!$entry->open($entry_file)) {
55             die "unable to open $entry_file: $!\n";
56         }
57         my $line;
58         while (defined($line = <$entry>)) {
59             chomp($line);
60             #print "line: $line\n";
61             my ($junk, $file, $version, $date) = split(/\//, $line);
62
63             #print "junk: $junk\nfile: $file\nver: $version\ndate: $date\n";
64             #print "last_mtime: " . localtime($last_mtime) . "\n";
65
66             if ($junk eq "D" ||
67                 $file eq "lustre.spec.in" ||
68                 $file !~ m/\.(c|h|am|in)$/) {
69                 next;
70             }
71
72             my $cur_dir = $entry_file;
73             $cur_dir =~ s/\/CVS\/Entries$//;
74             my @statbuf = stat("$cur_dir/$file");
75             my $mtime = $statbuf[9];
76             if (!defined($mtime)) {
77                 next;
78             }
79             my $local_date = gmtime($mtime);
80             if ($local_date ne $date &&
81                 $file ne "lustre.spec.in") {
82                 #print "$file : " . localtime($mtime) . "\n";
83                 $pristine = 0;
84             }
85
86             if ($mtime > $last_mtime) {
87                 $last_mtime = $mtime;
88             }
89
90             if ($date) {
91                 my @t = split(/ +/, $date);
92                 if (int(@t) != 5) {
93                     #print "skipping: $date\n";
94                     next;
95                 }
96                 my ($hours, $min, $sec) = split(/:/, $t[3]);
97                 my ($mon, $mday, $year) = ($t[1], $t[2], $t[4]);
98                 my $secs = 0;
99                 $mon = $months{$mon};
100                 $secs = timelocal($sec, $min, $hours, $mday, $mon, $year);
101                 if ($secs > $last_mtime) {
102                     $last_mtime = $secs;
103                 }
104             }
105         }
106         $entry->close();
107     }
108     return $last_mtime;
109 }
110
111 sub get_linuxdir()
112 {
113     my $config = new IO::File;
114     my ($line, $dir);
115     if (!$config->open("Makefile")) {
116         die "Run ./configure first\n";
117     }
118     while (defined($line = <$config>)) {
119         chomp($line);
120         if ($line =~ /LINUX :?= (.*)/) {
121             $dir = $1;
122             last;
123         }
124     }
125     $config->close();
126     my $ver = new IO::File;
127     if (!$ver->open("$dir/include/linux/version.h")) {
128         die "Run make dep on $dir\n";
129     }
130     while(defined($line = <$ver>)) {
131         $line =~ /\#define UTS_RELEASE "(.*)"/;
132         if ($1) {
133             $kernver = $1;
134             last;
135         }
136     }
137     $ver->close();
138     chomp($kernver);
139     $dir =~ s/\//\./g;
140     return $dir;
141 }
142
143 sub generate_ver($$$)
144 {
145     my $tag = shift;
146     my $mtime = shift;
147     my $linuxdir = shift;
148
149     #print "localtime: " . localtime($mtime) . "\n";
150
151     my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
152       localtime($mtime);
153     $year += 1900;
154     $mon++;
155     my $show_last = sprintf("%04d%02d%02d%02d%02d%02d", $year, $mon, $mday,
156                             $hour, $min, $sec);
157
158     print "#define BUILD_VERSION \"";
159
160     my $lustre_vers = $ENV{LUSTRE_VERS};
161
162     if ($lustre_vers) {
163         print "$tag-$lustre_vers\"\n";
164     } elsif ($pristine) {
165         print "$tag-$show_last-PRISTINE-$linuxdir-$kernver\"\n";
166     } else {
167         print "$tag-$show_last-CHANGED-$linuxdir-$kernver\"\n";
168     }
169 }
170 chomp(my $cwd = `pwd`);
171
172 # ARGV[0] = srcdir
173 # ARGV[1] = builddir
174
175 # for get_latest_mtime and get_tag you need to be in srcdir
176
177 if ($ARGV[0]) {
178     chdir($ARGV[0]);
179 }
180 my $tag = get_tag();
181 my $mtime = get_latest_mtime();
182
183 # for get_linuxdir you need to be in builddir
184
185 #if ($ARGV[1]) {
186 #   chdir($cwd);
187 #   chdir($ARGV[1]);
188 #}
189 #my $linuxdir = get_linuxdir();
190
191 my $linuxdir = '@LINUX@';
192 $linuxdir =~ s/\//\./g;
193 $kernver = '@LINUXRELEASE@';
194
195 generate_ver($tag, $mtime, $linuxdir);
196
197 exit(0);