Discard existing list of disabled protocols in read_disabled_protos_list()
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 #   svn_client - Use svn client i.s.o. ugly internal SVN file hack
33 #   format     - A strftime() formatted string to use as a template for
34 #                the version string.  The sequence "%#" will substitute
35 #                the SVN revision number.
36 #   pkg_enable - Enable or disable package versioning.
37 #   pkg_format - Like "format", but used for the package version.
38 #
39 # If run with the "-p" or "--package-version" argument, the
40 # AM_INIT_AUTOMAKE macro in configure.in and the VERSION macro in
41 # config.nmake will have the pkg_format template appended to the 
42 # version number.  svnversion.h will _not_ be generated if either
43 # argument is present.
44 #
45 # Default configuration:
46 #
47 # enable: 1
48 # svn_client: 0 <- This needs to change in order to support SVN 1.4
49 # format: SVN %Y%m%d%H%M%S
50 # pkg_enable: 1
51 # pkg_format: -SVN-%#
52 # am_init: 0
53
54 # XXX - We're pretty dumb about the "%#" substitution, and about having
55 # spaces in the package format.
56
57 use strict;
58
59 use Time::Local;
60 use POSIX qw(strftime);
61 use Getopt::Long;
62
63 my $version_file = 'svnversion.h';
64 my $package_string = "";
65 my $vconf_file = 'version.conf';
66 my $last = 0;
67 my $revision = 0;
68 my $pkg_version = 0;
69 my %version_pref = (
70         "enable"     => 1,
71         "svn_client" => 0,
72         "format"     => "SVN %Y%m%d%H%M%S",
73         "pkg_enable" => 1,
74         "pkg_format" => "-SVN-%#",
75         );
76 my $srcdir = ".";
77
78
79 # Run "svn info".  Parse out the most recent modification time and the
80 # revision number.
81 sub read_svn_info {
82         my $line;
83         my $version_format = $version_pref{"format"};
84         my $package_format = "";
85         my $in_entries = 0;
86         my $svn_name;
87         my $repo_version;
88
89         if ($version_pref{"pkg_enable"}) {
90                 $package_format = $version_pref{"pkg_format"};
91         }
92
93         if (!$version_pref{"svn_client"}) {
94                 # Start of ugly internal SVN file hack
95                 if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
96                         print ("Unable to open $srcdir/.svn/entries, trying 'svn info'\n");
97                         # Fall back to "svn info"
98                         $version_pref{"svn_client"} = 1;
99                 }
100
101                 else {
102                         # We need to find out whether our parser can handle the entries file
103                         $line = <ENTRIES>;
104                         chomp $line;
105                         if ($line eq '<?xml version="1.0" encoding="utf-8"?>') {
106                                 $repo_version = "pre1.4";
107                         } elsif ($line =~ /^8$/) {
108                                 $repo_version = "1.4";
109                         } else {
110                                 $repo_version = "unknown";
111                         }
112                 }
113         }
114         if ($version_pref{"svn_client"} || ($repo_version ne "pre1.4")) {
115                 $line = qx{svn info $srcdir};
116                 if ($line =~ /Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
117                         $last = timegm($6, $5, $4, $3, $2 - 1, $1);
118                 }
119                 if ($line =~ /Last Changed Rev: (\d+)/) {
120                         $revision = $1;
121                 }
122                 if ($line =~ /^\s*$/ || $revision =~ /^\s*$/) {
123                         $last = "unknown";
124                         $revision = "unknown";
125                 }
126         } else {
127                 # The entries schema is flat, so we can use regexes to parse its contents.
128                 while ($line = <ENTRIES>) {
129                         if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
130                                 $in_entries = 1;
131                                 $svn_name = "";
132                         }
133                         if ($in_entries) {
134                                 if ($line =~ /name="(.*)"/) { $svn_name = $1; }
135                                 if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
136                                         $last = timegm($6, $5, $4, $3, $2 - 1, $1);
137                                 }
138                                 if ($line =~ /revision="(\d+)"/) { $revision = $1; }
139                         }
140                         if ($line =~ /\/>/) {
141                                 if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
142                                                 $last && $revision) {
143                                         $in_entries = 0;
144                                         last;
145                                 }
146                         }
147                 }
148                 close ENTRIES;
149         }
150
151         # If we picked up the revision and modification time, 
152         # generate our strings.
153         if ($revision && $last) {
154                 $version_format =~ s/%#/$revision/;
155                 $package_format =~ s/%#/$revision/;
156                 $package_string = strftime($package_format, gmtime($last));
157         }
158                 
159 }
160
161
162 # Read configure.in, then write it back out with an updated 
163 # "AM_INIT_AUTOMAKE" line.
164 sub update_configure_in
165 {
166         my $line;
167         my $contents = "";
168         my $version = "";
169         
170         return if ($package_string eq "");
171         
172         open(CFGIN, "< configure.in") || die "Can't read configure.in!";
173         while ($line = <CFGIN>) {
174                 if ($line =~ /^AM_INIT_AUTOMAKE\(wireshark, (\d+)\.(\d+).(\d+)/) {
175                         $line = "AM_INIT_AUTOMAKE\(wireshark, $1.$2.$3$package_string)\n";
176                 }
177                 $contents .= $line
178         }
179         
180         open(CFGIN, "> configure.in") || die "Can't write configure.in!";
181         print(CFGIN $contents);
182         close(CFGIN);
183         print "configure.in has been updated.\n";
184 }
185
186 # Read config.nmake, then write it back out with an updated 
187 # "VERSION" line.
188 sub update_config_nmake
189 {
190         my $line;
191         my $contents = "";
192         my $version = "";
193         
194         return if ($package_string eq "");
195         
196         open(CFGIN, "< config.nmake") || die "Can't read config.nmake!";
197         while ($line = <CFGIN>) {
198                 if ($line =~ /^VERSION_EXTRA=/) {
199                         $line = "VERSION_EXTRA=$package_string\n";
200                 }
201                 $contents .= $line
202         }
203         
204         open(CFGIN, "> config.nmake") || die "Can't write config.nmake!";
205         print(CFGIN $contents);
206         close(CFGIN);
207         print "config.nmake has been updated.\n";
208 }
209
210
211
212 # Print the SVN version to $version_file.
213 # Don't change the file if it is not needed.
214 sub print_svn_version
215 {
216         my $svn_version;
217         my $needs_update = 1;
218
219         if ($pkg_version) { return; }
220
221         if ($last && $revision) {
222                 $svn_version = "#define SVNVERSION \"SVN Rev " . 
223                         $revision . "\"\n";
224         } else {
225                 $svn_version = "/* #define SVNVERSION \"\" */\n";
226         }
227         if (open(OLDVER, "<$version_file")) {
228                 if (<OLDVER> eq $svn_version) {
229                         print "$version_file is up-to-date.\n";
230                         $needs_update = 0;
231                 }
232                 close OLDVER;
233         }
234
235         if ($needs_update == 1) {
236                 # print "Updating $version_file so it contains:\n$svn_version";
237                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
238                 print VER "$svn_version";
239                 close VER;
240                 print "$version_file has been updated.\n";
241         }
242 }
243
244 # Read values from the configuration file, if it exists.
245 sub get_config {
246         my $arg;
247
248         # Get our command-line args
249         GetOptions("package-version", \$pkg_version);
250
251         if ($#ARGV >= 0) {
252                 $srcdir = $ARGV[0]
253         }
254
255
256         if (! open(FILE, "<$vconf_file")) {
257                 print STDERR "Version configuration file $vconf_file not "
258                 . "found.  Using defaults.\n";
259                 return 1;
260         }
261
262         while (<FILE>) {
263                 chomp;
264                 next if (/^#/);
265                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
266                 $version_pref{$1} = $3;
267         }
268         close FILE;
269         return 1;
270 }
271
272 ##
273 ## Start of code
274 ##
275
276 &get_config();
277
278 if (-d "$srcdir/.svn") {
279         print "This is a build from SVN (or a SVN snapshot).\n";
280         &read_svn_info();
281         if ($pkg_version) {
282                 print "Generating package version.  Ignoring $version_file\n";
283                 &update_configure_in;
284                 &update_config_nmake;
285         } elsif ($version_pref{"enable"} == 0) {
286                 print "Version tag disabled in $vconf_file.\n";
287                 $last = 0;
288                 $revision = 0;
289         } else {
290                 print "SVN version tag will be computed.\n";
291         }
292 } else {
293         print "This is not a SVN build.\n";
294 }
295
296 &print_svn_version;
297
298 __END__