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