Whamcloud - gitweb
Merge of b_md to HEAD:
[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             my $local_date = gmtime($mtime);
63             if ($local_date ne $date &&
64                 $file ne "lustre.spec.in") {
65                 #print "$file : " . localtime($mtime) . "\n";
66                 $pristine = 0;
67             }
68
69             if ($mtime > $last_mtime) {
70                 $last_mtime = $mtime;
71             }
72
73             if ($date) {
74                 my @t = split(/ +/, $date);
75                 if (int(@t) != 5) {
76                     #print "skipping: $date\n";
77                     next;
78                 }
79                 my ($hours, $min, $sec) = split(/:/, $t[3]);
80                 my ($mon, $mday, $year) = ($t[1], $t[2], $t[4]);
81                 my $secs = 0;
82                 $mon = $months{$mon};
83                 $secs = timelocal($sec, $min, $hours, $mday, $mon, $year);
84                 if ($secs > $last_mtime) {
85                     $last_mtime = $secs;
86                 }
87             }
88         }
89         $entry->close();
90     }
91     return $last_mtime;
92 }
93
94 sub get_linuxdir()
95 {
96     my $config = new IO::File;
97     my ($line, $dir);
98     if (!$config->open("Makefile")) {
99         die "Run ./configure first\n";
100     }
101     while (defined($line = <$config>)) {
102         chomp($line);
103         if ($line =~ /LINUX = (.*)/) {
104             $dir = $1;
105             last;
106         }
107     }
108     $config->close();
109     my $ver = new IO::File;
110     if (!$ver->open("$dir/include/linux/version.h")) {
111         die "Run make dep on $dir\n";
112     }
113     while(defined($line = <$ver>)) {
114         $line =~ /\#define UTS_RELEASE "(.*)"/;
115         if ($1) {
116             $kernver = $1;
117             last;
118         }
119     }
120     $ver->close();
121     chomp($kernver);
122     $dir =~ s/\//\./g;
123     return $dir;
124 }
125
126 sub generate_ver($$$)
127 {
128     my $tag = shift;
129     my $mtime = shift;
130     my $linuxdir = shift;
131
132     #print "localtime: " . localtime($mtime) . "\n";
133
134     my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
135       localtime($mtime);
136     $year += 1900;
137     $mon++;
138     my $show_last = $year . $mon . $mday . $hour . $min . $sec;
139
140     print "#define BUILD_VERSION \"";
141     if ($pristine) {
142         print "$tag-$show_last-PRISTINE-$linuxdir-$kernver\"\n";
143     } else {
144         print "$tag-$show_last-CHANGED-$linuxdir-$kernver\"\n";
145     }
146 }
147
148 if ($ARGV[0]) {
149     chdir($ARGV[0]);
150 }
151 my $linuxdir = get_linuxdir();
152 my $tag = get_tag();
153 my $mtime = get_latest_mtime();
154 generate_ver($tag, $mtime, $linuxdir);
155
156 exit(0);