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