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