From Pierre Juhen:
[obnox/wireshark/wip.git] / make-version.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2004 Jörg Mayer (see AUTHORS file)
4 #
5 # $Id$
6 #
7 # Wireshark - Network traffic analyzer
8 # By Gerald Combs <gerald@wireshark.org>
9 # Copyright 1998 Gerald Combs
10 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License
13 # as published by the Free Software Foundation; either version 2
14 # of the License, or (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 # usage:  ./make-version.pl [-p] [--package-version]
26 #
27 # If "version.conf" is present, it is parsed for configuration values.  
28 # Possible values are:
29 #
30 #   enable     - Enable or disable versioning.  Zero (0) disables, nonzero
31 #                enables.
32 #   format     - A strftime() formatted string to use as a template for
33 #                the version string.  The sequence "%#" will substitute
34 #                the SVN revision number.
35 #   pkg_enable - Enable or disable package versioning.
36 #   pkg_format - Like "format", but used for the package version.
37 #
38 # If run with the "-p" or "--package-version" argument, the
39 # AM_INIT_AUTOMAKE macro in configure.in and the VERSION macro in
40 # config.nmake will have the pkg_format template appended to the 
41 # version number.  svnversion.h will _not_ be generated if either
42 # argument is present.
43 #
44 # Default configuration:
45 #
46 # enable: 1
47 # format: SVN %Y%m%d%H%M%S
48 # pkg_enable: 1
49 # pkg_format: -SVN-%#
50 # am_init: 0
51
52 # XXX - We're pretty dumb about the "%#" substitution, and about having
53 # spaces in the package format.
54
55 use strict;
56
57 use Time::Local;
58 use POSIX qw(strftime);
59 use Getopt::Long;
60
61 my $version_file = 'svnversion.h';
62 my $package_string = "";
63 my $vconf_file = 'version.conf';
64 my $last = 0;
65 my $revision = 0;
66 my $pkg_version = 0;
67 my %version_pref = (
68         "enable"     => 1,
69         "format"     => "SVN %Y%m%d%H%M%S",
70         "pkg_enable" => 1,
71         "pkg_format" => "-SVN-%#",
72         );
73 my $srcdir = ".";
74
75
76 # Run "svn info".  Parse out the most recent modification time and the
77 # revision number.
78 sub read_svn_info {
79         my $line;
80         my $version_format = $version_pref{"format"};
81         my $package_format = "";
82         my $in_entries = 0;
83         my $svn_name;
84
85         if ($version_pref{"pkg_enable"}) {
86                 $package_format = $version_pref{"pkg_format"};
87         }
88
89         if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
90                 print ("Unable to get SVN info.\n");
91                 return;
92         }
93
94         # The entries schema is flat, so we can use regexes to parse its contents.
95         while ($line = <ENTRIES>) {
96                 if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
97                         $in_entries = 1;
98                         $svn_name = "";
99                 }
100                 if ($in_entries) {
101                         if ($line =~ /name="(.*)"/) { $svn_name = $1; }
102                         if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
103                                 $last = timegm($6, $5, $4, $3, $2 - 1, $1);
104                         }
105                         if ($line =~ /revision="(\d+)"/) { $revision = $1; }
106                 }
107                 if ($line =~ /\/>/) {
108                         if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
109                                         $last && $revision) {
110                                 $in_entries = 0;
111                                 $version_format =~ s/%#/$revision/;
112
113                                 $package_format =~ s/%#/$revision/;
114                                 $package_string = strftime($package_format, gmtime($last));
115
116                                 last;
117                         }
118                 }
119         }
120         close ENTRIES;
121 }
122
123
124 # Read configure.in, then write it back out with an updated 
125 # "AM_INIT_AUTOMAKE" line.
126 sub update_configure_in
127 {
128         my $line;
129         my $contents = "";
130         my $version = "";
131         
132         return if ($package_string eq "");
133         
134         open(CFGIN, "< configure.in") || die "Can't read configure.in!";
135         while ($line = <CFGIN>) {
136                 if ($line =~ /^AM_INIT_AUTOMAKE\(wireshark, (\d+)\.(\d+).(\d+)/) {
137                         $line = "AM_INIT_AUTOMAKE\(wireshark, $1.$2.$3$package_string)\n";
138                 }
139                 $contents .= $line
140         }
141         
142         open(CFGIN, "> configure.in") || die "Can't write configure.in!";
143         print(CFGIN $contents);
144         close(CFGIN);
145         print "configure.in has been updated.\n";
146 }
147
148 # Read config.nmake, then write it back out with an updated 
149 # "VERSION" line.
150 sub update_config_nmake
151 {
152         my $line;
153         my $contents = "";
154         my $version = "";
155         
156         return if ($package_string eq "");
157         
158         open(CFGIN, "< config.nmake") || die "Can't read config.nmake!";
159         while ($line = <CFGIN>) {
160                 if ($line =~ /^VERSION_EXTRA=/) {
161                         $line = "VERSION_EXTRA=$package_string\n";
162                 }
163                 $contents .= $line
164         }
165         
166         open(CFGIN, "> config.nmake") || die "Can't write config.nmake!";
167         print(CFGIN $contents);
168         close(CFGIN);
169         print "config.nmake has been updated.\n";
170 }
171
172
173
174 # Print the SVN version to $version_file.
175 # Don't change the file if it is not needed.
176 sub print_svn_version
177 {
178         my $svn_version;
179         my $needs_update = 1;
180
181         if ($pkg_version) { return; }
182
183         if ($last && $revision) {
184                 $svn_version = "#define SVNVERSION \"SVN Rev " . 
185                         $revision . "\"\n";
186         } else {
187                 $svn_version = "/* #define SVNVERSION \"\" */\n";
188         }
189         if (open(OLDVER, "<$version_file")) {
190                 if (<OLDVER> eq $svn_version) {
191                         print "$version_file is up-to-date.\n";
192                         $needs_update = 0;
193                 }
194                 close OLDVER;
195         }
196
197         if ($needs_update == 1) {
198                 # print "Updating $version_file so it contains:\n$svn_version";
199                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
200                 print VER "$svn_version";
201                 close VER;
202                 print "$version_file has been updated.\n";
203         }
204 }
205
206 # Read values from the configuration file, if it exists.
207 sub get_config {
208         my $arg;
209
210         # Get our command-line args
211         GetOptions("package-version", \$pkg_version);
212
213         if ($#ARGV >= 0) {
214                 $srcdir = $ARGV[0]
215         }
216
217
218         if (! open(FILE, "<$vconf_file")) {
219                 print STDERR "Version configuration file $vconf_file not "
220                 . "found.  Using defaults.\n";
221                 return 1;
222         }
223
224         while (<FILE>) {
225                 chomp;
226                 next if (/^#/);
227                 next unless (/^(\w+):\s+(\S.*)/);
228                 $version_pref{$1} = $2;
229         }
230         close FILE;
231         return 1;
232 }
233
234 ##
235 ## Start of code
236 ##
237
238 &get_config();
239
240 if (-d "./.svn") {
241         print "This is a build from SVN (or a SVN snapshot).\n";
242         &read_svn_info(".");
243         if ($pkg_version) {
244                 print "Generating package version.  Ignoring $version_file\n";
245                 &update_configure_in;
246                 &update_config_nmake;
247         } elsif ($version_pref{"enable"} == 0) {
248                 print "Version tag disabled in $vconf_file.\n";
249                 $last = 0;
250                 $revision = 0;
251         } else {
252                 print "SVN version tag will be computed.\n";
253         }
254 } else {
255         print "This is not a SVN build.\n";
256 }
257
258 &print_svn_version;
259
260 __END__