Make SSRC display (among others) unsigned. Fixes Ethereal bug 1002.
[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_format - Like "format", but used for the package version.
36 #
37 # If run with the "-p" or "--package-version" argument, the
38 # AM_INIT_AUTOMAKE macro in configure.in and the VERSION macro in
39 # config.nmake will have the pkg_format template appended to the 
40 # version number.  svnversion.h will _not_ be generated if either
41 # argument is present.
42 #
43 # Default configuration:
44 #
45 # enable: 1
46 # format: SVN %Y%m%d%H%M%S
47 # pkg_format: -SVN-%#
48 # am_init: 0
49
50 # XXX - We're pretty dumb about the "%#" substitution, and about having
51 # spaces in the package format.
52
53 use strict;
54
55 use Time::Local;
56 use POSIX qw(strftime);
57 use Getopt::Long;
58
59 my $version_file = 'svnversion.h';
60 my $package_string = "";
61 my $vconf_file = 'version.conf';
62 my $last = 0;
63 my $revision = 0;
64 my $pkg_version = 0;
65 my %version_pref = (
66         "enable"     => 1,
67         "format"     => "SVN %Y%m%d%H%M%S",
68         "pkg_format" => "-SVN-%#",
69         );
70 my $srcdir = ".";
71
72
73 # Run "svn info".  Parse out the most recent modification time and the
74 # revision number.
75 sub read_svn_info {
76         my $line;
77         my $version_format = $version_pref{"format"};
78         my $package_format = $version_pref{"pkg_format"};
79         my $in_entries = 0;
80         my $svn_name;
81
82         if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
83                 print ("Unable to get SVN info.\n");
84                 return;
85         }
86
87         # The entries schema is flat, so we can use regexes to parse its contents.
88         while ($line = <ENTRIES>) {
89                 if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
90                         $in_entries = 1;
91                         $svn_name = "";
92                 }
93                 if ($in_entries) {
94                         if ($line =~ /name="(.*)"/) { $svn_name = $1; }
95                         if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
96                                 $last = timegm($6, $5, $4, $3, $2 - 1, $1);
97                         }
98                         if ($line =~ /revision="(\d+)"/) { $revision = $1; }
99                 }
100                 if ($line =~ /\/>/) {
101                         if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
102                                         $last && $revision) {
103                                 $in_entries = 0;
104                                 $version_format =~ s/%#/$revision/;
105
106                                 $package_format =~ s/%#/$revision/;
107                                 $package_string = strftime($package_format, gmtime($last));
108
109                                 last;
110                         }
111                 }
112         }
113         close ENTRIES;
114 }
115
116
117 # Read configure.in, then write it back out with an updated 
118 # "AM_INIT_AUTOMAKE" line.
119 sub update_configure_in
120 {
121         my $line;
122         my $contents = "";
123         my $version = "";
124         
125         return if ($package_string eq "");
126         
127         open(CFGIN, "< configure.in") || die "Can't read configure.in!";
128         while ($line = <CFGIN>) {
129                 if ($line =~ /^AM_INIT_AUTOMAKE\(wireshark, (\d+)\.(\d+).(\d+)/) {
130                         $line = "AM_INIT_AUTOMAKE\(wireshark, $1.$2.$3$package_string)\n";
131                 }
132                 $contents .= $line
133         }
134         
135         open(CFGIN, "> configure.in") || die "Can't write configure.in!";
136         print(CFGIN $contents);
137         close(CFGIN);
138         print "configure.in has been updated.\n";
139 }
140
141 # Read config.nmake, then write it back out with an updated 
142 # "VERSION" line.
143 sub update_config_nmake
144 {
145         my $line;
146         my $contents = "";
147         my $version = "";
148         
149         return if ($package_string eq "");
150         
151         open(CFGIN, "< config.nmake") || die "Can't read config.nmake!";
152         while ($line = <CFGIN>) {
153                 if ($line =~ /^VERSION_EXTRA=/) {
154                         $line = "VERSION_EXTRA=$package_string\n";
155                 }
156                 $contents .= $line
157         }
158         
159         open(CFGIN, "> config.nmake") || die "Can't write config.nmake!";
160         print(CFGIN $contents);
161         close(CFGIN);
162         print "config.nmake has been updated.\n";
163 }
164
165
166
167 # Print the SVN version to $version_file.
168 # Don't change the file if it is not needed.
169 sub print_svn_version
170 {
171         my $svn_version;
172         my $needs_update = 1;
173
174         if ($pkg_version) { return; }
175
176         if ($last && $revision) {
177                 $svn_version = "#define SVNVERSION \"SVN Rev " . 
178                         $revision . "\"\n";
179         } else {
180                 $svn_version = "/* #define SVNVERSION \"\" */\n";
181         }
182         if (open(OLDVER, "<$version_file")) {
183                 if (<OLDVER> eq $svn_version) {
184                         print "$version_file is up-to-date.\n";
185                         $needs_update = 0;
186                 }
187                 close OLDVER;
188         }
189
190         if ($needs_update == 1) {
191                 # print "Updating $version_file so it contains:\n$svn_version";
192                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
193                 print VER "$svn_version";
194                 close VER;
195                 print "$version_file has been updated.\n";
196         }
197 }
198
199 # Read values from the configuration file, if it exists.
200 sub get_config {
201         my $arg;
202
203         # Get our command-line args
204         GetOptions("package-version", \$pkg_version);
205
206         if ($#ARGV >= 0) {
207                 $srcdir = $ARGV[0]
208         }
209
210
211         if (! open(FILE, "<$vconf_file")) {
212                 print STDERR "Version configuration file $vconf_file not "
213                 . "found.  Using defaults.\n";
214                 return 1;
215         }
216
217         while (<FILE>) {
218                 chomp;
219                 next if (/^#/);
220                 next unless (/^(\w+):\s+(\S.*)/);
221                 $version_pref{$1} = $2;
222         }
223         close FILE;
224         return 1;
225 }
226
227 ##
228 ## Start of code
229 ##
230
231 &get_config();
232
233 if (-d "./.svn") {
234         print "This is a build from SVN (or a SVN snapshot).\n";
235         &read_svn_info(".");
236         if ($pkg_version) {
237                 print "Generating package version.  Ignoring $version_file\n";
238                 &update_configure_in;
239                 &update_config_nmake;
240         } elsif ($version_pref{"enable"} == 0) {
241                 print "Version tag disabled in $vconf_file.\n";
242                 $last = 0;
243                 $revision = 0;
244         } else {
245                 print "SVN version tag will be computed.\n";
246         }
247 } else {
248         print "This is not a SVN build.\n";
249 }
250
251 &print_svn_version;
252
253 __END__