(Trivial) remove an extra blank line.
[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 # See below for usage
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 (it will also not be generated if 'is_release' is set
44 # in version.conf).
45 #
46 # Default configuration:
47 #
48 # enable: 1
49 # svn_client: 1
50 # format: SVN %Y%m%d%H%M%S
51 # pkg_enable: 1
52 # pkg_format: -SVN-%#
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 use Pod::Usage;
63 use IO::Handle;
64 use English;
65
66 my $version_file = 'svnversion.h';
67 my $package_string = "";
68 my $vconf_file = 'version.conf';
69 my $tortoise_file = "tortoise_template";
70 my $last_change = 0;
71 my $revision = 0;
72 my $repo_path = "unknown";
73 my $get_svn = 0;
74 my $set_version = 0;
75 my $set_release = 0;
76 my %version_pref = (
77         "version_major" => 1,
78         "version_minor" => 7,
79         "version_micro" => 1,
80         "version_build" => 0,
81
82         "enable"        => 1,
83         "svn_client"    => 1,
84         "tortoise_svn"  => 0,
85         "format"        => "SVN %Y%m%d%H%M%S",
86         "is_release"    => 0,
87
88         # Normal development builds
89         "pkg_enable" => 1,
90         "pkg_format" => "-SVN-%#",
91
92         # Development releases
93         #"pkg_enable" => 0,
94         #"pkg_format" => "",
95         );
96 my $srcdir = ".";
97 my $svn_info_cmd = "";
98
99 $ENV{LANG} = "C";  # Ensure we run with correct locale
100
101 # Run "svn info".  Parse out the most recent modification time and the
102 # revision number.
103 sub read_svn_info {
104         my $line;
105         my $version_format = $version_pref{"format"};
106         my $package_format = "";
107         my $in_entries = 0;
108         my $svn_name;
109         my $repo_version;
110         my $repo_root = undef;
111         my $repo_url = undef;
112         my $do_hack = 1;
113         my $info_source = "Unknown";
114
115         if ($version_pref{"pkg_enable"}) {
116                 $package_format = $version_pref{"pkg_format"};
117         }
118
119         if (-d "$srcdir/.svn") {
120                 $info_source = "Command line (svn info)";
121                 $svn_info_cmd = "svn info $srcdir";
122         } elsif (-d "$srcdir/.git/svn") {
123                 $info_source = "Command line (git-svn)";
124                 $svn_info_cmd = "(cd $srcdir; git svn info)";
125         }
126
127         if ($version_pref{"svn_client"}) {
128                 eval {
129                         use warnings "all";
130                         no warnings "all";
131                         $line = qx{$svn_info_cmd};
132                         if (defined($line)) {
133                                 if ($line =~ /Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
134                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
135                                 }
136                                 if ($line =~ /Last Changed Rev: (\d+)/) {
137                                         $revision = $1;
138                                 }
139                                 if ($line =~ /URL: (\S+)/) {
140                                         $repo_url = $1;
141                                 }
142                                 if ($line =~ /Repository Root: (\S+)/) {
143                                         $repo_root = $1;
144                                 }
145                         }
146                         1;
147                 };
148
149                 if ($last_change && $revision && $repo_url && $repo_root) {
150                         $do_hack = 0;
151                 }
152         } elsif ($version_pref{"tortoise_svn"}) {
153                 # Dynamically generic template file needed by TortoiseSVN
154                 open(TORTOISE, ">$tortoise_file");
155                 print TORTOISE "#define SVNVERSION \"\$WCREV\$\"\r\n";
156                 print TORTOISE "#define SVNPATH \"\$WCURL\$\"\r\n";
157                 close(TORTOISE);
158
159                 $info_source = "Command line (SubWCRev)";
160                 $svn_info_cmd = "SubWCRev $srcdir $tortoise_file $version_file";
161                 my $tortoise = system($svn_info_cmd);
162                 if ($tortoise == 0) {
163                         $do_hack = 0;
164                 }
165
166                 #clean up the template file
167                 unlink($tortoise_file);
168         }
169
170         if ($revision == 0) {
171                 # Fall back to config.nmake
172                 $info_source = "Prodding config.nmake";
173                 my $filepath = "config.nmake";
174                 open(CFGNMAKE, "< $filepath") || die "Can't read $filepath!";
175                 while ($line = <CFGNMAKE>) {
176                         if ($line =~ /^SVN_REVISION=(\d+)/) {
177                                 $revision = $1;
178                                 $do_hack = 0;
179                                 last;
180                         }
181                 }
182                 close (CFGNMAKE);
183         }
184
185         # 'svn info' failed or the user really wants us to dig around in .svn/entries
186         if ($do_hack) {
187                 # Start of ugly internal SVN file hack
188                 if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
189                         print ("Unable to open $srcdir/.svn/entries\n");
190                 } else {
191                         $info_source = "Prodding .svn";
192                         # We need to find out whether our parser can handle the entries file
193                         $line = <ENTRIES>;
194                         chomp $line;
195                         if ($line eq '<?xml version="1.0" encoding="utf-8"?>') {
196                                 $repo_version = "pre1.4";
197                         } elsif ($line =~ /^8$/) {
198                                 $repo_version = "1.4";
199                         } else {
200                                 $repo_version = "unknown";
201                         }
202
203                         if ($repo_version eq "pre1.4") {
204                                 # The entries schema is flat, so we can use regexes to parse its contents.
205                                 while ($line = <ENTRIES>) {
206                                         if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
207                                                 $in_entries = 1;
208                                                 $svn_name = "";
209                                         }
210                                         if ($in_entries) {
211                                                 if ($line =~ /name="(.*)"/) { $svn_name = $1; }
212                                                 if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
213                                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
214                                                 }
215                                                 if ($line =~ /revision="(\d+)"/) { $revision = $1; }
216                                         }
217                                         if ($line =~ /\/>/) {
218                                                 if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
219                                                                 $last_change && $revision) {
220                                                         $in_entries = 0;
221                                                         last;
222                                                 }
223                                         }
224                                         # XXX - Fetch the repository root & URL
225                                 }
226                         }
227                         close ENTRIES;
228                 }
229         }
230
231         # If we picked up the revision and modification time,
232         # generate our strings.
233         if ($revision && $last_change) {
234                 $version_format =~ s/%#/$revision/;
235                 $package_format =~ s/%#/$revision/;
236                 $package_string = strftime($package_format, gmtime($last_change));
237         }
238
239         if ($repo_url && $repo_root && index($repo_url, $repo_root) == 0) {
240                 $repo_path = substr($repo_url, length($repo_root));
241         }
242
243         if ($get_svn) {
244                 print <<"Fin";
245 SVN revision    : $revision
246 Revision source : $info_source
247 Release stamp   : $package_string
248 Fin
249         }
250 }
251
252
253 # Read configure.in, then write it back out with an updated
254 # "AC_INIT" line.
255 sub update_configure_in
256 {
257         my $line;
258         my $contents = "";
259         my $version = "";
260         my $filepath = "configure.in";
261
262         return if (!$set_version && $package_string eq "");
263
264         open(CFGIN, "< $filepath") || die "Can't read $filepath!";
265         while ($line = <CFGIN>) {
266                 if ($line =~ /^AC_INIT\(wireshark, (\d+)\.(\d+).(\d+)/) {
267                         $line = sprintf("AC_INIT\(wireshark, %d.%d.%d%s)\n",
268                                         $set_version ? $version_pref{"version_major"} : $1,
269                                         $set_version ? $version_pref{"version_minor"} : $2,
270                                         $set_version ? $version_pref{"version_micro"} : $3,
271                                         $set_release ? $package_string : ""
272                                        );
273
274                 }
275                 $contents .= $line
276         }
277
278         open(CFGIN, "> $filepath") || die "Can't write $filepath!";
279         print(CFGIN $contents);
280         close(CFGIN);
281         print "$filepath has been updated.\n";
282 }
283
284 # Read config.nmake, then write it back out with an updated
285 # "VERSION" line.
286 sub update_config_nmake
287 {
288         my $line;
289         my $contents = "";
290         my $version = "";
291         my $filepath = "config.nmake";
292
293         open(CFGNMAKE, "< $filepath") || die "Can't read $filepath!";
294         while ($line = <CFGNMAKE>) {
295                 if ($line =~ /^SVN_REVISION=/) {
296                         $line = sprintf("SVN_REVISION=%d\n", $revision);
297                 } elsif ($set_version && $line =~ /^VERSION_MAJOR=/) {
298                         $line = sprintf("VERSION_MAJOR=%d\n", $version_pref{"version_major"});
299                 } elsif ($set_version && $line =~ /^VERSION_MINOR=/) {
300                         $line = sprintf("VERSION_MINOR=%d\n", $version_pref{"version_minor"});
301                 } elsif ($set_version && $line =~ /^VERSION_MICRO=/) {
302                         $line = sprintf("VERSION_MICRO=%d\n", $version_pref{"version_micro"});
303                 } elsif ($line =~ /^VERSION_EXTRA=/) {
304                         $line = "VERSION_EXTRA=$package_string\n";
305                 }
306                 $contents .= $line
307         }
308
309         open(CFGNMAKE, "> $filepath") || die "Can't write $filepath!";
310         print(CFGNMAKE $contents);
311         close(CFGNMAKE);
312         print "$filepath has been updated.\n";
313 }
314
315 # Read docbook/release_notes.xml, then write it back out with an updated
316 # "WiresharkCurrentVersion" line.
317 sub update_release_notes
318 {
319         my $line;
320         my $contents = "";
321         my $version = "";
322         my $filepath = "docbook/release-notes.xml";
323
324         return if (!$set_version);
325
326         open(RELNOTES, "< $filepath") || die "Can't read $filepath!";
327         while ($line = <RELNOTES>) {
328                 #   <!ENTITY WiresharkCurrentVersion "1.7.1">
329
330                 if ($line =~ /<\!ENTITY\s+WiresharkCurrentVersion\s+/) {
331                         $line = sprintf("<!ENTITY WiresharkCurrentVersion \"%d.%d.%d\">\n",
332                                         $version_pref{"version_major"},
333                                         $version_pref{"version_minor"},
334                                         $version_pref{"version_micro"},
335                                        );
336                 }
337                 $contents .= $line
338         }
339
340         open(RELNOTES, "> $filepath") || die "Can't write $filepath!";
341         print(RELNOTES $contents);
342         close(RELNOTES);
343         print "$filepath has been updated.\n";
344 }
345
346 # Read debian/changelog, then write back out an updated version.
347 sub update_debian_changelog
348 {
349         my $line;
350         my $contents = "";
351         my $version = "";
352         my $filepath = "debian/changelog";
353
354         return if ($set_version == 0);
355
356         open(CHANGELOG, "< $filepath") || die "Can't read $filepath!";
357         while ($line = <CHANGELOG>) {
358                 if ($set_version && CHANGELOG->input_line_number() == 1) {
359                         $line = sprintf("wireshark (%d.%d.%d) unstable; urgency=low\n",
360                                         $version_pref{"version_major"},
361                                         $version_pref{"version_minor"},
362                                         $version_pref{"version_micro"},
363                                        );
364                 }
365                 $contents .= $line
366         }
367
368         open(CHANGELOG, "> $filepath") || die "Can't write $filepath!";
369         print(CHANGELOG $contents);
370         close(CHANGELOG);
371         print "$filepath has been updated.\n";
372 }
373
374 # Update distributed files that contain any version information
375 sub update_versioned_files
376 {
377         &update_configure_in;
378         &update_config_nmake;
379         &update_release_notes;
380         &update_debian_changelog;
381 }
382
383 # Print the SVN version to $version_file.
384 # Don't change the file if it is not needed.
385 sub print_svn_revision
386 {
387         my $svn_revision;
388         my $needs_update = 1;
389
390         if ($last_change && $revision) {
391                 $svn_revision = "#define SVNVERSION \"SVN Rev " .
392                         $revision . "\"\n" .
393                         "#define SVNPATH \"" . $repo_path . "\"\n";
394         } else {
395                 $svn_revision = "#define SVNVERSION \"SVN Rev Unknown\"\n" .
396                         "#define SVNPATH \"unknown\"\n";
397         }
398         if (open(OLDREV, "<$version_file")) {
399                 my $old_svn_revision = <OLDREV> . <OLDREV>;
400                 if ($old_svn_revision eq $svn_revision) {
401                         $needs_update = 0;
402                 }
403                 close OLDREV;
404         }
405
406         if (! $set_version && ! $set_release) { return; }
407
408         if ($needs_update) {
409                 # print "Updating $version_file so it contains:\n$svn_revision";
410                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
411                 print VER "$svn_revision";
412                 close VER;
413                 print "$version_file has been updated.\n";
414         } else {
415                 print "$version_file unchanged.\n";
416         }
417 }
418
419 # Read values from the configuration file, if it exists.
420 sub get_config {
421         my $arg;
422         my $show_help = 0;
423
424         # Get our command-line args
425         GetOptions(
426                    "help|h", \$show_help,
427                    "get-svn|g", \$get_svn,
428                    "set-version|v", \$set_version,
429                    "set-release|r|package-version|p", \$set_release
430                    ) || pod2usage(2);
431
432         if ($show_help) { pod2usage(1); }
433
434         if ( !( $show_help || $get_svn || $set_release ) ) {
435                 $set_version = 1;
436         }
437
438         if ($#ARGV >= 0) {
439                 $srcdir = $ARGV[0]
440         }
441
442         if (! open(FILE, "<$vconf_file")) {
443                 print STDERR "Version configuration file $vconf_file not "
444                 . "found.  Using defaults.\n";
445                 return 1;
446         }
447
448         while (<FILE>) {
449                 chomp;
450                 next if (/^#/);
451                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
452                 $version_pref{$1} = $3;
453         }
454         close FILE;
455         return 1;
456 }
457
458 ##
459 ## Start of code
460 ##
461
462 &get_config();
463
464 &read_svn_info();
465
466 &print_svn_revision;
467
468 if ($set_version || $set_release) {
469         if ($set_version) {
470                 print "Generating version information\n";
471         }
472
473         if ($version_pref{"enable"} == 0) {
474                 print "Release information disabled in $vconf_file.\n";
475                 $set_release = 0;
476         }
477
478         if ($set_release) {
479                 print "Generating release information\n";
480         }
481
482         &update_versioned_files;
483 }
484
485 __END__
486
487 =head1 NAM
488
489 make-version.pl - Get and set build-time version information for Wireshark
490
491 =head1 SYNOPSIS
492
493 make-version.pl [options] [source directory]
494
495   Options:
496
497     --help, -h                 This help message
498     --get-svn, -g              Print the SVN revision and source.
499     --set-version, -v          Set the major, minor, and micro versions.
500                                Resets the release information when used by
501                                itself.
502     --set-release, -r          Set the release information.
503     --package-version, -p      Deprecated. Same as --set-release.
504
505 Options can be used in any combination. If none are specified B<--set-version>
506 is assumed.