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